Ignore:
Timestamp:
01/16/12 23:12:50 (13 years ago)
Author:
tschipper
Message:
 
File:
1 edited

Legend:

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

    r235 r236  
    1 // Old, should not be used anymore after class-based QuestionEditor() is finished!!!!!
    2 var qUID, parentObjectUID, qName, qTag, qType, qAnswerLength;
    3 
    41var questionEditor = new QuestionEditor();
    5 
    6 function selectAnswerType(){
    7     var selectBox = document.getElementById("questionType");
    8     if (selectBox.value != undefined && selectBox.value != "") {
    9         qType = selectBox.value;
    10     }
    11     else {
    12         return;
    13     }
    14     removeWrongAnswerFields(selectBox);
    15     switch (qType) {
    16         case "int":
    17             selectIntType();
    18             break;
    19         case "scale":
    20             selectScaleType();
    21             break;
    22         case "choice":
    23             //selectChoiceType();
    24             break;
    25         case "text":
    26             //selectTextType();
    27             break;
    28         default:
    29             alert("Invalid answer type selected!");
    30             break;
    31     }
    32    
    33 }
    342
    353function createNewElement(tag, type, id, cl, value) {
     
    5321}
    5422
    55 function removeWrongAnswerFields(el) {
    56     while (el.nextSibling) {
    57         el.parentNode.removeChild(el.nextSibling);
    58     }
    59 }
    60 
    6123function updateIdentifier() {
    6224    var identField = document.getElementById("questionIdentifierField");
     
    6830}
    6931
    70 
    71 /////////////////////
    72 /* SCALE SELECTION */
    73 /////////////////////
    74 
    75 function selectScaleType() {
    76     // I heard you like walls of text!
    77     var content = document.getElementById("questionEditor_questionParams");
    78     // Add number of choices input
    79     var numChoicesBox = createNewElement("select", null, "numChoicesBox","questionParamField",null);
    80     var numChoicesBoxLabel = createNewInputLabel("Scale size:","numChoicesBox");
    81     for (var i = 0; i < 10; i++) {
    82         var option = createNewElement("option");
    83         option.text = i+1;
    84         option.value = i+1;
    85         numChoicesBox.appendChild(option);
    86     }
    87     addClass(numChoicesBoxLabel, "formLineBreak");
    88     content.appendChild(numChoicesBoxLabel);
    89     content.appendChild(numChoicesBox);
    90     // Add legends enabled input
    91     var legendsEnabledCheckBox = createNewElement("input","checkbox","legendsEnabledCheckbox","questionParamField",null);
    92     var legendsEnabledCheckBoxLabel = createNewInputLabel("Enable legends","legendsEnabledCheckBox");
    93     addClass(legendsEnabledCheckBoxLabel, "formLineBreak");
    94     content.appendChild(legendsEnabledCheckBoxLabel);
    95     content.appendChild(legendsEnabledCheckBox);
    96     // Add legend labels boxes
    97     var upperLegendBox = createNewElement("input","text","upperLegendBox","questionParamField");
    98     var lowerLegendBox = createNewElement("input","text","lowerLegendBox","questionParamField");
    99     var lowerLegendBoxLabel = createNewInputLabel("Lower legend","lowerLegendBox");
    100     var upperLegendBoxLabel = createNewInputLabel("Upper legend","upperLegendBox");
    101     addClass(lowerLegendBoxLabel,"formLineBreak");
    102     content.appendChild(lowerLegendBoxLabel);
    103     content.appendChild(lowerLegendBox);
    104     addClass(upperLegendBoxLabel,"formLineBreak");
    105     content.appendChild(upperLegendBoxLabel);
    106     content.appendChild(upperLegendBox);
    107     // Disable these boxes, since the checkbox is unchecked by default
    108     lowerLegendBox.disabled = true;
    109     upperLegendBox.disabled = true;
    110     if (legendsEnabledCheckBox.addEventListener) {
    111         legendsEnabledCheckBox.addEventListener("click", toggleScaleLegends, true);
    112     }
    113 }
    114 
    115 function toggleScaleLegends() {
    116     var content = document.getElementById("questionEditor_questionParams");
    117     var checkbox = document.getElementById("legendsEnabledCheckbox");
    118     var upperLegendBox = document.getElementById("upperLegendBox");
    119     var lowerLegendBox = document.getElementById("lowerLegendBox");
    120     if (checkbox.checked == true) {
    121         upperLegendBox.disabled = false;
    122         lowerLegendBox.disabled = false;
    123     }
    124     else {
    125         upperLegendBox.disabled = true;
    126         lowerLegendBox.disabled = true;
    127     }
    128 }
    129 
    130 ///////////////////////////////
    131 /* MULTIPLE CHOICE SELECTION */
    132 ///////////////////////////////
    133 
    134 function selectChoiceType() {
    135     var selectionBox = document.getElementById("questionType");
    136     var content = document.getElementById("questionEditor_questionParams");
    137 }
    138 
    139 function resizeTextArea() {
    140     var textArea = document.getElementById("questionEditor_bodyText");
    141     if (document.getElementById("hiddenScalingDiv")) {
    142         var hiddenDiv = document.getElementById("hiddenScalingDiv");
    143     }
    144     else {
    145         var hiddenDiv = document.createElement("div");
    146         hiddenDiv.style.visibility = "hidden";
    147         textArea.appendChild(hiddenDiv);
    148     }
    149    
    150     debugger;
    151     hiddenDiv.innerHTML = "";
    152     var userText = textArea.firstChild;
    153     alert(userText);
    154 }
    15532
    15633//////////////////////////////////////////
     
    16441    this.element = null;    // The parent div element containing the questionEditor
    16542    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
     43    this.paramSets = null;  // The currently enabled input sets to be displayed in the paramsElement.
     44    //Currently only supports a single param set (based on answer type), but functionality will scale to multiple sets as needed.
    16745   
    16846    // Methods
     
    20583       
    20684        var basicContainer = ce("div");
    207         basicContainer.className = "basicInputs";
     85        basicContainer.id = "basicInputs";
    20886       
    20987        var qeCodeField = createNewElement("input", "text", "qeCodeField", "qeParamField", null);
     
    276154        }
    277155       
     156        switch (ge("qeTypeField").value) {
     157            case "int":
     158                request.answerType = "int";
     159                request.minValue = parseInt(ge("qeMinValueField").value);
     160                request.maxValue = parseInt(ge("qeMaxValueField").value);
     161                break;
     162            case "scale":
     163                request.answerType = "scale";
     164                request.numChoices = parseInt(ge("qeNumChoicesField").value);
     165                request.legendsEnabled = ge("qeLegendsEnabledField").checked;
     166                request.lowerLegend = ge("qeLowerLegendField").value;
     167                request.upperLegend = ge("qeUpperLegendField").value;
     168                break;
     169            case "choice":
     170                request.answerType = "choice";
     171                break;
     172            case "text":
     173                request.answerType = "text";
     174                break;
     175        }
     176       
    278177        newAjaxRequest(requestString, "createObject.php", function(result){
    279178            // Display a success message, or throw an error.
    280         }, true);
     179            }, true);
    281180    }
    282181    this.reset = function() {
     
    318217        // Loop through all input containers in the paramsField
    319218        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) {
     219            if (me.paramsElement.childNodes[n].id == "basicInputs") continue;
     220            // Check if the id (inputSet) is currently in paramSets
     221            if (me.paramSets.indexOf(me.paramsElement.childNodes[n].id) < 0) {
    323222                me.paramsElement.childNodes[n].parentNode.removeChild(me.paramsElement.childNodes[n]);
    324223                n--;
     
    335234       
    336235        me.checkInputSets();
    337                
     236       
     237        var container = ce("div");
     238        container.id = "int_basic";
     239
    338240        var qeMinValueField = createNewElement("input", "text", "qeMinValueField", "qeParamField", null);
    339241        var qeMinValueField_lbl = createNewInputLabel("Minimum value: ", "qeMinValueField", "l");
    340242        var qeMaxValueField = createNewElement("input", "text", "qeMaxValueField", "qeParamField", null);
    341243        var qeMaxValueField_lbl = createNewInputLabel("Maximum value: ", "qeMaxValueField", "l");
    342         var container = ce("div");
    343         container.className = "int_basic";
     244       
    344245        container.appendChild(qeMinValueField_lbl);
    345246        container.appendChild(qeMinValueField);
     
    358259       
    359260        var container = ce("div");
    360         container.className = "scale_basic";
     261        container.id = "scale_basic";
    361262       
    362263        // Number of choices SELECT
     
    366267        for (var n = 2; n < 11; n++) {
    367268            var o = ce("option");
    368             o.value = n-1;
    369             o.text = n-1;
     269            o.value = n;
     270            o.text = n;
    370271            numChoicesField.appendChild(o);
    371272        }
     
    391292    }
    392293    this.type_Text = function () {
    393        
     294        if (me.paramSets.indexOf("text_basic") < 0) {
     295            me.paramSets = new Array("text_basic");
     296        }
     297        else return;
     298       
     299        me.checkInputSets();
     300       
     301        var container = ce("div");
     302        container.id="text_basic";
    394303    }
    395304    this.type_Choice = function() {
    396         debugger;
    397         if (me.paramSets.indexOf("choice_basic")) {
     305        //debugger;
     306        if (me.paramSets.indexOf("choice_basic") < 0) {
    398307            me.paramSets = new Array("choice_basic");
    399308        }
     
    403312       
    404313        var container = ce("div");
    405         container.className = "choice_basic";
     314        container.id = "choice_basic";
    406315        // num options SELECT
    407316        var numOptionsSelect = createNewElement("select", null, "qeNumOptionsField", "qeParamField", null);
     
    409318        for (var n = 2; n < 11; n++) {
    410319            var o = ce("option");
    411             o.value = n-1;
    412             o.text = n-1;
     320            o.value = n;
     321            o.text = n;
    413322            numOptionsSelect.appendChild(o);
    414323        }
    415324        container.appendChild(numOptionsSelect_lbl);
    416325        container.appendChild(numOptionsSelect);
    417        
     326        numOptionsSelect.addEventListener("change", me.type_Choice_CheckAnswerFields, true);
     327       
     328        var allowMultiple = createNewElement("input", "checkbox", "qeMultipleAnswersField", "qeParamField", null);
     329        var allowMultiple_lbl = createNewInputLabel("Allow multiple answers", "qeMultipleAnswersField", "l");
     330        container.appendChild(allowMultiple_lbl);
     331        container.appendChild(allowMultiple);
     332       
     333        var answersField = ce("div");
     334        answersField.className = "qeParamFieldset";
     335        answersField.id = "qeParamsAnswerFieldset"
     336        container.appendChild(answersField);
     337                       
    418338        me.paramsElement.appendChild(container);
    419     }
    420        
     339        me.type_Choice_CheckAnswerFields();
     340    }
     341    this.type_Choice_CheckAnswerFields = function() {
     342        debugger;
     343        var container = ge("qeParamsAnswerFieldset");
     344        var numAnswers = parseInt(document.getElementById("qeNumOptionsField").value, 10);
     345        var numAnswerFields = 0;
     346        {
     347            for (var i = container.childNodes.length-1; i >= 0; i--) {
     348                if (container.childNodes[i].className = "qeChoiceAnswerGroup") {
     349                    numAnswerFields++;
     350                }
     351            }
     352           
     353        }
     354       
     355        // If there already are the correct number of answer fields, exit the function
     356        if (numAnswers == numAnswerFields) return;
     357        else if (numAnswers > numAnswerFields) {
     358            // Extra inputs need to be added
     359            var n = numAnswers - numAnswerFields;
     360            for (var x = 0; x < n; x++) {
     361                var group = ce("div");
     362                group.className = "qeChoiceAnswerGroup";
     363                var field = createNewElement("input", "text", "qeAnswerField"+(numAnswerFields+x), "qeParamField", null);
     364                var field_lbl = createNewInputLabel((numAnswerFields+x), "qeAnswerField"+(numAnswerFields+x), "l");
     365               
     366                group.appendChild(field_lbl);
     367                group.appendChild(field);
     368                container.appendChild(group);
     369            }
     370        }
     371        else if (numAnswers < numAnswerFields) {
     372            // There are too many inputs and some need to be removed. Start at the end!
     373            // TODO: This is SO inefficient. There has to be a better way, perhaps adding elements to an array?
     374            // TODO: Another approach would be to use the previousSibling property as a way to prevent having to loop through the entire container tree every time an object needs to be removed.
     375            var n = numAnswerFields-numAnswers;
     376            for (var x = 0; x < n; x++) {
     377                for (var y = container.childNodes.length-1; y >= 0; y--) {
     378                    if (container.childNodes[y].className == "qeChoiceAnswerGroup") {
     379                        container.removeChild(container.childNodes[y]);
     380                        break;
     381                    }
     382                }
     383            }
     384        }
     385    }
     386     
    421387    // Editing
    422388    this.editQuestion = function(uid) {
Note: See TracChangeset for help on using the changeset viewer.