comparison js/core.js @ 2723:2e1cafe93c78

Moved all of the save comms into the Storage() node. #186 #190
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Sat, 15 Apr 2017 11:23:25 +0100
parents a087cb7b5972
children 9c01d5dd22a2
comparison
equal deleted inserted replaced
2722:a087cb7b5972 2723:2e1cafe93c78
406 406
407 popup.showPopup(); 407 popup.showPopup();
408 popup.popupContent.innerHTML = "<span>Please save the file below to give to your test supervisor</span><br>"; 408 popup.popupContent.innerHTML = "<span>Please save the file below to give to your test supervisor</span><br>";
409 popup.popupContent.appendChild(a); 409 popup.popupContent.appendChild(a);
410 } else { 410 } else {
411 var saveUrlSuffix = "";
412 var saveFilenamePrefix = specification.saveFilenamePrefix;
413 if (typeof (saveFilenamePrefix) === "string" && saveFilenamePrefix.length > 0) {
414 saveUrlSuffix = "&saveFilenamePrefix=" + saveFilenamePrefix;
415 }
416 var projectReturn = ""; 411 var projectReturn = "";
417 if (typeof specification.projectReturn == "string") { 412 if (typeof specification.projectReturn == "string") {
418 if (specification.projectReturn.substr(0, 4) == "http") { 413 if (specification.projectReturn.substr(0, 4) == "http") {
419 projectReturn = specification.projectReturn; 414 projectReturn = specification.projectReturn;
420 } 415 }
421 } 416 }
422 var saveURL = projectReturn + "php/save.php?key=" + storage.SessionKey.key + saveUrlSuffix; 417 storage.SessionKey.finish().then(function (resolved) {
423 var xmlhttp = new XMLHttpRequest(); 418 if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
424 xmlhttp.open("POST", saveURL, true); 419 window.location = specification.returnURL;
425 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 420 } else {
426 xmlhttp.onerror = function () { 421 popup.popupContent.textContent = specification.exitText;
427 console.log('Error saving file to server! Presenting download locally'); 422 }
423 }, function (message) {
424 console.log("Save: Error! " + message.textContent);
428 createProjectSave("local"); 425 createProjectSave("local");
429 }; 426 });
430 xmlhttp.onload = function () {
431 console.log(xmlhttp);
432 if (this.status >= 300) {
433 console.log("WARNING - Could not update at this time");
434 createProjectSave("local");
435 } else {
436 var parser = new DOMParser();
437 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
438 var response = xmlDoc.getElementsByTagName('response')[0];
439 if (response.getAttribute("state") == "OK") {
440 window.onbeforeunload = undefined;
441 var file = response.getElementsByTagName("file")[0];
442 console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B");
443 if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
444 window.location = specification.returnURL;
445 } else {
446 popup.popupContent.textContent = specification.exitText;
447 }
448 } else {
449 var message = response.getElementsByTagName("message");
450 console.log("Save: Error! " + message.textContent);
451 createProjectSave("local");
452 }
453 }
454 };
455 xmlhttp.send(file);
456 popup.showPopup(); 427 popup.showPopup();
457 popup.popupContent.innerHTML = null; 428 popup.popupContent.innerHTML = null;
458 popup.popupContent.textContent = "Submitting. Please Wait"; 429 popup.popupContent.textContent = "Submitting. Please Wait";
459 if (typeof (popup.hideNextButton) === "function") { 430 if (typeof (popup.hideNextButton) === "function") {
460 popup.hideNextButton(); 431 popup.hideNextButton();
3504 console.log("Intermediate save: Error! " + message.textContent); 3475 console.log("Intermediate save: Error! " + message.textContent);
3505 } 3476 }
3506 } 3477 }
3507 }; 3478 };
3508 xmlhttp.send([hold.innerHTML]); 3479 xmlhttp.send([hold.innerHTML]);
3480 },
3481 finish: function () {
3482 // Final upload to complete the test
3483 this.parent.finish();
3484 var hold = document.createElement("div");
3485 var clone = this.parent.root.cloneNode(true);
3486 hold.appendChild(clone);
3487 return new Promise(function (resolve, reject) {
3488 var saveURL = specification.returnURL + "php/save.php?key=" + this.key + "&saveFilenamePrefix=" + this.parent.filenamePrefix;
3489 var xmlhttp = new XMLHttpRequest();
3490 xmlhttp.open("POST", saveURL);
3491 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
3492 xmlhttp.onerror = function () {
3493 console.log('Error updating file to server!');
3494 createProjectSave("local");
3495 };
3496 xmlhttp.onload = function () {
3497 if (this.status >= 300) {
3498 console.log("WARNING - Could not update at this time");
3499 createProjectSave("local");
3500 } else {
3501 var parser = new DOMParser();
3502 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
3503 var response = xmlDoc.getElementsByTagName('response')[0];
3504 if (response.getAttribute("state") == "OK") {
3505 var file = response.getElementsByTagName("file")[0];
3506 console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B");
3507 resolve(response);
3508 } else {
3509 var message = response.getElementsByTagName("message");
3510 reject(message);
3511 }
3512 }
3513 };
3514 xmlhttp.send([hold.innerHTML]);
3515 });
3509 } 3516 }
3510 }; 3517 };
3511 3518
3512 this.createTestPageStore = function (specification) { 3519 this.createTestPageStore = function (specification) {
3513 var store = new this.pageNode(this, specification); 3520 var store = new this.pageNode(this, specification);
3642 }; 3649 };
3643 this.update = function () { 3650 this.update = function () {
3644 this.SessionKey.update(); 3651 this.SessionKey.update();
3645 }; 3652 };
3646 this.finish = function () { 3653 this.finish = function () {
3647 if (this.state === 0) {
3648 this.update();
3649 }
3650 this.state = 1; 3654 this.state = 1;
3651 this.root.setAttribute("state", "complete"); 3655 this.root.setAttribute("state", "complete");
3652 return this.root; 3656 return this.root;
3653 }; 3657 };
3654 3658