highlight-menus.js
3.72 KB
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
/* global wsNmhData */
jQuery(function($) {
'use strict';
var $adminMenu = $('#adminmenu'),
seenOnThisPage = {},
foundNewMenus;
/**
* Flag a menu URL as seen (i.e. no longer new).
*
* This function is both debounced and throttled. As long as you continue to call it,
* the AJAX request won't be triggered. It will be sent only once the function stops
* being called for N milliseconds. Additionally, it will wait at least N milliseconds
* between requests.
*/
var flagAsSeen = (function() {
var queue = [],
timeout = null,
waitMs = 500,
isRequestInProgress = false;
function sendQueuedUrls() {
if (queue.length > 0) {
if (isRequestInProgress) {
//console.warn('A request is already in progress');
}
isRequestInProgress = true;
$.ajax(
ajaxurl,
{
'method': 'POST',
'data': {
'action': wsNmhData['flagAction'],
'_ajax_nonce': wsNmhData['flagNonce'],
'urls' : JSON.stringify(queue)
},
'complete': function() {
isRequestInProgress = false;
if (queue.length > 0) {
timeout = setTimeout(sendQueuedUrls, waitMs);
}
}
}
);
}
queue = [];
timeout = null;
}
/**
* @param string menuUrl
*/
return function(menuUrl) {
if ((menuUrl === '') || seenOnThisPage.hasOwnProperty(menuUrl)) {
return;
}
seenOnThisPage[menuUrl] = true;
//In case the AJAX request doesn't succeed for whatever reason, lets *also*
//set a session cookie that contains the seen URLs.
if ($.cookie) {
$.cookie('ws_nmh_pending_seen_urls', JSON.stringify(seenOnThisPage));
}
queue.push(menuUrl);
if (isRequestInProgress) {
return;
}
if (timeout) {
//Move the timeout forward.
clearTimeout(timeout);
}
timeout = setTimeout(sendQueuedUrls, waitMs);
}
})();
function maybeFlagItem($item) {
var shouldFlagThis = true,
shouldFlagParent = false,
$link, $flag, $parent;
if ($item.hasClass('menu-top')) {
//Only flag top level menus when they no longer have any new submenus.
shouldFlagThis = $item
.find('> ul li.ws-nmh-is-new-menu')
.not('.wp-submenu-head')
.length === 0;
} else {
//If all of the submenus on this level are seen/not new, flag the parent menu as well.
shouldFlagParent = $item.siblings('li.ws-nmh-is-new-menu').not('.wp-submenu-head').length === 0;
}
if (!shouldFlagThis) {
return;
}
$link = $item.children('a').first();
$flag = $link.find('.ws-nmh-new-menu-flag');
$item.removeClass('ws-nmh-is-new-menu');
$link.removeClass('ws-nmh-is-new-menu');
if ($flag.length > 0) {
flagAsSeen($flag.data('nmh-menu-url'));
$flag.remove();
}
if (shouldFlagParent) {
$parent = $item.parentsUntil($adminMenu, 'li');
if (($parent.length > 0) && !$parent.is($item)) {
maybeFlagItem($parent);
}
}
}
var $newItems = $adminMenu.find('.ws-nmh-new-menu-flag').closest('li').addClass('ws-nmh-is-new-menu');
foundNewMenus = $newItems.length > 0;
//Flag the current item as seen.
$newItems.filter('.current, .wp-has-current-submenu').each(function() {
maybeFlagItem($(this));
});
//Flag any hidden items as seen. The user can't actually click hidden items,
//so the parent menu would permanently stay highlighted if we didn't do this.
if (foundNewMenus) {
//We want to run after all other $(document).ready callbacks because some
//of them might hide admin menu items by calling $('selector').hide().
setTimeout(function() {
$newItems.filter(':hidden').not('.wp-submenu-head').each(function() {
maybeFlagItem($(this));
});
}, 60);
}
if (foundNewMenus) {
$adminMenu.on('mouseenter click focusin', 'li.ws-nmh-is-new-menu', function() {
maybeFlagItem($(this).closest('li'));
});
}
});