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