events.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. sort_par: 'sort-name',
  13. sort_direction: -1,
  14. sort_as_int: 0,
  15. form_changed: 0,
  16. search_activated: 0,
  17. search_display_interval: 0,
  18. search_hover_interval: 0
  19. }
  20. };
  21. /*
  22. * Main method that invokes further event processing
  23. * @param root is root HTML DOM element that. Pass HTML DOM Element or css selector
  24. * @param event_type (eg: click, mouseover etc..)
  25. */
  26. VE.core.register = function(root, event_type) {
  27. var root = !root ? 'body' : root; // if elm is not passed just bind events to body DOM Element
  28. var event_type = !event_type ? 'click' : event_type; // set event type to "click" by default
  29. $(root).bind(event_type, function(evt) {
  30. var elm = $(evt.target);
  31. VE.core.dispatch(evt, elm, event_type); // dispatch captured event
  32. });
  33. }
  34. /*
  35. * Dispatch event that was previously registered
  36. * @param evt related event object
  37. * @param elm that was catched
  38. * @param event_type (eg: click, mouseover etc..)
  39. */
  40. VE.core.dispatch = function(evt, elm, event_type) {
  41. if ('undefined' == typeof VE.callbacks[event_type]) {
  42. return VE.helpers.warn('There is no corresponding object that should contain event callbacks for "'+event_type+'" event type');
  43. }
  44. // get class of element
  45. var classes = $(elm).attr('class');
  46. // if no classes are attached, then just stop any further processings
  47. if (!classes) {
  48. return; // no classes assigned
  49. }
  50. // split the classes and check if it related to function
  51. $(classes.split(/\s/)).each(function(i, key) {
  52. VE.callbacks[event_type][key] && VE.callbacks[event_type][key](evt, elm);
  53. });
  54. }
  55. //
  56. // CALLBACKS
  57. //
  58. /*
  59. * Suspend action
  60. */
  61. VE.callbacks.click.do_suspend = function(evt, elm) {
  62. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  63. var url = $('input[name="suspend_url"]', ref).val();
  64. var dialog_elm = ref.find('.confirmation-text-suspention');
  65. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  66. }
  67. /*
  68. * Unsuspend action
  69. */
  70. VE.callbacks.click.do_unsuspend = function(evt, elm) {
  71. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  72. var url = $('input[name="unsuspend_url"]', ref).val();
  73. var dialog_elm = ref.find('.confirmation-text-suspention');
  74. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  75. }
  76. /*
  77. * Delete action
  78. */
  79. VE.callbacks.click.do_delete = function(evt, elm) {
  80. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  81. var url = $('input[name="delete_url"]', ref).val();
  82. var dialog_elm = ref.find('.confirmation-text-delete');
  83. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  84. }
  85. /*
  86. * Create dialog box on the fly
  87. * @param elm Element which contains the dialog contents
  88. * @param dialog_title
  89. * @param confirmed_location_url URL that will be redirected to if user hit "OK"
  90. * @param custom_config Custom configuration parameters passed to dialog initialization (optional)
  91. */
  92. VE.helpers.createConfirmationDialog = function(elm, dialog_title, confirmed_location_url, custom_config) {
  93. var custom_config = !custom_config ? {} : custom_config;
  94. var config = {
  95. modal: true,
  96. autoOpen: true,
  97. width: 360,
  98. title: dialog_title,
  99. close: function() {
  100. $(this).dialog("destroy");
  101. },
  102. buttons: {
  103. "OK": function(event, ui) {
  104. location.href = confirmed_location_url;
  105. },
  106. "Cancel": function() {
  107. $(this).dialog("close");
  108. $(this).dialog("destroy");
  109. }
  110. }
  111. }
  112. config = $.extend(config, custom_config);
  113. var reference_copied = $(elm).clone();
  114. $(reference_copied).dialog(config);
  115. }
  116. /*
  117. * Simple debug output
  118. */
  119. VE.helpers.warn = function(msg) {
  120. alert('WARNING: ' + msg);
  121. }
  122. VE.helpers.extendPasswordFields = function() {
  123. var references = ['.password'];
  124. $(document).ready(function() {
  125. $(references).each(function(i, ref) {
  126. VE.helpers.initAdditionalPasswordFieldElements(ref);
  127. });
  128. });
  129. }
  130. VE.helpers.initAdditionalPasswordFieldElements = function(ref) {
  131. var enabled = $.cookie('hide_passwords') == '1' ? true : false;
  132. if (enabled) {
  133. VE.helpers.hidePasswordFieldText(ref);
  134. }
  135. $(ref).prop('autocomplete', 'off');
  136. var enabled_html = enabled ? '' : 'show-passwords-enabled-action';
  137. var html = '<span class="hide-password"><img class="toggle-psw-visibility-icon ' + enabled_html + '" onClick="VE.helpers.toggleHiddenPasswordText(\'' + ref + '\', this)" src="/images/toggle_password.png" /></span>';
  138. $(ref).after(html);
  139. }
  140. VE.helpers.hidePasswordFieldText = function(ref) {
  141. $.cookie('hide_passwords', '1', { expires: 365, path: '/' });
  142. $(ref).prop('type', 'password');
  143. }
  144. VE.helpers.revealPasswordFieldText = function(ref) {
  145. $.cookie('hide_passwords', '0', { expires: 365, path: '/' });
  146. $(ref).prop('type', 'text');
  147. }
  148. VE.helpers.toggleHiddenPasswordText = function(ref, triggering_elm) {
  149. $(triggering_elm).toggleClass('show-passwords-enabled-action');
  150. if ($(ref).prop('type') == 'text') {
  151. VE.helpers.hidePasswordFieldText(ref);
  152. }
  153. else {
  154. VE.helpers.revealPasswordFieldText(ref);
  155. }
  156. }
  157. VE.helpers.refresh_timer = {
  158. speed: 50,
  159. degr: 180,
  160. right: 0,
  161. left: 0,
  162. periodical: 0,
  163. first: 1,
  164. start: function(){
  165. this.periodical = setInterval(function(){VE.helpers.refresh_timer.turn()}, this.speed);
  166. },
  167. stop: function(){
  168. clearTimeout(this.periodical);
  169. },
  170. turn: function(){
  171. this.degr += 1;
  172. if (this.first && this.degr >= 361){
  173. this.first = 0;
  174. this.degr = 180;
  175. this.left.css({'-webkit-transform': 'rotate(180deg)'});
  176. this.left.css({'transform': 'rotate(180deg)'});
  177. this.left.children('.loader-half').addClass('dark');
  178. }
  179. if (!this.first && this.degr >= 360){
  180. this.first = 1;
  181. this.degr = 180;
  182. this.left.css({'-webkit-transform': 'rotate(0deg)'});
  183. this.right.css({'-webkit-transform': 'rotate(180deg)'});
  184. this.left.css({'transform': 'rotate(0deg)'});
  185. this.right.css({'transform': 'rotate(180deg)'});
  186. this.left.children('.loader-half').removeClass('dark');
  187. this.stop();
  188. location.reload();
  189. }
  190. if (this.first){
  191. this.right.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  192. this.right.css({'transform': 'rotate('+this.degr+'deg)'});
  193. }
  194. else{
  195. this.left.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  196. this.left.css({'transform': 'rotate('+this.degr+'deg)'});
  197. }
  198. }
  199. }
  200. VE.helpers.extendPasswordFields();