github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/public/panel_menu_items.js (about)

     1  (() => {
     2  	addInitHook("end_init", () => {
     3  
     4  // TODO: Move this into a JS file to reduce the number of possible problems
     5  var menuItems = {};
     6  let items = document.getElementsByClassName("panel_menu_item");
     7  for(let i=0; item=items[i]; i++) menuItems[i] = item.getAttribute("data-miid");
     8  
     9  Sortable.create(document.getElementById("panel_menu_item_holder"), {
    10  	sort: true,
    11  	onEnd: evt => {
    12  		log("pre menuItems",menuItems)
    13  		log("evt",evt)
    14  		let oldMiid = menuItems[evt.newIndex];
    15  		menuItems[evt.oldIndex] = oldMiid;
    16  		let newMiid = evt.item.getAttribute("data-miid");
    17  		log("newMiid",newMiid);
    18  		menuItems[evt.newIndex] = newMiid;
    19  		log("post menuItems",menuItems);
    20  	}
    21  });
    22  
    23  document.getElementById("panel_menu_items_order_button").addEventListener("click", () => {
    24  	let req = new XMLHttpRequest();
    25  	if(!req) {
    26  		log("Failed to create request");
    27  		return false;
    28  	}
    29  	req.onreadystatechange = () => {
    30  		try {
    31  			if(req.readyState!==XMLHttpRequest.DONE) return;
    32  			// TODO: Signal the error with a notice
    33  			if(req.status===200) {
    34  				let resp = JSON.parse(req.responseText);
    35  				log("resp",resp);
    36  				// TODO: Should we move other notices into TmplPhrases like this one?
    37  				pushNotice(phraseBox["panel"]["panel.themes_menus_items_order_updated"]);
    38  				if(resp.success==1) return;
    39  			}
    40  		} catch(e) {
    41  			console.error("e",e)
    42  		}
    43  		console.trace();
    44  	}
    45  	// ? - Is encodeURIComponent the right function for this?
    46  	let spl = document.location.pathname.split("/");
    47  	req.open("POST","/panel/themes/menus/item/order/edit/submit/"+parseInt(spl[spl.length-1],10)+"?s="+encodeURIComponent(me.User.S));
    48  	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    49  	let items = "";
    50  	for(let i=0; item=menuItems[i];i++) items += item+",";
    51  	if(items.length > 0) items = items.slice(0,-1);
    52  	req.send("js=1&items={"+items+"}");
    53  });
    54  
    55  });
    56  })()