events.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Init kinda namespace object
  2. var VE = { // Vesta Events object
  3. core: {}, // core functions
  4. navigation: {
  5. state: {
  6. active_menu: 1,
  7. menu_selector: '.l-stat__col',
  8. menu_active_selector: '.l-stat__col--active'
  9. }
  10. }, // menu and element navigation functions
  11. callbacks: { // events callback functions
  12. click: {},
  13. mouseover: {},
  14. mouseout: {},
  15. keypress: {}
  16. },
  17. helpers: {}, // simple handy methods
  18. tmp: {
  19. sort_par: 'sort-name',
  20. sort_direction: -1,
  21. sort_as_int: 0,
  22. form_changed: 0,
  23. search_activated: 0,
  24. search_display_interval: 0,
  25. search_hover_interval: 0
  26. }
  27. };
  28. /*
  29. * Main method that invokes further event processing
  30. * @param root is root HTML DOM element that. Pass HTML DOM Element or css selector
  31. * @param event_type (eg: click, mouseover etc..)
  32. */
  33. VE.core.register = function(root, event_type) {
  34. var root = !root ? 'body' : root; // if elm is not passed just bind events to body DOM Element
  35. var event_type = !event_type ? 'click' : event_type; // set event type to "click" by default
  36. $(root).bind(event_type, function(evt) {
  37. var elm = $(evt.target);
  38. VE.core.dispatch(evt, elm, event_type); // dispatch captured event
  39. });
  40. }
  41. /*
  42. * Dispatch event that was previously registered
  43. * @param evt related event object
  44. * @param elm that was catched
  45. * @param event_type (eg: click, mouseover etc..)
  46. */
  47. VE.core.dispatch = function(evt, elm, event_type) {
  48. if ('undefined' == typeof VE.callbacks[event_type]) {
  49. return VE.helpers.warn('There is no corresponding object that should contain event callbacks for "'+event_type+'" event type');
  50. }
  51. // get class of element
  52. var classes = $(elm).attr('class');
  53. // if no classes are attached, then just stop any further processings
  54. if (!classes) {
  55. return; // no classes assigned
  56. }
  57. // split the classes and check if it related to function
  58. $(classes.split(/\s/)).each(function(i, key) {
  59. VE.callbacks[event_type][key] && VE.callbacks[event_type][key](evt, elm);
  60. });
  61. }
  62. //
  63. // CALLBACKS
  64. //
  65. /*
  66. * Suspend action
  67. */
  68. VE.callbacks.click.do_suspend = function(evt, elm) {
  69. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  70. var url = $('input[name="suspend_url"]', ref).val();
  71. var dialog_elm = ref.find('.confirmation-text-suspention');
  72. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  73. }
  74. /*
  75. * Unsuspend action
  76. */
  77. VE.callbacks.click.do_unsuspend = function(evt, elm) {
  78. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  79. var url = $('input[name="unsuspend_url"]', ref).val();
  80. var dialog_elm = ref.find('.confirmation-text-suspention');
  81. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  82. }
  83. /*
  84. * Delete action
  85. */
  86. VE.callbacks.click.do_delete = function(evt, elm) {
  87. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  88. var url = $('input[name="delete_url"]', ref).val();
  89. var dialog_elm = ref.find('.confirmation-text-delete');
  90. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  91. }
  92. /*
  93. * Create dialog box on the fly
  94. * @param elm Element which contains the dialog contents
  95. * @param dialog_title
  96. * @param confirmed_location_url URL that will be redirected to if user hit "OK"
  97. * @param custom_config Custom configuration parameters passed to dialog initialization (optional)
  98. */
  99. VE.helpers.createConfirmationDialog = function(elm, dialog_title, confirmed_location_url, custom_config) {
  100. var custom_config = !custom_config ? {} : custom_config;
  101. var config = {
  102. modal: true,
  103. autoOpen: true,
  104. width: 360,
  105. title: dialog_title,
  106. close: function() {
  107. $(this).dialog("destroy");
  108. },
  109. buttons: {
  110. "OK": function(event, ui) {
  111. location.href = confirmed_location_url;
  112. },
  113. "Cancel": function() {
  114. $(this).dialog("close");
  115. $(this).dialog("destroy");
  116. }
  117. }
  118. }
  119. config = $.extend(config, custom_config);
  120. var reference_copied = $(elm).clone();
  121. $(reference_copied).dialog(config);
  122. }
  123. /*
  124. * Simple debug output
  125. */
  126. VE.helpers.warn = function(msg) {
  127. alert('WARNING: ' + msg);
  128. }
  129. VE.helpers.extendPasswordFields = function() {
  130. var references = ['.password'];
  131. $(document).ready(function() {
  132. $(references).each(function(i, ref) {
  133. VE.helpers.initAdditionalPasswordFieldElements(ref);
  134. });
  135. });
  136. }
  137. VE.helpers.initAdditionalPasswordFieldElements = function(ref) {
  138. var enabled = $.cookie('hide_passwords') == '1' ? true : false;
  139. if (enabled) {
  140. VE.helpers.hidePasswordFieldText(ref);
  141. }
  142. $(ref).prop('autocomplete', 'off');
  143. var enabled_html = enabled ? '' : 'show-passwords-enabled-action';
  144. 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>';
  145. $(ref).after(html);
  146. }
  147. VE.helpers.hidePasswordFieldText = function(ref) {
  148. $.cookie('hide_passwords', '1', { expires: 365, path: '/' });
  149. $(ref).prop('type', 'password');
  150. }
  151. VE.helpers.revealPasswordFieldText = function(ref) {
  152. $.cookie('hide_passwords', '0', { expires: 365, path: '/' });
  153. $(ref).prop('type', 'text');
  154. }
  155. VE.helpers.toggleHiddenPasswordText = function(ref, triggering_elm) {
  156. $(triggering_elm).toggleClass('show-passwords-enabled-action');
  157. if ($(ref).prop('type') == 'text') {
  158. VE.helpers.hidePasswordFieldText(ref);
  159. }
  160. else {
  161. VE.helpers.revealPasswordFieldText(ref);
  162. }
  163. }
  164. VE.helpers.refresh_timer = {
  165. speed: 50,
  166. degr: 180,
  167. right: 0,
  168. left: 0,
  169. periodical: 0,
  170. first: 1,
  171. start: function(){
  172. this.periodical = setInterval(function(){VE.helpers.refresh_timer.turn()}, this.speed);
  173. },
  174. stop: function(){
  175. clearTimeout(this.periodical);
  176. },
  177. turn: function(){
  178. this.degr += 1;
  179. if (this.first && this.degr >= 361){
  180. this.first = 0;
  181. this.degr = 180;
  182. this.left.css({'-webkit-transform': 'rotate(180deg)'});
  183. this.left.css({'transform': 'rotate(180deg)'});
  184. this.left.children('.loader-half').addClass('dark');
  185. }
  186. if (!this.first && this.degr >= 360){
  187. this.first = 1;
  188. this.degr = 180;
  189. this.left.css({'-webkit-transform': 'rotate(0deg)'});
  190. this.right.css({'-webkit-transform': 'rotate(180deg)'});
  191. this.left.css({'transform': 'rotate(0deg)'});
  192. this.right.css({'transform': 'rotate(180deg)'});
  193. this.left.children('.loader-half').removeClass('dark');
  194. this.stop();
  195. location.reload();
  196. }
  197. if (this.first){
  198. this.right.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  199. this.right.css({'transform': 'rotate('+this.degr+'deg)'});
  200. }
  201. else{
  202. this.left.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  203. this.left.css({'transform': 'rotate('+this.degr+'deg)'});
  204. }
  205. }
  206. }
  207. VE.navigation.enter_focused = function() {
  208. if($(VE.navigation.state.menu_selector + '.focus a').attr('href')){
  209. location.href=($(VE.navigation.state.menu_selector + '.focus a').attr('href'));
  210. }
  211. }
  212. VE.navigation.move_focus_left = function(){
  213. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  214. if(index == -1)
  215. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  216. if(index > 0){
  217. $(VE.navigation.state.menu_selector).removeClass('focus');
  218. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  219. } else {
  220. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  221. }
  222. }
  223. VE.navigation.move_focus_right = function(){
  224. var max_index = $(VE.navigation.state.menu_selector).length-1;
  225. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  226. if(index == -1)
  227. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  228. if(index < max_index){
  229. $(VE.navigation.state.menu_selector).removeClass('focus');
  230. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  231. }
  232. }
  233. VE.navigation.switch_menu = function(){
  234. if(VE.navigation.state.active_menu == 0){
  235. VE.navigation.state.active_menu = 1;
  236. VE.navigation.state.menu_selector = '.l-stat__col';
  237. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  238. $('.l-menu').removeClass('active');
  239. $('.l-stat').addClass('active');
  240. } else {
  241. VE.navigation.state.active_menu = 0;
  242. VE.navigation.state.menu_selector = '.l-menu__item';
  243. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  244. $('.l-menu').addClass('active');
  245. $('.l-stat').removeClass('active');
  246. }
  247. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  248. if(index == -1){
  249. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  250. if(index == -1)
  251. index = 0;
  252. $($(VE.navigation.state.menu_selector)[index]).addClass('focus');
  253. }
  254. }
  255. VE.navigation.init = function(){
  256. if($('.l-menu__item.l-menu__item--active').length){
  257. // VE.navigation.switch_menu();
  258. VE.navigation.state.active_menu = 0;
  259. VE.navigation.state.menu_selector = '.l-menu__item';
  260. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  261. $('.l-menu').addClass('active');
  262. $('.l-stat').removeClass('active');
  263. } else {
  264. $('.l-stat').addClass('active');
  265. }
  266. }
  267. VE.helpers.extendPasswordFields();