Mercurial > hg > webaudioevaluationtool
comparison js/core.js @ 3090:385bb2e03ab7
Merge branch 'vnext' into Dev_main
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Fri, 12 Jan 2018 15:40:34 +0000 |
parents | 39ffb48949de a169e79ba224 |
children | 30ba1b9ed867 |
comparison
equal
deleted
inserted
replaced
3088:12b90c6ff910 | 3090:385bb2e03ab7 |
---|---|
3696 if (specification.postTest !== undefined) { | 3696 if (specification.postTest !== undefined) { |
3697 this.globalPostTest = new this.surveyNode(this, this.root, specification.postTest); | 3697 this.globalPostTest = new this.surveyNode(this, this.root, specification.postTest); |
3698 } | 3698 } |
3699 }; | 3699 }; |
3700 | 3700 |
3701 this.SessionKey = (function (parent) { | |
3702 var returnURL = ""; | |
3703 if (window.returnURL !== undefined) { | |
3704 returnURL = String(window.returnURL); | |
3705 } | |
3706 | |
3707 function postUpdate() { | |
3708 // Return a new promise. | |
3709 var hold = document.createElement("div"); | |
3710 var clone = parent.root.cloneNode(true); | |
3711 hold.appendChild(clone); | |
3712 return new Promise(function (resolve, reject) { | |
3713 // Do the usual XHR stuff | |
3714 console.log("Requested save..."); | |
3715 var req = new XMLHttpRequest(); | |
3716 req.open("POST", returnURL + "php/save.php?key=" + sessionKey + "&saveFilenamePrefix=" + parent.filenamePrefix); | |
3717 req.setRequestHeader('Content-Type', 'text/xml'); | |
3718 | |
3719 req.onload = function () { | |
3720 // This is called even on 404 etc | |
3721 // so check the status | |
3722 if (this.status >= 300) { | |
3723 console.log("WARNING - Could not update at this time"); | |
3724 } else { | |
3725 var parser = new DOMParser(); | |
3726 var xmlDoc = parser.parseFromString(req.responseText, "application/xml"); | |
3727 var response = xmlDoc.getElementsByTagName('response')[0]; | |
3728 if (response.getAttribute("state") == "OK") { | |
3729 var file = response.getElementsByTagName("file")[0]; | |
3730 console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B"); | |
3731 resolve(true); | |
3732 } else { | |
3733 var message = response.getElementsByTagName("message"); | |
3734 console.log("Intermediate save: Error! " + message.textContent); | |
3735 reject("Intermediate save: Error! " + message.textContent); | |
3736 } | |
3737 } | |
3738 }; | |
3739 | |
3740 // Handle network errors | |
3741 req.onerror = function () { | |
3742 reject(Error("Network Error")); | |
3743 }; | |
3744 | |
3745 // Make the request | |
3746 req.send([hold.innerHTML]); | |
3747 }); | |
3748 }; | |
3749 | |
3750 function keyPromise() { | |
3751 return new Promise(function (resolve, reject) { | |
3752 var req = new XMLHttpRequest(); | |
3753 req.open("GET", returnURL + "php/requestKey.php?saveFilenamePrefix=" + parent.filenamePrefix, true); | |
3754 req.onload = function () { | |
3755 // This is called even on 404 etc | |
3756 // so check the status | |
3757 if (req.status == 200) { | |
3758 // Resolve the promise with the response text | |
3759 resolve(req.response); | |
3760 } else { | |
3761 // Otherwise reject with the status text | |
3762 // which will hopefully be a meaningful error | |
3763 reject(Error(req.statusText)); | |
3764 } | |
3765 }; | |
3766 | |
3767 // Handle network errors | |
3768 req.onerror = function () { | |
3769 reject(Error("Network Error")); | |
3770 }; | |
3771 | |
3772 req.send(); | |
3773 }) | |
3774 } | |
3775 | |
3776 var requestChains = null; | |
3777 var sessionKey = null; | |
3778 var object = {}; | |
3779 | |
3780 Object.defineProperties(object, { | |
3781 "key": { | |
3782 "get": function () { | |
3783 return sessionKey; | |
3784 }, | |
3785 "set": function (a) { | |
3786 throw ("Cannot set read-only property") | |
3787 } | |
3788 }, | |
3789 "request": { | |
3790 "value": new XMLHttpRequest() | |
3791 }, | |
3792 "parent": { | |
3793 "value": parent | |
3794 }, | |
3795 "requestKey": { | |
3796 "value": function () { | |
3797 requestChains = keyPromise().then(function (response) { | |
3798 function throwerror() { | |
3799 sessionKey = null; | |
3800 throw ("An unspecified error occured, no server key could be generated"); | |
3801 } | |
3802 var parse = new DOMParser(); | |
3803 var xml = parse.parseFromString(response, "text/xml"); | |
3804 if (response.length === 0) { | |
3805 throwerror(); | |
3806 } | |
3807 if (xml.getElementsByTagName("state").length > 0) { | |
3808 if (xml.getElementsByTagName("state")[0].textContent == "OK") { | |
3809 sessionKey = xml.getAllElementsByTagName("key")[0].textContent; | |
3810 parent.root.setAttribute("key", sessionKey); | |
3811 parent.root.setAttribute("state", "empty"); | |
3812 return (true); | |
3813 } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { | |
3814 sessionKey = null; | |
3815 console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); | |
3816 return (false); | |
3817 } | |
3818 } else { | |
3819 throwerror(); | |
3820 } | |
3821 return (true); | |
3822 }); | |
3823 } | |
3824 }, | |
3825 "update": { | |
3826 "value": function () { | |
3827 if (this.key == null || requestChains === undefined) { | |
3828 throw ("Cannot save as key == null"); | |
3829 } | |
3830 this.parent.root.setAttribute("state", "update"); | |
3831 requestChains = requestChains.then(postUpdate); | |
3832 } | |
3833 }, | |
3834 "finish": { | |
3835 "value": function () { | |
3836 if (this.key == null || requestChains === undefined) { | |
3837 throw ("Cannot save as key == null"); | |
3838 } | |
3839 this.parent.finish(); | |
3840 return requestChains.then(postUpdate()).then(function () { | |
3841 console.log("OK"); | |
3842 }, function () { | |
3843 createProjectSave("local"); | |
3844 }) | |
3845 } | |
3846 } | |
3847 }); | |
3848 return object; | |
3849 })(this); | |
3850 /* | |
3701 this.SessionKey = { | 3851 this.SessionKey = { |
3702 key: null, | 3852 key: null, |
3703 request: new XMLHttpRequest(), | 3853 request: new XMLHttpRequest(), |
3704 parent: this, | 3854 parent: this, |
3705 handleEvent: function () { | 3855 handleEvent: function () { |
3706 var parse = new DOMParser(); | 3856 |
3707 var xml = parse.parseFromString(this.request.response, "text/xml"); | |
3708 if (this.request.response.length === 0) { | |
3709 console.error("An unspecified error occured, no server key could be generated"); | |
3710 return; | |
3711 } | |
3712 if (xml.getElementsByTagName("state").length > 0) { | |
3713 if (xml.getElementsByTagName("state")[0].textContent == "OK") { | |
3714 this.key = xml.getAllElementsByTagName("key")[0].textContent; | |
3715 this.parent.root.setAttribute("key", this.key); | |
3716 this.parent.root.setAttribute("state", "empty"); | |
3717 this.update(); | |
3718 return; | |
3719 } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { | |
3720 this.key = null; | |
3721 console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); | |
3722 return; | |
3723 } | |
3724 } | |
3725 this.key = null; | |
3726 console.error("An unspecified error occured, no server key could be generated"); | |
3727 }, | 3857 }, |
3728 requestKey: function () { | 3858 requestKey: function () { |
3729 // For new servers, request a new key from the server | 3859 |
3730 var returnURL = ""; | |
3731 if (typeof specification.projectReturn == "string") { | |
3732 if (specification.projectReturn.substr(0, 4) == "http") { | |
3733 returnURL = specification.projectReturn; | |
3734 } | |
3735 } | |
3736 this.request.open("GET", returnURL + "php/requestKey.php?saveFilenamePrefix=" + this.parent.filenamePrefix, true); | |
3737 this.request.addEventListener("load", this); | |
3738 this.request.send(); | |
3739 }, | 3860 }, |
3740 update: function () { | 3861 update: function () { |
3741 if (this.key === null) { | 3862 if (this.key === null) { |
3742 console.log("Cannot save as key == null"); | 3863 console.log("Cannot save as key == null"); |
3743 return; | 3864 return; |
3816 }; | 3937 }; |
3817 xmlhttp.send([hold.innerHTML]); | 3938 xmlhttp.send([hold.innerHTML]); |
3818 }); | 3939 }); |
3819 } | 3940 } |
3820 }; | 3941 }; |
3821 | 3942 */ |
3822 this.createTestPageStore = function (specification) { | 3943 this.createTestPageStore = function (specification) { |
3823 var store = new this.pageNode(this, specification); | 3944 var store = new this.pageNode(this, specification); |
3824 this.testPages.push(store); | 3945 this.testPages.push(store); |
3825 return this.testPages[this.testPages.length - 1]; | 3946 return this.testPages[this.testPages.length - 1]; |
3826 }; | 3947 }; |