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