dbSelectionInfoTimeout = null; currentlySelected = { titles: [], uids: [], type: null } shoppingCart = { contents: { titles: [], uids: [], types: [] } } function infoPanelTimeout(event) { // If no event, crash. if (event == undefined || event == null) return; // Cancel previous timeout if present. if (dbSelectionInfoTimeout) { window.clearTimeout(dbSelectionInfoTimeout); } // Gather parameters about event. var dbSelectionInfoTime = 500; var selectBox = event.target; var values = getMultipleListValue(selectBox).values; var titles = getMultipleListValue(selectBox).titles; var uid = values[values.length-1]; var type; switch (selectBox.id) { case "dbSessionInstancesList": // This should be session instance, not session. Testing purpose only. type = "Session"; break; case "dbGamedataList": // This is obviously not the real type, change before committing type = "Application"; break; default: alert("No valid type passed!"); break; } currentlySelected.uids = values; currentlySelected.titles = titles; currentlySelected.type = type; // Set timeout of 500ms, then execute ajaxInfoRequest with previously gathered parameters. dbSelectionInfoTimeout = window.setTimeout(ajaxInfoRequest, dbSelectionInfoTime, uid, ge("dbInfoPanelContent"), type); } function getMultipleListValue(element) { // Get multiple values from multiple select box //TODO: var result = { values: [], titles: [] } for (var i = 0; i < element.options.length; i++) { if (element.options[i].selected == true) { result.values.push(element.options[i].value); result.titles.push(element.options[i].text); } } return result; } function addDataToList() { // Take selected objects and add to shopping cart // set element var element; switch (currentlySelected.type){ case "Session": element = ge("dbSessionInstancesList"); break; case "Application": element = ge("dbGamedataList"); break; default: // ??? break; } var uid; while (uid = currentlySelected.uids.pop()) { // Add to shopping cart shoppingCart.contents.uids.push(uid); shoppingCart.contents.titles.push(currentlySelected.titles.pop()); shoppingCart.contents.types.push(currentlySelected.type); // Remove option from list for (var i = 0; i < element.options.length; i++) { if (element.options[i].value == uid) { element.remove(i); break; } } } // Then add display of shopping cart contents drawShoppingCart(); } function removeFromList(event) { if (event == null || event == undefined) return; var id = event.target.parentNode.id; var uid = id.split("shoppingCartItem")[1]; // find index, remove from object var indexInCart = shoppingCart.contents.uids.indexOf(uid); var tempTitle = shoppingCart.contents.titles[indexInCart]; var cn = confirm("Are you sure you want to remove: "+tempTitle+"?"); if (cn == false) return; shoppingCart.contents.uids.splice(indexInCart, 1); var title = shoppingCart.contents.titles.splice(indexInCart, 1)[0]; var type = shoppingCart.contents.types.splice(indexInCart, 1)[0]; //Then replace element back into relevant list // First set element var element; switch (type){ case "Session": element = ge("dbSessionInstancesList"); break; case "Application": element = ge("dbGamedataList"); break; default: // ??? break; } var option = ce("option"); option.value = uid; option.text = title; element.add(option,null); // Lastly, refresh drawShoppingCart(); } function drawShoppingCart() { var scElement = ge("shoppingCartContent"); scElement.innerHTML = ""; for (var i = 0; i < shoppingCart.contents.uids.length; i++) { var newLine = ce("div"); newLine.className = "shoppingCartItem"; newLine.id = "shoppingCartItem"+shoppingCart.contents.uids[i]; var icon = ce("image"); icon.src = "images/ui/DeleteIcon.gif"; icon.addEventListener("click", function(event){ // TODO: Actually write this function removeFromList(event); }, false); var titleField = ce("text"); titleField.innerHTML = shoppingCart.contents.titles[i]; newLine.appendChild(icon); newLine.appendChild(titleField); scElement.appendChild(newLine); } // reset type after empty currentlySelected.type = null; } function configureDashboard() { var data = JSON.stringify(shoppingCart); var form = ce("form"); //changed to dashboardGraphSelection.php form.action = "dashboardGraphSelection.php"; form.method = "POST"; var input = ce("input"); input.type = "hidden"; input.name = "selectedData"; input.value = data; form.appendChild(input); form.submit(); }