From 2c2414dd8ef7fda2389216b61b06f29ef7d76c25 Mon Sep 17 00:00:00 2001 From: Graham Trott Date: Tue, 14 Apr 2020 10:42:55 +0100 Subject: [PATCH] Define REST path in index.html --- easycoder/README.md | 6 +- easycoder/easycoder-min.js | 119 +++++++++++++++++++------------------ easycoder/easycoder.js | 23 ++++++- js/easycoder/Browser.js | 6 +- js/easycoder/Main.js | 2 + js/easycoder/Rest.js | 15 ++++- 6 files changed, 103 insertions(+), 68 deletions(-) diff --git a/easycoder/README.md b/easycoder/README.md index ba67922..86d6312 100644 --- a/easycoder/README.md +++ b/easycoder/README.md @@ -1,7 +1,7 @@ # EasyCoder distribution file set - Here are the files that can be used to create standalone EasyCoder applications. Not all will be needed for any given application but it's probably simplest to copy them all. The file `plugins,js` defines which plugins will be used by default (causing them to be loaded) so it should be edited if a different set is required. + Here are the files that can be used to create standalone EasyCoder applications. Not all will be needed for any given application but it's probably simplest to copy them all. The main `easycoder.js` contains everything that will be needed for a wide range of applications. Extra functionality is supplied by the files in the `plugins` folder. These will be requested where they are needed, using the `require` keyword. - Most of these files, particularly those in the `plugin` folder, are simply copies of the source set at `js`. The two files `easycoder.js` and `easycoder-min.js` are built from those sources; the latter is a minimized version of the former. + The files in the `plugin` folder are simply copies of the source set at `js`. The two files `easycoder.js` and `easycoder-min.js` are built from their sources; the latter is a minimized version of the former. - To use EasyCoder, put the entire `easycoder` folder onto your webserver (keeping the same name) and in your HTML page header call for `easycoder.js` or `easycoder-min.js` (the former is more useful for debugging). This will call in other files as it needs them. + To use EasyCoder, put the entire `easycoder` folder onto your webserver (giving it any suitable name) and in your HTML page header call for `easycoder.js` or `easycoder-min.js` (the former is more useful for debugging). This will call in other files as it needs them. diff --git a/easycoder/easycoder-min.js b/easycoder/easycoder-min.js index c3699a1..b0f85f7 100644 --- a/easycoder/easycoder-min.js +++ b/easycoder/easycoder-min.js @@ -120,7 +120,7 @@ switch(b.direction){case "left":a.onSwipeLeft=b.pc+2;break;case "right":a.onSwip d){document.pickIndex=d;c.pickIndex=d;c.mouseDownPc=b.pc+2;"ontouchstart"in c?(c.addEventListener("touchstart",function(b){var c=b.targetTouches[0].target;document.pickX=b.touches[0].clientX;document.pickY=b.touches[0].clientY;c.blur();setTimeout(function(){document.pickRecord.index=c.pickIndex;a.run(c.mouseDownPc)},1)},!1),c.addEventListener("touchmove",function(b){document.dragX=b.touches[0].clientX;document.dragY=b.touches[0].clientY;setTimeout(function(){a.run(document.mouseMovePc)},1);return!1}, !1),c.addEventListener("touchend",function(){setTimeout(function(){a.run(document.mouseUpPc)},1);return!1})):c.onmousedown=function(b){b=b?b:window.event;b.stopPropagation();if(0=document.documentElement.clientHeight};case "br":return{type:"constant",numeric:!1,content:decodeURIComponent("%3Cbr%20%2F%3E")};case "attributeOf":return c=a.getSymbolRecord(b.symbol),b=a.getValue(b.attribute),d=c.element[c.index],0===b.indexOf("data-")?a.getSimpleValue(d.dataset[b.substr(5)]):a.getSimpleValue(d[b]);case "style":return c=a.getSymbolRecord(b.target),b=a.getValue(b.style),d=c.element[c.index], -a.getSimpleValue(d.style[b]);case "confirm":return{type:"boolean",content:window.confirm(a.getValue(b.text))};case "prompt":return d=a.getValue(b.text),b=a.getValue(b.pre),{type:"constant",numeric:!1,content:b?window.prompt(d,b):window.prompt(d)};case "contentOf":c=a.getSymbolRecord(b.symbol);d=c.element[c.index];switch(c.keyword){case "input":case "textarea":b=d.value;break;case "pre":b=d.innerHTML;break;default:b=d.innerHTML.split("\n").join("")}return{type:"constant",numeric:!1,content:b};case "selected":return c= -a.getSymbolRecord(b.symbol),d=c.element[c.index],a=d.selectedIndex,d=0<=a?d.options[a].text:"",b="index"===b.arg?a:d,{type:"constant",numeric:!1,content:b};case "top":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetTop:a.getBoundingClientRect().top);return{type:"constant",numeric:!0,content:b};case "bottom":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY+ -window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().bottom);return{type:"constant",numeric:!0,content:b};case "left":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenLeft};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetLeft:a.getBoundingClientRect().left);return{type:"constant",numeric:!0,content:b};case "right":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenX+ -window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().right);return{type:"constant",numeric:!0,content:b};case "width":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().width);return{type:"constant",numeric:!0,content:b};case "height":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerHeight};c=a.getSymbolRecord(b.symbol); -b=Math.round(c.element[c.index].getBoundingClientRect().height);return{type:"constant",numeric:!0,content:b};case "color":return{type:"constant",numeric:!1,content:"#"+a.value.evaluate(a,b.value).content.toString(16).padStart(6,"0")};case "docPath":return{type:"constant",numeric:!1,content:a.docPath};case "storageKeys":return{type:"constant",numeric:!1,content:JSON.stringify(Object.keys(localStorage))};case "location":return{type:"constant",numeric:!1,content:window.location.href};case "historyState":return{type:"constant", -numeric:!1,content:window.history.state};case "scrollPosition":return{type:"constant",numeric:!0,content:scrollPosition};case "varName":return{type:"constant",numeric:!1,content:a.varName};case "varIndex":return{type:"constant",numeric:!0,content:a.varIndex};case "key":return{type:"constant",numeric:!1,content:a.key};case "hostname":return{type:"constant",numeric:!1,content:location.hostname};case "screen":return{type:"constant",numeric:!0,content:screen[b.attribute]};case "pickPosition":return{type:"constant", -numeric:!1,content:JSON.stringify({x:document.pickX,y:document.pickY})};case "dragPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.dragX,y:document.dragY})}}}},condition:{compile:function(a){if(a.tokenIs("confirm"))return{domain:"browser",type:"confirm",value:a.getNextValue()};if(a.tokenIs("element")&&a.nextIsSymbol()){var b=a.getSymbolRecord();if("dom"===b.extra){var c=a.nextToken();if("has"===c){if(a.nextTokenIs("the")&&a.next(),a.tokenIs("focus"))return a.next(),{domain:"browser", -type:"focus",element:b.name}}else if("contains"===c)return a=a.getNextValue(),{domain:"browser",type:"contains",element:b.name,position:a}}}return null},test:function(a,b){switch(b.type){case "confirm":return confirm(a.getValue(b.value));case "focus":var c=a.getSymbolRecord(b.element);return c.element[c.index]===document.activeElement;case "contains":c=a.getSymbolRecord(b.element);var d=c.element[c.index].getBoundingClientRect();c=Math.round(d.left);var e=Math.round(d.right),f=Math.round(d.top);d= -Math.round(d.bottom);b=JSON.parse(a.getValue(b.position));a=b.x;b=b.y;return a>=c&&a<=e&&b>=f&&b<=d?!0:!1}}},setStyles:function(a,b){a=document.getElementById(a);b=b.split(";");b=$jscomp.makeIterator(b);for(var c=b.next();!c.done;c=b.next())c=c.value.split(":"),a.setAttribute(c[0],c[1])}},scrollPosition=0;window.addEventListener("scroll",function(){scrollPosition=this.scrollY}); -window.onpopstate=function(a){window.EasyCoder.timestamp=Date.now();(a=JSON.parse(a.state))&&a.script&&((a=window.EasyCoder.scripts[a.script])?a.onBrowserBack&&a.run(a.onBrowserBack):console.log("No script property in window state object"))}; +a)}}}},a.ajaxCommand=b,(f=document.getElementById("easycoder-rest"))||a.runtimeError(b.lino,"No REST server defined"),b=d.startsWith("http")?d:""+window.location.origin+f+"/"+d,e.open("POST",b),e.send(c);return 0}},getHandler:function(a){switch(a){case "a":return EasyCoder_Browser.A;case "alert":return EasyCoder_Browser.Alert;case "attach":return EasyCoder_Browser.Attach;case "audioclip":return EasyCoder_Browser.Audioclip;case "blockquote":return EasyCoder_Browser.BLOCKQUOTE;case "button":return EasyCoder_Browser.BUTTON; +case "canvas":return EasyCoder_Browser.CANVAS;case "clear":return EasyCoder_Browser.Clear;case "convert":return EasyCoder_Browser.Convert;case "create":return EasyCoder_Browser.Create;case "disable":return EasyCoder_Browser.Disable;case "div":return EasyCoder_Browser.DIV;case "enable":return EasyCoder_Browser.Enable;case "fieldset":return EasyCoder_Browser.FIELDSET;case "file":return EasyCoder_Browser.FILE;case "focus":return EasyCoder_Browser.Focus;case "form":return EasyCoder_Browser.FORM;case "fullscreen":return EasyCoder_Browser.FullScreen; +case "get":return EasyCoder_Browser.Get;case "h1":return EasyCoder_Browser.H1;case "h2":return EasyCoder_Browser.H2;case "h3":return EasyCoder_Browser.H3;case "h4":return EasyCoder_Browser.H4;case "h5":return EasyCoder_Browser.H5;case "h6":return EasyCoder_Browser.H6;case "highlight":return EasyCoder_Browser.Highlight;case "history":return EasyCoder_Browser.History;case "hr":return EasyCoder_Browser.HR;case "image":return EasyCoder_Browser.IMAGE;case "img":return EasyCoder_Browser.IMG;case "input":return EasyCoder_Browser.INPUT; +case "label":return EasyCoder_Browser.LABEL;case "legend":return EasyCoder_Browser.LEGEND;case "li":return EasyCoder_Browser.LI;case "location":return EasyCoder_Browser.Location;case "mail":return EasyCoder_Browser.Mail;case "on":return EasyCoder_Browser.On;case "option":return EasyCoder_Browser.OPTION;case "p":return EasyCoder_Browser.P;case "play":return EasyCoder_Browser.Play;case "pre":return EasyCoder_Browser.PRE;case "progress":return EasyCoder_Browser.PROGRESS;case "put":return EasyCoder_Browser.Put; +case "remove":return EasyCoder_Browser.Remove;case "request":return EasyCoder_Browser.Request;case "select":return EasyCoder_Browser.SELECT;case "scroll":return EasyCoder_Browser.Scroll;case "section":return EasyCoder_Browser.SECTION;case "set":return EasyCoder_Browser.Set;case "span":return EasyCoder_Browser.SPAN;case "table":return EasyCoder_Browser.TABLE;case "tr":return EasyCoder_Browser.TR;case "td":return EasyCoder_Browser.TD;case "textarea":return EasyCoder_Browser.TEXTAREA;case "trace":return EasyCoder_Browser.Trace; +case "ul":return EasyCoder_Browser.UL;case "upload":return EasyCoder_Browser.Upload;default:return null}},run:function(a){var b=a[a.pc],c=EasyCoder_Browser.getHandler(b.keyword);c||a.runtimeError(b.lino,"Unknown keyword '"+b.keyword+"' in 'browser' package");return c.run(a)},value:{compile:function(a){if(a.isSymbol()){var b=a.getSymbolRecord();if(a.nextTokenIs("exists"))return"dom"===b.extra?(a.next(),{domain:"browser",type:"exists",value:b.name}):null;switch(b.keyword){case "file":case "input":case "select":case "textarea":return{domain:"browser", +type:b.keyword,value:b.name}}return null}a.tokenIs("the")&&a.next();var c=!1;a.tokenIs("offset")&&(c=!0,a.next());b=a.getToken();switch(b){case "mobile":case "portrait":case "landscape":case "br":case "location":case "key":case "hostname":return a.next(),{domain:"browser",type:b};case "content":case "text":if(a.nextTokenIs("of")){if(a.nextIsSymbol())return b=a.getSymbolRecord(),a.next(),{domain:"browser",type:"contentOf",symbol:b.name};throw Error("'"+a.getToken()+"' is not a symbol");}break;case "selected":b= +a.nextToken();if(["index","item"].includes(b)&&["in","of"].includes(a.nextToken())&&a.nextIsSymbol()&&(c=a.getSymbolRecord(),["ul","ol","select"].includes(c.keyword)))return a.next(),{domain:"browser",type:"selected",symbol:c.name,arg:b};break;case "color":return a.next(),a=a.getValue(),{domain:"browser",type:b,value:a};case "attribute":c=a.getNextValue();if(a.tokenIs("of")&&(a.next(),a.isSymbol()&&(b=a.getSymbolRecord(),"dom"===b.extra)))return a.next(),{domain:"browser",type:"attributeOf",attribute:c, +symbol:b.name};break;case "style":c=a.getNextValue();if(a.tokenIs("of")&&a.nextIsSymbol()){var d=a.getSymbolRecord();if("dom"===d.extra)return a.next(),{domain:"browser",type:b,style:c,target:d.name}}break;case "confirm":return b=a.getNextValue(),{domain:"browser",type:"confirm",text:b};case "prompt":return b=a.getNextValue(),c=null,a.tokenIs("with")&&(c=a.getNextValue()),{domain:"browser",type:"prompt",text:b,pre:c};case "screen":c=a.nextToken();if(["width","height"].includes(c))return a.next(), +{domain:"browser",type:b,attribute:c};break;case "top":case "bottom":case "left":case "right":case "width":case "height":return EasyCoder_Browser.value.getCoord(a,b,c);case "scroll":if(a.nextTokenIs("position"))return a.next(),{domain:"browser",type:"scrollPosition"};break;case "document":if(a.nextTokenIs("path"))return a.next(),{domain:"browser",type:"docPath"};break;case "storage":if(a.nextTokenIs("keys"))return a.next(),{domain:"browser",type:"storageKeys"};break;case "parent":switch(a.nextToken()){case "name":return a.next(), +{domain:"browser",type:"varName"};case "index":return a.next(),{domain:"browser",type:"varIndex"}}break;case "history":if(a.nextTokenIs("state"))return a.next(),{domain:"browser",type:"historyState"};break;case "pick":case "drag":if(a.nextTokenIs("position"))return a.next(),{domain:"browser",type:b+"Position"}}return null},getCoord:function(a,b,c){if(a.nextTokenIs("of")){if(a.nextTokenIs("window"))return a.next(),{domain:"browser",type:b,symbol:"window",offset:c};if(a.isSymbol()){var d=a.getSymbolRecord(); +if("dom"===d.extra)return a.next(),{domain:"browser",type:b,symbol:d.name,offset:c}}}return null},get:function(a,b){switch(b.type){case "file":case "input":case "select":case "textarea":var c=a.getSymbolRecord(b.value);var d=c.element[c.index];return{type:"constant",numeric:!1,content:d.value};case "exists":return c=a.getSymbolRecord(b.value),{domain:"browser",type:"boolean",content:"undefined"!==typeof c.element[c.index]};case "mobile":return{domain:"browser",type:"boolean",content:"undefined"!== +typeof window.orientation||-1!==navigator.userAgent.indexOf("IEMobile")};case "portrait":return{domain:"browser",type:"boolean",content:document.documentElement.clientWidth=document.documentElement.clientHeight};case "br":return{type:"constant",numeric:!1,content:decodeURIComponent("%3Cbr%20%2F%3E")};case "attributeOf":return c=a.getSymbolRecord(b.symbol),b= +a.getValue(b.attribute),d=c.element[c.index],0===b.indexOf("data-")?a.getSimpleValue(d.dataset[b.substr(5)]):a.getSimpleValue(d[b]);case "style":return c=a.getSymbolRecord(b.target),b=a.getValue(b.style),d=c.element[c.index],a.getSimpleValue(d.style[b]);case "confirm":return{type:"boolean",content:window.confirm(a.getValue(b.text))};case "prompt":return d=a.getValue(b.text),b=a.getValue(b.pre),{type:"constant",numeric:!1,content:b?window.prompt(d,b):window.prompt(d)};case "contentOf":c=a.getSymbolRecord(b.symbol); +d=c.element[c.index];switch(c.keyword){case "input":case "textarea":b=d.value;break;case "pre":b=d.innerHTML;break;default:b=d.innerHTML.split("\n").join("")}return{type:"constant",numeric:!1,content:b};case "selected":return c=a.getSymbolRecord(b.symbol),d=c.element[c.index],a=d.selectedIndex,d=0<=a?d.options[a].text:"",b="index"===b.arg?a:d,{type:"constant",numeric:!1,content:b};case "top":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY};c=a.getSymbolRecord(b.symbol); +a=c.element[c.index];b=Math.round(b.offset?a.offsetTop:a.getBoundingClientRect().top);return{type:"constant",numeric:!0,content:b};case "bottom":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY+window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().bottom);return{type:"constant",numeric:!0,content:b};case "left":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenLeft};c=a.getSymbolRecord(b.symbol); +a=c.element[c.index];b=Math.round(b.offset?a.offsetLeft:a.getBoundingClientRect().left);return{type:"constant",numeric:!0,content:b};case "right":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenX+window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().right);return{type:"constant",numeric:!0,content:b};case "width":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerWidth};c=a.getSymbolRecord(b.symbol); +b=Math.round(c.element[c.index].getBoundingClientRect().width);return{type:"constant",numeric:!0,content:b};case "height":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().height);return{type:"constant",numeric:!0,content:b};case "color":return{type:"constant",numeric:!1,content:"#"+a.value.evaluate(a,b.value).content.toString(16).padStart(6,"0")};case "docPath":return{type:"constant", +numeric:!1,content:a.docPath};case "storageKeys":return{type:"constant",numeric:!1,content:JSON.stringify(Object.keys(localStorage))};case "location":return{type:"constant",numeric:!1,content:window.location.href};case "historyState":return{type:"constant",numeric:!1,content:window.history.state};case "scrollPosition":return{type:"constant",numeric:!0,content:scrollPosition};case "varName":return{type:"constant",numeric:!1,content:a.varName};case "varIndex":return{type:"constant",numeric:!0,content:a.varIndex}; +case "key":return{type:"constant",numeric:!1,content:a.key};case "hostname":return{type:"constant",numeric:!1,content:location.hostname};case "screen":return{type:"constant",numeric:!0,content:screen[b.attribute]};case "pickPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.pickX,y:document.pickY})};case "dragPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.dragX,y:document.dragY})}}}},condition:{compile:function(a){if(a.tokenIs("confirm"))return{domain:"browser", +type:"confirm",value:a.getNextValue()};if(a.tokenIs("element")&&a.nextIsSymbol()){var b=a.getSymbolRecord();if("dom"===b.extra){var c=a.nextToken();if("has"===c){if(a.nextTokenIs("the")&&a.next(),a.tokenIs("focus"))return a.next(),{domain:"browser",type:"focus",element:b.name}}else if("contains"===c)return a=a.getNextValue(),{domain:"browser",type:"contains",element:b.name,position:a}}}return null},test:function(a,b){switch(b.type){case "confirm":return confirm(a.getValue(b.value));case "focus":var c= +a.getSymbolRecord(b.element);return c.element[c.index]===document.activeElement;case "contains":c=a.getSymbolRecord(b.element);var d=c.element[c.index].getBoundingClientRect();c=Math.round(d.left);var e=Math.round(d.right),f=Math.round(d.top);d=Math.round(d.bottom);b=JSON.parse(a.getValue(b.position));a=b.x;b=b.y;return a>=c&&a<=e&&b>=f&&b<=d?!0:!1}}},setStyles:function(a,b){a=document.getElementById(a);b=b.split(";");b=$jscomp.makeIterator(b);for(var c=b.next();!c.done;c=b.next())c=c.value.split(":"), +a.setAttribute(c[0],c[1])}},scrollPosition=0;window.addEventListener("scroll",function(){scrollPosition=this.scrollY});window.onpopstate=function(a){window.EasyCoder.timestamp=Date.now();(a=JSON.parse(a.state))&&a.script&&((a=window.EasyCoder.scripts[a.script])?a.onBrowserBack&&a.run(a.onBrowserBack):console.log("No script property in window state object"))}; var EasyCoder_Json={name:"EasyCoder_JSON",Json:{compile:function(a){var b=a.getLino(),c=a.nextToken();switch(c){case "set":a.next();if(a.isSymbol())if(c=a.getSymbolRecord(),"variable"===c.keyword){if(a.nextTokenIs("to")){var d=a.nextToken();if('["array","object"]'.includes(d))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:"setVariable",target:c.name,type:d}),!0}}else if("select"===c.keyword&&a.nextTokenIs("from")&&(a.next(),a.isSymbol()&&(d=a.getSymbolRecord(),"variable"=== d.keyword))){var e=null;a.nextTokenIs("as")&&(e=a.getNextValue());a.addCommand({domain:"json",keyword:"json",lino:b,request:"setList",target:c.name,source:d.name,display:e});return!0}break;case "sort":case "shuffle":case "format":if(a.nextIsSymbol()&&(d=a.getSymbolRecord(),"variable"===d.keyword))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,target:d.name}),!0;break;case "parse":if(a.nextTokenIs("url")&&(d=a.getNextValue(),a.tokenIs("as")&&a.nextIsSymbol()&&(e=a.getSymbolRecord(), "variable"===e.keyword)))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,source:d,target:e.name}),!0;break;case "delete":d=a.nextToken();if(["property","element"].includes(d)&&(e=a.getNextValue(),["from","of"].includes(a.getToken())&&a.nextIsSymbol())){var f=a.getSymbolRecord();if("variable"===f.keyword)return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,what:d,value:e,target:f.name}),!0}break;case "rename":d=a.getNextValue();if(a.tokenIs("to")&& @@ -188,38 +188,39 @@ c.value[c.index].content=JSON.stringify(e)}return b.pc+1}},getHandler:function(a a.next();if(c.isVHolder)return{domain:"json",type:b,name:c.name}}}else if("index"===b&&a.nextTokenIs("of")&&(c=a.getNextValue(),a.tokenIs("in")))return a=a.getNextValue(),{domain:"json",type:b,item:c,list:a}}return null},get:function(a,b){switch(b.type){case "size":case "count":b=a.getSymbolRecord(b.name);a=a.getValue(b.value[b.index]);try{var c=JSON.parse(a)}catch(e){c=[]}return{type:"constant",numeric:!0,content:c?c.length:0};case "keys":return b=a.getSymbolRecord(b.name),c=(a=a.getValue(b.value[b.index]))? JSON.stringify(Object.keys(JSON.parse(a)).sort()):"[]",{type:"constant",numeric:!1,content:c};case "index":var d=a.getValue(b.item);c=JSON.parse(a.getValue(b.list)).findIndex(function(a){return a===d});return{type:"constant",numeric:!0,content:c}}}},condition:{compile:function(){},test:function(){}}},EasyCoder_Rest={name:"EasyCoder_Rest",Rest:{compile:function(a){var b=a.getLino();switch(a.nextToken()){case "get":if(a.nextIsSymbol(!0)){var c=a.getSymbolRecord();if("variable"===c.keyword&&a.nextTokenIs("from")){var d= a.getNextValue(),e=a.getPc();a.addCommand({domain:"rest",keyword:"rest",lino:b,request:"get",target:c.name,url:d,onError:null});a.tokenIs("or")&&(a.next(),a.getCommandAt(e).onError=a.getPc()+1,a.completeHandler());return!0}}break;case "post":c=null;if(a.nextTokenIs("to"))a.next();else if(c=a.getValue(),a.tokenIs("to"))a.next();else break;d=a.getValue();if(!d)throw Error(command.lino,"No URL present");e=null;if(a.tokenIs("giving")&&a.nextIsSymbol())if(e=a.getSymbolRecord(),e.isVHolder)e=e.name,a.next(); -else throw Error("'"+e.name+"' cannot hold a value");a.addCommand({domain:"rest",keyword:"rest",lino:b,request:"post",value:c,url:d,target:e,onError:a.getPc()+2});onError=null;a.tokenIs("or")&&(a.next(),a.completeHandler());return!0}return!1},createCORSRequest:function(a,b){var c=new XMLHttpRequest;"withCredentials"in c?c.open(a,b,!0):"undefined"!=typeof XDomainRequest?(c=new XDomainRequest,c.open(a,b)):c=null;return c},run:function(a){var b=a[a.pc],c=a.getValue(b.url);c=c.startsWith("http")?c:"/"=== -c[0]?c.substr(1):window.location.origin+"/"+c;var d=EasyCoder_Rest.Rest.createCORSRequest(b.request,c);if(d){d.script=a.script;d.pc=a.pc;d.onload=function(){var c=EasyCoder.scripts[d.script],e=c[d.pc];if(200<=d.status&&400>d.status){var g=d.responseText.trim();if(e.target){var h=a.getSymbolRecord(b.target);h.value[h.index]={type:"constant",numeric:!1,content:g};h.used=!0}c.run(e.pc+1)}else g=d.status+" "+d.statusText,e.onError?(c.errorMessage="Exception trapped: "+g,c.run(e.onError)):c.runtimeError(e.lino, -"Error: "+g)};d.onerror=function(){b.onError?(a.errorMessage=this.responseText,a.run(b.onError)):a.runtimeError(b.lino,this.responseText)};switch(b.request){case "get":d.send();break;case "post":var e=a.getValue(b.value);console.log("POST to "+c);d.setRequestHeader("Content-type","application/json; charset=UTF-8");d.send(e)}return 0}a.runtimeError(b.lino,"CORS not supported")}},getHandler:function(a){switch(a){case "rest":return EasyCoder_Rest.Rest;default:return null}},run:function(a){var b=a[a.pc], -c=EasyCoder_Rest.getHandler(b.keyword);c||a.runtimeError(b.lino,"Unknown keyword '"+b.keyword+"' in 'rest' package");return c.run(a)},value:{compile:function(){return null},get:function(){return null}},condition:{compile:function(){},test:function(){}}},EasyCoder_Compare=function(a,b,c){b=a.value.evaluate(a,b);a=a.value.evaluate(a,c);c=b.content;var d=a.content;c&&b.numeric?a.numeric||(d=""===d||"-"===d||"undefined"===typeof d?0:parseInt(d)):(d&&a.numeric&&(d=d.toString()),"undefined"===typeof c&& -(c=""),"undefined"===typeof d&&(d=""));return c>d?1:c')}l+="
"}g.innerHTML=h+" "+l;g.style.display="block";b.$jscomp$loop$prop$run$143=document.getElementById("easycoder-run-button");b.$jscomp$loop$prop$step$144=document.getElementById("easycoder-step-button");b.$jscomp$loop$prop$run$143.onclick= -function(b){return function(){b.$jscomp$loop$prop$run$143.blur();a.tracing=!1;document.getElementById("easycoder-tracer-content").style.display="none";try{EasyCoder_Run.run(a,a.resume)}catch(q){var c="Error in run handler: "+q.message;console.log(c);alert(c)}}}(b);b.$jscomp$loop$prop$step$144.onclick=function(b){return function(){console.log("step");b.$jscomp$loop$prop$step$144.blur();a.tracing=!0;document.getElementById("easycoder-tracer-content").style.display="block";try{a.run(a.resume)}catch(q){var c= -"Error in step handler: "+q.message;console.log(c);alert(c)}}}(b)}a.resume=a.pc;a.pc=0}break}b={$jscomp$loop$prop$run$143:b.$jscomp$loop$prop$run$143,$jscomp$loop$prop$step$144:b.$jscomp$loop$prop$step$144}}},exit:function(a){a.onExit&&a.run(a.onExit);var b=a.parent,c=a.afterExit;delete EasyCoder.scripts[a.script];a.module&&delete a.module.program;Object.keys(a).forEach(function(b){delete a[b]});b&&c&&EasyCoder.scripts[b].run(c)}},EasyCoder_Compiler={name:"EasyCoder_Compiler",getTokens:function(){return this.tokens}, -addWarning:function(a){this.warnings.push(a)},warning:function(a){this.addWarning(a)},unrecognisedSymbol:function(a){this.addWarning("Unrecognised symbol '"+a+"'")},getWarnings:function(){return this.warnings},getIndex:function(){return this.index},next:function(a){this.index+=void 0===a?1:a},peek:function(){return this.tokens[this.index+1].token},more:function(){return this.index=this.tokens.length?null:this.tokens[this.index]?this.tokens[this.index].token: -null},nextToken:function(){this.next();return this.getToken()},tokenIs:function(a){return this.index>=this.tokens.length?!1:a===this.tokens[this.index].token},nextTokenIs:function(a){this.next();return this.tokenIs(a)},skip:function(a){if(this.index>=this.tokens.length)return null;this.next();this.tokenIs(a)&&this.next()},prev:function(){this.index--},getLino:function(){return this.index>=this.tokens.length?0:this.tokens[this.index].lino},getTarget:function(a){a=void 0===a?this.index:a;return this.tokens[a].token}, -getTargetPc:function(a){a=void 0===a?this.index:a;return this.symbols[this.getTarget(a)].pc},getCommandAt:function(a){return this.program[a]},isSymbol:function(a){a=void 0===a?!1:a;if(this.getTarget()in this.symbols)return!0;if(a)throw Error("Unknown symbol: '"+this.getTarget()+"'");return!1},nextIsSymbol:function(a){a=void 0===a?!1:a;this.next();return this.isSymbol(a)},getSymbol:function(a){if(this.isSymbol(void 0===a?!1:a))return this.symbols[this.getToken()]},getSymbolPc:function(a){return this.getSymbol(void 0=== -a?!1:a).pc},getSymbolRecord:function(){var a=this.program[this.getSymbolPc(!0)];a.used=!0;return a},getSymbols:function(){return this.symbols},getProgram:function(){return this.program},getPc:function(){return this.program.length},getValue:function(){return this.value.compile(this)},getNextValue:function(){this.next();return this.getValue()},getCondition:function(){return this.condition.compile(this)},constant:function(a,b){return this.value.constant(a,void 0===b?!1:b)},addCommand:function(a){this.program.push(Object.assign({}, -{pc:this.program.length},a))},addSymbol:function(a,b){this.symbols[a]={pc:b}},mark:function(){this.savedMark=this.index},rewind:function(){this.index=this.savedMark},rewindTo:function(a){this.index=a},completeHandler:function(){var a=this.getLino(),b=this.getPc();this.addCommand({domain:"core",keyword:"goto",lino:a,goto:0});this.compileOne();this.continue?(this.addCommand({domain:"core",keyword:"goto",lino:a,goto:this.getPc()+1}),this.continue=!1):this.addCommand({domain:"core",keyword:"stop",lino:a, -next:0});this.getCommandAt(b).goto=this.getPc();return!0},compileVariable:function(a,b,c,d){c=void 0===c?!1:c;d=void 0===d?null:d;this.next();var e=this.getLino(),f=this.getTokens()[this.getIndex()];if(this.symbols[f.token])throw Error("Duplicate variable name '"+f.token+"'");var k=this.getPc();this.next();this.addSymbol(f.token,k);a={domain:a,keyword:b,lino:e,isSymbol:!0,used:!1,isVHolder:c,name:f.token,elements:1,index:0,value:[{}],element:[],extra:d};"dom"===d&&(a.element=[]);this.addCommand(a); -return a},compileToken:function(){var a=this.getToken();if(a){this.mark();for(var b=$jscomp.makeIterator(Object.keys(this.domain)),c=b.next();!c.done;c=b.next()){if((c=this.domain[c.value])&&(c=c.getHandler(a))&&c.compile(this))return;this.rewind()}console.log("No handler found");throw Error("I don't understand '"+a+"...'");}},compileOne:function(){var a=this.getToken();if(a){this.warnings=[];var b=this.program.length;if(a.endsWith(":")){a=a.substring(0,a.length-1);if(this.symbols[a])throw Error("Duplicate symbol: '"+ -a+"'");this.symbols[a]={pc:b};this.index++}else this.compileToken()}},compileFromHere:function(a){for(;this.indexe?0:e;ee.status){var g=e.responseText.trim();if(d.target){var h=a.getSymbolRecord(b.target);h.value[h.index]={type:"constant",numeric:!1, +content:g};h.used=!0}c.run(d.pc+1)}else g=e.status+" "+e.statusText,d.onError?(c.errorMessage="Exception trapped: "+g,c.run(d.onError)):c.runtimeError(d.lino,"Error: "+g)};e.onerror=function(){b.onError?(a.errorMessage=this.responseText,a.run(b.onError)):a.runtimeError(b.lino,this.responseText)};switch(b.request){case "get":e.send();break;case "post":c=a.getValue(b.value),console.log("POST to "+d),e.setRequestHeader("Content-type","application/json; charset=UTF-8"),e.send(c)}return 0}a.runtimeError(b.lino, +"CORS not supported")}},getHandler:function(a){switch(a){case "rest":return EasyCoder_Rest.Rest;default:return null}},run:function(a){var b=a[a.pc],c=EasyCoder_Rest.getHandler(b.keyword);c||a.runtimeError(b.lino,"Unknown keyword '"+b.keyword+"' in 'rest' package");return c.run(a)},value:{compile:function(){return null},get:function(){return null}},condition:{compile:function(){},test:function(){}}},EasyCoder_Compare=function(a,b,c){b=a.value.evaluate(a,b);a=a.value.evaluate(a,c);c=b.content;var d= +a.content;c&&b.numeric?a.numeric||(d=""===d||"-"===d||"undefined"===typeof d?0:parseInt(d)):(d&&a.numeric&&(d=d.toString()),"undefined"===typeof c&&(c=""),"undefined"===typeof d&&(d=""));return c>d?1:c')}l+="
"}g.innerHTML=h+" "+l;g.style.display="block";b.$jscomp$loop$prop$run$144= +document.getElementById("easycoder-run-button");b.$jscomp$loop$prop$step$145=document.getElementById("easycoder-step-button");b.$jscomp$loop$prop$run$144.onclick=function(b){return function(){b.$jscomp$loop$prop$run$144.blur();a.tracing=!1;document.getElementById("easycoder-tracer-content").style.display="none";try{EasyCoder_Run.run(a,a.resume)}catch(q){var c="Error in run handler: "+q.message;console.log(c);alert(c)}}}(b);b.$jscomp$loop$prop$step$145.onclick=function(b){return function(){console.log("step"); +b.$jscomp$loop$prop$step$145.blur();a.tracing=!0;document.getElementById("easycoder-tracer-content").style.display="block";try{a.run(a.resume)}catch(q){var c="Error in step handler: "+q.message;console.log(c);alert(c)}}}(b)}a.resume=a.pc;a.pc=0}break}b={$jscomp$loop$prop$run$144:b.$jscomp$loop$prop$run$144,$jscomp$loop$prop$step$145:b.$jscomp$loop$prop$step$145}}},exit:function(a){a.onExit&&a.run(a.onExit);var b=a.parent,c=a.afterExit;delete EasyCoder.scripts[a.script];a.module&&delete a.module.program; +Object.keys(a).forEach(function(b){delete a[b]});b&&c&&EasyCoder.scripts[b].run(c)}},EasyCoder_Compiler={name:"EasyCoder_Compiler",getTokens:function(){return this.tokens},addWarning:function(a){this.warnings.push(a)},warning:function(a){this.addWarning(a)},unrecognisedSymbol:function(a){this.addWarning("Unrecognised symbol '"+a+"'")},getWarnings:function(){return this.warnings},getIndex:function(){return this.index},next:function(a){this.index+=void 0===a?1:a},peek:function(){return this.tokens[this.index+ +1].token},more:function(){return this.index=this.tokens.length?null:this.tokens[this.index]?this.tokens[this.index].token:null},nextToken:function(){this.next();return this.getToken()},tokenIs:function(a){return this.index>=this.tokens.length?!1:a===this.tokens[this.index].token},nextTokenIs:function(a){this.next();return this.tokenIs(a)},skip:function(a){if(this.index>=this.tokens.length)return null;this.next();this.tokenIs(a)&&this.next()}, +prev:function(){this.index--},getLino:function(){return this.index>=this.tokens.length?0:this.tokens[this.index].lino},getTarget:function(a){a=void 0===a?this.index:a;return this.tokens[a].token},getTargetPc:function(a){a=void 0===a?this.index:a;return this.symbols[this.getTarget(a)].pc},getCommandAt:function(a){return this.program[a]},isSymbol:function(a){a=void 0===a?!1:a;if(this.getTarget()in this.symbols)return!0;if(a)throw Error("Unknown symbol: '"+this.getTarget()+"'");return!1},nextIsSymbol:function(a){a= +void 0===a?!1:a;this.next();return this.isSymbol(a)},getSymbol:function(a){if(this.isSymbol(void 0===a?!1:a))return this.symbols[this.getToken()]},getSymbolPc:function(a){return this.getSymbol(void 0===a?!1:a).pc},getSymbolRecord:function(){var a=this.program[this.getSymbolPc(!0)];a.used=!0;return a},getSymbols:function(){return this.symbols},getProgram:function(){return this.program},getPc:function(){return this.program.length},getValue:function(){return this.value.compile(this)},getNextValue:function(){this.next(); +return this.getValue()},getCondition:function(){return this.condition.compile(this)},constant:function(a,b){return this.value.constant(a,void 0===b?!1:b)},addCommand:function(a){this.program.push(Object.assign({},{pc:this.program.length},a))},addSymbol:function(a,b){this.symbols[a]={pc:b}},mark:function(){this.savedMark=this.index},rewind:function(){this.index=this.savedMark},rewindTo:function(a){this.index=a},completeHandler:function(){var a=this.getLino(),b=this.getPc();this.addCommand({domain:"core", +keyword:"goto",lino:a,goto:0});this.compileOne();this.continue?(this.addCommand({domain:"core",keyword:"goto",lino:a,goto:this.getPc()+1}),this.continue=!1):this.addCommand({domain:"core",keyword:"stop",lino:a,next:0});this.getCommandAt(b).goto=this.getPc();return!0},compileVariable:function(a,b,c,d){c=void 0===c?!1:c;d=void 0===d?null:d;this.next();var e=this.getLino(),f=this.getTokens()[this.getIndex()];if(this.symbols[f.token])throw Error("Duplicate variable name '"+f.token+"'");var k=this.getPc(); +this.next();this.addSymbol(f.token,k);a={domain:a,keyword:b,lino:e,isSymbol:!0,used:!1,isVHolder:c,name:f.token,elements:1,index:0,value:[{}],element:[],extra:d};"dom"===d&&(a.element=[]);this.addCommand(a);return a},compileToken:function(){var a=this.getToken();if(a){this.mark();for(var b=$jscomp.makeIterator(Object.keys(this.domain)),c=b.next();!c.done;c=b.next()){if((c=this.domain[c.value])&&(c=c.getHandler(a))&&c.compile(this))return;this.rewind()}console.log("No handler found");throw Error("I don't understand '"+ +a+"...'");}},compileOne:function(){var a=this.getToken();if(a){this.warnings=[];var b=this.program.length;if(a.endsWith(":")){a=a.substring(0,a.length-1);if(this.symbols[a])throw Error("Duplicate symbol: '"+a+"'");this.symbols[a]={pc:b};this.index++}else this.compileToken()}},compileFromHere:function(a){for(;this.indexe?0:e;e { const command = program[program.pc]; const url = program.getValue(command.url); - const path = url.startsWith(`http`) ? url - : url[0] === `/` ? url.substr(1) : `${window.location.origin}/${url}`; + const rest = document.getElementById(`easycoder-rest`); + let path = url; + if (!url.startsWith(`http`)) { + if (url[0] == `/`) { + path = url.substr(1); + } else { + const rest = document.getElementById(`easycoder-rest`).innerText; + if (!rest) { + program.runtimeError(command.lino, `No REST server defined`); + } + path = `${window.location.origin}${rest}/${url}`; + } + } const request = EasyCoder_Rest.Rest.createCORSRequest(command.request, path); if (!request) { @@ -8276,6 +8291,8 @@ const EasyCoder = { }, start: function(source) { + EasyCoder.restPath = `rest.php`; + EasyCoder.scriptIndex = 0; const script = source.split(`\n`); if (!this.tokenising) { diff --git a/js/easycoder/Browser.js b/js/easycoder/Browser.js index 474d9a8..9425548 100644 --- a/js/easycoder/Browser.js +++ b/js/easycoder/Browser.js @@ -2558,7 +2558,11 @@ const EasyCoder_Browser = { } }; program.ajaxCommand = command; - const postpath = path.startsWith(`http`) ? path : `${window.location.origin}//${path}`; + const rest = document.getElementById(`easycoder-rest`); + if (!rest) { + program.runtimeError(command.lino, `No REST server defined`); + } + const postpath = path.startsWith(`http`) ? path : `${window.location.origin}${rest}/${path}`; ajax.open(`POST`, postpath); ajax.send(formData); } diff --git a/js/easycoder/Main.js b/js/easycoder/Main.js index 669b878..e71f12d 100644 --- a/js/easycoder/Main.js +++ b/js/easycoder/Main.js @@ -363,6 +363,8 @@ const EasyCoder = { }, start: function(source) { + EasyCoder.restPath = `rest.php`; + EasyCoder.scriptIndex = 0; const script = source.split(`\n`); if (!this.tokenising) { diff --git a/js/easycoder/Rest.js b/js/easycoder/Rest.js index 581f3bf..028b768 100644 --- a/js/easycoder/Rest.js +++ b/js/easycoder/Rest.js @@ -110,8 +110,19 @@ const EasyCoder_Rest = { run: (program) => { const command = program[program.pc]; const url = program.getValue(command.url); - const path = url.startsWith(`http`) ? url - : url[0] === `/` ? url.substr(1) : `${window.location.origin}/${url}`; + const rest = document.getElementById(`easycoder-rest`); + let path = url; + if (!url.startsWith(`http`)) { + if (url[0] == `/`) { + path = url.substr(1); + } else { + const rest = document.getElementById(`easycoder-rest`).innerText; + if (!rest) { + program.runtimeError(command.lino, `No REST server defined`); + } + path = `${window.location.origin}${rest}/${url}`; + } + } const request = EasyCoder_Rest.Rest.createCORSRequest(command.request, path); if (!request) {