PageSourceSearch

https://ehrc.org/wp-content/themes/zoorya/js/move-new-menus-tool.js

js ehrc.org collected 2026-10-02 05:32:53 UTC 3,891 bytes, 108 lines download raw bytes

1// Global variable to store the state before the move
2var previousState = null;
3
4function move(targetMenu, fromPosition, toContainer, toPosition) {
5    // Validate inputs
6    if (!Number.isInteger(fromPosition) || fromPosition < 1) {
7        console.error("Invalid fromPosition:", fromPosition);
8        return;
9    }
10
11    if (!Number.isInteger(toContainer) || (toContainer !== 1 && toContainer !== 2)) {
12        console.error("Invalid toContainer:", toContainer);
13        return;
14    }
15
16
17    if (!Number.isInteger(toPosition) || toPosition < 1) {
18        console.error("Invalid toPosition:", toPosition);
19        return;
20    }
21    
22    var targetMainMenu = (targetMenu == 1) ? "mega-menu-2812-999" : "mega-menu-282-999-0";
23    // Find the mega menu item to be moved
24    var menuItemId = findMenuItemIdByPosition(fromPosition, targetMainMenu);
25    var menuItem = document.getElementById(menuItemId);
26
27    if (!menuItem) {
28        console.error("Mega menu item not found.");
29        return;
30    }
31
32    // Store the previous state before the move
33    previousState = menuItem.parentNode.cloneNode(true);
34
35    // Handle which column is chosen based on which menu is choosen
36    var firstTargetMainMenu = (targetMenu == 1) ? "mega-menu-2812-0-2" : "mega-menu-282-0-2";
37    var secondTargetMainMenu = (targetMenu == 1) ? "mega-menu-2812-0-3" : "mega-menu-282-0-3";
38    
39    
40    // Find the target container and its UL element
41    var targetContainerId = (toContainer === 1) ? firstTargetMainMenu : secondTargetMainMenu;
42    var targetContainer = document.getElementById(targetContainerId);
43
44    if (!targetContainer) {
45        console.error("Target container not found:", targetContainerId);
46        return;
47    }
48
49    var targetUl = targetContainer.querySelector("ul.mega-sub-menu");
50
51    if (!targetUl) {
52        console.error("Target UL not found inside the container:", targetContainerId);
53        return;
54    }
55
56    // Find the position in the target UL to insert the item
57    var targetLiElements = targetUl.querySelectorAll("li.mega-menu-item");
58    var insertPosition = Math.min(toPosition - 1, targetLiElements.length); // clamp to valid index
59
60    // Remove the menu item from its current position
61    menuItem.parentNode.removeChild(menuItem);
62
63    // Insert the menu item into the target position
64    if (insertPosition === targetLiElements.length) {
65        targetUl.appendChild(menuItem); // Insert at the end
66    } else {
67        targetUl.insertBefore(menuItem, targetLiElements[insertPosition]);
68    }
69
70    // Log the move operation for undo purposes
71    // console.log("Moved item:", menuItemId, "from position", fromPosition, "to", targetContainerId, "at position", toPosition);
72}
73
74// Function to find the menu item ID by position under a specific container
75function findMenuItemIdByPosition(position, containerId) {
76    var container = document.getElementById(containerId);
77    if (!container) {
78        console.error("Container not found:", containerId);
79        return null;
80    }
81
82    var menuItems = container.querySelectorAll("li.mega-menu-item");
83    if (position <= menuItems.length) {
84        return menuItems[position - 1].id;
85    } else {
86        console.error("Menu item not found at position:", position);
87        return null;
88    }
89}
90
91// comment evrything out and start again to understand the sections. 1=(region/areas of work) 2= (1st element in special column) 3=(col 1/col 2) 4= (posistion in target col)
92// move(1, 1, 1, 5); // Moves the first item under mega-menu-282-999-0 to second position under mega-menu-282-0-2
93// move(1, 1, 2, 2);
94// move(1, 1, 2, 4);
95// move(1, 1, 2, 5);
96// move(1, 1, 2, 3);
97move(1, 1, 1, 5);
98move(1, 1, 1, 6);
99move(1, 1, 2, 1);
100move(1, 1, 2, 3);
101move(1, 1, 2, 5);
102move(1, 1, 2, 6);
103
104move(2, 1, 1, 2);
105move(2, 1, 2, 4);
106
107
108

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.