source: Dev/trunk/js/sequencerScripts.js @ 219

Last change on this file since 219 was 218, checked in by fpvanagthoven, 13 years ago

Laatste versies van alles!

File size: 37.0 KB
Line 
1/*
2 * You can change the type of sequencer by including a hidden input field with id "contentTypeField" in your document.
3 * The initEditor() method will then adjust internal variables in the sequencer object to match that type of content.
4 *
5 * Note: code makes heavy use of shorthand functions:
6 * ce(var) = document.createElement(var);
7 * ge(var) = document.getElementById(var);
8 * Defined in generalscripts.js
9 */
10
11var sequencer = createVarArray();
12
13// Basic functions, intialization, updating
14
15function createVarArray(){
16    // Function that returns creates the global variable object, the sequencer's variable storage.
17   
18    if (sequencer) delete window.sequencer;
19    return {   // GLOBAL VAR TO STORE SEQUENCER SETTINGS!
20        uid: "",                // The unique id of this sequencer (not DB related!). This will help to avoid global var conflicts. Assign this randomly! (STRING)
21        session: {              // Properties of the currently loaded session
22            title: "",              // Title or name (STRING)
23            uid: "",                // Database UID of the current session (STRING)
24            pipeline: {             // Pipeline
25                uids: [],               // Uids of objects in pipeline (STRING)
26                types: [],              // Types of objects in pipeline (STRING)
27                upToDate: []            // Whether or not object displays are up to date (BOOL)
28            }
29        },
30        survey: {               // Properties of the loaded survey, if applicable (either this or the session tree above is filled in!) [THIS IS NOT CURRENTLY USED AND I SHOULD DELIBERATE ON WHAT VARIABLES TO PUT IN THIS TREE TO MAKE A FUNCTIONAL EDITOR THAT ALSO MATCHES THE DB FORMAT!]
31            title: "",              // Title or the name of the survey (STRING)
32            uid: "",                // Uid of the survey (STRING)
33            description: "",        // Description of the survey (STRING)
34            questions: {            // Properties of the questions contained within this survey
35                uids: [],               // An array of uids of the questions, in the order that they appear in the survey (STRING)
36                upToDate: []            // Whether or not a certain question needs an update (BOOL)
37            }
38        },
39        state: {                // Operating state of the sequencer
40            editing: false,         // Whether or not one of the contained child objects is currently being edited or in edit mode. Which one can be determined from the selectedObject property. (BOOL)
41            updating: false,        // Whether or not new steps are currently being queried (to disable any further actions) (BOOL)
42            numSteps: 0,            // Number of objects currently drawn in the editor (not necessarily same as number of objects in pipeline/survey!) (INTEGER)
43            loaded: false,          // Whether or not the sequencer content has been updated for the first time (BOOL)
44            selectedObject: {       // Properties of the currently selected step
45                uid: "",                // UID of this step (STRING)
46                index: null             // The 'index' of this step in the current sequencer view (NOT the pipeline!) (INTEGER)
47            },
48            pages: {                // State regarding page division of content objects
49                total: null,            // The number of pages the content is divided across
50                currentPage: null       // The page that is currently displayed in the sequencer           
51            }
52        },
53        settings: {             // Various settings to determine the workings of the sequencer
54            content: {              // Properties related to the content view of the sequencer
55                contentType: null,      // Type of the loaded parent object (STRING)
56                width: null,            // Width of the viewing area (INTEGER)
57                height: null,           // Height of the viewing area (INTEGER)
58                maxObjects: null,       // The maximum number of content elements to be displayed at once time (INTEGER)
59                orientation: null,      // Whether the editor should be a vertical or horizontal editor (STRING)
60                pages: false            // Whether or not to divide content across pages
61            },
62            efficientUpdating: true // Whether or not to use selective querying of the database for new step objects. True will only refresh out-of-date steps, False will refresh the whole pipeline (BOOL)
63        }
64    };
65
66}
67
68function loadSequencer() {
69    // Reads hidden fields created by PHP, copies the values into the global var then deletes them.
70   
71    // Load hidden fields and set required properties in global object var.
72    try {
73        // settings fields first
74        initSequencer();
75       
76        switch (sequencer.settings.content.contentType.toLowerCase()) {
77            case "session":
78                // Content-related fields next
79                var fPipelineString = ge("pipelineStringField");
80                var fPipelineTypes = ge("pipelineTypeField");
81                var fSessionUid = ge("sessionField");
82                var fSessionTitle = ge("sessionTitleField");
83                sequencer.session.title = fSessionTitle.value;
84                sequencer.session.uid = fSessionUid.value;
85                sequencer.session.pipeline.uids = stringToArray(fPipelineString.value, ",");
86                sequencer.session.pipeline.types = stringToArray(fPipelineTypes.value, ",");
87                sequencer.session.pipeline.upToDate = new Array();
88       
89                for (var i = 0; i < sequencer.session.pipeline.uids.length; i++) {
90                    sequencer.session.pipeline.upToDate.push(true);
91                }
92                break;
93            case "survey":
94                var fSurveyUid = ge("surveyUidField");
95                var fQuestionUids = ge("questionUidsField");
96                var fSurveyTitle = ge("surveyTitleField");
97                var fnumQuestions = ge("numQuestionsField");
98                var fSurveyDescription = ge("surveyDescriptionField");
99                sequencer.survey.title = fSurveyTitle.value;
100                sequencer.survey.uid = fSurveyUid.value;
101                sequencer.survey.description = fSurveyDescription.value;
102                sequencer.survey.questions.uids = stringToArray(fQuestionUids.value, ",");
103                break;
104            default:
105                break;
106        }
107       
108       
109        sequencer.state.numSteps = 0;
110        sequencer.state.loaded = false;
111        sequencer.settings.efficientUpdating = true;
112    }
113    catch (e) {
114        // Alert developer of any errors while setting these variables
115        for (error in e) alert(error.message);
116    }
117    // Then remove the hidden fields from the HTML document
118    var hiddenInputs = ge("hiddenInputs");
119    hiddenInputs.parentNode.removeChild(hiddenInputs);
120   
121    // finally, run updateSequencer to refresh the visual display of the pipeline
122    updateSequencer();
123}
124
125function initSequencer() {
126    // Called from loadSequencer(). Sets sequencer.settings properties depending on content type and values passed by PHP in hidden fields (currently only one), then removes these fields.
127    // Example: stuff like editor orientation, width/height, content type, maxObjects contained, etc...
128   
129    // load settings fields first
130    var fContentType = ge("sContentTypeField");
131    var content = ge("seqContent");
132    //sequencer.settings.content.contentType = fContentType.value.toLowerCase();
133    sequencer.settings.content.contentType = fContentType.value;
134    //Then select settings from a few presets
135   
136    switch (sequencer.settings.content.contentType.toLowerCase()) {
137        case "session":
138            sequencer.settings.content.orientation = "horizontal";
139            sequencer.settings.content.width = 800;
140            sequencer.settings.content.height = 125;
141            content.style.width = sequencer.settings.content.width+"px";
142            content.style.height = sequencer.settings.content.height+"px";
143            addClass(content, "horizontal");
144            break;
145        case "survey":
146            sequencer.settings.content.orientation = "vertical";
147            sequencer.settings.content.width = 650;     //guesstimated
148            var roomH = screen.availHeight;
149            sequencer.settings.content.height = roomH - 200;
150            content.style.width = sequencer.settings.content.width+"px";
151            content.style.height = sequencer.settings.content.height+"px";
152            addClass(content, "vertical");
153            break;
154        default:
155            break;
156    }
157    fContentType.parentNode.parentNode.removeChild(fContentType.parentNode);
158}
159
160// Updating, drawing, formatting
161
162function updateSequencer() {
163    // Code that manages drawing and adding of new visual representations of objects in the sequencer.
164
165    /*
166         * Description:
167         * This function updates the visual elements in the sequencer content view to match the current state of the sequencer.session.pipeline property.
168         * It queries the database for object properties via AJAX (returnStep/Display/.php), then inserts divider div's in between where needed.
169         */
170       
171    switch (sequencer.settings.content.contentType.toLowerCase()) {
172        case "session":
173            updateSequencer_Session();
174            break;
175        case "survey":
176            updateSequencer_Survey();
177            break;
178        default:
179            // Why would this even be called?
180            break;
181    }
182}
183
184function updateSequencer_Session() {
185    var content = ge("seqContentWrapper");
186    var requestString, needsUpdating;
187    var args;
188   
189    if (sequencer.state.loaded == false || sequencer.settings.efficientUpdating == false) {  // This is the first update of the sequencer since page load, therefore it contains no previous steps
190        // Stop running this function if the pipeline does not contain any elements to draw
191        if (sequencer.session.pipeline.uids.length == 0 || !sequencer.session.pipeline.uids.length) return;
192        // First clear the entire content wrapper, just for safety and in case efficientUpdating is off
193        while (content.firstChild) {
194            content.removeChild(content.firstChild);
195        }
196        args = [];
197        needsUpdating = [];
198        for (var i = 0; i < sequencer.session.pipeline.uids.length; i++) {
199            args.push({
200                uid: sequencer.session.pipeline.uids[i],
201                type: sequencer.session.pipeline.types[i]
202            });
203            needsUpdating.push(new Array(i, sequencer.session.pipeline.uids[i], sequencer.session.pipeline.types[i]));
204        }
205       
206        requestString = "args="+JSON.stringify(args);
207        newAjaxRequest(requestString, "returnObjectDisplay.php", function(result){
208            content.removeChild(loadingGif);
209            insertNewObjects(result.responseText, needsUpdating);
210        }, true);
211        sequencer.state.loaded = true;
212        var loadingGif = ce("div");
213        loadingGif.innerHTML = "<img src='images/ui/ajax-loader-round.gif' style='float: left; margin:auto auto;' />";
214        content.appendChild(loadingGif);
215    }
216    else {
217        // This means that one or more steps are being added, not an entire pipeline's worth of them
218        needsUpdating = new Array();
219        args = [];
220        // Add steps that need updating to the needsUpdating array (index, uid, type).
221        for (var i = 0; i < sequencer.session.pipeline.uids.length; i++) {
222            if (sequencer.session.pipeline.upToDate[i] == true) continue;
223            needsUpdating.push(new Array(i, sequencer.session.pipeline.uids[i], sequencer.session.pipeline.types[i]));
224            args.push({
225                uid: sequencer.session.pipeline.uids[i],
226                type: sequencer.session.pipeline.types[i]
227            });       
228        }
229       
230        requestString = "args="+JSON.stringify(args);
231        newAjaxRequest(requestString, "returnObjectDisplay.php", function(result){
232            console.log(result.responseText);
233            insertNewObjects(result.responseText, needsUpdating);
234        }, true);
235       
236        // Optional bit with the loading GIF
237        for (var i = 0; i < needsUpdating.length; i++) {
238            var loadingDiv = ce("div");
239            loadingDiv.className = "displayStep loading";
240            loadingDiv.innerHTML = "<img src='images/ui/ajax-loader-round.gif' />";
241            if (needsUpdating[i][0] > sequencer.state.numSteps-1) {
242                content.appendChild(loadingDiv);
243                sequencer.state.numSteps++;
244            }
245            else {
246                content.replaceChild(loadingDiv, content.childNodes[i][0]*2);
247            }
248        }
249        updateDividers();
250    // End optional
251    }
252}
253
254// THIS NEEDS SOME WORK! A LOT OF IT ACTUALLY!
255function updateSequencer_Survey() {
256   
257    var content = ge("seqContentWrapper");
258    var requestString, needsUpdating, args;
259    // Create a reference to the correct field, this might not be needed if we do not generalize the updateSequencer function and instead keep two separate update functions for separate content types.
260    var variables = sequencer.survey;
261   
262    if (sequencer.state.loaded == false || sequencer.settings.efficientUpdating == false) {  // This is the first update of the sequencer since page load, therefore it contains no previous steps
263        // Stop running this function if the questions array does not contain any elements to draw
264        if (sequencer.survey.questions.uids.length == 0 || !sequencer.survey.questions.uids.length) return;
265        // First clear the entire content wrapper, just for safety and in case efficientUpdating is off
266        while (content.firstChild) {
267            content.removeChild(content.firstChild);
268        }
269        args = [];
270        needsUpdating = [];
271        for (var i = 0; i < sequencer.survey.questions.uids.length; i++) {
272            args.push({
273                uid: sequencer.survey.questions.uids[i],
274                type: "Question"
275            });
276            needsUpdating.push(new Array(i, sequencer.survey.questions.uids[i], "Question"));
277        }
278       
279        requestString = "args="+JSON.stringify(args);
280        newAjaxRequest(requestString, "returnObjectDisplay.php", function(result){
281            content.removeChild(loadingGif);
282            insertNewObjects(result.responseText, needsUpdating);
283            sequencer.state.loaded = true;
284        }, true);
285        sequencer.state.loaded = false;
286        var loadingGif = ce("div");
287        loadingGif.innerHTML = "<img src='images/ui/ajax-loader-round.gif' style='float: left; margin:auto auto;' />";
288        content.appendChild(loadingGif);
289    }
290    else {
291
292        // This means that one or more steps are being added, not an entire pipeline's worth of them
293        needsUpdating = new Array();
294        args = [];
295        // Add steps that need updating to the needsUpdating array (index, uid, type).
296        for (var i = 0; i < sequencer.survey.questions.uids.length; i++) {
297            if (sequencer.survey.questions.upToDate[i] == true) continue;
298            needsUpdating.push(new Array(i, sequencer.survey.questions.uids[i], "Question"));
299            args.push({
300                uid: sequencer.survey.questions.uids[i],
301                type: "Question"
302            });       
303        }
304       
305        requestString = "args="+JSON.stringify(args);
306        newAjaxRequest(requestString, "returnObjectDisplay.php", function(result){
307            //console.log(result.responseText);
308            insertNewObjects(result.responseText, needsUpdating);
309        }, true);
310       
311        // Optional bit with the loading GIF
312        for (var i = 0; i < needsUpdating.length; i++) {
313            var loadingDiv = ce("div");
314            loadingDiv.className = "displayStep loading";
315            loadingDiv.innerHTML = "<img src='images/ui/ajax-loader-round.gif' />";
316            if (needsUpdating[i][0] > sequencer.state.numSteps-1) {
317                content.appendChild(loadingDiv);
318                sequencer.state.numSteps++;
319            }
320            else {
321                content.replaceChild(loadingDiv, content.childNodes[i][0]*2);
322            }
323        }
324        updateDividers();
325    // End optional
326       
327    }
328       
329}
330
331function updateDividers() {
332    // Function that checks for correct number of dividers in the pipeline, adds or deletes as needed.
333   
334    var content = ge("seqContentWrapper");
335    // Loop through all elements in seqContentWrapper
336    for (var i = 0; i < content.childNodes.length; i++) {
337        var element = content.childNodes[i];
338        // If the element is not a displayStep, continue with the next childNode.
339        if (!hasClass(element, "displayStep")) {
340            continue;
341        }
342        // Get the two elements next to the current element, if they don't exist or are not displaySteps, store false.
343        var lastElement = (element.previousSibling && element.previousSibling.nodeName == "DIV") ? element.previousSibling : false;
344        var nextElement = (element.nextSibling && element.nextSibling.nodeName == "DIV") ? element.nextSibling : false;
345        // If these elements exist, and they are not dividers, add an element in between.
346        if (lastElement != false) {
347            if (!hasClass(lastElement, "divider")){
348                var newDivider = ce("div");
349                addClass(newDivider, "divider");
350                addClass(newDivider, sequencer.settings.content.orientation);
351                content.insertBefore(newDivider, element);
352                delete newDivider;
353            }
354        }
355        if (nextElement != false) {
356            if (!hasClass(nextElement, "divider")){
357                var newDivider = ce("div");
358                addClass(newDivider, "divider");
359                addClass(newDivider, sequencer.settings.content.orientation);
360                content.insertBefore(newDivider, nextElement);
361                delete newDivider;
362            }
363        }
364    }
365   
366    // Because updateDividers is always called last, we set updating to false here, so the user can again issue commands to the pipeline
367    sequencer.state.updating = false;   // Re-enable new actions
368}
369
370// Adding new objects
371
372function submitToolbox(type) {
373    // Handles new object creation code when user clicks on a toolbox button
374   
375    // Do not accept new creation requests if the sequencer is still updating a previous object.
376    if (sequencer.state.updating == true) return;
377    sequencer.state.updating = true;
378    deselectStep();   
379    var c = "objectToCreate="+type;
380    var u = "createObject.php";
381       
382    newAjaxRequest(c, u, function(result) {
383        sequencer.session.pipeline.uids.push(removeNL(result.responseText));
384        sequencer.session.pipeline.types.push(type);
385        sequencer.session.pipeline.upToDate.push(false);
386        updateSequencer();
387    }, true);
388}
389
390function insertNewObjects(responseText, needsUpdating) {
391    //console.log(responseText);
392    // Container function that calls different insertNewX() functions depending on content type. Called from updateSequencer().
393    var response = JSON.parse(responseText);
394    // For now I assume that only one type of element can be displayed in the editor at one time. Therefore, the type of response[0] is the type of all elements of response.
395    switch (sequencer.settings.content.contentType.toLowerCase()) {
396        case "session":
397            insertNewSteps(response, needsUpdating);
398            break;
399        case "survey":
400            insertNewQuestions(response, needsUpdating);
401            break;
402        default:
403            break;
404               
405    }
406}
407
408function addQuestion_Click() {
409    if (sequencer.state.updating == true) return;
410    sequencer.state.updating = true;
411    deselectStep();   
412    var c = "objectToCreate=question";
413    var u = "createObject.php";
414       
415    newAjaxRequest(c, u, function(result) {
416        debugger;
417        sequencer.survey.questions.uids.push(removeNL(result.responseText));
418        sequencer.survey.questions.upToDate.push(false);
419        updateSequencer();
420    }, true);
421}
422
423//> Session specific
424
425function insertNewSteps(response, needsUpdating) {
426    /*
427         * This is a function displaying how to handle the visual object representation in solely javascript.
428         * Communication of relevant variables between PHP and JS happens in JSON format.
429         * PHP returns a JSON array of objects to be created by JS
430         * JS then loops through this array and creates DIVS to be inserted into the sequencer.
431         * These are inserted at the position needsUpdating gives us.
432         */
433    var content = ge("seqContentWrapper");
434    // Remove optional loading images
435    for (var  i = 0; i < content.childNodes.length; i++) {
436        if (hasClass(content.childNodes[i], "loading")) {
437            content.removeChild(content.childNodes[i]);
438        }
439    }
440    // End optional
441   
442    for (var i = 0; i < response.length; i++) {
443        var tempDiv = ce("div");
444        tempDiv.id = response[i].uid;
445        tempDiv.className = "displayStep";
446        var divImageContainer = ce("div");
447        divImageContainer.className = "displayStepIcon";
448        divImageContainer.addEventListener("click", function(){
449            clickStep(this.parentNode.id);
450        }, false);
451        var divImage = ce("img");
452        divImage.src = "images/icons/"+response[i].type.toLowerCase()+".png";
453        divImageContainer.appendChild(divImage);
454        tempDiv.appendChild(divImageContainer);
455        var divLabel = ce("p");
456        divLabel.innerHTML = response[i].title;
457        tempDiv.appendChild(divLabel);
458       
459        // This for needs to loop backwards so that the steps at the end of the pipeline are added or changed first. This keeps the childNodes index for all further steps intact.
460        for (var j = needsUpdating.length-1; j >= 0; j--) {
461            if (needsUpdating[j][1] != response[i].uid) continue;
462            if (needsUpdating[j][0] > sequencer.state.numSteps-1) {
463                content.appendChild(tempDiv);
464            }
465            else {
466                content.replaceChild(tempDiv, content.childNodes[j][0]*2);
467            }
468            sequencer.session.pipeline.upToDate[needsUpdating[j][0]] = true;
469        }
470    }
471   
472    updateDividers();
473}
474
475function deleteStep() {
476    // Delete a step from both the pipeline.uids variable and its visual representation from the sequencer. Does not actually delete object from database!
477   
478    // check if a step is selected
479    if (sequencer.state.selectedObject.uid == null) {
480        return;
481    }
482    var uid = sequencer.state.selectedObject.uid;
483    // deselect the step to reset the info panel and selection code
484    deselectStep();
485    // splice the step's data from the pipeline
486    var index = sequencer.session.pipeline.uids.indexOf(uid);
487    if (index >= 0 && index < sequencer.session.pipeline.uids.length) {
488        sequencer.session.pipeline.uids.splice(index, 1);
489        sequencer.session.pipeline.types.splice(index, 1);
490        sequencer.session.pipeline.upToDate.splice(index, 1);
491        // Then delete the step visually
492        var element = ge(uid);
493        var divider;
494        if (!element.nextSibling) {
495            // the element is at the end of the pipeline
496            // therefore we remove the previous divider.
497            // Note: it could also be the only element left in the pipeline!
498            divider = (element.previousSibling) ? element.previousSibling : false;
499            if (divider != false) {
500                divider.parentNode.removeChild(divider);
501            }
502        }
503        else {
504            // the element is at any position except the last, therefore we remove the next divider
505            divider = (element.nextSibling) ? element.nextSibling : false;
506            if (divider != false) {
507                divider.parentNode.removeChild(divider);
508            }
509        }
510       
511        // Finally, remove the element itself.
512        element.parentNode.removeChild(element);
513        sequencer.state.numSteps--;
514       
515    }
516}
517
518//> Survey specific
519
520function insertNewQuestions(response, needsUpdating) {
521    //Code that inserts or replaces new object displays in the sequencer. Question version.
522    var content = ge("seqContentWrapper");
523    // Loop through returned question objects
524    for (var i = 0; i < response.length; i++) {
525       
526        /*
527         * The following block of code defines the layout and composition of the question display
528         */
529       
530        // Define the outer frame
531        var frameDiv = ce("div");
532        frameDiv.className = "smallFrame question";
533        frameDiv.id = response[i].uid;
534        var titleDiv = ce("div");
535        titleDiv.className = "smallTitle";
536        var numberDiv = ce("div");
537        numberDiv.className = "listNumber";
538        numberDiv.innerHTML = i.toString();
539        titleDiv.appendChild(numberDiv);
540        titleDiv.innerHTML += response[i].uid;
541        frameDiv.appendChild(titleDiv);
542       
543        // The frame now has a header bar
544        // On to the content frame
545        // Will use new "ce();" function, shorthand for ce
546       
547        var contentDiv = ce("div");
548        contentDiv.className = "content";
549        var questionBody = ce("p");
550        questionBody.innerHTML = response[i].description;
551        var questionParamsDiv = ce("div");
552        questionParamsDiv.className = "questionParamsView";
553        questionParamsDiv.innerHTML = "Object type: "+response[i].type;
554        contentDiv.appendChild(questionBody);
555        contentDiv.appendChild(questionParamsDiv);
556        frameDiv.appendChild(contentDiv);
557       
558        // And finally the controls div
559        var controlsDiv = ce("div");
560        controlsDiv.className = "controls";
561        var editButton = ce("input");
562        var removeButton = ce("input");
563        editButton.value = "Edit";
564        removeButton.value = "Remove";
565        editButton.className = "smallButton";
566        removeButton.className = "smallButton";
567        editButton.addEventListener("click", function(e){
568            debugger;
569            selectStep(this.parentNode.parentNode.id);
570            if (sequencer.state.editing != true) swapQuestionState();
571        }, false);
572        removeButton.addEventListener("click", function(e){
573            alert('Removing not yet supported!');
574        }, false);
575        controlsDiv.appendChild(editButton);
576        controlsDiv.appendChild(removeButton);
577        frameDiv.addEventListener("click", function(){
578            clickStep(this.id);
579        }, false);
580        frameDiv.appendChild(controlsDiv);
581       
582        /*
583         * This is where the layout code ends
584         * We proceed to insertion of the created DIV into the document
585         */
586       
587        // We now have a full question display DIV contained in the frameDiv variable. We should now add this to the sequencer content.
588        for (var j = needsUpdating.length - 1; j >= 0; j--) {
589            if (needsUpdating[j][1] != response[i].uid) continue;
590            if (needsUpdating[j][0] > sequencer.state.numSteps-1) {
591                content.appendChild(frameDiv);
592                sequencer.state.numSteps++;
593            }
594            else {
595                content.replaceChild(frameDiv, content.childNodes[needsUpdating[j][0]*2]);
596            }
597            sequencer.survey.questions.upToDate[needsUpdating[j][0]] = true;
598           
599        }
600    }
601   
602    sequencer.state.updating = false;   //re-enable user commands
603}
604
605function saveSurvey(confirmSave) {
606    // Sends an AJAX request to the PHP server that saves the current object.
607    var answer = (confirmSave == true) ? confirm("Save changes?") : true;
608    if (answer == false) return;
609   
610    // Check for object type being edited, adjust requestString and target URL to this information.
611    var request = new Object(), url, requestString;
612    switch (sequencer.settings.content.contentType.toLowerCase()) {
613        case "survey":
614            url = "saveSurvey.php";
615            request.title = sequencer.survey.title;
616            request.uid = sequencer.survey.uid;
617            request.description = sequencer.survey.description;
618            request.questions = new Object();
619            request.questions.uids = sequencer.survey.questions.uids;
620            requestString = "args="+JSON.stringify(request);
621            console.log(request);
622            newAjaxRequest(requestString, url, function(result){
623                console.log(result.responseText);
624            }, true);
625            break;
626        case "blaaaat":
627            url = "savesession.php";
628            request.pipeline.uids = sequencer.session.pipeline.uids;
629            request.pipeline.types = sequencer.session.pipeline.types;
630            break;
631    }
632}
633
634
635
636
637
638// general functions and user actions
639
640function clickStep(uid) {
641    // Handles selection of steps
642   
643    if (uid == sequencer.state.selectedObject.uid) {  // user deselected a currently selected step.
644        deselectStep();
645    }
646    else {
647        if (sequencer.state.selectedObject.uid != undefined && sequencer.state.selectedObject.uid != "") {
648            // Change selection if something is already selected
649            deselectStep();
650            selectStep(uid);
651        }
652        else {
653            // Make new selection if nothing was selected
654            selectStep(uid);
655        }       
656    }
657}
658
659function selectStep(uid) {
660    // Called from clickStep(), manages CSS class assignment and updating of state variables. Also calls for info panel update.
661   
662    var element = ge(uid);
663    if (element) {
664        addClass(element, "selected");
665        var type;
666        switch (sequencer.settings.content.contentType.toLowerCase()) {
667            case "session":
668                type = sequencer.session.pipeline.types[sequencer.session.pipeline.uids.indexOf(uid)];
669                break;
670            case "survey":
671                type = "Question";
672                break;
673            default:
674                //Dunno
675                break;
676        }
677        (type != "Question") ? ajaxInfoRequest(uid, ge("infoPanelContent"), type) : type=type /*This does nothing*/;
678        sequencer.state.selectedObject.uid = uid;
679        sequencer.state.selectedObject.index = null;  //Don't know how to do this yet.
680    }
681}
682
683function deselectStep() {
684    // Called from clickStep(). Handles unassignment and updating of state variables. Clears info panel.
685   
686    if (!sequencer.state.selectedObject.uid) return;
687    var element = ge(sequencer.state.selectedObject.uid);
688    removeClass(element, "selected");
689    sequencer.state.selectedObject.uid = null;
690    sequencer.state.selectedObject.index = null;
691    var infoPanel = ge("infoPanelContent");
692    if (infoPanel) {
693        while (infoPanel.firstChild) infoPanel.removeChild(infoPanel.firstChild);
694    }
695}
696
697
698
699function savePipeline (confirmSave) {
700    // Sends an AJAX request to the PHP server that saves the current pipeline. Does not yet support surveys.
701   
702    // First check if user should confirm save action or not.
703    var answer;
704    if (confirmSave == true) {
705        answer = confirm("Save changes to pipeline?");
706    }
707    else {
708        answer = true;
709    }
710    if (answer == false) return;
711    // Then compose requestString for savesession.php, containing pipeline uids, types and the session uid.
712    // TODO: should eventually include stuff like session name as well!
713    var requestString = "uids=";
714    requestString += arrayToString(sequencer.session.pipeline.uids, ",");
715    requestString = requestString.slice(0, requestString.length - 1);   // remove trailing commas
716    requestString += "&types=";
717    requestString += arrayToString(sequencer.session.pipeline.types, ",");
718    requestString = requestString.slice(0, requestString.length - 1);   // remove trailing commas
719    requestString += "&sessionUid=";
720    requestString += sequencer.session.uid;
721    console.log(requestString);
722    newAjaxRequest(requestString, "savesession.php", function(result){
723        console.log(result.responseText);
724    }, true);
725}
726
727function editStep() {
728    // Redirects the browser to the appropriate editor for the selected step type.
729   
730    // first save
731    savePipeline(false);
732    // Then post relevant information so the next editor page knows what object it is supposed to be editing.
733    var postForm = ce("form");
734    var type = sequencer.session.pipeline.types[sequencer.session.pipeline.uids.indexOf(sequencer.state.selectedObject.uid)];
735    postForm.action = type.toLowerCase()+"Editor.php";
736    postForm.method = "POST";
737    var objectUid = ce("input");
738    objectUid.type = "hidden";
739    objectUid.name = "objectUid";
740    objectUid.value = sequencer.state.selectedObject.uid;
741    postForm.appendChild(objectUid);
742    postForm.submit();
743}
744
745function moveStep (direction) {
746    // Moves the selected step up/down (left/right) in the pipeline.
747   
748    // Check if a step is selected
749    if (sequencer.state.selectedObject.uid == null || direction == null) return;
750    // Check if the step is not at either end of the pipeline
751    var index = sequencer.session.pipeline.uids.indexOf(sequencer.state.selectedObject.uid);
752    if (index == -1) return;
753    if ((index < 0) || (index >= sequencer.session.pipeline.uids.length) || (index == 0 && direction == -1) || (index == sequencer.session.pipeline.uids.length - 1 && direction == 1)) {
754        alert("Cannot move out of bounds!");
755        return;
756    }
757   
758    // Find the two elements in the editor content display
759    var content = ge("seqContentWrapper");
760    var element = ge(sequencer.session.pipeline.uids[index]);
761    var otherElement = ge(sequencer.session.pipeline.uids[index+direction]);
762    // First buffer the two elements
763    var tempElement = element.cloneNode(true);
764    var tempOtherElement = otherElement.cloneNode(true);
765    var placeHolderElement = ce("div");
766    placeHolderElement.id = "placeholder_element";
767    content.replaceChild(placeHolderElement, otherElement);
768    content.replaceChild(tempOtherElement, element);
769    content.replaceChild(tempElement, placeHolderElement);
770    //This should work.
771    // A-B     Start positions, backup to tA and tB
772    // A-X     Replace B with placeholder X
773    // B-X     Replace A with tB
774    // B-A     Replace placeholder X with tA.
775    // The two elements are now swapped.
776     
777    // Now swap the array entries.
778    sequencer.session.pipeline.uids[index] = sequencer.session.pipeline.uids.splice(index+direction, 1, sequencer.session.pipeline.uids[index])[0];
779    sequencer.session.pipeline.types[index] = sequencer.session.pipeline.types.splice(index+direction, 1, sequencer.session.pipeline.types[index])[0];
780   
781    // Finally, rebind the onCLick events to work properly again. I don't know if this 'clones' the event as well (therefore re-adding them casuses memory bloat...) or not.
782    for (var i = 0; i < tempOtherElement.childNodes.length; i++){
783        var childNode = tempOtherElement.childNodes[i];
784        if (hasClass(childNode, "displayStepIcon")) {
785            childNode.addEventListener("click", function() {
786                clickStep(this.parentNode.id);
787            }, false);
788        }
789    }
790    for (var i = 0; i < tempElement.childNodes.length; i++){
791        var childNode = tempElement.childNodes[i];
792        if (hasClass(childNode, "displayStepIcon")) {
793            childNode.addEventListener("click", function() {
794                clickStep(this.parentNode.id);
795            }, false);
796        }
797    }
798   
799   
800   
801     
802// The alternative is to use event bubbling to capture the event on a higher level.
803// Basically, we bind a super-structure onclick event that uses e.target|| event.srcElement to determine which element to move and select.
804// http://stackoverflow.com/questions/29624/how-to-maintain-correct-javascript-event-after-using-clonenodetrue
805// Pro: clean implementation, less events binded.
806// Con: Difficult, this already works fine, probably tougher to use in conjunction with multifunctionality (sessions, surveys, questionsets, etc in one kind of editor)
807     
808}
809
810// WORK IN PROGRESS
811
812
813
814
815
816
817
818
819/******************/
820/* TEMP FUNCTIONS */
821/******************/
822
823// Temp function that creates a dummy question to test the insertNewQuestions function.
824// NOTE: CAN BE REMOVED!
825function debug_addQuestion() {
826    // Derp, natuurlijk werkt de addQuestion call niet twee keer. Hij creeert twee keer exact hetzelfde object. Bottom line: het werkt. Nu de editing code voor deze questions gaan schrijven!
827    var response = [{
828        uid: "1234567890testuid",
829        title: "dummyQuestion",
830        description: "This is a dummy question, not a real one!",
831        type: "question"
832    }];
833   
834    var needsUpdating = [[
835    0,
836    "1234567890testuid",
837    "question"
838    ]];
839   
840    insertNewQuestions(response, needsUpdating);
841}
842
843// Temp function that articially switches content type when the page is already loaded, to easily test layout changes and content flow.
844// NOTE: CAN BE REMOVED!
845function debug_switchType() {
846    var content = ge("seqContent");
847    if (sequencer.settings.content.contentType == "session") {
848        // Set to survey
849        sequencer.settings.content.contentType = "survey";
850        sequencer.settings.content.orientation = "vertical";
851        removeClass(content, "horizontal");
852        addClass(content, "vertical");
853    }
854    else {
855        // Set to session
856        sequencer.settings.content.contentType = "session";
857        sequencer.settings.content.orientation = "horizontal";
858        removeClass(content, "vertical");
859        addClass(content, "horizontal");
860    }
861}
Note: See TracBrowser for help on using the repository browser.