source: Dev/branches/Cartis/js/dashboardScripts.js @ 202

Last change on this file since 202 was 202, checked in by cartis, 13 years ago

Dropdown list inclusief shoppingCart
Functionaliteit in dashbboard scripts

File size: 4.8 KB
RevLine 
[202]1dbSelectionInfoTimeout = null;
2currentlySelected = {
3    titles: [],
4    uids: [],
5    type: null
6}
7
8shoppingCart = {
9    contents: {
10        titles: [],
11        uids: [],
12        types: []
13    }
14}
15
16function infoPanelTimeout(event) {
17    // If no event, crash.
18    if (event == undefined || event == null) return;
19    // Cancel previous timeout if present.
20    if (dbSelectionInfoTimeout) {
21        window.clearTimeout(dbSelectionInfoTimeout);
22    }
23    // Gather parameters about event.
24    var dbSelectionInfoTime = 500;
25    var selectBox = event.target;
26    var values = getMultipleListValue(selectBox).values;
27    var titles = getMultipleListValue(selectBox).titles;
28    var uid = values[values.length-1];
29    var type;
30    switch (selectBox.id) {
31        case "dbSessionInstancesList":
32            // This should be session instance, not session. Testing purpose only.
33            type = "Session";
34            break;
35        case "dbGamedataList":
36            // This is obviously not the real type, change before committing
37            type = "Application";
38            break;
39        default:
40            alert("No valid type passed!");
41            break;
42    }
43    currentlySelected.uids = values;
44    currentlySelected.titles = titles;
45    currentlySelected.type = type;
46    // Set timeout of 500ms, then execute ajaxInfoRequest with previously gathered parameters.
47    dbSelectionInfoTimeout = window.setTimeout(ajaxInfoRequest, dbSelectionInfoTime, uid, ge("dbInfoPanelContent"), type);
48   
49}
50
51function getMultipleListValue(element) {
52    // Get multiple values from multiple select box
53    //TODO:
54    var result = {
55        values: [],
56        titles: []
57    }
58       
59    for (var i = 0; i < element.options.length; i++) {
60        if (element.options[i].selected == true) {
61            result.values.push(element.options[i].value);
62            result.titles.push(element.options[i].text);
63        }
64    }
65    return result;
66}
67
68function addDataToList() {
69    // Take selected objects and add to shopping cart
70       
71    // set element
72    var element;
73    switch (currentlySelected.type){
74        case "Session":
75            element = ge("dbSessionInstancesList");
76            break;
77        case "Application":
78            element = ge("dbGamedataList");
79            break;
80        default:
81            // ???
82            break;
83    }
84    var uid;
85    while (uid = currentlySelected.uids.pop()) {
86        // Add to shopping cart
87        shoppingCart.contents.uids.push(uid);
88        shoppingCart.contents.titles.push(currentlySelected.titles.pop());
89        shoppingCart.contents.types.push(currentlySelected.type);
90       
91        // Remove option from list
92        for (var i = 0; i < element.options.length; i++) {
93            if (element.options[i].value == uid) {
94                element.remove(i);
95                break;
96            }
97        }
98    }
99   
100    // Then add display of shopping cart contents
101    drawShoppingCart();
102   
103}
104
105function removeFromList(event) {
106    if (event == null || event == undefined) return;
107    var id = event.target.parentNode.id;
108    var uid = id.split("shoppingCartItem")[1];
109    // find index, remove from object
110    var indexInCart = shoppingCart.contents.uids.indexOf(uid);
111    shoppingCart.contents.uids.splice(indexInCart, 1);
112    var title = shoppingCart.contents.titles.splice(indexInCart, 1)[0];
113    var type = shoppingCart.contents.types.splice(indexInCart, 1)[0];
114    //Then replace element back into relevant list
115    // First set element
116    var element;
117    switch (type){
118        case "Session":
119            element = ge("dbSessionInstancesList");
120            break;
121        case "Application":
122            element = ge("dbGamedataList");
123            break;
124        default:
125            // ???
126            break;
127    }
128   
129    var option = ce("option");
130    option.value = uid;
131    option.text = title;
132    element.add(option,null);
133   
134   
135   
136    // Lastly, refresh
137    drawShoppingCart();
138    }
139
140function drawShoppingCart() {
141        var scElement = ge("shoppingCartContent");
142        scElement.innerHTML = "";
143        for (var i = 0; i < shoppingCart.contents.uids.length; i++) {
144            var newLine = ce("div");
145            newLine.className = "shoppingCartItem";
146            newLine.id = "shoppingCartItem"+shoppingCart.contents.uids[i];
147            var icon = ce("image");
148            icon.src = "images/ui/DeleteIcon.gif";
149            icon.addEventListener("click", function(event){
150                // TODO: Actually write this function
151                removeFromList(event);
152            }, false);
153            var titleField = ce("text");
154            titleField.innerHTML = shoppingCart.contents.titles[i];
155            newLine.appendChild(icon);
156            newLine.appendChild(titleField);
157            scElement.appendChild(newLine);
158        }
159   
160        // reset type after empty
161        currentlySelected.type = null;
162    }
Note: See TracBrowser for help on using the repository browser.