events.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. /*
  213. VE.navigation.move_focus_left = function(){
  214. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  215. if(index == -1)
  216. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  217. if(index > 0){
  218. $(VE.navigation.state.menu_selector).removeClass('focus');
  219. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  220. } else {
  221. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  222. }
  223. }
  224. VE.navigation.move_focus_right = function(){
  225. var max_index = $(VE.navigation.state.menu_selector).length-1;
  226. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  227. if(index == -1)
  228. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  229. if(index < max_index){
  230. $(VE.navigation.state.menu_selector).removeClass('focus');
  231. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  232. }
  233. }
  234. VE.navigation.switch_menu = function(){
  235. if(VE.navigation.state.active_menu == 0){
  236. VE.navigation.state.active_menu = 1;
  237. VE.navigation.state.menu_selector = '.l-stat__col';
  238. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  239. $('.l-menu').removeClass('active');
  240. $('.l-stat').addClass('active');
  241. } else {
  242. VE.navigation.state.active_menu = 0;
  243. VE.navigation.state.menu_selector = '.l-menu__item';
  244. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  245. $('.l-menu').addClass('active');
  246. $('.l-stat').removeClass('active');
  247. }
  248. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  249. if(index == -1){
  250. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  251. if(index == -1)
  252. index = 0;
  253. $($(VE.navigation.state.menu_selector)[index]).addClass('focus');
  254. }
  255. }
  256. */
  257. VE.navigation.move_focus_left = function(){
  258. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  259. if(index == -1)
  260. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  261. $(VE.navigation.state.menu_selector).removeClass('focus');
  262. if(index > 0){
  263. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  264. } else {
  265. VE.navigation.switch_menu('last');
  266. }
  267. }
  268. VE.navigation.move_focus_right = function(){
  269. var max_index = $(VE.navigation.state.menu_selector).length-1;
  270. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  271. if(index == -1)
  272. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  273. $(VE.navigation.state.menu_selector).removeClass('focus');
  274. if(index < max_index){
  275. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  276. } else {
  277. VE.navigation.switch_menu('first');
  278. }
  279. }
  280. VE.navigation.switch_menu = function(position){
  281. position = position || 'first'; // last
  282. if(VE.navigation.state.active_menu == 0){
  283. VE.navigation.state.active_menu = 1;
  284. VE.navigation.state.menu_selector = '.l-stat__col';
  285. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  286. $('.l-menu').removeClass('active');
  287. $('.l-stat').addClass('active');
  288. if(position == 'first'){
  289. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  290. } else {
  291. var max_index = $(VE.navigation.state.menu_selector).length-1;
  292. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  293. }
  294. } else {
  295. VE.navigation.state.active_menu = 0;
  296. VE.navigation.state.menu_selector = '.l-menu__item';
  297. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  298. $('.l-menu').addClass('active');
  299. $('.l-stat').removeClass('active');
  300. if(position == 'first'){
  301. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  302. } else {
  303. var max_index = $(VE.navigation.state.menu_selector).length-1;
  304. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  305. }
  306. }
  307. }
  308. VE.navigation.init = function(){
  309. if($('.l-menu__item.l-menu__item--active').length){
  310. // VE.navigation.switch_menu();
  311. VE.navigation.state.active_menu = 0;
  312. VE.navigation.state.menu_selector = '.l-menu__item';
  313. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  314. $('.l-menu').addClass('active');
  315. $('.l-stat').removeClass('active');
  316. } else {
  317. $('.l-stat').addClass('active');
  318. }
  319. }
  320. VE.helpers.extendPasswordFields();