author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* WordPress Administration Navigation Menu |
|
3 |
* Interface JS functions |
|
4 |
* |
|
5 |
* @version 2.0.0 |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Administration |
|
9 | 9 |
* @output wp-admin/js/nav-menu.js |
0 | 10 |
*/ |
11 |
||
16 | 12 |
/* global menus, postboxes, columns, isRtl, ajaxurl, wpNavMenu */ |
0 | 13 |
|
14 |
(function($) { |
|
15 |
||
5 | 16 |
var api; |
17 |
||
9 | 18 |
/** |
19 |
* Contains all the functions to handle WordPress navigation menus administration. |
|
20 |
* |
|
21 |
* @namespace wpNavMenu |
|
22 |
*/ |
|
23 |
api = window.wpNavMenu = { |
|
0 | 24 |
|
25 |
options : { |
|
26 |
menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
globalMaxDepth: 11, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
sortableItems: '> *', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
targetTolerance: 0 |
0 | 30 |
}, |
31 |
||
32 |
menuList : undefined, // Set in init. |
|
33 |
targetList : undefined, // Set in init. |
|
34 |
menusChanged : false, |
|
35 |
isRTL: !! ( 'undefined' != typeof isRtl && isRtl ), |
|
36 |
negateIfRTL: ( 'undefined' != typeof isRtl && isRtl ) ? -1 : 1, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
lastSearch: '', |
0 | 38 |
|
39 |
// Functions that run on init. |
|
40 |
init : function() { |
|
41 |
api.menuList = $('#menu-to-edit'); |
|
42 |
api.targetList = api.menuList; |
|
43 |
||
44 |
this.jQueryExtensions(); |
|
45 |
||
46 |
this.attachMenuEditListeners(); |
|
47 |
||
48 |
this.attachQuickSearchListeners(); |
|
49 |
this.attachThemeLocationsListeners(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
this.attachMenuSaveSubmitListeners(); |
0 | 51 |
|
52 |
this.attachTabsPanelListeners(); |
|
53 |
||
54 |
this.attachUnsavedChangesListener(); |
|
55 |
||
56 |
if ( api.menuList.length ) |
|
57 |
this.initSortables(); |
|
58 |
||
59 |
if ( menus.oneThemeLocationNoMenus ) |
|
60 |
$( '#posttype-page' ).addSelectedToMenu( api.addMenuItemToBottom ); |
|
61 |
||
62 |
this.initManageLocations(); |
|
63 |
||
64 |
this.initAccessibility(); |
|
65 |
||
66 |
this.initToggles(); |
|
5 | 67 |
|
68 |
this.initPreviewing(); |
|
0 | 69 |
}, |
70 |
||
71 |
jQueryExtensions : function() { |
|
16 | 72 |
// jQuery extensions. |
0 | 73 |
$.fn.extend({ |
74 |
menuItemDepth : function() { |
|
75 |
var margin = api.isRTL ? this.eq(0).css('margin-right') : this.eq(0).css('margin-left'); |
|
76 |
return api.pxToDepth( margin && -1 != margin.indexOf('px') ? margin.slice(0, -2) : 0 ); |
|
77 |
}, |
|
78 |
updateDepthClass : function(current, prev) { |
|
79 |
return this.each(function(){ |
|
80 |
var t = $(this); |
|
81 |
prev = prev || t.menuItemDepth(); |
|
82 |
$(this).removeClass('menu-item-depth-'+ prev ) |
|
83 |
.addClass('menu-item-depth-'+ current ); |
|
84 |
}); |
|
85 |
}, |
|
86 |
shiftDepthClass : function(change) { |
|
87 |
return this.each(function(){ |
|
88 |
var t = $(this), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
depth = t.menuItemDepth(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
newDepth = depth + change; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
t.removeClass( 'menu-item-depth-'+ depth ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
.addClass( 'menu-item-depth-'+ ( newDepth ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
if ( 0 === newDepth ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
t.find( '.is-submenu' ).hide(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
} |
0 | 98 |
}); |
99 |
}, |
|
100 |
childMenuItems : function() { |
|
101 |
var result = $(); |
|
102 |
this.each(function(){ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
var t = $(this), depth = t.menuItemDepth(), next = t.next( '.menu-item' ); |
0 | 104 |
while( next.length && next.menuItemDepth() > depth ) { |
105 |
result = result.add( next ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
next = next.next( '.menu-item' ); |
0 | 107 |
} |
108 |
}); |
|
109 |
return result; |
|
110 |
}, |
|
111 |
shiftHorizontally : function( dir ) { |
|
112 |
return this.each(function(){ |
|
113 |
var t = $(this), |
|
114 |
depth = t.menuItemDepth(), |
|
115 |
newDepth = depth + dir; |
|
116 |
||
16 | 117 |
// Change .menu-item-depth-n class. |
0 | 118 |
t.moveHorizontally( newDepth, depth ); |
119 |
}); |
|
120 |
}, |
|
121 |
moveHorizontally : function( newDepth, depth ) { |
|
122 |
return this.each(function(){ |
|
123 |
var t = $(this), |
|
124 |
children = t.childMenuItems(), |
|
125 |
diff = newDepth - depth, |
|
126 |
subItemText = t.find('.is-submenu'); |
|
127 |
||
16 | 128 |
// Change .menu-item-depth-n class. |
0 | 129 |
t.updateDepthClass( newDepth, depth ).updateParentMenuItemDBId(); |
130 |
||
16 | 131 |
// If it has children, move those too. |
0 | 132 |
if ( children ) { |
5 | 133 |
children.each(function() { |
0 | 134 |
var t = $(this), |
135 |
thisDepth = t.menuItemDepth(), |
|
136 |
newDepth = thisDepth + diff; |
|
137 |
t.updateDepthClass(newDepth, thisDepth).updateParentMenuItemDBId(); |
|
138 |
}); |
|
139 |
} |
|
140 |
||
16 | 141 |
// Show "Sub item" helper text. |
0 | 142 |
if (0 === newDepth) |
143 |
subItemText.hide(); |
|
144 |
else |
|
145 |
subItemText.show(); |
|
146 |
}); |
|
147 |
}, |
|
148 |
updateParentMenuItemDBId : function() { |
|
149 |
return this.each(function(){ |
|
150 |
var item = $(this), |
|
151 |
input = item.find( '.menu-item-data-parent-id' ), |
|
5 | 152 |
depth = parseInt( item.menuItemDepth(), 10 ), |
0 | 153 |
parentDepth = depth - 1, |
154 |
parent = item.prevAll( '.menu-item-depth-' + parentDepth ).first(); |
|
155 |
||
16 | 156 |
if ( 0 === depth ) { // Item is on the top level, has no parent. |
0 | 157 |
input.val(0); |
158 |
} else { // Find the parent item, and retrieve its object id. |
|
159 |
input.val( parent.find( '.menu-item-data-db-id' ).val() ); |
|
160 |
} |
|
161 |
}); |
|
162 |
}, |
|
163 |
hideAdvancedMenuItemFields : function() { |
|
164 |
return this.each(function(){ |
|
165 |
var that = $(this); |
|
166 |
$('.hide-column-tog').not(':checked').each(function(){ |
|
167 |
that.find('.field-' + $(this).val() ).addClass('hidden-field'); |
|
168 |
}); |
|
169 |
}); |
|
170 |
}, |
|
171 |
/** |
|
172 |
* Adds selected menu items to the menu. |
|
173 |
* |
|
9 | 174 |
* @ignore |
175 |
* |
|
0 | 176 |
* @param jQuery metabox The metabox jQuery object. |
177 |
*/ |
|
178 |
addSelectedToMenu : function(processMethod) { |
|
5 | 179 |
if ( 0 === $('#menu-to-edit').length ) { |
0 | 180 |
return false; |
181 |
} |
|
182 |
||
183 |
return this.each(function() { |
|
184 |
var t = $(this), menuItems = {}, |
|
5 | 185 |
checkboxes = ( menus.oneThemeLocationNoMenus && 0 === t.find( '.tabs-panel-active .categorychecklist li input:checked' ).length ) ? t.find( '#page-all li input[type="checkbox"]' ) : t.find( '.tabs-panel-active .categorychecklist li input:checked' ), |
186 |
re = /menu-item\[([^\]]*)/; |
|
0 | 187 |
|
188 |
processMethod = processMethod || api.addMenuItemToBottom; |
|
189 |
||
190 |
// If no items are checked, bail. |
|
191 |
if ( !checkboxes.length ) |
|
192 |
return false; |
|
193 |
||
16 | 194 |
// Show the Ajax spinner. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
t.find( '.button-controls .spinner' ).addClass( 'is-active' ); |
0 | 196 |
|
16 | 197 |
// Retrieve menu item data. |
0 | 198 |
$(checkboxes).each(function(){ |
199 |
var t = $(this), |
|
200 |
listItemDBIDMatch = re.exec( t.attr('name') ), |
|
201 |
listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10); |
|
202 |
||
203 |
if ( this.className && -1 != this.className.indexOf('add-to-top') ) |
|
204 |
processMethod = api.addMenuItemToTop; |
|
205 |
menuItems[listItemDBID] = t.closest('li').getItemData( 'add-menu-item', listItemDBID ); |
|
206 |
}); |
|
207 |
||
16 | 208 |
// Add the items. |
0 | 209 |
api.addItemToMenu(menuItems, processMethod, function(){ |
16 | 210 |
// Deselect the items and hide the Ajax spinner. |
211 |
checkboxes.prop( 'checked', false ); |
|
212 |
t.find( '.button-controls .select-all' ).prop( 'checked', false ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
t.find( '.button-controls .spinner' ).removeClass( 'is-active' ); |
0 | 214 |
}); |
215 |
}); |
|
216 |
}, |
|
217 |
getItemData : function( itemType, id ) { |
|
218 |
itemType = itemType || 'menu-item'; |
|
219 |
||
220 |
var itemData = {}, i, |
|
221 |
fields = [ |
|
222 |
'menu-item-db-id', |
|
223 |
'menu-item-object-id', |
|
224 |
'menu-item-object', |
|
225 |
'menu-item-parent-id', |
|
226 |
'menu-item-position', |
|
227 |
'menu-item-type', |
|
228 |
'menu-item-title', |
|
229 |
'menu-item-url', |
|
230 |
'menu-item-description', |
|
231 |
'menu-item-attr-title', |
|
232 |
'menu-item-target', |
|
233 |
'menu-item-classes', |
|
234 |
'menu-item-xfn' |
|
235 |
]; |
|
236 |
||
237 |
if( !id && itemType == 'menu-item' ) { |
|
238 |
id = this.find('.menu-item-data-db-id').val(); |
|
239 |
} |
|
240 |
||
241 |
if( !id ) return itemData; |
|
242 |
||
243 |
this.find('input').each(function() { |
|
244 |
var field; |
|
245 |
i = fields.length; |
|
246 |
while ( i-- ) { |
|
247 |
if( itemType == 'menu-item' ) |
|
248 |
field = fields[i] + '[' + id + ']'; |
|
249 |
else if( itemType == 'add-menu-item' ) |
|
250 |
field = 'menu-item[' + id + '][' + fields[i] + ']'; |
|
251 |
||
252 |
if ( |
|
253 |
this.name && |
|
254 |
field == this.name |
|
255 |
) { |
|
256 |
itemData[fields[i]] = this.value; |
|
257 |
} |
|
258 |
} |
|
259 |
}); |
|
260 |
||
261 |
return itemData; |
|
262 |
}, |
|
263 |
setItemData : function( itemData, itemType, id ) { // Can take a type, such as 'menu-item', or an id. |
|
264 |
itemType = itemType || 'menu-item'; |
|
265 |
||
266 |
if( !id && itemType == 'menu-item' ) { |
|
267 |
id = $('.menu-item-data-db-id', this).val(); |
|
268 |
} |
|
269 |
||
270 |
if( !id ) return this; |
|
271 |
||
272 |
this.find('input').each(function() { |
|
273 |
var t = $(this), field; |
|
274 |
$.each( itemData, function( attr, val ) { |
|
275 |
if( itemType == 'menu-item' ) |
|
276 |
field = attr + '[' + id + ']'; |
|
277 |
else if( itemType == 'add-menu-item' ) |
|
278 |
field = 'menu-item[' + id + '][' + attr + ']'; |
|
279 |
||
280 |
if ( field == t.attr('name') ) { |
|
281 |
t.val( val ); |
|
282 |
} |
|
283 |
}); |
|
284 |
}); |
|
285 |
return this; |
|
286 |
} |
|
287 |
}); |
|
288 |
}, |
|
289 |
||
290 |
countMenuItems : function( depth ) { |
|
291 |
return $( '.menu-item-depth-' + depth ).length; |
|
292 |
}, |
|
293 |
||
294 |
moveMenuItem : function( $this, dir ) { |
|
295 |
||
5 | 296 |
var items, newItemPosition, newDepth, |
297 |
menuItems = $( '#menu-to-edit li' ), |
|
0 | 298 |
menuItemsCount = menuItems.length, |
299 |
thisItem = $this.parents( 'li.menu-item' ), |
|
300 |
thisItemChildren = thisItem.childMenuItems(), |
|
301 |
thisItemData = thisItem.getItemData(), |
|
5 | 302 |
thisItemDepth = parseInt( thisItem.menuItemDepth(), 10 ), |
303 |
thisItemPosition = parseInt( thisItem.index(), 10 ), |
|
0 | 304 |
nextItem = thisItem.next(), |
305 |
nextItemChildren = nextItem.childMenuItems(), |
|
5 | 306 |
nextItemDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1, |
0 | 307 |
prevItem = thisItem.prev(), |
5 | 308 |
prevItemDepth = parseInt( prevItem.menuItemDepth(), 10 ), |
0 | 309 |
prevItemId = prevItem.getItemData()['menu-item-db-id']; |
310 |
||
311 |
switch ( dir ) { |
|
312 |
case 'up': |
|
5 | 313 |
newItemPosition = thisItemPosition - 1; |
0 | 314 |
|
16 | 315 |
// Already at top. |
0 | 316 |
if ( 0 === thisItemPosition ) |
317 |
break; |
|
318 |
||
16 | 319 |
// If a sub item is moved to top, shift it to 0 depth. |
0 | 320 |
if ( 0 === newItemPosition && 0 !== thisItemDepth ) |
321 |
thisItem.moveHorizontally( 0, thisItemDepth ); |
|
322 |
||
16 | 323 |
// If prev item is sub item, shift to match depth. |
0 | 324 |
if ( 0 !== prevItemDepth ) |
325 |
thisItem.moveHorizontally( prevItemDepth, thisItemDepth ); |
|
326 |
||
327 |
// Does this item have sub items? |
|
328 |
if ( thisItemChildren ) { |
|
5 | 329 |
items = thisItem.add( thisItemChildren ); |
16 | 330 |
// Move the entire block. |
0 | 331 |
items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); |
332 |
} else { |
|
333 |
thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); |
|
334 |
} |
|
335 |
break; |
|
336 |
case 'down': |
|
337 |
// Does this item have sub items? |
|
338 |
if ( thisItemChildren ) { |
|
5 | 339 |
items = thisItem.add( thisItemChildren ), |
0 | 340 |
nextItem = menuItems.eq( items.length + thisItemPosition ), |
341 |
nextItemChildren = 0 !== nextItem.childMenuItems().length; |
|
342 |
||
343 |
if ( nextItemChildren ) { |
|
5 | 344 |
newDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1; |
0 | 345 |
thisItem.moveHorizontally( newDepth, thisItemDepth ); |
346 |
} |
|
347 |
||
348 |
// Have we reached the bottom? |
|
349 |
if ( menuItemsCount === thisItemPosition + items.length ) |
|
350 |
break; |
|
351 |
||
352 |
items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId(); |
|
353 |
} else { |
|
16 | 354 |
// If next item has sub items, shift depth. |
0 | 355 |
if ( 0 !== nextItemChildren.length ) |
356 |
thisItem.moveHorizontally( nextItemDepth, thisItemDepth ); |
|
357 |
||
16 | 358 |
// Have we reached the bottom? |
0 | 359 |
if ( menuItemsCount === thisItemPosition + 1 ) |
360 |
break; |
|
361 |
thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId(); |
|
362 |
} |
|
363 |
break; |
|
364 |
case 'top': |
|
16 | 365 |
// Already at top. |
0 | 366 |
if ( 0 === thisItemPosition ) |
367 |
break; |
|
368 |
// Does this item have sub items? |
|
369 |
if ( thisItemChildren ) { |
|
5 | 370 |
items = thisItem.add( thisItemChildren ); |
16 | 371 |
// Move the entire block. |
0 | 372 |
items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); |
373 |
} else { |
|
374 |
thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); |
|
375 |
} |
|
376 |
break; |
|
377 |
case 'left': |
|
16 | 378 |
// As far left as possible. |
0 | 379 |
if ( 0 === thisItemDepth ) |
380 |
break; |
|
381 |
thisItem.shiftHorizontally( -1 ); |
|
382 |
break; |
|
383 |
case 'right': |
|
16 | 384 |
// Can't be sub item at top. |
0 | 385 |
if ( 0 === thisItemPosition ) |
386 |
break; |
|
16 | 387 |
// Already sub item of prevItem. |
0 | 388 |
if ( thisItemData['menu-item-parent-id'] === prevItemId ) |
389 |
break; |
|
390 |
thisItem.shiftHorizontally( 1 ); |
|
391 |
break; |
|
392 |
} |
|
393 |
$this.focus(); |
|
394 |
api.registerChange(); |
|
395 |
api.refreshKeyboardAccessibility(); |
|
396 |
api.refreshAdvancedAccessibility(); |
|
397 |
}, |
|
398 |
||
399 |
initAccessibility : function() { |
|
5 | 400 |
var menu = $( '#menu-to-edit' ); |
401 |
||
0 | 402 |
api.refreshKeyboardAccessibility(); |
403 |
api.refreshAdvancedAccessibility(); |
|
404 |
||
16 | 405 |
// Refresh the accessibility when the user comes close to the item in any way. |
5 | 406 |
menu.on( 'mouseenter.refreshAccessibility focus.refreshAccessibility touchstart.refreshAccessibility' , '.menu-item' , function(){ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
api.refreshAdvancedAccessibilityOfItem( $( this ).find( 'a.item-edit' ) ); |
5 | 408 |
} ); |
409 |
||
410 |
// We have to update on click as well because we might hover first, change the item, and then click. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
menu.on( 'click', 'a.item-edit', function() { |
5 | 412 |
api.refreshAdvancedAccessibilityOfItem( $( this ) ); |
413 |
} ); |
|
414 |
||
16 | 415 |
// Links for moving items. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
menu.on( 'click', '.menus-move', function () { |
5 | 417 |
var $this = $( this ), |
418 |
dir = $this.data( 'dir' ); |
|
419 |
||
420 |
if ( 'undefined' !== typeof dir ) { |
|
421 |
api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), dir ); |
|
422 |
} |
|
0 | 423 |
}); |
424 |
}, |
|
425 |
||
5 | 426 |
/** |
427 |
* refreshAdvancedAccessibilityOfItem( [itemToRefresh] ) |
|
428 |
* |
|
429 |
* Refreshes advanced accessibility buttons for one menu item. |
|
430 |
* Shows or hides buttons based on the location of the menu item. |
|
431 |
* |
|
16 | 432 |
* @param {Object} itemToRefresh The menu item that might need its advanced accessibility buttons refreshed |
5 | 433 |
*/ |
434 |
refreshAdvancedAccessibilityOfItem : function( itemToRefresh ) { |
|
435 |
||
16 | 436 |
// Only refresh accessibility when necessary. |
5 | 437 |
if ( true !== $( itemToRefresh ).data( 'needs_accessibility_refresh' ) ) { |
438 |
return; |
|
439 |
} |
|
440 |
||
441 |
var thisLink, thisLinkText, primaryItems, itemPosition, title, |
|
442 |
parentItem, parentItemId, parentItemName, subItems, |
|
443 |
$this = $( itemToRefresh ), |
|
444 |
menuItem = $this.closest( 'li.menu-item' ).first(), |
|
445 |
depth = menuItem.menuItemDepth(), |
|
446 |
isPrimaryMenuItem = ( 0 === depth ), |
|
447 |
itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(), |
|
448 |
position = parseInt( menuItem.index(), 10 ), |
|
449 |
prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ), |
|
450 |
prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(), |
|
451 |
prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(), |
|
452 |
totalMenuItems = $('#menu-to-edit li').length, |
|
453 |
hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length; |
|
454 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
menuItem.find( '.field-move' ).toggle( totalMenuItems > 1 ); |
5 | 456 |
|
457 |
// Where can they move this menu item? |
|
458 |
if ( 0 !== position ) { |
|
459 |
thisLink = menuItem.find( '.menus-move-up' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
thisLink.attr( 'aria-label', menus.moveUp ).css( 'display', 'inline' ); |
5 | 461 |
} |
462 |
||
463 |
if ( 0 !== position && isPrimaryMenuItem ) { |
|
464 |
thisLink = menuItem.find( '.menus-move-top' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
thisLink.attr( 'aria-label', menus.moveToTop ).css( 'display', 'inline' ); |
5 | 466 |
} |
467 |
||
468 |
if ( position + 1 !== totalMenuItems && 0 !== position ) { |
|
469 |
thisLink = menuItem.find( '.menus-move-down' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' ); |
5 | 471 |
} |
472 |
||
473 |
if ( 0 === position && 0 !== hasSameDepthSibling ) { |
|
474 |
thisLink = menuItem.find( '.menus-move-down' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' ); |
5 | 476 |
} |
477 |
||
478 |
if ( ! isPrimaryMenuItem ) { |
|
479 |
thisLink = menuItem.find( '.menus-move-left' ), |
|
480 |
thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
thisLink.attr( 'aria-label', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).text( thisLinkText ).css( 'display', 'inline' ); |
5 | 482 |
} |
483 |
||
484 |
if ( 0 !== position ) { |
|
485 |
if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) { |
|
486 |
thisLink = menuItem.find( '.menus-move-right' ), |
|
487 |
thisLinkText = menus.under.replace( '%s', prevItemNameRight ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
thisLink.attr( 'aria-label', menus.moveUnder.replace( '%s', prevItemNameRight ) ).text( thisLinkText ).css( 'display', 'inline' ); |
5 | 489 |
} |
490 |
} |
|
491 |
||
492 |
if ( isPrimaryMenuItem ) { |
|
493 |
primaryItems = $( '.menu-item-depth-0' ), |
|
494 |
itemPosition = primaryItems.index( menuItem ) + 1, |
|
495 |
totalMenuItems = primaryItems.length, |
|
496 |
||
16 | 497 |
// String together help text for primary menu items. |
5 | 498 |
title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems ); |
499 |
} else { |
|
500 |
parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(), |
|
501 |
parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), |
|
502 |
parentItemName = parentItem.find( '.menu-item-title' ).text(), |
|
503 |
subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), |
|
504 |
itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1; |
|
505 |
||
16 | 506 |
// String together help text for sub menu items. |
5 | 507 |
title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName ); |
508 |
} |
|
509 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
$this.attr( 'aria-label', title ); |
5 | 511 |
|
16 | 512 |
// Mark this item's accessibility as refreshed. |
5 | 513 |
$this.data( 'needs_accessibility_refresh', false ); |
514 |
}, |
|
515 |
||
516 |
/** |
|
517 |
* refreshAdvancedAccessibility |
|
518 |
* |
|
519 |
* Hides all advanced accessibility buttons and marks them for refreshing. |
|
520 |
*/ |
|
0 | 521 |
refreshAdvancedAccessibility : function() { |
522 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
// Hide all the move buttons by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
$( '.menu-item-settings .field-move .menus-move' ).hide(); |
0 | 525 |
|
16 | 526 |
// Mark all menu items as unprocessed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
$( 'a.item-edit' ).data( 'needs_accessibility_refresh', true ); |
0 | 528 |
|
16 | 529 |
// All open items have to be refreshed or they will show no links. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
$( '.menu-item-edit-active a.item-edit' ).each( function() { |
5 | 531 |
api.refreshAdvancedAccessibilityOfItem( this ); |
532 |
} ); |
|
0 | 533 |
}, |
534 |
||
535 |
refreshKeyboardAccessibility : function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
$( 'a.item-edit' ).off( 'focus' ).on( 'focus', function(){ |
0 | 537 |
$(this).off( 'keydown' ).on( 'keydown', function(e){ |
538 |
||
5 | 539 |
var arrows, |
540 |
$this = $( this ), |
|
541 |
thisItem = $this.parents( 'li.menu-item' ), |
|
542 |
thisItemData = thisItem.getItemData(); |
|
0 | 543 |
|
16 | 544 |
// Bail if it's not an arrow key. |
0 | 545 |
if ( 37 != e.which && 38 != e.which && 39 != e.which && 40 != e.which ) |
546 |
return; |
|
547 |
||
16 | 548 |
// Avoid multiple keydown events. |
0 | 549 |
$this.off('keydown'); |
550 |
||
16 | 551 |
// Bail if there is only one menu item. |
0 | 552 |
if ( 1 === $('#menu-to-edit li').length ) |
553 |
return; |
|
554 |
||
16 | 555 |
// If RTL, swap left/right arrows. |
5 | 556 |
arrows = { '38': 'up', '40': 'down', '37': 'left', '39': 'right' }; |
0 | 557 |
if ( $('body').hasClass('rtl') ) |
558 |
arrows = { '38' : 'up', '40' : 'down', '39' : 'left', '37' : 'right' }; |
|
559 |
||
560 |
switch ( arrows[e.which] ) { |
|
561 |
case 'up': |
|
562 |
api.moveMenuItem( $this, 'up' ); |
|
563 |
break; |
|
564 |
case 'down': |
|
565 |
api.moveMenuItem( $this, 'down' ); |
|
566 |
break; |
|
567 |
case 'left': |
|
568 |
api.moveMenuItem( $this, 'left' ); |
|
569 |
break; |
|
570 |
case 'right': |
|
571 |
api.moveMenuItem( $this, 'right' ); |
|
572 |
break; |
|
573 |
} |
|
16 | 574 |
// Put focus back on same menu item. |
0 | 575 |
$( '#edit-' + thisItemData['menu-item-db-id'] ).focus(); |
576 |
return false; |
|
577 |
}); |
|
578 |
}); |
|
579 |
}, |
|
580 |
||
5 | 581 |
initPreviewing : function() { |
582 |
// Update the item handle title when the navigation label is changed. |
|
583 |
$( '#menu-to-edit' ).on( 'change input', '.edit-menu-item-title', function(e) { |
|
584 |
var input = $( e.currentTarget ), title, titleEl; |
|
585 |
title = input.val(); |
|
586 |
titleEl = input.closest( '.menu-item' ).find( '.menu-item-title' ); |
|
587 |
// Don't update to empty title. |
|
588 |
if ( title ) { |
|
589 |
titleEl.text( title ).removeClass( 'no-title' ); |
|
590 |
} else { |
|
16 | 591 |
titleEl.text( wp.i18n._x( '(no label)', 'missing menu item navigation label' ) ).addClass( 'no-title' ); |
5 | 592 |
} |
593 |
} ); |
|
594 |
}, |
|
595 |
||
0 | 596 |
initToggles : function() { |
16 | 597 |
// Init postboxes. |
0 | 598 |
postboxes.add_postbox_toggles('nav-menus'); |
599 |
||
16 | 600 |
// Adjust columns functions for menus UI. |
0 | 601 |
columns.useCheckboxesForHidden(); |
602 |
columns.checked = function(field) { |
|
603 |
$('.field-' + field).removeClass('hidden-field'); |
|
5 | 604 |
}; |
0 | 605 |
columns.unchecked = function(field) { |
606 |
$('.field-' + field).addClass('hidden-field'); |
|
5 | 607 |
}; |
16 | 608 |
// Hide fields. |
0 | 609 |
api.menuList.hideAdvancedMenuItemFields(); |
610 |
||
611 |
$('.hide-postbox-tog').click(function () { |
|
612 |
var hidden = $( '.accordion-container li.accordion-section' ).filter(':hidden').map(function() { return this.id; }).get().join(','); |
|
613 |
$.post(ajaxurl, { |
|
614 |
action: 'closed-postboxes', |
|
615 |
hidden: hidden, |
|
616 |
closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(), |
|
617 |
page: 'nav-menus' |
|
618 |
}); |
|
619 |
}); |
|
620 |
}, |
|
621 |
||
622 |
initSortables : function() { |
|
623 |
var currentDepth = 0, originalDepth, minDepth, maxDepth, |
|
624 |
prev, next, prevBottom, nextThreshold, helperHeight, transport, |
|
625 |
menuEdge = api.menuList.offset().left, |
|
626 |
body = $('body'), maxChildDepth, |
|
627 |
menuMaxDepth = initialMenuMaxDepth(); |
|
628 |
||
5 | 629 |
if( 0 !== $( '#menu-to-edit li' ).length ) |
0 | 630 |
$( '.drag-instructions' ).show(); |
631 |
||
632 |
// Use the right edge if RTL. |
|
633 |
menuEdge += api.isRTL ? api.menuList.width() : 0; |
|
634 |
||
635 |
api.menuList.sortable({ |
|
636 |
handle: '.menu-item-handle', |
|
637 |
placeholder: 'sortable-placeholder', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
items: api.options.sortableItems, |
0 | 639 |
start: function(e, ui) { |
640 |
var height, width, parent, children, tempHolder; |
|
641 |
||
16 | 642 |
// Handle placement for RTL orientation. |
0 | 643 |
if ( api.isRTL ) |
644 |
ui.item[0].style.right = 'auto'; |
|
645 |
||
646 |
transport = ui.item.children('.menu-item-transport'); |
|
647 |
||
648 |
// Set depths. currentDepth must be set before children are located. |
|
649 |
originalDepth = ui.item.menuItemDepth(); |
|
650 |
updateCurrentDepth(ui, originalDepth); |
|
651 |
||
16 | 652 |
// Attach child elements to parent. |
653 |
// Skip the placeholder. |
|
0 | 654 |
parent = ( ui.item.next()[0] == ui.placeholder[0] ) ? ui.item.next() : ui.item; |
655 |
children = parent.childMenuItems(); |
|
656 |
transport.append( children ); |
|
657 |
||
658 |
// Update the height of the placeholder to match the moving item. |
|
659 |
height = transport.outerHeight(); |
|
16 | 660 |
// If there are children, account for distance between top of children and parent. |
0 | 661 |
height += ( height > 0 ) ? (ui.placeholder.css('margin-top').slice(0, -2) * 1) : 0; |
662 |
height += ui.helper.outerHeight(); |
|
663 |
helperHeight = height; |
|
16 | 664 |
height -= 2; // Subtract 2 for borders. |
0 | 665 |
ui.placeholder.height(height); |
666 |
||
667 |
// Update the width of the placeholder to match the moving item. |
|
668 |
maxChildDepth = originalDepth; |
|
669 |
children.each(function(){ |
|
670 |
var depth = $(this).menuItemDepth(); |
|
671 |
maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth; |
|
672 |
}); |
|
16 | 673 |
width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width. |
674 |
width += api.depthToPx(maxChildDepth - originalDepth); // Account for children. |
|
675 |
width -= 2; // Subtract 2 for borders. |
|
0 | 676 |
ui.placeholder.width(width); |
677 |
||
678 |
// Update the list of menu items. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
tempHolder = ui.placeholder.next( '.menu-item' ); |
16 | 680 |
tempHolder.css( 'margin-top', helperHeight + 'px' ); // Set the margin to absorb the placeholder. |
681 |
ui.placeholder.detach(); // Detach or jQuery UI will think the placeholder is a menu item. |
|
682 |
$(this).sortable( 'refresh' ); // The children aren't sortable. We should let jQuery UI know. |
|
683 |
ui.item.after( ui.placeholder ); // Reattach the placeholder. |
|
684 |
tempHolder.css('margin-top', 0); // Reset the margin. |
|
0 | 685 |
|
686 |
// Now that the element is complete, we can update... |
|
687 |
updateSharedVars(ui); |
|
688 |
}, |
|
689 |
stop: function(e, ui) { |
|
5 | 690 |
var children, subMenuTitle, |
691 |
depthChange = currentDepth - originalDepth; |
|
0 | 692 |
|
16 | 693 |
// Return child elements to the list. |
0 | 694 |
children = transport.children().insertAfter(ui.item); |
695 |
||
16 | 696 |
// Add "sub menu" description. |
5 | 697 |
subMenuTitle = ui.item.find( '.item-title .is-submenu' ); |
0 | 698 |
if ( 0 < currentDepth ) |
699 |
subMenuTitle.show(); |
|
700 |
else |
|
701 |
subMenuTitle.hide(); |
|
702 |
||
16 | 703 |
// Update depth classes. |
5 | 704 |
if ( 0 !== depthChange ) { |
0 | 705 |
ui.item.updateDepthClass( currentDepth ); |
706 |
children.shiftDepthClass( depthChange ); |
|
707 |
updateMenuMaxDepth( depthChange ); |
|
708 |
} |
|
16 | 709 |
// Register a change. |
0 | 710 |
api.registerChange(); |
711 |
// Update the item data. |
|
712 |
ui.item.updateParentMenuItemDBId(); |
|
713 |
||
16 | 714 |
// Address sortable's incorrectly-calculated top in Opera. |
0 | 715 |
ui.item[0].style.top = 0; |
716 |
||
16 | 717 |
// Handle drop placement for rtl orientation. |
0 | 718 |
if ( api.isRTL ) { |
719 |
ui.item[0].style.left = 'auto'; |
|
720 |
ui.item[0].style.right = 0; |
|
721 |
} |
|
722 |
||
723 |
api.refreshKeyboardAccessibility(); |
|
724 |
api.refreshAdvancedAccessibility(); |
|
725 |
}, |
|
726 |
change: function(e, ui) { |
|
727 |
// Make sure the placeholder is inside the menu. |
|
728 |
// Otherwise fix it, or we're in trouble. |
|
729 |
if( ! ui.placeholder.parent().hasClass('menu') ) |
|
730 |
(prev.length) ? prev.after( ui.placeholder ) : api.menuList.prepend( ui.placeholder ); |
|
731 |
||
732 |
updateSharedVars(ui); |
|
733 |
}, |
|
734 |
sort: function(e, ui) { |
|
735 |
var offset = ui.helper.offset(), |
|
736 |
edge = api.isRTL ? offset.left + ui.helper.width() : offset.left, |
|
737 |
depth = api.negateIfRTL * api.pxToDepth( edge - menuEdge ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
|
16 | 739 |
/* |
740 |
* Check and correct if depth is not within range. |
|
741 |
* Also, if the dragged element is dragged upwards over an item, |
|
742 |
* shift the placeholder to a child position. |
|
743 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
if ( depth > maxDepth || offset.top < ( prevBottom - api.options.targetTolerance ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
depth = maxDepth; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
} else if ( depth < minDepth ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
depth = minDepth; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
} |
0 | 749 |
|
750 |
if( depth != currentDepth ) |
|
751 |
updateCurrentDepth(ui, depth); |
|
752 |
||
16 | 753 |
// If we overlap the next element, manually shift downwards. |
0 | 754 |
if( nextThreshold && offset.top + helperHeight > nextThreshold ) { |
755 |
next.after( ui.placeholder ); |
|
756 |
updateSharedVars( ui ); |
|
5 | 757 |
$( this ).sortable( 'refreshPositions' ); |
0 | 758 |
} |
759 |
} |
|
760 |
}); |
|
761 |
||
762 |
function updateSharedVars(ui) { |
|
763 |
var depth; |
|
764 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
prev = ui.placeholder.prev( '.menu-item' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
next = ui.placeholder.next( '.menu-item' ); |
0 | 767 |
|
768 |
// Make sure we don't select the moving item. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
if( prev[0] == ui.item[0] ) prev = prev.prev( '.menu-item' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
if( next[0] == ui.item[0] ) next = next.next( '.menu-item' ); |
0 | 771 |
|
772 |
prevBottom = (prev.length) ? prev.offset().top + prev.height() : 0; |
|
773 |
nextThreshold = (next.length) ? next.offset().top + next.height() / 3 : 0; |
|
774 |
minDepth = (next.length) ? next.menuItemDepth() : 0; |
|
775 |
||
776 |
if( prev.length ) |
|
777 |
maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth; |
|
778 |
else |
|
779 |
maxDepth = 0; |
|
780 |
} |
|
781 |
||
782 |
function updateCurrentDepth(ui, depth) { |
|
783 |
ui.placeholder.updateDepthClass( depth, currentDepth ); |
|
784 |
currentDepth = depth; |
|
785 |
} |
|
786 |
||
787 |
function initialMenuMaxDepth() { |
|
788 |
if( ! body[0].className ) return 0; |
|
789 |
var match = body[0].className.match(/menu-max-depth-(\d+)/); |
|
5 | 790 |
return match && match[1] ? parseInt( match[1], 10 ) : 0; |
0 | 791 |
} |
792 |
||
793 |
function updateMenuMaxDepth( depthChange ) { |
|
794 |
var depth, newDepth = menuMaxDepth; |
|
795 |
if ( depthChange === 0 ) { |
|
796 |
return; |
|
797 |
} else if ( depthChange > 0 ) { |
|
798 |
depth = maxChildDepth + depthChange; |
|
799 |
if( depth > menuMaxDepth ) |
|
800 |
newDepth = depth; |
|
801 |
} else if ( depthChange < 0 && maxChildDepth == menuMaxDepth ) { |
|
802 |
while( ! $('.menu-item-depth-' + newDepth, api.menuList).length && newDepth > 0 ) |
|
803 |
newDepth--; |
|
804 |
} |
|
805 |
// Update the depth class. |
|
806 |
body.removeClass( 'menu-max-depth-' + menuMaxDepth ).addClass( 'menu-max-depth-' + newDepth ); |
|
807 |
menuMaxDepth = newDepth; |
|
808 |
} |
|
809 |
}, |
|
810 |
||
811 |
initManageLocations : function () { |
|
812 |
$('#menu-locations-wrap form').submit(function(){ |
|
813 |
window.onbeforeunload = null; |
|
814 |
}); |
|
815 |
$('.menu-location-menus select').on('change', function () { |
|
816 |
var editLink = $(this).closest('tr').find('.locations-edit-menu-link'); |
|
817 |
if ($(this).find('option:selected').data('orig')) |
|
818 |
editLink.show(); |
|
819 |
else |
|
820 |
editLink.hide(); |
|
821 |
}); |
|
822 |
}, |
|
823 |
||
824 |
attachMenuEditListeners : function() { |
|
825 |
var that = this; |
|
826 |
$('#update-nav-menu').bind('click', function(e) { |
|
827 |
if ( e.target && e.target.className ) { |
|
828 |
if ( -1 != e.target.className.indexOf('item-edit') ) { |
|
829 |
return that.eventOnClickEditLink(e.target); |
|
830 |
} else if ( -1 != e.target.className.indexOf('menu-save') ) { |
|
831 |
return that.eventOnClickMenuSave(e.target); |
|
832 |
} else if ( -1 != e.target.className.indexOf('menu-delete') ) { |
|
833 |
return that.eventOnClickMenuDelete(e.target); |
|
834 |
} else if ( -1 != e.target.className.indexOf('item-delete') ) { |
|
835 |
return that.eventOnClickMenuItemDelete(e.target); |
|
836 |
} else if ( -1 != e.target.className.indexOf('item-cancel') ) { |
|
837 |
return that.eventOnClickCancelLink(e.target); |
|
838 |
} |
|
839 |
} |
|
840 |
}); |
|
9 | 841 |
|
842 |
$( '#menu-name' ).on( 'input', _.debounce( function () { |
|
843 |
var menuName = $( document.getElementById( 'menu-name' ) ), |
|
844 |
menuNameVal = menuName.val(); |
|
845 |
||
846 |
if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) { |
|
847 |
// Add warning for invalid menu name. |
|
848 |
menuName.parent().addClass( 'form-invalid' ); |
|
849 |
} else { |
|
850 |
// Remove warning for valid menu name. |
|
851 |
menuName.parent().removeClass( 'form-invalid' ); |
|
852 |
} |
|
853 |
}, 500 ) ); |
|
854 |
||
0 | 855 |
$('#add-custom-links input[type="text"]').keypress(function(e){ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
$('#customlinkdiv').removeClass('form-invalid'); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
|
0 | 858 |
if ( e.keyCode === 13 ) { |
859 |
e.preventDefault(); |
|
5 | 860 |
$( '#submit-customlinkdiv' ).click(); |
0 | 861 |
} |
862 |
}); |
|
863 |
}, |
|
864 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
attachMenuSaveSubmitListeners : function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* When a navigation menu is saved, store a JSON representation of all form data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* in a single input to avoid PHP `max_input_vars` limitations. See #14134. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
$( '#update-nav-menu' ).submit( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
var navMenuData = $( '#update-nav-menu' ).serializeArray(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
$( '[name="nav-menu-data"]' ).val( JSON.stringify( navMenuData ) ); |
0 | 873 |
}); |
874 |
}, |
|
875 |
||
876 |
attachThemeLocationsListeners : function() { |
|
877 |
var loc = $('#nav-menu-theme-locations'), params = {}; |
|
5 | 878 |
params.action = 'menu-locations-save'; |
0 | 879 |
params['menu-settings-column-nonce'] = $('#menu-settings-column-nonce').val(); |
880 |
loc.find('input[type="submit"]').click(function() { |
|
881 |
loc.find('select').each(function() { |
|
882 |
params[this.name] = $(this).val(); |
|
883 |
}); |
|
5 | 884 |
loc.find( '.spinner' ).addClass( 'is-active' ); |
885 |
$.post( ajaxurl, params, function() { |
|
886 |
loc.find( '.spinner' ).removeClass( 'is-active' ); |
|
0 | 887 |
}); |
888 |
return false; |
|
889 |
}); |
|
890 |
}, |
|
891 |
||
892 |
attachQuickSearchListeners : function() { |
|
9 | 893 |
var searchTimer; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
// Prevent form submission. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
$( '#nav-menu-meta' ).on( 'submit', function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
897 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
}); |
0 | 899 |
|
9 | 900 |
$( '#nav-menu-meta' ).on( 'input', '.quick-search', function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
var $this = $( this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
$this.attr( 'autocomplete', 'off' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
if ( searchTimer ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
clearTimeout( searchTimer ); |
0 | 907 |
} |
908 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
searchTimer = setTimeout( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
api.updateQuickSearchResults( $this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
}, 500 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
}).on( 'blur', '.quick-search', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
api.lastSearch = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
}); |
0 | 915 |
}, |
916 |
||
917 |
updateQuickSearchResults : function(input) { |
|
918 |
var panel, params, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
minSearchLength = 2, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
q = input.val(); |
0 | 921 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
/* |
16 | 923 |
* Minimum characters for a search. Also avoid a new Ajax search when |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
* the pressed key (e.g. arrows) doesn't change the searched term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
if ( q.length < minSearchLength || api.lastSearch == q ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
api.lastSearch = q; |
0 | 931 |
|
932 |
panel = input.parents('.tabs-panel'); |
|
933 |
params = { |
|
934 |
'action': 'menu-quick-search', |
|
935 |
'response-format': 'markup', |
|
936 |
'menu': $('#menu').val(), |
|
937 |
'menu-settings-column-nonce': $('#menu-settings-column-nonce').val(), |
|
938 |
'q': q, |
|
939 |
'type': input.attr('name') |
|
940 |
}; |
|
941 |
||
5 | 942 |
$( '.spinner', panel ).addClass( 'is-active' ); |
0 | 943 |
|
944 |
$.post( ajaxurl, params, function(menuMarkup) { |
|
945 |
api.processQuickSearchQueryResponse(menuMarkup, params, panel); |
|
946 |
}); |
|
947 |
}, |
|
948 |
||
949 |
addCustomLink : function( processMethod ) { |
|
9 | 950 |
var url = $('#custom-menu-item-url').val().trim(), |
0 | 951 |
label = $('#custom-menu-item-name').val(); |
952 |
||
953 |
processMethod = processMethod || api.addMenuItemToBottom; |
|
954 |
||
16 | 955 |
if ( '' === url || 'https://' == url || 'http://' == url ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
$('#customlinkdiv').addClass('form-invalid'); |
0 | 957 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
} |
0 | 959 |
|
16 | 960 |
// Show the Ajax spinner. |
5 | 961 |
$( '.customlinkdiv .spinner' ).addClass( 'is-active' ); |
0 | 962 |
this.addLinkToMenu( url, label, processMethod, function() { |
16 | 963 |
// Remove the Ajax spinner. |
5 | 964 |
$( '.customlinkdiv .spinner' ).removeClass( 'is-active' ); |
16 | 965 |
// Set custom link form back to defaults. |
0 | 966 |
$('#custom-menu-item-name').val('').blur(); |
16 | 967 |
$( '#custom-menu-item-url' ).val( '' ).attr( 'placeholder', 'https://' ); |
0 | 968 |
}); |
969 |
}, |
|
970 |
||
971 |
addLinkToMenu : function(url, label, processMethod, callback) { |
|
972 |
processMethod = processMethod || api.addMenuItemToBottom; |
|
973 |
callback = callback || function(){}; |
|
974 |
||
975 |
api.addItemToMenu({ |
|
976 |
'-1': { |
|
977 |
'menu-item-type': 'custom', |
|
978 |
'menu-item-url': url, |
|
979 |
'menu-item-title': label |
|
980 |
} |
|
981 |
}, processMethod, callback); |
|
982 |
}, |
|
983 |
||
984 |
addItemToMenu : function(menuItem, processMethod, callback) { |
|
985 |
var menu = $('#menu').val(), |
|
5 | 986 |
nonce = $('#menu-settings-column-nonce').val(), |
987 |
params; |
|
0 | 988 |
|
989 |
processMethod = processMethod || function(){}; |
|
990 |
callback = callback || function(){}; |
|
991 |
||
992 |
params = { |
|
993 |
'action': 'add-menu-item', |
|
994 |
'menu': menu, |
|
995 |
'menu-settings-column-nonce': nonce, |
|
996 |
'menu-item': menuItem |
|
997 |
}; |
|
998 |
||
999 |
$.post( ajaxurl, params, function(menuMarkup) { |
|
1000 |
var ins = $('#menu-instructions'); |
|
1001 |
||
16 | 1002 |
menuMarkup = $.trim( menuMarkup ); // Trim leading whitespaces. |
0 | 1003 |
processMethod(menuMarkup, params); |
1004 |
||
16 | 1005 |
// Make it stand out a bit more visually, by adding a fadeIn. |
0 | 1006 |
$( 'li.pending' ).hide().fadeIn('slow'); |
1007 |
$( '.drag-instructions' ).show(); |
|
1008 |
if( ! ins.hasClass( 'menu-instructions-inactive' ) && ins.siblings().length ) |
|
1009 |
ins.addClass( 'menu-instructions-inactive' ); |
|
1010 |
||
1011 |
callback(); |
|
1012 |
}); |
|
1013 |
}, |
|
1014 |
||
1015 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
* Process the add menu item request response into menu list item. Appends to menu. |
0 | 1017 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
* @param {string} menuMarkup The text server response of menu item markup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1020 |
* @fires document#menu-item-added Passes menuMarkup as a jQuery object. |
0 | 1021 |
*/ |
5 | 1022 |
addMenuItemToBottom : function( menuMarkup ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1023 |
var $menuMarkup = $( menuMarkup ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1024 |
$menuMarkup.hideAdvancedMenuItemFields().appendTo( api.targetList ); |
0 | 1025 |
api.refreshKeyboardAccessibility(); |
1026 |
api.refreshAdvancedAccessibility(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1027 |
$( document ).trigger( 'menu-item-added', [ $menuMarkup ] ); |
0 | 1028 |
}, |
1029 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1030 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
* Process the add menu item request response into menu list item. Prepends to menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1032 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1033 |
* @param {string} menuMarkup The text server response of menu item markup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1034 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1035 |
* @fires document#menu-item-added Passes menuMarkup as a jQuery object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1036 |
*/ |
5 | 1037 |
addMenuItemToTop : function( menuMarkup ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1038 |
var $menuMarkup = $( menuMarkup ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1039 |
$menuMarkup.hideAdvancedMenuItemFields().prependTo( api.targetList ); |
0 | 1040 |
api.refreshKeyboardAccessibility(); |
1041 |
api.refreshAdvancedAccessibility(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
$( document ).trigger( 'menu-item-added', [ $menuMarkup ] ); |
0 | 1043 |
}, |
1044 |
||
1045 |
attachUnsavedChangesListener : function() { |
|
1046 |
$('#menu-management input, #menu-management select, #menu-management, #menu-management textarea, .menu-location-menus select').change(function(){ |
|
1047 |
api.registerChange(); |
|
1048 |
}); |
|
1049 |
||
5 | 1050 |
if ( 0 !== $('#menu-to-edit').length || 0 !== $('.menu-location-menus select').length ) { |
0 | 1051 |
window.onbeforeunload = function(){ |
1052 |
if ( api.menusChanged ) |
|
16 | 1053 |
return wp.i18n.__( 'The changes you made will be lost if you navigate away from this page.' ); |
0 | 1054 |
}; |
1055 |
} else { |
|
16 | 1056 |
// Make the post boxes read-only, as they can't be used yet. |
0 | 1057 |
$( '#menu-settings-column' ).find( 'input,select' ).end().find( 'a' ).attr( 'href', '#' ).unbind( 'click' ); |
1058 |
} |
|
1059 |
}, |
|
1060 |
||
1061 |
registerChange : function() { |
|
1062 |
api.menusChanged = true; |
|
1063 |
}, |
|
1064 |
||
1065 |
attachTabsPanelListeners : function() { |
|
1066 |
$('#menu-settings-column').bind('click', function(e) { |
|
16 | 1067 |
var selectAreaMatch, selectAll, panelId, wrapper, items, |
0 | 1068 |
target = $(e.target); |
1069 |
||
1070 |
if ( target.hasClass('nav-tab-link') ) { |
|
1071 |
||
1072 |
panelId = target.data( 'type' ); |
|
1073 |
||
1074 |
wrapper = target.parents('.accordion-section-content').first(); |
|
1075 |
||
16 | 1076 |
// Upon changing tabs, we want to uncheck all checkboxes. |
1077 |
$( 'input', wrapper ).prop( 'checked', false ); |
|
0 | 1078 |
|
1079 |
$('.tabs-panel-active', wrapper).removeClass('tabs-panel-active').addClass('tabs-panel-inactive'); |
|
1080 |
$('#' + panelId, wrapper).removeClass('tabs-panel-inactive').addClass('tabs-panel-active'); |
|
1081 |
||
1082 |
$('.tabs', wrapper).removeClass('tabs'); |
|
1083 |
target.parent().addClass('tabs'); |
|
1084 |
||
16 | 1085 |
// Select the search bar. |
0 | 1086 |
$('.quick-search', wrapper).focus(); |
1087 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1088 |
// Hide controls in the search tab if no items found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
if ( ! wrapper.find( '.tabs-panel-active .menu-item-title' ).length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1090 |
wrapper.addClass( 'has-no-menu-item' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1091 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
wrapper.removeClass( 'has-no-menu-item' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
|
0 | 1095 |
e.preventDefault(); |
16 | 1096 |
} else if ( target.hasClass( 'select-all' ) ) { |
1097 |
selectAreaMatch = target.closest( '.button-controls' ).data( 'items-type' ); |
|
1098 |
if ( selectAreaMatch ) { |
|
1099 |
items = $( '#' + selectAreaMatch + ' .tabs-panel-active .menu-item-title input' ); |
|
1100 |
||
1101 |
if ( items.length === items.filter( ':checked' ).length && ! target.is( ':checked' ) ) { |
|
1102 |
items.prop( 'checked', false ); |
|
1103 |
} else if ( target.is( ':checked' ) ) { |
|
1104 |
items.prop( 'checked', true ); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
} else if ( target.hasClass( 'menu-item-checkbox' ) ) { |
|
1108 |
selectAreaMatch = target.closest( '.tabs-panel-active' ).parent().attr( 'id' ); |
|
1109 |
if ( selectAreaMatch ) { |
|
1110 |
items = $( '#' + selectAreaMatch + ' .tabs-panel-active .menu-item-title input' ); |
|
1111 |
selectAll = $( '.button-controls[data-items-type="' + selectAreaMatch + '"] .select-all' ); |
|
1112 |
||
1113 |
if ( items.length === items.filter( ':checked' ).length && ! selectAll.is( ':checked' ) ) { |
|
1114 |
selectAll.prop( 'checked', true ); |
|
1115 |
} else if ( selectAll.is( ':checked' ) ) { |
|
1116 |
selectAll.prop( 'checked', false ); |
|
1117 |
} |
|
0 | 1118 |
} |
1119 |
} else if ( target.hasClass('submit-add-to-menu') ) { |
|
1120 |
api.registerChange(); |
|
1121 |
||
1122 |
if ( e.target.id && 'submit-customlinkdiv' == e.target.id ) |
|
1123 |
api.addCustomLink( api.addMenuItemToBottom ); |
|
1124 |
else if ( e.target.id && -1 != e.target.id.indexOf('submit-') ) |
|
1125 |
$('#' + e.target.id.replace(/submit-/, '')).addSelectedToMenu( api.addMenuItemToBottom ); |
|
1126 |
return false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
}); |
0 | 1129 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
* Delegate the `click` event and attach it just to the pagination |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
* links thus excluding the current page `<span>`. See ticket #35577. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
$( '#nav-menu-meta' ).on( 'click', 'a.page-numbers', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
var $container = $( this ).closest( '.inside' ); |
0 | 1136 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
$.post( ajaxurl, this.href.replace( /.*\?/, '' ).replace( /action=([^&]*)/, '' ) + '&action=menu-get-metabox', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
function( resp ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
var metaBoxData = $.parseJSON( resp ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
toReplace; |
0 | 1141 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
if ( -1 === resp.indexOf( 'replace-id' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
} |
0 | 1145 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
// Get the post type menu meta box to update. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
toReplace = document.getElementById( metaBoxData['replace-id'] ); |
0 | 1148 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
if ( ! metaBoxData.markup || ! toReplace ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
return; |
0 | 1151 |
} |
1152 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
// Update the post type menu meta box with new content from the response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
$container.html( metaBoxData.markup ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
return false; |
0 | 1159 |
}); |
1160 |
}, |
|
1161 |
||
1162 |
eventOnClickEditLink : function(clickedEl) { |
|
1163 |
var settings, item, |
|
1164 |
matchedSection = /#(.*)$/.exec(clickedEl.href); |
|
16 | 1165 |
|
0 | 1166 |
if ( matchedSection && matchedSection[1] ) { |
1167 |
settings = $('#'+matchedSection[1]); |
|
1168 |
item = settings.parent(); |
|
5 | 1169 |
if( 0 !== item.length ) { |
0 | 1170 |
if( item.hasClass('menu-item-edit-inactive') ) { |
1171 |
if( ! settings.data('menu-item-data') ) { |
|
1172 |
settings.data( 'menu-item-data', settings.getItemData() ); |
|
1173 |
} |
|
1174 |
settings.slideDown('fast'); |
|
1175 |
item.removeClass('menu-item-edit-inactive') |
|
1176 |
.addClass('menu-item-edit-active'); |
|
1177 |
} else { |
|
1178 |
settings.slideUp('fast'); |
|
1179 |
item.removeClass('menu-item-edit-active') |
|
1180 |
.addClass('menu-item-edit-inactive'); |
|
1181 |
} |
|
1182 |
return false; |
|
1183 |
} |
|
1184 |
} |
|
1185 |
}, |
|
1186 |
||
1187 |
eventOnClickCancelLink : function(clickedEl) { |
|
1188 |
var settings = $( clickedEl ).closest( '.menu-item-settings' ), |
|
1189 |
thisMenuItem = $( clickedEl ).closest( '.menu-item' ); |
|
16 | 1190 |
|
1191 |
thisMenuItem.removeClass( 'menu-item-edit-active' ).addClass( 'menu-item-edit-inactive' ); |
|
1192 |
settings.setItemData( settings.data( 'menu-item-data' ) ).hide(); |
|
1193 |
// Restore the title of the currently active/expanded menu item. |
|
1194 |
thisMenuItem.find( '.menu-item-title' ).text( settings.data( 'menu-item-data' )['menu-item-title'] ); |
|
1195 |
||
0 | 1196 |
return false; |
1197 |
}, |
|
1198 |
||
5 | 1199 |
eventOnClickMenuSave : function() { |
0 | 1200 |
var locs = '', |
1201 |
menuName = $('#menu-name'), |
|
1202 |
menuNameVal = menuName.val(); |
|
16 | 1203 |
|
1204 |
// Cancel and warn if invalid menu name. |
|
9 | 1205 |
if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) { |
1206 |
menuName.parent().addClass( 'form-invalid' ); |
|
0 | 1207 |
return false; |
1208 |
} |
|
16 | 1209 |
// Copy menu theme locations. |
0 | 1210 |
$('#nav-menu-theme-locations select').each(function() { |
1211 |
locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />'; |
|
1212 |
}); |
|
1213 |
$('#update-nav-menu').append( locs ); |
|
16 | 1214 |
// Update menu item position data. |
0 | 1215 |
api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } ); |
1216 |
window.onbeforeunload = null; |
|
1217 |
||
1218 |
return true; |
|
1219 |
}, |
|
1220 |
||
5 | 1221 |
eventOnClickMenuDelete : function() { |
16 | 1222 |
// Delete warning AYS. |
1223 |
if ( window.confirm( wp.i18n.__( 'You are about to permanently delete this menu.\n\'Cancel\' to stop, \'OK\' to delete.' ) ) ) { |
|
0 | 1224 |
window.onbeforeunload = null; |
1225 |
return true; |
|
1226 |
} |
|
1227 |
return false; |
|
1228 |
}, |
|
1229 |
||
1230 |
eventOnClickMenuItemDelete : function(clickedEl) { |
|
1231 |
var itemID = parseInt(clickedEl.id.replace('delete-', ''), 10); |
|
16 | 1232 |
|
0 | 1233 |
api.removeMenuItem( $('#menu-item-' + itemID) ); |
1234 |
api.registerChange(); |
|
1235 |
return false; |
|
1236 |
}, |
|
1237 |
||
1238 |
/** |
|
1239 |
* Process the quick search response into a search result |
|
1240 |
* |
|
1241 |
* @param string resp The server response to the query. |
|
1242 |
* @param object req The request arguments. |
|
1243 |
* @param jQuery panel The tabs panel we're searching in. |
|
1244 |
*/ |
|
1245 |
processQuickSearchQueryResponse : function(resp, req, panel) { |
|
1246 |
var matched, newID, |
|
1247 |
takenIDs = {}, |
|
1248 |
form = document.getElementById('nav-menu-meta'), |
|
5 | 1249 |
pattern = /menu-item[(\[^]\]*/, |
0 | 1250 |
$items = $('<div>').html(resp).find('li'), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
wrapper = panel.closest( '.accordion-section-content' ), |
16 | 1252 |
selectAll = wrapper.find( '.button-controls .select-all' ), |
0 | 1253 |
$item; |
1254 |
||
1255 |
if( ! $items.length ) { |
|
16 | 1256 |
$('.categorychecklist', panel).html( '<li><p>' + wp.i18n.__( 'No results found.' ) + '</p></li>' ); |
5 | 1257 |
$( '.spinner', panel ).removeClass( 'is-active' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
wrapper.addClass( 'has-no-menu-item' ); |
0 | 1259 |
return; |
1260 |
} |
|
1261 |
||
1262 |
$items.each(function(){ |
|
1263 |
$item = $(this); |
|
1264 |
||
16 | 1265 |
// Make a unique DB ID number. |
0 | 1266 |
matched = pattern.exec($item.html()); |
1267 |
||
1268 |
if ( matched && matched[1] ) { |
|
1269 |
newID = matched[1]; |
|
1270 |
while( form.elements['menu-item[' + newID + '][menu-item-type]'] || takenIDs[ newID ] ) { |
|
1271 |
newID--; |
|
1272 |
} |
|
1273 |
||
1274 |
takenIDs[newID] = true; |
|
1275 |
if ( newID != matched[1] ) { |
|
1276 |
$item.html( $item.html().replace(new RegExp( |
|
1277 |
'menu-item\\[' + matched[1] + '\\]', 'g'), |
|
1278 |
'menu-item[' + newID + ']' |
|
1279 |
) ); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
}); |
|
1283 |
||
1284 |
$('.categorychecklist', panel).html( $items ); |
|
5 | 1285 |
$( '.spinner', panel ).removeClass( 'is-active' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
wrapper.removeClass( 'has-no-menu-item' ); |
16 | 1287 |
|
1288 |
if ( selectAll.is( ':checked' ) ) { |
|
1289 |
selectAll.prop( 'checked', false ); |
|
1290 |
} |
|
0 | 1291 |
}, |
1292 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* Remove a menu item. |
16 | 1295 |
* |
1296 |
* @param {Object} el The element to be removed as a jQuery object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
* @fires document#menu-removing-item Passes the element to be removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
*/ |
0 | 1300 |
removeMenuItem : function(el) { |
1301 |
var children = el.childMenuItems(); |
|
1302 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
$( document ).trigger( 'menu-removing-item', [ el ] ); |
0 | 1304 |
el.addClass('deleting').animate({ |
1305 |
opacity : 0, |
|
1306 |
height: 0 |
|
1307 |
}, 350, function() { |
|
1308 |
var ins = $('#menu-instructions'); |
|
1309 |
el.remove(); |
|
1310 |
children.shiftDepthClass( -1 ).updateParentMenuItemDBId(); |
|
5 | 1311 |
if ( 0 === $( '#menu-to-edit li' ).length ) { |
0 | 1312 |
$( '.drag-instructions' ).hide(); |
1313 |
ins.removeClass( 'menu-instructions-inactive' ); |
|
1314 |
} |
|
5 | 1315 |
api.refreshAdvancedAccessibility(); |
0 | 1316 |
}); |
1317 |
}, |
|
1318 |
||
1319 |
depthToPx : function(depth) { |
|
1320 |
return depth * api.options.menuItemDepthPerLevel; |
|
1321 |
}, |
|
1322 |
||
1323 |
pxToDepth : function(px) { |
|
1324 |
return Math.floor(px / api.options.menuItemDepthPerLevel); |
|
1325 |
} |
|
1326 |
||
1327 |
}; |
|
1328 |
||
1329 |
$(document).ready(function(){ wpNavMenu.init(); }); |
|
1330 |
||
1331 |
})(jQuery); |