PluginProbe
Admin and Site Enhancements (ASE) / 8.6.2
Admin and Site Enhancements (ASE) v8.6.2
9.1.1 9.1.0 9.0.2 9.0.1 9.0.0 8.9.2 8.9.1 8.9.0 8.8.8 8.8.7 8.8.6 8.8.5 8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.3 8.7.2 8.7.1 8.2.1 8.2.2 8.2.3 8.3.0 8.3.1 All 273 releases
admin-site-enhancements / assets / js / custom-admin-menu.js
custom-admin-menu.js
127 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 'use strict';
3
4 /**
5 * Get an element by ID without using CSS selector parsing.
6 *
7 * This avoids jQuery selector syntax errors when IDs contain percent-encoded
8 * sequences (e.g. %e3%83...) which can happen for multibyte site languages.
9 *
10 * @param {string} id Element ID (without '#').
11 * @return {Object} jQuery object (empty set when not found).
12 */
13 function asenhaGetById( id ) {
14 var el = document.getElementById( id );
15 return el ? $( el ) : $();
16 }
17
18
19
20 // Get localized strings with fallback to empty object
21 var strings = (typeof amoPageVars !== 'undefined' && amoPageVars.strings) ? amoPageVars.strings : {};
22
23
24
25 $(document).ready( function() {
26
27 // ----- Menu Ordering -----
28
29 // Initialize sortable elements for parent menu items: https://api.jqueryui.com/sortable/
30 $('#custom-admin-menu').sortable({
31 items: '> li',
32 opacity: 0.6,
33 placeholder: 'sortable-placeholder',
34 tolerance: 'pointer',
35 revert: 250
36 });
37
38 // Get the default/current menu order
39 let menuOrder = $('#custom-admin-menu').sortable("toArray").toString();
40
41 // Set hidden input value for saving in options
42 document.getElementById('custom_menu_order').value = menuOrder;
43
44 // Save custom order into a comma-separated string, triggerred after each drag and drop of menu item
45 // https://api.jqueryui.com/sortable/#event-update
46 // https://api.jqueryui.com/sortable/#method-toArray
47 $('#custom-admin-menu').on( 'sortupdate', function( event, ui) {
48
49 // Get the updated menu order
50 let menuOrder = $('#custom-admin-menu').sortable("toArray").toString();
51
52 // Set hidden input value for saving in options
53 document.getElementById('custom_menu_order').value = menuOrder;
54
55 });
56
57
58
59 // ----- Parent Menu Item Hiding -----
60
61 // Prepare constant to store IDs of menu items that will be hidden
62 if ( document.getElementById('custom_menu_hidden') != null ) {
63 var hiddenMenuItems = document.getElementById('custom_menu_hidden').value.split(","); // array
64 } else {
65 var hiddenMenuItems = []; // array
66 }
67
68
69 // Detect which menu items are being checked. Ref: https://stackoverflow.com/a/3871602
70 Array.from(document.getElementsByClassName('parent-menu-hide-checkbox')).forEach(function(item,index,array) {
71
72 item.addEventListener('click', event => {
73
74 if (event.target.checked) {
75
76 // Add ID of menu item to array
77 hiddenMenuItems.push(event.target.dataset.menuItemId);
78
79 } else {
80
81 // Remove ID of menu item from array
82 const start = hiddenMenuItems.indexOf(event.target.dataset.menuItemId);
83 const deleteCount = 1;
84 hiddenMenuItems.splice(start, deleteCount);
85
86 }
87
88 // Set hidden input value
89 document.getElementById('custom_menu_hidden').value = hiddenMenuItems;
90
91 });
92
93 });
94
95 // Clicking on header save button
96 $('#amo-save-changes').click( function(e) {
97
98 e.preventDefault();
99
100 // Prepare variable to store ID-Title pairs of menu items
101 var customMenuTitles = []; // empty array
102
103 // Initialize other variables
104 var menuItemId = '';
105 var customTitle = '';
106
107 // Save default/custom title values. Ref: https://stackoverflow.com/a/3871602
108 Array.from(document.getElementsByClassName('menu-item-custom-title')).forEach(function(item,index,array) {
109
110 menuItemId = item.dataset.menuItemId;
111 customTitle = item.value;
112 customMenuTitles.push(menuItemId + '__' + customTitle);
113
114 });
115
116 // Set hidden input value
117 document.getElementById('custom_menu_titles').value = customMenuTitles;
118
119 });
120
121
122
123 }); // End of $(document).ready()
124
125
126
127 })( jQuery );