PluginProbe
Post Grid Gutenberg Blocks – PostX / trunk
Post Grid Gutenberg Blocks – PostX vtrunk
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / assets / js / flexmenu.js

flexmenu.js in Post Grid Gutenberg Blocks – PostX trunk, at assets/js/flexmenu.js

197 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* jQuery.flexMenu 1.6.2
2 https://github.com/352Media/flexMenu
3 Description: If a list is too long for all items to fit on one line, display a popup menu instead.
4 Dependencies: jQuery, Modernizr (optional). Without Modernizr, the menu can only be shown on click (not hover). */
5
6 (function (factory) {
7 if (typeof define === "function" && define.amd) {
8 // AMD. Register as an anonymous module.
9 define(["jquery"], factory);
10 } else {
11 // Browser globals
12 factory(jQuery);
13 }
14 })(function ($) {
15 var windowWidth = $(window).width(); // Store the window width
16 var windowHeight = $(window).height(); // Store the window height
17 var flexObjects = [], // Array of all flexMenu objects
18 resizeTimeout;
19 // When the page is resized, adjust the flexMenus.
20 function adjustFlexMenu() {
21 if (
22 $(window).width() !== windowWidth ||
23 $(window).height() !== windowHeight
24 ) {
25 $(flexObjects).each(function () {
26 $(this)
27 .flexMenu({
28 undo: true,
29 })
30 .flexMenu(this.options);
31 });
32 windowWidth = $(window).width(); // Store the window width if it changed
33 windowHeight = $(window).height(); // Store the window height if it changed
34 }
35 }
36 function collapseAllExcept($menuToAvoid) {
37 var $activeMenus, $menusToCollapse;
38 $activeMenus = $("li.flexMenu-viewMore.active");
39 $menusToCollapse = $activeMenus.not($menuToAvoid);
40 $menusToCollapse.removeClass("active").find("> ul").hide();
41 }
42 $(window).on("resize", function () {
43 clearTimeout(resizeTimeout);
44 resizeTimeout = setTimeout(function () {
45 adjustFlexMenu();
46 }, 200);
47 });
48 $.fn.flexMenu = function (options) {
49 var checkFlexObject,
50 s = $.extend(
51 {
52 threshold: 2, // [integer] If there are this many items or fewer in the list, we will not display a "View More" link and will instead let the list break to the next line. This is useful in cases where adding a "view more" link would actually cause more things to break to the next line.
53 cutoff: 2, // [integer] If there is space for this many or fewer items outside our "more" popup, just move everything into the more menu. In that case, also use linkTextAll and linkTitleAll instead of linkText and linkTitle. To disable this feature, just set this value to 0.
54 linkText: "More", // [string] What text should we display on the "view more" link?
55 linkTitle: "View More", // [string] What should the title of the "view more" button be?
56 linkTextAll: "Menu", // [string] If we hit the cutoff, what text should we display on the "view more" link?
57 linkTitleAll: "Open/Close Menu", // [string] If we hit the cutoff, what should the title of the "view more" button be?
58 shouldApply: function () {
59 return true;
60 }, // [function] Function called before applying flexMenu. If it returns false, it will not be applied.
61 showOnHover: true, // [boolean] Should we we show the menu on hover? If not, we'll require a click. If we're on a touch device - or if Modernizr is not available - we'll ignore this setting and only show the menu on click. The reason for this is that touch devices emulate hover events in unpredictable ways, causing some taps to do nothing.
62 popupAbsolute: true, // [boolean] Should we absolutely position the popup? Usually this is a good idea. That way, the popup can appear over other content and spill outside a parent that has overflow: hidden set. If you want to do something different from this in CSS, just set this option to false.
63 popupClass: "", // [string] If this is set, this class will be added to the popup
64 undo: false, // [boolean] Move the list items back to where they were before, and remove the "View More" link.
65 },
66 options
67 );
68 this.options = s; // Set options on object
69 checkFlexObject = $.inArray(this, flexObjects); // Checks if this object is already in the flexObjects array
70 if (checkFlexObject >= 0) {
71 flexObjects.splice(checkFlexObject, 1); // Remove this object if found
72 } else {
73 flexObjects.push(this); // Add this object to the flexObjects array
74 }
75 return this.each(function () {
76 var $this = $(this),
77 $items = $this.find("> li"),
78 $firstItem = $items.first(),
79 $lastItem = $items.last(),
80 numItems = $items.length,
81 firstItemTop = Math.floor($firstItem.offset().top),
82 firstItemHeight = Math.floor($firstItem.outerHeight(true)),
83 $lastChild,
84 keepLooking,
85 $moreItem,
86 $moreLink,
87 numToRemove,
88 allInPopup = false,
89 $menu,
90 i;
91 function needsMenu($itemOfInterest) {
92 var result =
93 Math.ceil($itemOfInterest.offset().top) >=
94 firstItemTop + firstItemHeight
95 ? true
96 : false;
97 // Values may be calculated from em and give us something other than round numbers. Browsers may round these inconsistently. So, let's round numbers to make it easier to trigger flexMenu.
98 return result;
99 }
100 if (
101 needsMenu($lastItem) &&
102 numItems > s.threshold &&
103 !s.undo &&
104 $this.is(":visible") &&
105 s.shouldApply()
106 ) {
107 var $popup = $(
108 '<ul class="flexMenu-popup" style="display:none;' +
109 (s.popupAbsolute ? " position: absolute;" : "") +
110 '"></ul>'
111 );
112 // Add class if popupClass option is set
113 $popup.addClass(s.popupClass);
114 // Move all list items after the first to this new popup ul
115 for (i = numItems; i > 1; i--) {
116 // Find all of the list items that have been pushed below the first item. Put those items into the popup menu. Put one additional item into the popup menu to cover situations where the last item is shorter than the "more" text.
117 $lastChild = $this.find("> li:last-child");
118 keepLooking = needsMenu($lastChild);
119 // If there only a few items left in the navigation bar, move them all to the popup menu.
120 if (i - 1 <= s.cutoff) {
121 // We've removed the ith item, so i - 1 gives us the number of items remaining.
122 $($this.children().get().reverse()).appendTo($popup);
123 allInPopup = true;
124 break;
125 }
126 if (!keepLooking) {
127 break;
128 } else {
129 $lastChild.appendTo($popup);
130 }
131 }
132 if (allInPopup) {
133 $this.append(
134 '<li class="flexMenu-viewMore flexMenu-allInPopup"><a href="#" title="' +
135 s.linkTitleAll +
136 '">' +
137 s.linkTextAll +
138 "</a></li>"
139 );
140 } else {
141 $this.append(
142 '<li class="flexMenu-viewMore"><a href="#" title="' +
143 s.linkTitle +
144 '">' +
145 s.linkText +
146 "</a></li>"
147 );
148 }
149 $moreItem = $this.find("> li.flexMenu-viewMore");
150 // Check to see whether the more link has been pushed down. This might happen if the link immediately before it is especially wide.
151 if (needsMenu($moreItem)) {
152 $this.find("> li:nth-last-child(2)").appendTo($popup);
153 }
154 // Our popup menu is currently in reverse order. Let's fix that.
155 $popup.children().each(function (i, li) {
156 $popup.prepend(li);
157 });
158 $moreItem.append($popup);
159 $moreLink = $this.find("> li.flexMenu-viewMore > a");
160 $moreLink.on("click", function (e) {
161 // Collapsing any other open flexMenu
162 collapseAllExcept($moreItem);
163 // Open and Set active the one being interacted with.
164 $popup.toggle();
165 $moreItem.toggleClass("active");
166 e.preventDefault();
167 });
168 if (
169 s.showOnHover &&
170 typeof Modernizr !== "undefined" &&
171 !Modernizr.touch
172 ) {
173 // If requireClick is false AND touch is unsupported, then show the menu on hover. If Modernizr is not available, assume that touch is unsupported. Through the magic of lazy evaluation, we can check for Modernizr and start using it in the same if statement. Reversing the order of these variables would produce an error.
174 $moreItem.hover(
175 function () {
176 $popup.show();
177 $(this).addClass("active");
178 },
179 function () {
180 $popup.hide();
181 $(this).removeClass("active");
182 }
183 );
184 }
185 } else if (s.undo && $this.find("ul.flexMenu-popup")) {
186 $menu = $this.find("ul.flexMenu-popup");
187 numToRemove = $menu.find("li").length;
188 for (i = 1; i <= numToRemove; i++) {
189 $menu.find("> li:first-child").appendTo($this);
190 }
191 $menu.remove();
192 $this.find("> li.flexMenu-viewMore").remove();
193 }
194 });
195 };
196 });
197