events.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Init kinda namespace object
  2. var VE = { // Vesta Events object
  3. core: {}, // core functions
  4. callbacks: { // events callback functions
  5. click: {},
  6. mouseover: {},
  7. mouseout: {},
  8. keypress: {}
  9. },
  10. helpers: {}, // simple handy methods
  11. tmp: {}
  12. };
  13. /*
  14. * Main method that invokes further event processing
  15. * @param root is root HTML DOM element that. Pass HTML DOM Element or css selector
  16. * @param event_type (eg: click, mouseover etc..)
  17. */
  18. VE.core.register = function(root, event_type) {
  19. var root = !root ? 'body' : root; // if elm is not passed just bind events to body DOM Element
  20. var event_type = !event_type ? 'click' : event_type; // set event type to "click" by default
  21. $(root).bind(event_type, function(evt) {
  22. var elm = $(evt.target);
  23. VE.core.dispatch(evt, elm, event_type); // dispatch captured event
  24. });
  25. }
  26. /*
  27. * Dispatch event that was previously registered
  28. * @param evt related event object
  29. * @param elm that was catched
  30. * @param event_type (eg: click, mouseover etc..)
  31. */
  32. VE.core.dispatch = function(evt, elm, event_type) {
  33. if ('undefined' == typeof VE.callbacks[event_type]) {
  34. return VE.helpers.warn('There is no corresponding object that should contain event callbacks for "'+event_type+'" event type');
  35. }
  36. // get class of element
  37. var classes = $(elm).attr('class');
  38. // if no classes are attached, then just stop any further processings
  39. if (!classes) {
  40. return; // no classes assigned
  41. }
  42. // split the classes and check if it related to function
  43. $(classes.split(/\s/)).each(function(i, key) {
  44. VE.callbacks[event_type][key] && VE.callbacks[event_type][key](evt, elm);
  45. });
  46. }
  47. //
  48. // CALLBACKS
  49. //
  50. /*
  51. * Suspend action
  52. */
  53. VE.callbacks.click.do_suspend = function(evt, elm) {
  54. var ref = elm.hasClass('data-controls') ? elm : elm.parents('.data-controls');
  55. var url = $('input[name="suspend_url"]', ref).val();
  56. var dialog_elm = ref.find('.confirmation-text-suspention');
  57. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  58. }
  59. /*
  60. * Unsuspend action
  61. */
  62. VE.callbacks.click.do_unsuspend = function(evt, elm) {
  63. var ref = elm.hasClass('data-controls') ? elm : elm.parents('.data-controls');
  64. var url = $('input[name="unsuspend_url"]', ref).val();
  65. var dialog_elm = ref.find('.confirmation-text-suspention');
  66. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  67. }
  68. /*
  69. * Delete action
  70. */
  71. VE.callbacks.click.do_delete = function(evt, elm) {
  72. var ref = elm.hasClass('data-controls') ? elm : elm.parents('.data-controls');
  73. var url = $('input[name="delete_url"]', ref).val();
  74. var dialog_elm = ref.find('.confirmation-text-delete');
  75. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  76. }
  77. /*
  78. * Create dialog box on the fly
  79. * @param elm Element which contains the dialog contents
  80. * @param dialog_title
  81. * @param confirmed_location_url URL that will be redirected to if user hit "OK"
  82. * @param custom_config Custom configuration parameters passed to dialog initialization (optional)
  83. */
  84. VE.helpers.createConfirmationDialog = function(elm, dialog_title, confirmed_location_url, custom_config) {
  85. var custom_config = !custom_config ? {} : custom_config;
  86. var config = {
  87. modal: true,
  88. autoOpen: true,
  89. width: 360,
  90. title: dialog_title,
  91. close: function() {
  92. $(this).dialog("destroy");
  93. },
  94. buttons: {
  95. "OK": function(event, ui) {
  96. location.href = confirmed_location_url;
  97. },
  98. "Cancel": function() {
  99. $(this).dialog("close");
  100. $(this).dialog("destroy");
  101. }
  102. }
  103. }
  104. config = $.extend(config, custom_config);
  105. var reference_copied = $(elm).clone();
  106. $(reference_copied).dialog(config);
  107. }
  108. /*
  109. * Simple debug output
  110. */
  111. VE.helpers.warn = function(msg) {
  112. alert('WARNING: ' + msg);
  113. }