admin_menu.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @file
  3. */
  4. (function ($) {
  5. Drupal.admin = Drupal.admin || {};
  6. Drupal.admin.behaviors = Drupal.admin.behaviors || {};
  7. Drupal.admin.hashes = Drupal.admin.hashes || {};
  8. /**
  9. * Core behavior for Administration menu.
  10. *
  11. * Test whether there is an administration menu is in the output and execute all
  12. * registered behaviors.
  13. */
  14. Drupal.behaviors.adminMenu = {
  15. attach: function (context, settings) {
  16. // Initialize settings.
  17. settings.admin_menu = $.extend({
  18. suppress: false,
  19. margin_top: false,
  20. position_fixed: false,
  21. tweak_modules: false,
  22. tweak_permissions: false,
  23. tweak_tabs: false,
  24. destination: '',
  25. basePath: settings.basePath,
  26. hash: 0,
  27. replacements: {}
  28. }, settings.admin_menu || {});
  29. // Check whether administration menu should be suppressed.
  30. if (settings.admin_menu.suppress) {
  31. return;
  32. }
  33. var $adminMenu = $('#admin-menu:not(.admin-menu-processed)', context);
  34. // Client-side caching; if administration menu is not in the output, it is
  35. // fetched from the server and cached in the browser.
  36. if (!$adminMenu.length && settings.admin_menu.hash) {
  37. Drupal.admin.getCache(settings.admin_menu.hash, function (response) {
  38. if (typeof response == 'string' && response.length > 0) {
  39. $('body', context).append(response);
  40. }
  41. var $adminMenu = $('#admin-menu:not(.admin-menu-processed)', context);
  42. // Apply our behaviors.
  43. Drupal.admin.attachBehaviors(context, settings, $adminMenu);
  44. // Allow resize event handlers to recalculate sizes/positions.
  45. $(window).triggerHandler('resize');
  46. });
  47. }
  48. // If the menu is in the output already, this means there is a new version.
  49. else {
  50. // Apply our behaviors.
  51. Drupal.admin.attachBehaviors(context, settings, $adminMenu);
  52. }
  53. }
  54. };
  55. /**
  56. * Collapse fieldsets on Modules page.
  57. */
  58. Drupal.behaviors.adminMenuCollapseModules = {
  59. attach: function (context, settings) {
  60. if (settings.admin_menu.tweak_modules) {
  61. $('#system-modules fieldset:not(.collapsed)', context).addClass('collapsed');
  62. }
  63. }
  64. };
  65. /**
  66. * Collapse modules on Permissions page.
  67. */
  68. Drupal.behaviors.adminMenuCollapsePermissions = {
  69. attach: function (context, settings) {
  70. if (settings.admin_menu.tweak_permissions) {
  71. // Freeze width of first column to prevent jumping.
  72. $('#permissions th:first', context).css({ width: $('#permissions th:first', context).width() });
  73. // Attach click handler.
  74. $modules = $('#permissions tr:has(td.module)', context).once('admin-menu-tweak-permissions', function () {
  75. var $module = $(this);
  76. $module.bind('click.admin-menu', function () {
  77. // @todo Replace with .nextUntil() in jQuery 1.4.
  78. $module.nextAll().each(function () {
  79. var $row = $(this);
  80. if ($row.is(':has(td.module)')) {
  81. return false;
  82. }
  83. $row.toggleClass('element-hidden');
  84. });
  85. });
  86. });
  87. // Collapse all but the targeted permission rows set.
  88. if (window.location.hash.length) {
  89. $modules = $modules.not(':has(' + window.location.hash + ')');
  90. }
  91. $modules.trigger('click.admin-menu');
  92. }
  93. }
  94. };
  95. /**
  96. * Apply margin to page.
  97. *
  98. * Note that directly applying marginTop does not work in IE. To prevent
  99. * flickering/jumping page content with client-side caching, this is a regular
  100. * Drupal behavior.
  101. */
  102. Drupal.behaviors.adminMenuMarginTop = {
  103. attach: function (context, settings) {
  104. if (!settings.admin_menu.suppress && settings.admin_menu.margin_top) {
  105. $('body:not(.admin-menu)', context).addClass('admin-menu');
  106. }
  107. }
  108. };
  109. /**
  110. * Retrieve content from client-side cache.
  111. *
  112. * @param hash
  113. * The md5 hash of the content to retrieve.
  114. * @param onSuccess
  115. * A callback function invoked when the cache request was successful.
  116. */
  117. Drupal.admin.getCache = function (hash, onSuccess) {
  118. if (Drupal.admin.hashes.hash !== undefined) {
  119. return Drupal.admin.hashes.hash;
  120. }
  121. $.ajax({
  122. cache: true,
  123. type: 'GET',
  124. dataType: 'text', // Prevent auto-evaluation of response.
  125. global: false, // Do not trigger global AJAX events.
  126. url: Drupal.settings.admin_menu.basePath.replace(/admin_menu/, 'js/admin_menu/cache/' + hash),
  127. success: onSuccess,
  128. complete: function (XMLHttpRequest, status) {
  129. Drupal.admin.hashes.hash = status;
  130. }
  131. });
  132. };
  133. /**
  134. * TableHeader callback to determine top viewport offset.
  135. *
  136. * @see toolbar.js
  137. */
  138. Drupal.admin.height = function () {
  139. var $adminMenu = $('#admin-menu');
  140. var height = $adminMenu.outerHeight();
  141. // In IE, Shadow filter adds some extra height, so we need to remove it from
  142. // the returned height.
  143. if ($adminMenu.css('filter') && $adminMenu.css('filter').match(/DXImageTransform\.Microsoft\.Shadow/)) {
  144. height -= $adminMenu.get(0).filters.item("DXImageTransform.Microsoft.Shadow").strength;
  145. }
  146. return height;
  147. };
  148. /**
  149. * @defgroup admin_behaviors Administration behaviors.
  150. * @{
  151. */
  152. /**
  153. * Attach administrative behaviors.
  154. */
  155. Drupal.admin.attachBehaviors = function (context, settings, $adminMenu) {
  156. if ($adminMenu.length) {
  157. $adminMenu.addClass('admin-menu-processed');
  158. $.each(Drupal.admin.behaviors, function () {
  159. this(context, settings, $adminMenu);
  160. });
  161. }
  162. };
  163. /**
  164. * Apply 'position: fixed'.
  165. */
  166. Drupal.admin.behaviors.positionFixed = function (context, settings, $adminMenu) {
  167. if (settings.admin_menu.position_fixed) {
  168. $adminMenu.addClass('admin-menu-position-fixed');
  169. $adminMenu.css('position', 'fixed');
  170. }
  171. };
  172. /**
  173. * Move page tabs into administration menu.
  174. */
  175. Drupal.admin.behaviors.pageTabs = function (context, settings, $adminMenu) {
  176. if (settings.admin_menu.tweak_tabs) {
  177. var $tabs = $(context).find('ul.tabs.primary');
  178. $adminMenu.find('#admin-menu-wrapper > ul').eq(1)
  179. .append($tabs.find('li').addClass('admin-menu-tab'));
  180. $(context).find('ul.tabs.secondary')
  181. .appendTo('#admin-menu-wrapper > ul > li.admin-menu-tab.active')
  182. .removeClass('secondary');
  183. $tabs.remove();
  184. }
  185. };
  186. /**
  187. * Perform dynamic replacements in cached menu.
  188. */
  189. Drupal.admin.behaviors.replacements = function (context, settings, $adminMenu) {
  190. for (var item in settings.admin_menu.replacements) {
  191. $(item, $adminMenu).html(settings.admin_menu.replacements[item]);
  192. }
  193. };
  194. /**
  195. * Inject destination query strings for current page.
  196. */
  197. Drupal.admin.behaviors.destination = function (context, settings, $adminMenu) {
  198. if (settings.admin_menu.destination) {
  199. $('a.admin-menu-destination', $adminMenu).each(function () {
  200. this.search += (!this.search.length ? '?' : '&') + Drupal.settings.admin_menu.destination;
  201. });
  202. }
  203. };
  204. /**
  205. * Apply JavaScript-based hovering behaviors.
  206. *
  207. * @todo This has to run last. If another script registers additional behaviors
  208. * it will not run last.
  209. */
  210. Drupal.admin.behaviors.hover = function (context, settings, $adminMenu) {
  211. // Delayed mouseout.
  212. $('li.expandable', $adminMenu).hover(
  213. function () {
  214. // Stop the timer.
  215. clearTimeout(this.sfTimer);
  216. // Display child lists.
  217. $('> ul', this)
  218. .css({left: 'auto', display: 'block'})
  219. // Immediately hide nephew lists.
  220. .parent().siblings('li').children('ul').css({left: '-999em', display: 'none'});
  221. },
  222. function () {
  223. // Start the timer.
  224. var uls = $('> ul', this);
  225. this.sfTimer = setTimeout(function () {
  226. uls.css({left: '-999em', display: 'none'});
  227. }, 400);
  228. }
  229. );
  230. };
  231. /**
  232. * Apply the search bar functionality.
  233. */
  234. Drupal.admin.behaviors.search = function (context, settings, $adminMenu) {
  235. // @todo Add a HTML ID.
  236. var $input = $('input.admin-menu-search', $adminMenu);
  237. // Initialize the current search needle.
  238. var needle = $input.val();
  239. // Cache of all links that can be matched in the menu.
  240. var links;
  241. // Minimum search needle length.
  242. var needleMinLength = 2;
  243. // Append the results container.
  244. var $results = $('<div />').insertAfter($input);
  245. /**
  246. * Executes the search upon user input.
  247. */
  248. function keyupHandler() {
  249. var matches, $html, value = $(this).val();
  250. // Only proceed if the search needle has changed.
  251. if (value !== needle) {
  252. needle = value;
  253. // Initialize the cache of menu links upon first search.
  254. if (!links && needle.length >= needleMinLength) {
  255. // @todo Limit to links in dropdown menus; i.e., skip menu additions.
  256. links = buildSearchIndex($adminMenu.find('li:not(.admin-menu-action, .admin-menu-action li) > a'));
  257. }
  258. // Empty results container when deleting search text.
  259. if (needle.length < needleMinLength) {
  260. $results.empty();
  261. }
  262. // Only search if the needle is long enough.
  263. if (needle.length >= needleMinLength && links) {
  264. matches = findMatches(needle, links);
  265. // Build the list in a detached DOM node.
  266. $html = buildResultsList(matches);
  267. // Display results.
  268. $results.empty().append($html);
  269. }
  270. }
  271. }
  272. /**
  273. * Builds the search index.
  274. */
  275. function buildSearchIndex($links) {
  276. return $links
  277. .map(function () {
  278. var text = (this.textContent || this.innerText);
  279. // Skip menu entries that do not contain any text (e.g., the icon).
  280. if (typeof text === 'undefined') {
  281. return;
  282. }
  283. return {
  284. text: text,
  285. textMatch: text.toLowerCase(),
  286. element: this
  287. };
  288. });
  289. }
  290. /**
  291. * Searches the index for a given needle and returns matching entries.
  292. */
  293. function findMatches(needle, links) {
  294. var needleMatch = needle.toLowerCase();
  295. // Select matching links from the cache.
  296. return $.grep(links, function (link) {
  297. return link.textMatch.indexOf(needleMatch) !== -1;
  298. });
  299. }
  300. /**
  301. * Builds the search result list in a detached DOM node.
  302. */
  303. function buildResultsList(matches) {
  304. var $html = $('<ul class="dropdown admin-menu-search-results" />');
  305. $.each(matches, function () {
  306. var result = this.text;
  307. var $element = $(this.element);
  308. // Check whether there is a top-level category that can be prepended.
  309. var $category = $element.closest('#admin-menu-wrapper > ul > li');
  310. var categoryText = $category.find('> a').text()
  311. if ($category.length && categoryText) {
  312. result = categoryText + ': ' + result;
  313. }
  314. var $result = $('<li><a href="' + $element.attr('href') + '">' + result + '</a></li>');
  315. $result.data('original-link', $(this.element).parent());
  316. $html.append($result);
  317. });
  318. return $html;
  319. }
  320. /**
  321. * Highlights selected result.
  322. */
  323. function resultsHandler(e) {
  324. var $this = $(this);
  325. var show = e.type === 'mouseenter' || e.type === 'focusin';
  326. $this.trigger(show ? 'showPath' : 'hidePath', [this]);
  327. }
  328. /**
  329. * Closes the search results and clears the search input.
  330. */
  331. function resultsClickHandler(e, link) {
  332. var $original = $(this).data('original-link');
  333. $original.trigger('mouseleave');
  334. $input.val('').trigger('keyup');
  335. }
  336. /**
  337. * Shows the link in the menu that corresponds to a search result.
  338. */
  339. function highlightPathHandler(e, link) {
  340. if (link) {
  341. var $original = $(link).data('original-link');
  342. var show = e.type === 'showPath';
  343. // Toggle an additional CSS class to visually highlight the matching link.
  344. // @todo Consider using same visual appearance as regular hover.
  345. $original.toggleClass('highlight', show);
  346. $original.trigger(show ? 'mouseenter' : 'mouseleave');
  347. }
  348. }
  349. // Attach showPath/hidePath handler to search result entries.
  350. $results.delegate('li', 'mouseenter mouseleave focus blur', resultsHandler);
  351. // Hide the result list after a link has been clicked, useful for overlay.
  352. $results.delegate('li', 'click', resultsClickHandler);
  353. // Attach hover/active highlight behavior to search result entries.
  354. $adminMenu.delegate('.admin-menu-search-results li', 'showPath hidePath', highlightPathHandler);
  355. // Attach the search input event handler.
  356. $input.bind('keyup search', keyupHandler);
  357. };
  358. /**
  359. * @} End of "defgroup admin_behaviors".
  360. */
  361. })(jQuery);