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