[233] | 1 | var qUID, parentObjectUID, qName, qTag, qType, qAnswerLength; |
---|
| 2 | |
---|
| 3 | function selectAnswerType(){ |
---|
| 4 | var selectBox = document.getElementById("questionType"); |
---|
| 5 | if (selectBox.value != undefined && selectBox.value != "") { |
---|
| 6 | qType = selectBox.value; |
---|
| 7 | } |
---|
| 8 | else { |
---|
| 9 | return; |
---|
| 10 | } |
---|
| 11 | removeWrongAnswerFields(selectBox); |
---|
| 12 | switch (qType) { |
---|
| 13 | case "int": |
---|
| 14 | selectIntType(); |
---|
| 15 | break; |
---|
| 16 | case "scale": |
---|
| 17 | selectScaleType(); |
---|
| 18 | break; |
---|
| 19 | case "choice": |
---|
| 20 | //selectChoiceType(); |
---|
| 21 | break; |
---|
| 22 | case "text": |
---|
| 23 | //selectTextType(); |
---|
| 24 | break; |
---|
| 25 | default: |
---|
| 26 | alert("Invalid answer type selected!"); |
---|
| 27 | break; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | function createNewElement(tag, type, id, cl, value) { |
---|
| 33 | var newElement = document.createElement(tag); |
---|
| 34 | if (type != undefined) newElement.type = type; |
---|
| 35 | if (id != undefined) newElement.id = id; |
---|
| 36 | if (cl != undefined) newElement.className = cl; |
---|
| 37 | if (value != undefined) { |
---|
| 38 | newElement.value = value; |
---|
| 39 | newElement.text = value; |
---|
| 40 | } |
---|
| 41 | return newElement; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | function createNewInputLabel(text, target) { |
---|
| 45 | var newLabel = document.createElement("label"); |
---|
| 46 | if (target) newLabel.setAttribute("for",target); |
---|
| 47 | newLabel.innerHTML = text; |
---|
| 48 | return newLabel; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | function removeWrongAnswerFields(el) { |
---|
| 52 | while (el.nextSibling) { |
---|
| 53 | el.parentNode.removeChild(el.nextSibling); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | function updateIdentifier() { |
---|
| 58 | var identField = document.getElementById("questionIdentifierField"); |
---|
| 59 | if (identField.value == undefined && identField.value == "") { |
---|
| 60 | return; |
---|
| 61 | } |
---|
| 62 | var headerField = document.getElementById("header_identifier"); |
---|
| 63 | headerField.innerHTML = identField.value; |
---|
| 64 | } |
---|
| 65 | |
---|
| 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 | } |
---|
| 87 | |
---|
| 88 | ///////////////////// |
---|
| 89 | /* SCALE SELECTION */ |
---|
| 90 | ///////////////////// |
---|
| 91 | |
---|
| 92 | function selectScaleType() { |
---|
| 93 | // I heard you like walls of text! |
---|
| 94 | var content = document.getElementById("questionEditor_questionParams"); |
---|
| 95 | // Add number of choices input |
---|
| 96 | var numChoicesBox = createNewElement("select", null, "numChoicesBox","questionParamField",null); |
---|
| 97 | var numChoicesBoxLabel = createNewInputLabel("Scale size:","numChoicesBox"); |
---|
| 98 | for (var i = 0; i < 10; i++) { |
---|
| 99 | var option = createNewElement("option"); |
---|
| 100 | option.text = i+1; |
---|
| 101 | option.value = i+1; |
---|
| 102 | numChoicesBox.appendChild(option); |
---|
| 103 | } |
---|
| 104 | addClass(numChoicesBoxLabel, "formLineBreak"); |
---|
| 105 | content.appendChild(numChoicesBoxLabel); |
---|
| 106 | content.appendChild(numChoicesBox); |
---|
| 107 | // Add legends enabled input |
---|
| 108 | var legendsEnabledCheckBox = createNewElement("input","checkbox","legendsEnabledCheckbox","questionParamField",null); |
---|
| 109 | var legendsEnabledCheckBoxLabel = createNewInputLabel("Enable legends","legendsEnabledCheckBox"); |
---|
| 110 | addClass(legendsEnabledCheckBoxLabel, "formLineBreak"); |
---|
| 111 | content.appendChild(legendsEnabledCheckBoxLabel); |
---|
| 112 | content.appendChild(legendsEnabledCheckBox); |
---|
| 113 | // Add legend labels boxes |
---|
| 114 | var upperLegendBox = createNewElement("input","text","upperLegendBox","questionParamField"); |
---|
| 115 | var lowerLegendBox = createNewElement("input","text","lowerLegendBox","questionParamField"); |
---|
| 116 | var lowerLegendBoxLabel = createNewInputLabel("Lower legend","lowerLegendBox"); |
---|
| 117 | var upperLegendBoxLabel = createNewInputLabel("Upper legend","upperLegendBox"); |
---|
| 118 | addClass(lowerLegendBoxLabel,"formLineBreak"); |
---|
| 119 | content.appendChild(lowerLegendBoxLabel); |
---|
| 120 | content.appendChild(lowerLegendBox); |
---|
| 121 | addClass(upperLegendBoxLabel,"formLineBreak"); |
---|
| 122 | content.appendChild(upperLegendBoxLabel); |
---|
| 123 | content.appendChild(upperLegendBox); |
---|
| 124 | // Disable these boxes, since the checkbox is unchecked by default |
---|
| 125 | lowerLegendBox.disabled = true; |
---|
| 126 | upperLegendBox.disabled = true; |
---|
| 127 | if (legendsEnabledCheckBox.addEventListener) { |
---|
| 128 | legendsEnabledCheckBox.addEventListener("click", toggleScaleLegends, true); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | function toggleScaleLegends() { |
---|
| 133 | var content = document.getElementById("questionEditor_questionParams"); |
---|
| 134 | var checkbox = document.getElementById("legendsEnabledCheckbox"); |
---|
| 135 | var upperLegendBox = document.getElementById("upperLegendBox"); |
---|
| 136 | var lowerLegendBox = document.getElementById("lowerLegendBox"); |
---|
| 137 | if (checkbox.checked == true) { |
---|
| 138 | upperLegendBox.disabled = false; |
---|
| 139 | lowerLegendBox.disabled = false; |
---|
| 140 | } |
---|
| 141 | else { |
---|
| 142 | upperLegendBox.disabled = true; |
---|
| 143 | lowerLegendBox.disabled = true; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /////////////////////////////// |
---|
| 148 | /* MULTIPLE CHOICE SELECTION */ |
---|
| 149 | /////////////////////////////// |
---|
| 150 | |
---|
| 151 | function selectChoiceType() { |
---|
| 152 | var selectionBox = document.getElementById("questionType"); |
---|
| 153 | var content = document.getElementById("questionEditor_questionParams"); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | function resizeTextArea() { |
---|
| 157 | var textArea = document.getElementById("questionEditor_bodyText"); |
---|
| 158 | if (document.getElementById("hiddenScalingDiv")) { |
---|
| 159 | var hiddenDiv = document.getElementById("hiddenScalingDiv"); |
---|
| 160 | } |
---|
| 161 | else { |
---|
| 162 | var hiddenDiv = document.createElement("div"); |
---|
| 163 | hiddenDiv.style.visibility = "hidden"; |
---|
| 164 | textArea.appendChild(hiddenDiv); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | debugger; |
---|
| 168 | hiddenDiv.innerHTML = ""; |
---|
| 169 | var userText = textArea.firstChild; |
---|
| 170 | alert(userText); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | function QuestionEditor() { |
---|
| 174 | // Properties |
---|
| 175 | this.uid = null; |
---|
| 176 | this.element = null; |
---|
| 177 | this.paramsElement = null; |
---|
| 178 | |
---|
| 179 | // Basic functionality |
---|
| 180 | this.setValues = function(arguments) { |
---|
| 181 | // Query question information from database, then fill element fields with correct information. |
---|
| 182 | |
---|
| 183 | } |
---|
| 184 | this.init = function() { |
---|
| 185 | // 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; |
---|
| 190 | // Header |
---|
| 191 | var titleDiv = ce("div"); |
---|
| 192 | titleDiv.className = "smallTitle"; |
---|
| 193 | var numberDiv = ce("div"); |
---|
| 194 | numberDiv.className = "listNumber"; |
---|
| 195 | numberDiv.innerHTML = "4"; |
---|
| 196 | var nameSpan = ce("span"); |
---|
| 197 | nameSpan.id = "qTitleField"; |
---|
| 198 | nameSpan.innerHTML = "New question"; |
---|
| 199 | titleDiv.appendChild(numberDiv); |
---|
| 200 | titleDiv.innerHTML += "Editing: "; |
---|
| 201 | titleDiv.appendChild(nameSpan); |
---|
| 202 | this.element.appendChild(titleDiv); |
---|
| 203 | |
---|
| 204 | //Content area |
---|
| 205 | var contentDiv = ce("div"); |
---|
| 206 | contentDiv.className = "content"; |
---|
| 207 | var bodyText = createNewElement("textarea", null, "qBodyTextField", "qBodyTextField", null); |
---|
| 208 | bodyText.value = "Question body text goes here"; |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | // The dynamic questionParams div, where all the control elements and inputs will be located |
---|
| 212 | var questionParams = ce("div"); |
---|
| 213 | this.paramsElement = questionParams; |
---|
| 214 | 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(); |
---|
| 228 | }, false); |
---|
| 229 | // Add the select options. Do this in a block scope to prevent the o1 var from messing things up. |
---|
| 230 | // Also helps in structuring code. |
---|
| 231 | { |
---|
| 232 | var o1 = ce("option"); |
---|
| 233 | o1.value = null; |
---|
| 234 | o1.text = ""; |
---|
| 235 | qTypeField.appendChild(o1); |
---|
| 236 | |
---|
| 237 | o1 = ce("option"); |
---|
| 238 | o1.value = "int"; |
---|
| 239 | o1.text = "Integer"; |
---|
| 240 | qTypeField.appendChild(o1); |
---|
| 241 | |
---|
| 242 | o1 = ce("option"); |
---|
| 243 | o1.value = "scale"; |
---|
| 244 | o1.text = "Scale"; |
---|
| 245 | qTypeField.appendChild(o1); |
---|
| 246 | |
---|
| 247 | o1 = ce("option"); |
---|
| 248 | o1.value = "choice"; |
---|
| 249 | o1.text = "Multiple choice"; |
---|
| 250 | qTypeField.appendChild(o1); |
---|
| 251 | |
---|
| 252 | o1 = ce("option"); |
---|
| 253 | o1.value = "text"; |
---|
| 254 | o1.text = "Text"; |
---|
| 255 | qTypeField.appendChild(o1); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | contentDiv.appendChild(questionParams); |
---|
| 259 | this.element.appendChild(contentDiv); |
---|
| 260 | |
---|
| 261 | // Controls bar |
---|
| 262 | var controlsDiv = ce("div"); |
---|
| 263 | controlsDiv.className = "controls"; |
---|
| 264 | var btnDiscard = createNewElement("input", "button", "btnDiscard", null, "Discard"); |
---|
| 265 | var btnSave = createNewElement("input", "button", "btnSave", null, "Save"); |
---|
| 266 | controlsDiv.appendChild(btnDiscard); |
---|
| 267 | controlsDiv.appendChild(btnSave); |
---|
| 268 | btnSave.addEventListener("click", function(){ |
---|
| 269 | swapQuestionState(true); |
---|
| 270 | }, false); |
---|
| 271 | btnDiscard.addEventListener("click", function(){ |
---|
| 272 | swapQuestionState(false); |
---|
| 273 | }, false); |
---|
| 274 | this.element.appendChild(controlsDiv); |
---|
| 275 | 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 |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | newAjaxRequest(requestString, "createObject.php", function(result){ |
---|
| 293 | // Display a success message, or throw an error. |
---|
| 294 | }, true); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // Updating input fields |
---|
| 298 | this.selectAnswertype = function () { |
---|
| 299 | // Switch statement, call this.type_x where x is the relevant answer type. |
---|
| 300 | // For all this.type_X funtions: |
---|
| 301 | // Use the createNewElement and createNewInputLabel methods to add controls or input fields. |
---|
| 302 | // Input field convention: class = questionParamField, id = qTypeField / qScaleSize, etc... |
---|
| 303 | // Input label class: "l" or "r" depending on alignment with respect to parent ("for") input element. |
---|
| 304 | // Important: use the this.paramsElement to access the questionParams div! |
---|
| 305 | // 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! |
---|
| 306 | } |
---|
| 307 | this.type_Scale = function () { |
---|
| 308 | |
---|
| 309 | } |
---|
| 310 | this.type_Integer = function () { |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | this.type_Text = function () { |
---|
| 314 | |
---|
| 315 | } |
---|
| 316 | 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 | } |
---|