src/pyams_skin/resources/js/myams-menus.js
changeset 557 bca7a7e058a3
equal deleted inserted replaced
-1:000000000000 557:bca7a7e058a3
       
     1 /**
       
     2  * MyAMS menus management
       
     3  */
       
     4 (function($, globals) {
       
     5 
       
     6 	var ams = globals.MyAMS;
       
     7 
       
     8 	$.fn.extend({
       
     9 
       
    10 		/**
       
    11 		 * Context menu handler
       
    12 		 */
       
    13 		contextMenu: function(settings) {
       
    14 
       
    15 			function getMenuPosition(mouse, direction, scrollDir) {
       
    16 				var win = $(window)[direction](),
       
    17 					menu = $(settings.menuSelector)[direction](),
       
    18 					position = mouse;
       
    19 				// opening menu would pass the side of the page
       
    20 				if (mouse + menu > win && menu < mouse) {
       
    21 					position -= menu;
       
    22 				}
       
    23 				return position;
       
    24 			}
       
    25 
       
    26 			return this.each(function () {
       
    27 
       
    28 				// Open context menu
       
    29 				$('a', $(settings.menuSelector)).each(function() {
       
    30 					$(this).data('ams-context-menu', true);
       
    31 				});
       
    32 				$(this).on("contextmenu", function (e) {
       
    33 					// return native menu if pressing control
       
    34 					if (e.ctrlKey) {
       
    35 						return;
       
    36 					}
       
    37 					// open menu
       
    38 					$(settings.menuSelector).data("invokedOn", $(e.target))
       
    39 											.show()
       
    40 											.css({
       
    41 												position: 'fixed',
       
    42 												left: getMenuPosition(e.clientX, 'width', 'scrollLeft') - 10,
       
    43 												top: getMenuPosition(e.clientY, 'height', 'scrollTop') - 10
       
    44 											})
       
    45 											.off('click')
       
    46 											.on('click', function (e) {
       
    47 												$(this).hide();
       
    48 												var invokedOn = $(this).data("invokedOn");
       
    49 												var selectedMenu = $(e.target);
       
    50 												settings.menuSelected.call(this, invokedOn, selectedMenu);
       
    51 												ams.event && ams.event.stop(e);
       
    52 											});
       
    53 					return false;
       
    54 				});
       
    55 
       
    56 				// make sure menu closes on any click
       
    57 				$(document).click(function () {
       
    58 					$(settings.menuSelector).hide();
       
    59 				});
       
    60 			});
       
    61 		},
       
    62 
       
    63 		/*
       
    64 		 * Main menus manager
       
    65 		 */
       
    66 		myams_menu: function(options) {
       
    67 			// Extend our default options with those provided
       
    68 			var defaults = {
       
    69 				accordion : true,
       
    70 				speed : 200,
       
    71 				closedSign : '<em class="fa fa-angle-down"></em>',
       
    72 				openedSign : '<em class="fa fa-angle-up"></em>'
       
    73 			};
       
    74 			var settings = $.extend({}, defaults, options);
       
    75 
       
    76 			// Assign current element to variable, in this case is UL element
       
    77 			var menu = $(this);
       
    78 
       
    79 			// Add a mark [+] to a multilevel menu
       
    80 			menu.find("LI").each(function() {
       
    81 				var menuItem = $(this);
       
    82 				if (menuItem.find("UL").size() > 0) {
       
    83 
       
    84 					// add the multilevel sign next to the link
       
    85 					menuItem.find("A:first")
       
    86 							 .append("<b class='collapse-sign'>" + settings.closedSign + "</b>");
       
    87 
       
    88 					// avoid jumping to the top of the page when the href is an #
       
    89 					var firstLink = menuItem.find("A:first");
       
    90 					if (firstLink.attr('href') === "#") {
       
    91 						firstLink.click(function() {
       
    92 							return false;
       
    93 						});
       
    94 					}
       
    95 				}
       
    96 			});
       
    97 
       
    98 			// Open active level
       
    99 			menu.find("LI.active").each(function() {
       
   100 				var activeParent = $(this).parents('UL');
       
   101 				var activeItem = activeParent.parent('LI');
       
   102 				activeParent.slideDown(settings.speed);
       
   103 				activeItem.find("b:first").html(settings.openedSign);
       
   104 				activeItem.addClass("open");
       
   105 			});
       
   106 
       
   107 			menu.find("LI A").on('click', function() {
       
   108 				var link = $(this);
       
   109 				if (link.hasClass('active')) {
       
   110 					return;
       
   111 				}
       
   112 				var href = link.attr('href').replace(/^#/,'');
       
   113 				var parentUL = link.parent().find("UL");
       
   114 				if (settings.accordion) {
       
   115 					var parents = link.parent().parents("UL");
       
   116 					var visible = menu.find("UL:visible");
       
   117 					visible.each(function(visibleIndex) {
       
   118 						var close = true;
       
   119 						parents.each(function(parentIndex) {
       
   120 							if (parents[parentIndex] === visible[visibleIndex]) {
       
   121 								close = false;
       
   122 								return false;
       
   123 							}
       
   124 						});
       
   125 						if (close) {
       
   126 							if (parentUL !== visible[visibleIndex]) {
       
   127 								var visibleItem = $(visible[visibleIndex]);
       
   128 								if (href || !visibleItem.hasClass('active')) {
       
   129 									visibleItem.slideUp(settings.speed, function () {
       
   130 										$(this).parent("LI")
       
   131 											   .removeClass('open')
       
   132 											   .find("B:first")
       
   133 											   .delay(settings.speed)
       
   134 											   .html(settings.closedSign);
       
   135 									});
       
   136 								}
       
   137 							}
       
   138 						}
       
   139 					});
       
   140 				}
       
   141 				var firstUL = link.parent().find("UL:first");
       
   142 				if (!href && firstUL.is(":visible") && !firstUL.hasClass("active")) {
       
   143 					firstUL.slideUp(settings.speed, function() {
       
   144 						link.parent("LI")
       
   145 							.removeClass("open")
       
   146 							.find("B:first")
       
   147 							.delay(settings.speed)
       
   148 							.html(settings.closedSign);
       
   149 					});
       
   150 				} else /*if (link.attr('href') !== location.hash)*/ {
       
   151 					firstUL.slideDown(settings.speed, function() {
       
   152 						link.parent("LI")
       
   153 							.addClass("open")
       
   154 							.find("B:first")
       
   155 							.delay(settings.speed)
       
   156 							.html(settings.openedSign);
       
   157 					});
       
   158 				}
       
   159 			});
       
   160 		}
       
   161 	});
       
   162 
       
   163 })(jQuery, this);