Mercurial > hg > webaudioevaluationtool
comparison js/core.js @ 2967:f82cda734a1e
Major reworking of SessionKey object
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Fri, 12 Jan 2018 13:11:49 +0000 |
parents | 31a7a34bad6b |
children | d9121773632d |
comparison
equal
deleted
inserted
replaced
2966:8a30d54bd4e2 | 2967:f82cda734a1e |
---|---|
3647 if (specification.postTest !== undefined) { | 3647 if (specification.postTest !== undefined) { |
3648 this.globalPostTest = new this.surveyNode(this, this.root, specification.postTest); | 3648 this.globalPostTest = new this.surveyNode(this, this.root, specification.postTest); |
3649 } | 3649 } |
3650 }; | 3650 }; |
3651 | 3651 |
3652 this.SessionKey = (function (parent) { | |
3653 function postUpdate() { | |
3654 // Return a new promise. | |
3655 var hold = document.createElement("div"); | |
3656 var clone = parent.root.cloneNode(true); | |
3657 hold.appendChild(clone); | |
3658 return new Promise(function (resolve, reject) { | |
3659 // Do the usual XHR stuff | |
3660 var req = new XMLHttpRequest(); | |
3661 req.open("POST", returnURL + "php/save.php?key=" + key + "&saveFilenamePrefix=" + parent.filenamePrefix); | |
3662 req.setRequestHeader('Content-Type', 'text/xml'); | |
3663 | |
3664 req.onload = function () { | |
3665 // This is called even on 404 etc | |
3666 // so check the status | |
3667 if (this.status >= 300) { | |
3668 console.log("WARNING - Could not update at this time"); | |
3669 } else { | |
3670 var parser = new DOMParser(); | |
3671 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); | |
3672 var response = xmlDoc.getElementsByTagName('response')[0]; | |
3673 if (response.getAttribute("state") == "OK") { | |
3674 var file = response.getElementsByTagName("file")[0]; | |
3675 console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B"); | |
3676 resolve(true); | |
3677 } else { | |
3678 var message = response.getElementsByTagName("message"); | |
3679 console.log("Intermediate save: Error! " + message.textContent); | |
3680 reject("Intermediate save: Error! " + message.textContent); | |
3681 } | |
3682 } | |
3683 }; | |
3684 | |
3685 // Handle network errors | |
3686 req.onerror = function () { | |
3687 reject(Error("Network Error")); | |
3688 }; | |
3689 | |
3690 // Make the request | |
3691 req.send([hold.innerHTML]); | |
3692 }); | |
3693 }; | |
3694 | |
3695 function keyPromise() { | |
3696 return new Promise(function (resolve, reject) { | |
3697 var req = new XMLHttpRequest(); | |
3698 req.open("GET", returnURL + "php/requestKey.php?saveFilenamePrefix=" + parent.filenamePrefix, true); | |
3699 req.onload = function () { | |
3700 // This is called even on 404 etc | |
3701 // so check the status | |
3702 if (req.status == 200) { | |
3703 // Resolve the promise with the response text | |
3704 resolve(req.response); | |
3705 } else { | |
3706 // Otherwise reject with the status text | |
3707 // which will hopefully be a meaningful error | |
3708 reject(Error(req.statusText)); | |
3709 } | |
3710 }; | |
3711 | |
3712 // Handle network errors | |
3713 req.onerror = function () { | |
3714 reject(Error("Network Error")); | |
3715 }; | |
3716 | |
3717 req.send(); | |
3718 }) | |
3719 } | |
3720 | |
3721 var requestChains = null; | |
3722 var object = {}; | |
3723 | |
3724 Object.defineProperties(object, { | |
3725 "key": { | |
3726 "get": function () { | |
3727 return key; | |
3728 }, | |
3729 "set": function (a) { | |
3730 throw ("Cannot set read-only property") | |
3731 } | |
3732 }, | |
3733 "request": { | |
3734 "value": new XMLHttpRequest() | |
3735 }, | |
3736 "parent": { | |
3737 "value": parent | |
3738 }, | |
3739 "requestKey": { | |
3740 "value": function () { | |
3741 requestChains = keyPromise().then(function (response) { | |
3742 function throwerror() { | |
3743 key = null; | |
3744 throw ("An unspecified error occured, no server key could be generated"); | |
3745 } | |
3746 var parse = new DOMParser(); | |
3747 var xml = parse.parseFromString(response, "text/xml"); | |
3748 if (this.request.response.length === 0) { | |
3749 throwerror(); | |
3750 } | |
3751 if (xml.getElementsByTagName("state").length > 0) { | |
3752 if (xml.getElementsByTagName("state")[0].textContent == "OK") { | |
3753 key = xml.getAllElementsByTagName("key")[0].textContent; | |
3754 this.parent.root.setAttribute("key", this.key); | |
3755 this.parent.root.setAttribute("state", "empty"); | |
3756 this.update(); | |
3757 return (true); | |
3758 } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { | |
3759 key = null; | |
3760 console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); | |
3761 return (true); | |
3762 } | |
3763 } else { | |
3764 throwerror(); | |
3765 } | |
3766 return (true); | |
3767 }); | |
3768 } | |
3769 }, | |
3770 "update": function () { | |
3771 if (this.key == null || requestChains === undefined) { | |
3772 throw ("Cannot save as key == null"); | |
3773 } | |
3774 this.parent.root.setAttribute("state", "update"); | |
3775 requestChains = requestChains.then(postUpdate()); | |
3776 }, | |
3777 "finish": function () { | |
3778 if (this.key == null || requestChains === undefined) { | |
3779 throw ("Cannot save as key == null"); | |
3780 } | |
3781 this.parent.finish(); | |
3782 requestChains.then(postUpdate()).then(function () { | |
3783 console.log("OK"); | |
3784 }, function () { | |
3785 createProjectSave("local"); | |
3786 }) | |
3787 } | |
3788 }) | |
3789 return object; | |
3790 })(this); | |
3791 /* | |
3652 this.SessionKey = { | 3792 this.SessionKey = { |
3653 key: null, | 3793 key: null, |
3654 request: new XMLHttpRequest(), | 3794 request: new XMLHttpRequest(), |
3655 parent: this, | 3795 parent: this, |
3656 handleEvent: function () { | 3796 handleEvent: function () { |
3657 var parse = new DOMParser(); | 3797 |
3658 var xml = parse.parseFromString(this.request.response, "text/xml"); | |
3659 if (this.request.response.length === 0) { | |
3660 console.error("An unspecified error occured, no server key could be generated"); | |
3661 return; | |
3662 } | |
3663 if (xml.getElementsByTagName("state").length > 0) { | |
3664 if (xml.getElementsByTagName("state")[0].textContent == "OK") { | |
3665 this.key = xml.getAllElementsByTagName("key")[0].textContent; | |
3666 this.parent.root.setAttribute("key", this.key); | |
3667 this.parent.root.setAttribute("state", "empty"); | |
3668 this.update(); | |
3669 return; | |
3670 } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { | |
3671 this.key = null; | |
3672 console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); | |
3673 return; | |
3674 } | |
3675 } | |
3676 this.key = null; | |
3677 console.error("An unspecified error occured, no server key could be generated"); | |
3678 }, | 3798 }, |
3679 requestKey: function () { | 3799 requestKey: function () { |
3680 // For new servers, request a new key from the server | 3800 |
3681 var returnURL = ""; | |
3682 if (typeof specification.projectReturn == "string") { | |
3683 if (specification.projectReturn.substr(0, 4) == "http") { | |
3684 returnURL = specification.projectReturn; | |
3685 } | |
3686 } | |
3687 this.request.open("GET", returnURL + "php/requestKey.php?saveFilenamePrefix=" + this.parent.filenamePrefix, true); | |
3688 this.request.addEventListener("load", this); | |
3689 this.request.send(); | |
3690 }, | 3801 }, |
3691 update: function () { | 3802 update: function () { |
3692 if (this.key === null) { | 3803 if (this.key === null) { |
3693 console.log("Cannot save as key == null"); | 3804 console.log("Cannot save as key == null"); |
3694 return; | 3805 return; |
3767 }; | 3878 }; |
3768 xmlhttp.send([hold.innerHTML]); | 3879 xmlhttp.send([hold.innerHTML]); |
3769 }); | 3880 }); |
3770 } | 3881 } |
3771 }; | 3882 }; |
3772 | 3883 */ |
3773 this.createTestPageStore = function (specification) { | 3884 this.createTestPageStore = function (specification) { |
3774 var store = new this.pageNode(this, specification); | 3885 var store = new this.pageNode(this, specification); |
3775 this.testPages.push(store); | 3886 this.testPages.push(store); |
3776 return this.testPages[this.testPages.length - 1]; | 3887 return this.testPages[this.testPages.length - 1]; |
3777 }; | 3888 }; |