custom-admin-menu.js
| 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 | $(document).ready( function() { |
| 19 | |
| 20 | // ----- Menu Ordering ----- |
| 21 | |
| 22 | // Initialize sortable elements for parent menu items: https://api.jqueryui.com/sortable/ |
| 23 | $('#custom-admin-menu').sortable({ |
| 24 | items: '> li', |
| 25 | opacity: 0.6, |
| 26 | placeholder: 'sortable-placeholder', |
| 27 | tolerance: 'pointer', |
| 28 | revert: 250 |
| 29 | }); |
| 30 | |
| 31 | // Get the default/current menu order |
| 32 | let menuOrder = $('#custom-admin-menu').sortable("toArray").toString(); |
| 33 | |
| 34 | // Set hidden input value for saving in options |
| 35 | document.getElementById('custom_menu_order').value = menuOrder; |
| 36 | |
| 37 | // Save custom order into a comma-separated string, triggerred after each drag and drop of menu item |
| 38 | // https://api.jqueryui.com/sortable/#event-update |
| 39 | // https://api.jqueryui.com/sortable/#method-toArray |
| 40 | $('#custom-admin-menu').on( 'sortupdate', function( event, ui) { |
| 41 | |
| 42 | // Get the updated menu order |
| 43 | let menuOrder = $('#custom-admin-menu').sortable("toArray").toString(); |
| 44 | |
| 45 | // Set hidden input value for saving in options |
| 46 | document.getElementById('custom_menu_order').value = menuOrder; |
| 47 | |
| 48 | }); |
| 49 | |
| 50 | |
| 51 | |
| 52 | // ----- Parent Menu Item Hiding ----- |
| 53 | |
| 54 | // Prepare constant to store IDs of menu items that will be hidden |
| 55 | if ( document.getElementById('custom_menu_hidden') != null ) { |
| 56 | var hiddenMenuItems = document.getElementById('custom_menu_hidden').value.split(","); // array |
| 57 | } else { |
| 58 | var hiddenMenuItems = []; // array |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // Detect which menu items are being checked. Ref: https://stackoverflow.com/a/3871602 |
| 63 | Array.from(document.getElementsByClassName('parent-menu-hide-checkbox')).forEach(function(item,index,array) { |
| 64 | |
| 65 | item.addEventListener('click', event => { |
| 66 | |
| 67 | if (event.target.checked) { |
| 68 | |
| 69 | // Add ID of menu item to array |
| 70 | hiddenMenuItems.push(event.target.dataset.menuItemId); |
| 71 | |
| 72 | } else { |
| 73 | |
| 74 | // Remove ID of menu item from array |
| 75 | const start = hiddenMenuItems.indexOf(event.target.dataset.menuItemId); |
| 76 | const deleteCount = 1; |
| 77 | hiddenMenuItems.splice(start, deleteCount); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | // Set hidden input value |
| 82 | document.getElementById('custom_menu_hidden').value = hiddenMenuItems; |
| 83 | |
| 84 | }); |
| 85 | |
| 86 | }); |
| 87 | |
| 88 | // Clicking on header save button |
| 89 | $('#amo-save-changes').click( function(e) { |
| 90 | |
| 91 | e.preventDefault(); |
| 92 | |
| 93 | // Prepare variable to store ID-Title pairs of menu items |
| 94 | var customMenuTitles = []; // empty array |
| 95 | |
| 96 | // Initialize other variables |
| 97 | var menuItemId = ''; |
| 98 | var customTitle = ''; |
| 99 | |
| 100 | // Save default/custom title values. Ref: https://stackoverflow.com/a/3871602 |
| 101 | Array.from(document.getElementsByClassName('menu-item-custom-title')).forEach(function(item,index,array) { |
| 102 | |
| 103 | menuItemId = item.dataset.menuItemId; |
| 104 | customTitle = item.value; |
| 105 | customMenuTitles.push(menuItemId + '__' + customTitle); |
| 106 | |
| 107 | }); |
| 108 | |
| 109 | // Set hidden input value |
| 110 | document.getElementById('custom_menu_titles').value = customMenuTitles; |
| 111 | |
| 112 | }); |
| 113 | |
| 114 | }); // End of $(document).ready() |
| 115 | |
| 116 | |
| 117 | |
| 118 | })( jQuery ); |