Ignore:
Timestamp:
01/13/12 18:11:16 (13 years ago)
Author:
fpvanagthoven
Message:

getObject.php is de nieuwe pagina die gewoon PHP objecten in JSON zet en echoet. Nu werkt alleen de questionEditor er op, uiteindelijk zou dit ook de infopanel en de sequencer moeten gaan serven.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/jos-branch/js/questionEditorScripts.js

    r233 r235  
     1// Old, should not be used anymore after class-based QuestionEditor() is finished!!!!!
    12var qUID, parentObjectUID, qName, qTag, qType, qAnswerLength;
     3
     4var questionEditor = new QuestionEditor();
    25
    36function selectAnswerType(){
     
    4245}
    4346
    44 function createNewInputLabel(text, target) {
     47function createNewInputLabel(text, target, side) {
    4548    var newLabel = document.createElement("label");
    4649    if (target) newLabel.setAttribute("for",target);
    4750    newLabel.innerHTML = text;
     51    if (side) newLabel.className = side;
    4852    return newLabel;
    4953}
     
    6468}
    6569
    66 
    67 ///////////////////
    68 /* INT SELECTION */
    69 ///////////////////
    70 
    71 function selectIntType() {
    72     var selectBox = document.getElementById("questionType");
    73     var content = document.getElementById("questionEditor_questionParams");
    74     // Add minimum value input
    75     var minValueBox = createNewElement("input","text","minValueBox", "questionParamField", null);
    76     var minValueBoxLabel = createNewInputLabel("Minimum value:", "minValueBox");
    77     addClass(minValueBoxLabel, "formLineBreak");
    78     content.appendChild(minValueBoxLabel);
    79     content.appendChild(minValueBox);
    80     // Add maximum value input
    81     var maxValueBox = createNewElement("input", "text", "maxValueBox", "questionParamField", null);
    82     var maxValueBoxLabel = createNewInputLabel("Maximum value:","maxValueBox");
    83     addClass(maxValueBoxLabel, "formLineBreak");
    84     content.appendChild(maxValueBoxLabel);
    85     content.appendChild(maxValueBox);
    86 }
    8770
    8871/////////////////////
     
    171154}
    172155
     156//////////////////////////////////////////
     157/* QUESTION EDITOR CLASS BASED APPROACH */
     158//////////////////////////////////////////
     159
    173160function QuestionEditor() {
    174161    // Properties   
    175     this.uid = null;
    176     this.element = null;
    177     this.paramsElement = null;
    178    
     162    this.uid = null;    // The uid of the question contained in this editor
     163    var me = this;      // Retarded self-reference because: Javascript
     164    this.element = null;    // The parent div element containing the questionEditor
     165    this.paramsElement = null;  // The parent parameters element where all the input sets will be located
     166    this.paramSets = null;  // The currently enabled input sets to be displayed in the paramsElement
     167   
     168    // Methods
    179169    // Basic functionality
    180170    this.setValues = function(arguments) {
    181         // Query question information from database, then fill element fields with correct information.
    182        
     171        var question = JSON.parse(arguments);       
    183172    }
    184173    this.init = function() {
    185174        // Outer div
    186         this.element = ce("div");
    187         this.element.className = "smallFrame questionEditor";
    188         this.element.id = sequencer.state.selectedObject.uid;
    189         this.uid = sequencer.state.selectedObject.uid;
     175        me.element = ce("div");
     176        me.element.className = "smallFrame questionEditor";
     177        me.element.id = sequencer.state.selectedObject.uid;
     178        me.uid = sequencer.state.selectedObject.uid;
    190179        // Header
    191180        var titleDiv = ce("div");
     
    193182        var numberDiv = ce("div");
    194183        numberDiv.className = "listNumber";
    195         numberDiv.innerHTML = "4";
     184        numberDiv.innerHTML = "4"; //TODO
    196185        var nameSpan = ce("span");
    197         nameSpan.id = "qTitleField";
     186        nameSpan.id = "qeTitleField";
    198187        nameSpan.innerHTML = "New question";
    199188        titleDiv.appendChild(numberDiv);
    200189        titleDiv.innerHTML += "Editing: ";
    201190        titleDiv.appendChild(nameSpan);
    202         this.element.appendChild(titleDiv);
     191        me.element.appendChild(titleDiv);
    203192   
    204193        //Content area
    205194        var contentDiv = ce("div");
    206195        contentDiv.className = "content";
    207         var bodyText = createNewElement("textarea", null, "qBodyTextField", "qBodyTextField", null);
     196        var bodyText = createNewElement("textarea", null, "qeBodyTextField", "qeBodyTextField", null);
    208197        bodyText.value = "Question body text goes here";
    209    
    210    
     198        contentDiv.appendChild(bodyText);
     199       
    211200        // The dynamic questionParams div, where all the control elements and inputs will be located
    212201        var questionParams = ce("div");
    213         this.paramsElement = questionParams;
     202        me.paramsElement = questionParams;
    214203        questionParams.className = "questionParams";
    215         contentDiv.appendChild(bodyText);
    216         var qIdentField = createNewElement("input", "text", "qIdentField", "questionParamField", null);
    217         var qIdentField_lbl = createNewInputLabel("Question code:","qIdentField", "l");
    218         questionParams.appendChild(qIdentField_lbl);
    219         questionParams.appendChild(qIdentField);
    220    
    221         var qTypeField = createNewElement("select", null, "qTypeField", "questionParamField", null);
    222         var qTypeField_lbl = createNewInputLabel("Answer type:","qTypeField", "l");
    223         questionParams.appendChild(qTypeField_lbl);
    224         questionParams.appendChild(qTypeField);
    225         qTypeField.addEventListener("change", function(){
    226             debugger;
    227             selectAnswerType();
     204        questionParams.id = "qeQuestionParamsDiv";
     205       
     206        var basicContainer = ce("div");
     207        basicContainer.className = "basicInputs";
     208       
     209        var qeCodeField = createNewElement("input", "text", "qeCodeField", "qeParamField", null);
     210        var qeCodeField_lbl = createNewInputLabel("Question code:","qeCodeField", "l");
     211        basicContainer.appendChild(qeCodeField_lbl);
     212        basicContainer.appendChild(qeCodeField);
     213   
     214        var qeTypeField = createNewElement("select", null, "qeTypeField", "qeParamField", null);
     215        var qeTypeField_lbl = createNewInputLabel("Answer type:","qeTypeField", "l");
     216        basicContainer.appendChild(qeTypeField_lbl);
     217        basicContainer.appendChild(qeTypeField);
     218        questionParams.appendChild(basicContainer);
     219        qeTypeField.addEventListener("change", function(){
     220            //debugger;
     221            me.selectAnswerType();
    228222        }, false);
    229223        // Add the select options. Do this in a block scope to prevent the o1 var from messing things up.
     
    233227            o1.value = null;
    234228            o1.text = "";
    235             qTypeField.appendChild(o1);
     229            qeTypeField.appendChild(o1);
    236230       
    237231            o1 = ce("option");
    238232            o1.value = "int";
    239233            o1.text = "Integer";
    240             qTypeField.appendChild(o1);
     234            qeTypeField.appendChild(o1);
    241235       
    242236            o1 = ce("option");
    243237            o1.value = "scale";
    244238            o1.text = "Scale";
    245             qTypeField.appendChild(o1);
     239            qeTypeField.appendChild(o1);
    246240       
    247241            o1 = ce("option");
    248242            o1.value = "choice";
    249243            o1.text = "Multiple choice";
    250             qTypeField.appendChild(o1);
     244            qeTypeField.appendChild(o1);
    251245       
    252246            o1 = ce("option");
    253247            o1.value = "text";
    254248            o1.text = "Text";
    255             qTypeField.appendChild(o1);
     249            qeTypeField.appendChild(o1);
    256250        }
    257251   
    258252        contentDiv.appendChild(questionParams);
    259         this.element.appendChild(contentDiv);
     253        me.element.appendChild(contentDiv);
    260254   
    261255        // Controls bar
     
    267261        controlsDiv.appendChild(btnSave);
    268262        btnSave.addEventListener("click", function(){
    269             swapQuestionState(true);
     263            me.save();
    270264        }, false);
    271265        btnDiscard.addEventListener("click", function(){
    272             swapQuestionState(false);
     266            me.discard();
    273267        }, false);
    274         this.element.appendChild(controlsDiv);
     268        me.element.appendChild(controlsDiv);
     269        me.paramSets = new Array();
     270        me.paramSets.push("basic");
     271    }
     272    this.save = function() {
    275273        var request = {
    276             "uid": this.uid,
    277             "type": "Question"
    278         }
    279         var requestString = "args="+JSON.stringify(request);
    280         newAjaxRequest(requestString, "returnObjectDisplay.php", function(result){
    281             // Fill in the element with the correct values
    282             // Result is sent to setValues in object form already, perhaps better idea to send in string format and parse within the target method?
    283             this.setValues(JSON.parse(result.responseText));
    284         }, true);
    285     }
    286     this.submit = function() {
    287         var request = {
    288             "title": ge("qTitleField").value,
    289             "type": ge("qTypeField").value
     274            "title": ge("qeTitleField").value,
     275            "type": ge("qeTypeField").value
    290276        }
    291277       
     
    294280        }, true);
    295281    }
     282    this.reset = function() {
     283        me.init();
     284    }
    296285   
    297286    // Updating input fields
    298     this.selectAnswertype = function () {
     287    this.selectAnswerType = function () {
    299288        // Switch statement, call this.type_x where x is the relevant answer type.
    300289        // For all this.type_X funtions:
     
    304293        // Important: use the this.paramsElement to access the questionParams div!
    305294        // To fully make use of this class-based approach, the editor should be added to a global variable. This global variable should be removed on page unload!
     295   
     296        var type = ge("qeTypeField").value;
     297        switch (type) {
     298            case "int":
     299                me.type_Integer();
     300                break;
     301            case "scale":
     302                me.type_Scale();
     303                break;
     304            case "choice":
     305                me.type_Choice();
     306                break;
     307            case "text":
     308                me.type_Text();
     309                break;
     310            default:
     311                //Do nothing
     312                break;
     313               
     314        }
     315   
     316    }
     317    this.checkInputSets = function () {
     318        // Loop through all input containers in the paramsField
     319        for (var n = 0; n < me.paramsElement.childNodes.length; n++) {
     320            if (me.paramsElement.childNodes[n].className == "basicInputs") continue;
     321            // Check if the class (inputSet) is currently in paramSets
     322            if (me.paramSets.indexOf(me.paramsElement.childNodes[n].className) < 0) {
     323                me.paramsElement.childNodes[n].parentNode.removeChild(me.paramsElement.childNodes[n]);
     324                n--;
     325            }
     326           
     327        }
     328    }
     329   
     330    this.type_Integer = function () {
     331        if (me.paramSets.indexOf("int_basic") < 0) {
     332            me.paramSets = new Array("int_basic");
     333        }
     334        else return;
     335       
     336        me.checkInputSets();
     337               
     338        var qeMinValueField = createNewElement("input", "text", "qeMinValueField", "qeParamField", null);
     339        var qeMinValueField_lbl = createNewInputLabel("Minimum value: ", "qeMinValueField", "l");
     340        var qeMaxValueField = createNewElement("input", "text", "qeMaxValueField", "qeParamField", null);
     341        var qeMaxValueField_lbl = createNewInputLabel("Maximum value: ", "qeMaxValueField", "l");
     342        var container = ce("div");
     343        container.className = "int_basic";
     344        container.appendChild(qeMinValueField_lbl);
     345        container.appendChild(qeMinValueField);
     346        container.appendChild(qeMaxValueField_lbl);
     347        container.appendChild(qeMaxValueField);
     348        me.paramsElement.appendChild(container);
     349       
    306350    }
    307351    this.type_Scale = function () {
    308        
    309     }
    310     this.type_Integer = function () {
    311        
     352        if (me.paramSets.indexOf("scale_basic") < 0) {
     353            me.paramSets = new Array("scale_basic");
     354        }
     355        else return;
     356        // Clear any input sets that should not be there
     357        me.checkInputSets();
     358       
     359        var container = ce("div");
     360        container.className = "scale_basic";
     361       
     362        // Number of choices SELECT
     363        var numChoicesField = createNewElement("select", null, "qeNumChoicesField", "qeParamField", null);
     364        var numChoicesField_lbl = createNewInputLabel("Number of choices", "qeNumChoicesField", "l");
     365        // SELECT options
     366        for (var n = 2; n < 11; n++) {
     367            var o = ce("option");
     368            o.value = n-1;
     369            o.text = n-1;
     370            numChoicesField.appendChild(o);
     371        }
     372        container.appendChild(numChoicesField_lbl);
     373        container.appendChild(numChoicesField);
     374       
     375        // Scale labels CHECKBOX and TEXTs
     376        var legendsEnabledField = createNewElement("input", "checkbox", "qeLegendsEnabledField", "qeParamField", null);
     377        var legendsEnabledField_lbl = createNewInputLabel("Enable legends", "qeLegendsEnabledField", "l");
     378        container.appendChild(legendsEnabledField_lbl);
     379        container.appendChild(legendsEnabledField);
     380       
     381        var upperLegendText = createNewElement("input", "text", "qeUpperLegendField", "qeParamField", null);
     382        var lowerLegendText = createNewElement("input", "text", "qeLowerLegendField", "qeParamField", null);
     383        var upperLegendText_lbl = createNewInputLabel("Upper legend", "qeUpperLegendField", "l");
     384        var lowerLegendText_lbl = createNewInputLabel("Lower legend", "qeLowerLegendField", "l");
     385        container.appendChild(lowerLegendText_lbl);
     386        container.appendChild(lowerLegendText);
     387        container.appendChild(upperLegendText_lbl);
     388        container.appendChild(upperLegendText);
     389       
     390        me.paramsElement.appendChild(container);
    312391    }
    313392    this.type_Text = function () {
     
    315394    }
    316395    this.type_Choice = function() {
    317        
    318     }
    319    
    320 }
    321 
    322 function newEditor() {
    323     // Get variables, ofc not null...
    324     var arguments = null;
    325     var editor = new QuestionEditor(arguments);
    326     editor.setValues(arguments);
    327     ge("seqContentWrapper").appendChild(editor.element);
    328 // Etc. We can put the switchModes() command in here as well, I'd say...
    329 // Or should that be in the QuestionEditor class? That's probably better, since it has direct access to all the internal variables.
    330 }
    331 
    332 function startEditingQuestion(uid) {
    333     if (sequencer.state.editing == true) return;
    334    
    335     var element = ge(uid);
    336     var editor = new QuestionEditor();
    337     var newElement = editor.createElement();
    338     // Query parameters
    339     editor.setValues(parameters);
    340     element.parentNode.replaceChild(newElement, element);
    341     sequencer.state.editing = true;
    342 }
     396        debugger;
     397        if (me.paramSets.indexOf("choice_basic")) {
     398            me.paramSets = new Array("choice_basic");
     399        }
     400        else return;
     401       
     402        me.checkInputSets();
     403       
     404        var container = ce("div");
     405        container.className = "choice_basic";
     406        // num options SELECT
     407        var numOptionsSelect = createNewElement("select", null, "qeNumOptionsField", "qeParamField", null);
     408        var numOptionsSelect_lbl = createNewInputLabel("Number of options", "qeNumOptionsField", "l");
     409        for (var n = 2; n < 11; n++) {
     410            var o = ce("option");
     411            o.value = n-1;
     412            o.text = n-1;
     413            numOptionsSelect.appendChild(o);
     414        }
     415        container.appendChild(numOptionsSelect_lbl);
     416        container.appendChild(numOptionsSelect);
     417       
     418        me.paramsElement.appendChild(container);
     419    }
     420       
     421    // Editing
     422    this.editQuestion = function(uid) {
     423        if (sequencer.state.editing == true) return;
     424        if (sequencer.state.loaded == false) return;
     425        sequencer.state.editing = true;
     426       
     427        var request = {
     428            "type": "Question",
     429            "uid": uid
     430        }
     431       
     432        var requestString = "args="+JSON.stringify(request);
     433        sequencer.state.loaded = false;
     434        newAjaxRequest(requestString, getObject.php, function(result){
     435            // Once results are in
     436            questionEditor.setValues(result.responseText);
     437            sequencer.state.loaded = true;
     438        }, true);
     439    }
     440    this.createNewQuestion = function() {
     441        if (sequencer.state.editing == true) return;
     442        if (sequencer.state.loading == true) return;
     443        sequencer.state.editing = true;
     444   
     445        me.reset();
     446        var container = ge("seqContentWrapper");
     447        container.appendChild(me.element);
     448    }
     449}
Note: See TracChangeset for help on using the changeset viewer.