events.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. notifications: {},
  12. callbacks: { // events callback functions
  13. click: {},
  14. mouseover: {},
  15. mouseout: {},
  16. keypress: {}
  17. },
  18. helpers: {}, // simple handy methods
  19. tmp: {
  20. sort_par: 'sort-name',
  21. sort_direction: -1,
  22. sort_as_int: 0,
  23. form_changed: 0,
  24. search_activated: 0,
  25. search_display_interval: 0,
  26. search_hover_interval: 0
  27. }
  28. };
  29. /*
  30. * Main method that invokes further event processing
  31. * @param root is root HTML DOM element that. Pass HTML DOM Element or css selector
  32. * @param event_type (eg: click, mouseover etc..)
  33. */
  34. VE.core.register = function(root, event_type) {
  35. var root = !root ? 'body' : root; // if elm is not passed just bind events to body DOM Element
  36. var event_type = !event_type ? 'click' : event_type; // set event type to "click" by default
  37. $(root).bind(event_type, function(evt) {
  38. var elm = $(evt.target);
  39. VE.core.dispatch(evt, elm, event_type); // dispatch captured event
  40. });
  41. }
  42. /*
  43. * Dispatch event that was previously registered
  44. * @param evt related event object
  45. * @param elm that was catched
  46. * @param event_type (eg: click, mouseover etc..)
  47. */
  48. VE.core.dispatch = function(evt, elm, event_type) {
  49. if ('undefined' == typeof VE.callbacks[event_type]) {
  50. return VE.helpers.warn('There is no corresponding object that should contain event callbacks for "'+event_type+'" event type');
  51. }
  52. // get class of element
  53. var classes = $(elm).attr('class');
  54. // if no classes are attached, then just stop any further processings
  55. if (!classes) {
  56. return; // no classes assigned
  57. }
  58. // split the classes and check if it related to function
  59. $(classes.split(/\s/)).each(function(i, key) {
  60. VE.callbacks[event_type][key] && VE.callbacks[event_type][key](evt, elm);
  61. });
  62. }
  63. //
  64. // CALLBACKS
  65. //
  66. /*
  67. * Suspend action
  68. */
  69. VE.callbacks.click.do_suspend = function(evt, elm) {
  70. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  71. var url = $('input[name="suspend_url"]', ref).val();
  72. var dialog_elm = ref.find('.confirmation-text-suspention');
  73. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  74. }
  75. /*
  76. * Unsuspend action
  77. */
  78. VE.callbacks.click.do_unsuspend = function(evt, elm) {
  79. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  80. var url = $('input[name="unsuspend_url"]', ref).val();
  81. var dialog_elm = ref.find('.confirmation-text-suspention');
  82. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  83. }
  84. /*
  85. * Delete action
  86. */
  87. VE.callbacks.click.do_delete = function(evt, elm) {
  88. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  89. var url = $('input[name="delete_url"]', ref).val();
  90. var dialog_elm = ref.find('.confirmation-text-delete');
  91. VE.helpers.createConfirmationDialog(dialog_elm, '', url);
  92. }
  93. /*
  94. * Create dialog box on the fly
  95. * @param elm Element which contains the dialog contents
  96. * @param dialog_title
  97. * @param confirmed_location_url URL that will be redirected to if user hit "OK"
  98. * @param custom_config Custom configuration parameters passed to dialog initialization (optional)
  99. */
  100. VE.helpers.createConfirmationDialog = function(elm, dialog_title, confirmed_location_url, custom_config) {
  101. var custom_config = !custom_config ? {} : custom_config;
  102. var config = {
  103. modal: true,
  104. autoOpen: true,
  105. width: 360,
  106. title: dialog_title,
  107. close: function() {
  108. $(this).dialog("destroy");
  109. },
  110. buttons: {
  111. "OK": function(event, ui) {
  112. location.href = confirmed_location_url;
  113. },
  114. "Cancel": function() {
  115. $(this).dialog("close");
  116. $(this).dialog("destroy");
  117. }
  118. }
  119. }
  120. config = $.extend(config, custom_config);
  121. var reference_copied = $(elm).clone();
  122. $(reference_copied).dialog(config);
  123. }
  124. /*
  125. * Simple debug output
  126. */
  127. VE.helpers.warn = function(msg) {
  128. alert('WARNING: ' + msg);
  129. }
  130. VE.helpers.extendPasswordFields = function() {
  131. var references = ['.password'];
  132. $(document).ready(function() {
  133. $(references).each(function(i, ref) {
  134. VE.helpers.initAdditionalPasswordFieldElements(ref);
  135. });
  136. });
  137. }
  138. VE.helpers.initAdditionalPasswordFieldElements = function(ref) {
  139. var enabled = $.cookie('hide_passwords') == '1' ? true : false;
  140. if (enabled) {
  141. VE.helpers.hidePasswordFieldText(ref);
  142. }
  143. $(ref).prop('autocomplete', 'off');
  144. var enabled_html = enabled ? '' : 'show-passwords-enabled-action';
  145. 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>';
  146. $(ref).after(html);
  147. }
  148. VE.helpers.hidePasswordFieldText = function(ref) {
  149. $.cookie('hide_passwords', '1', { expires: 365, path: '/' });
  150. $(ref).prop('type', 'password');
  151. }
  152. VE.helpers.revealPasswordFieldText = function(ref) {
  153. $.cookie('hide_passwords', '0', { expires: 365, path: '/' });
  154. $(ref).prop('type', 'text');
  155. }
  156. VE.helpers.toggleHiddenPasswordText = function(ref, triggering_elm) {
  157. $(triggering_elm).toggleClass('show-passwords-enabled-action');
  158. if ($(ref).prop('type') == 'text') {
  159. VE.helpers.hidePasswordFieldText(ref);
  160. }
  161. else {
  162. VE.helpers.revealPasswordFieldText(ref);
  163. }
  164. }
  165. VE.helpers.refresh_timer = {
  166. speed: 50,
  167. degr: 180,
  168. right: 0,
  169. left: 0,
  170. periodical: 0,
  171. first: 1,
  172. start: function(){
  173. this.periodical = setInterval(function(){VE.helpers.refresh_timer.turn()}, this.speed);
  174. },
  175. stop: function(){
  176. clearTimeout(this.periodical);
  177. },
  178. turn: function(){
  179. this.degr += 1;
  180. if (this.first && this.degr >= 361){
  181. this.first = 0;
  182. this.degr = 180;
  183. this.left.css({'-webkit-transform': 'rotate(180deg)'});
  184. this.left.css({'transform': 'rotate(180deg)'});
  185. this.left.children('.loader-half').addClass('dark');
  186. }
  187. if (!this.first && this.degr >= 360){
  188. this.first = 1;
  189. this.degr = 180;
  190. this.left.css({'-webkit-transform': 'rotate(0deg)'});
  191. this.right.css({'-webkit-transform': 'rotate(180deg)'});
  192. this.left.css({'transform': 'rotate(0deg)'});
  193. this.right.css({'transform': 'rotate(180deg)'});
  194. this.left.children('.loader-half').removeClass('dark');
  195. this.stop();
  196. location.reload();
  197. }
  198. if (this.first){
  199. this.right.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  200. this.right.css({'transform': 'rotate('+this.degr+'deg)'});
  201. }
  202. else{
  203. this.left.css({'-webkit-transform': 'rotate('+this.degr+'deg)'});
  204. this.left.css({'transform': 'rotate('+this.degr+'deg)'});
  205. }
  206. }
  207. }
  208. VE.navigation.enter_focused = function() {
  209. if($(VE.navigation.state.menu_selector + '.focus a').attr('href')){
  210. location.href=($(VE.navigation.state.menu_selector + '.focus a').attr('href'));
  211. }
  212. }
  213. /*
  214. VE.navigation.move_focus_left = function(){
  215. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  216. if(index == -1)
  217. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  218. if(index > 0){
  219. $(VE.navigation.state.menu_selector).removeClass('focus');
  220. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  221. } else {
  222. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  223. }
  224. }
  225. VE.navigation.move_focus_right = function(){
  226. var max_index = $(VE.navigation.state.menu_selector).length-1;
  227. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  228. if(index == -1)
  229. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  230. if(index < max_index){
  231. $(VE.navigation.state.menu_selector).removeClass('focus');
  232. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  233. }
  234. }
  235. VE.navigation.switch_menu = function(){
  236. if(VE.navigation.state.active_menu == 0){
  237. VE.navigation.state.active_menu = 1;
  238. VE.navigation.state.menu_selector = '.l-stat__col';
  239. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  240. $('.l-menu').removeClass('active');
  241. $('.l-stat').addClass('active');
  242. } else {
  243. VE.navigation.state.active_menu = 0;
  244. VE.navigation.state.menu_selector = '.l-menu__item';
  245. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  246. $('.l-menu').addClass('active');
  247. $('.l-stat').removeClass('active');
  248. }
  249. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  250. if(index == -1){
  251. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  252. if(index == -1)
  253. index = 0;
  254. $($(VE.navigation.state.menu_selector)[index]).addClass('focus');
  255. }
  256. }
  257. */
  258. VE.navigation.move_focus_left = function(){
  259. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  260. if(index == -1)
  261. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  262. $(VE.navigation.state.menu_selector).removeClass('focus');
  263. if(index > 0){
  264. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  265. } else {
  266. VE.navigation.switch_menu('last');
  267. }
  268. }
  269. VE.navigation.move_focus_right = function(){
  270. var max_index = $(VE.navigation.state.menu_selector).length-1;
  271. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  272. if(index == -1)
  273. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  274. $(VE.navigation.state.menu_selector).removeClass('focus');
  275. if(index < max_index){
  276. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  277. } else {
  278. VE.navigation.switch_menu('first');
  279. }
  280. }
  281. VE.navigation.switch_menu = function(position){
  282. position = position || 'first'; // last
  283. if(VE.navigation.state.active_menu == 0){
  284. VE.navigation.state.active_menu = 1;
  285. VE.navigation.state.menu_selector = '.l-stat__col';
  286. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  287. $('.l-menu').removeClass('active');
  288. $('.l-stat').addClass('active');
  289. if(position == 'first'){
  290. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  291. } else {
  292. var max_index = $(VE.navigation.state.menu_selector).length-1;
  293. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  294. }
  295. } else {
  296. VE.navigation.state.active_menu = 0;
  297. VE.navigation.state.menu_selector = '.l-menu__item';
  298. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  299. $('.l-menu').addClass('active');
  300. $('.l-stat').removeClass('active');
  301. if(position == 'first'){
  302. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  303. } else {
  304. var max_index = $(VE.navigation.state.menu_selector).length-1;
  305. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  306. }
  307. }
  308. }
  309. VE.notifications.get_list = function(){
  310. /// TODO get notifications only once
  311. $.ajax({
  312. url: "/list/notifications/?ajax=1",
  313. dataType: "json"
  314. }).done(function(data) {
  315. var acc = [];
  316. $.each(data, function(i, elm){
  317. var tpl = Tpl.get('notification', 'WEB');
  318. if(elm.ACK == 'no')
  319. tpl.set(':UNSEEN', 'unseen');
  320. else
  321. tpl.set(':UNSEEN', '');
  322. tpl.set(':ID', elm.ID);
  323. tpl.set(':TYPE', elm.TYPE);
  324. tpl.set(':TOPIC', elm.TOPIC);
  325. tpl.set(':NOTICE', elm.NOTICE);
  326. acc.push(tpl.finalize());
  327. });
  328. $('.notification-container').html(acc.done()).show();
  329. $('.notification-container .mark-seen').click(function(event){
  330. /// TODO add token
  331. VE.notifications.mark_seen($(event.target).attr('id').replace("notification-", ""));
  332. // VE.notifications.delete($(event.target).attr('id').replace("notification-", ""));
  333. });
  334. });
  335. }
  336. VE.notifications.delete = function(id){
  337. $('#notification-'+id).parents('li').remove();
  338. $.ajax({
  339. url: "/delete/notification/?delete=1&notification_id="+id+"&token="+$('#token').attr('token')
  340. });
  341. }
  342. VE.notifications.mark_seen = function(id){
  343. $('#notification-'+id).parents('li').removeClass('unseen');
  344. $.ajax({
  345. url: "/delete/notification/?notification_id="+id+"&token="+$('#token').attr('token')
  346. });
  347. if($('.notification-container .unseen').length == 0)
  348. $('.l-profile__notifications').removeClass('updates');
  349. }
  350. VE.navigation.init = function(){
  351. if($('.l-menu__item.l-menu__item--active').length){
  352. // VE.navigation.switch_menu();
  353. VE.navigation.state.active_menu = 0;
  354. VE.navigation.state.menu_selector = '.l-menu__item';
  355. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  356. $('.l-menu').addClass('active');
  357. $('.l-stat').removeClass('active');
  358. } else {
  359. $('.l-stat').addClass('active');
  360. }
  361. }
  362. VE.helpers.extendPasswordFields();