diff options
| author | john.j.camilleri <john.j.camilleri@chalmers.se> | 2012-11-20 13:56:56 +0000 |
|---|---|---|
| committer | john.j.camilleri <john.j.camilleri@chalmers.se> | 2012-11-20 13:56:56 +0000 |
| commit | 09c4f8410eba31bafc567a7d4115d62681665938 (patch) | |
| tree | 929de876163c301a6d3bea28a59d018c3a11eda7 | |
| parent | 3eaeaed8fd741bb1f33c76032e7292df08b35a9f (diff) | |
Syntax editor: update to use common js files
| -rw-r--r-- | src/www/syntax-editor/README.md | 3 | ||||
| -rw-r--r-- | src/www/syntax-editor/ast.js (renamed from src/www/syntax-editor/js/ast.js) | 56 | ||||
| -rw-r--r-- | src/www/syntax-editor/editor.css (renamed from src/www/syntax-editor/ui/style.css) | 2 | ||||
| -rw-r--r-- | src/www/syntax-editor/editor.html | 17 | ||||
| -rw-r--r-- | src/www/syntax-editor/editor.js (renamed from src/www/syntax-editor/js/editor.js) | 8 | ||||
| -rw-r--r-- | src/www/syntax-editor/editor_menu.js (renamed from src/www/syntax-editor/js/editor_menu.js) | 0 | ||||
| -rw-r--r-- | src/www/syntax-editor/js/pgf_online.js | 80 | ||||
| -rw-r--r-- | src/www/syntax-editor/js/support.js | 329 |
8 files changed, 45 insertions, 450 deletions
diff --git a/src/www/syntax-editor/README.md b/src/www/syntax-editor/README.md index 647792d48..dec633476 100644 --- a/src/www/syntax-editor/README.md +++ b/src/www/syntax-editor/README.md @@ -9,7 +9,7 @@ An improved version of the [old syntax editor][1]. ## Notes -Tested with latest Chrome and Firefox. +- Tested with latest Chrome and Firefox (only). ## TODO @@ -19,7 +19,6 @@ Tested with latest Chrome and Firefox. - UI issue with DisambPhrasebookEng - more prominence to Disamb-linearizations - ambiguity: (optionally) parse all the resulting linearizations/variants and point out those which are ambiguous -- random-generate a non-empty tree as a starting point - try to retain subtree when replacing node - add undo/redo (or back/forward) navigation - structure the set of fridge magnets some more. Even though they diff --git a/src/www/syntax-editor/js/ast.js b/src/www/syntax-editor/ast.js index 633bdc2b6..143b9836c 100644 --- a/src/www/syntax-editor/js/ast.js +++ b/src/www/syntax-editor/ast.js @@ -35,10 +35,9 @@ function NodeID(x) { function ASTNode(data) { for(var d in data) this[d]=data[d]; this.children = []; - // if (children != undefined) - // for (c in children) { - // this.children.push( new ASTNode(children[c]) ); - // } + for (c in data.children) { + this.children.push( new ASTNode(data.children[c]) ); + } this.hasChildren = function(){ return this.children.length > 0; } @@ -84,7 +83,6 @@ function AST(fun, cat) { this.find(this.current).cat = c; } - // Add a single fun at current node this.add = function(fun, cat) { this._add(this.current, newNode(fun,cat)); @@ -101,33 +99,39 @@ function AST(fun, cat) { this._setSubtree(this.current, node); } - // set tree at given id to - this._setSubtree = function(id, node) { - var x = this.find(id); - for (var n in node) x[n] = node[n]; + // set tree at given id + this._setSubtree = function(id, subtree) { + var lid = id.get().slice(); // clone NodeID array + var node = this.root; + + if (lid.length==1) + // Insert at root + this.root = new ASTNode(subtree); + else { + lid.shift(); // throw away root + while (lid.length>1 && node.hasChildren()) { + node = node.children[lid.shift()]; + } + node.children[lid.shift()] = new ASTNode(subtree); + } - x.traverse(function(node){ - if (!node.children) node.children=[]; - // TODO: this doesn't work! - //node = new ASTNode(node); - }) } // id should be a list of child indices [0,1,0] // or a string separated by commas "0,1,0" - this.find = function(_id) { - var id = undefined - switch (typeof _id) { - case "number": id = [_id]; break; - case "string": id = _id.split(","); break; - case "object": id = _id.get().slice(); break; // clone NodeID array + this.find = function(id) { + var lid = undefined + switch (typeof id) { + case "number": lid = [id]; break; + case "string": lid = id.split(","); break; + case "object": lid = id.get().slice(); break; // clone NodeID array } var node = this.root; - if (id[0] == 0) id.shift(); - while (id.length>0 && node.children.length>0) { - node = node.children[id.shift()]; + if (lid[0] == 0) lid.shift(); + while (lid.length>0 && node.children.length>0) { + node = node.children[lid.shift()]; } - if (id.length>0) + if (lid.length>0) return undefined; return node; } @@ -187,8 +191,8 @@ function AST(fun, cat) { var s = ""; function visit(node) { s += node.fun ? node.fun : "?" ; -// if (!node.hasChildren()) - if (node.children.length == 0) + if (!node.hasChildren()) +// if (node.children.length == 0) return; for (i in node.children) { s += " ("; diff --git a/src/www/syntax-editor/ui/style.css b/src/www/syntax-editor/editor.css index 127b1ad2a..8640a819f 100644 --- a/src/www/syntax-editor/ui/style.css +++ b/src/www/syntax-editor/editor.css @@ -43,7 +43,7 @@ select#to_menu #linearisations { - background: rgba(170, 170, 170, 0.5); + /* background: rgba(170, 170, 170, 0.5); */ padding:0.5em; margin:0.5em 0; } diff --git a/src/www/syntax-editor/editor.html b/src/www/syntax-editor/editor.html index d641998ab..bb6b49fa8 100644 --- a/src/www/syntax-editor/editor.html +++ b/src/www/syntax-editor/editor.html @@ -5,7 +5,7 @@ <link rel="author" href="http://www.grammaticalframework.org/~john/" title="John J. Camilleri"> <title>Syntax Editor</title> <link rel="stylesheet" type="text/css" href="http://cloud.grammaticalframework.org/minibar/minibar.css" /> - <link rel="stylesheet" type="text/css" href="ui/style.css" /> + <link rel="stylesheet" type="text/css" href="editor.css" /> </head> <body> <h2>Syntax Editor</h2> @@ -17,16 +17,15 @@ John J. Camilleri, November 2012 </small> - <script type="text/javascript" src="js/support.js"></script> - <script type="text/javascript" src="js/pgf_online.js"></script> - <script type="text/javascript" src="js/ast.js"></script> - <script type="text/javascript" src="js/editor_menu.js"></script> - <script type="text/javascript" src="js/editor.js"></script> + <script type="text/javascript" src="../js/support.js"></script> + <script type="text/javascript" src="../js/pgf_online.js"></script> + <script type="text/javascript" src="ast.js"></script> + <script type="text/javascript" src="editor_menu.js"></script> + <script type="text/javascript" src="editor.js"></script> <script type="text/javascript"> - // Code taken from minibar_online.js var server_options = { - //grammars_url: "http://www.grammaticalframework.org/grammars/", - //grammars_url: "http://localhost:41296/grammars/", + // grammars_url: "http://www.grammaticalframework.org/grammars/", + grammars_url: "http://localhost:41296/grammars/", } var editor_options = { target: "editor" diff --git a/src/www/syntax-editor/js/editor.js b/src/www/syntax-editor/editor.js index 1f6a374b5..33b8b496c 100644 --- a/src/www/syntax-editor/js/editor.js +++ b/src/www/syntax-editor/editor.js @@ -243,7 +243,6 @@ Editor.prototype.delete_refinement = function() { // Generate random subtree from current node Editor.prototype.generate_random = function() { var t = this; - t.ui.refinements.innerHTML = "..."; t.ast.removeChildren(); var args = { cat: t.ast.getCat(), @@ -263,7 +262,7 @@ Editor.prototype.generate_random = function() { server.get_random(args, cont, err); } -// Import AST from string representation +// Import AST from string representation, setting at current node Editor.prototype.import_ast = function(abstr) { var t = this; var args = { @@ -272,7 +271,9 @@ Editor.prototype.import_ast = function(abstr) { var cont = function(tree){ // Build tree of just fun, then populate with cats t.ast.setSubtree(tree); + /// TODO: traverse only subtree, not everything! t.ast.traverse(function(node){ + if (!node.fun) return; var info = t.lookup_fun(node.fun); node.cat = info.cat; }); @@ -282,7 +283,8 @@ Editor.prototype.import_ast = function(abstr) { server.pgf_call("abstrjson", args, cont); } -// Look up information for a function, hopefully from cache +// Look up information for a function +// This will absolutely fail on dependant types Editor.prototype.lookup_fun = function(fun) { var t = this; var def = t.grammar_constructors.funs[fun].def; diff --git a/src/www/syntax-editor/js/editor_menu.js b/src/www/syntax-editor/editor_menu.js index ded47f54e..ded47f54e 100644 --- a/src/www/syntax-editor/js/editor_menu.js +++ b/src/www/syntax-editor/editor_menu.js diff --git a/src/www/syntax-editor/js/pgf_online.js b/src/www/syntax-editor/js/pgf_online.js deleted file mode 100644 index 6fe23b13e..000000000 --- a/src/www/syntax-editor/js/pgf_online.js +++ /dev/null @@ -1,80 +0,0 @@ - -/* --- Grammar access object ------------------------------------------------ */ - -function pgf_online(options) { - var server = { - // State variables (private): - grammars_url: "/grammars/", - other_grammars_urls: [], - grammar_list: null, - current_grammar_url: null, - - // Methods: - switch_grammar: function(grammar_url,cont) { - this.current_grammar_url=this.grammars_url+grammar_url; - if(cont) cont(); - }, - add_grammars_url: function(grammars_url,cont) { - this.other_grammars_urls.push(grammars_url); - if(cont) cont(); - }, - switch_to_other_grammar: function(grammar_url,cont) { - this.current_grammar_url=grammar_url; - if(cont) cont(); - }, - get_grammarlist: function(cont,err) { - if(this.grammar_list) cont(this.grammar_list) - else http_get_json(this.grammars_url+"grammars.cgi",cont,err); - }, - get_grammarlists: function(cont,err) { // May call cont several times! - var ds=this.other_grammars_urls; - var n=1+ds.length; - function pair(dir) { - return function(grammar_list){cont(dir,grammar_list,n)} - } - function ignore_error(err) { console.log(err) } - this.get_grammarlist(pair(this.grammars_url),err) - for(var i in ds) - http_get_json(ds[i]+"grammars.cgi",pair(ds[i]),ignore_error); - }, - pgf_call: function(cmd,args,cont,err) { - var url=this.current_grammar_url+"?command="+cmd+encodeArgs(args) - http_get_json(url,cont,err); - }, - - get_languages: function(cont,err) { - this.pgf_call("grammar",{},cont,err); - }, - grammar_info: function(cont,err) { - this.pgf_call("grammar",{},cont,err); - }, - - get_random: function(args,cont,err) { // cat, limit - args.random=Math.random(); // side effect!! - this.pgf_call("random",args,cont,err); - }, - linearize: function(args,cont,err) { // tree, to - this.pgf_call("linearize",args,cont,err); - }, - complete: function(args,cont,err) { // from, input, cat, limit - this.pgf_call("complete",args,cont,err); - }, - parse: function(args,cont,err) { // from, input, cat - this.pgf_call("parse",args,cont,err); - }, - translate: function(args,cont,err) { // from, input, cat, to - this.pgf_call("translate",args,cont,err); - }, - translategroup: function(args,cont,err) { // from, input, cat, to - this.pgf_call("translategroup",args,cont,err); - }, - browse: function(args,cont,err) { // id, format - if(!args.format) args.format="json"; // sife effect!! - this.pgf_call("browse",args,cont,err); - } - }; - for(var o in options) server[o]=options[o]; - if(server.grammar_list && server.grammar_list.length>0) - server.switch_grammar(server.grammar_list[0]); - return server; -} diff --git a/src/www/syntax-editor/js/support.js b/src/www/syntax-editor/js/support.js deleted file mode 100644 index e82f4fc9c..000000000 --- a/src/www/syntax-editor/js/support.js +++ /dev/null @@ -1,329 +0,0 @@ -/* --- Accessing document elements ------------------------------------------ */ - -function element(id) { - return document.getElementById(id); -} - -/* --- JavaScript tricks ---------------------------------------------------- */ - -// To be able to use object methods that refer to "this" as callbacks -// See section 3.3 of https://github.com/spencertipping/js-in-ten-minutes/raw/master/js-in-ten-minutes.pdf -function bind(f, this_value) { - return function () {return f.apply (this_value, arguments)}; -}; - -// Implement Array.isArray for older browsers that lack it. -// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray -if(!Array.isArray) { - Array.isArray = function (arg) { - return Object.prototype.toString.call(arg) == '[object Array]'; - }; -} - -/* --- JSONP ---------------------------------------------------------------- */ - -// Inspired by the function jsonp from -// http://www.west-wind.com/Weblog/posts/107136.aspx -// See also http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/ -// http://en.wikipedia.org/wiki/JSONP -function jsonp(url,callback) -{ - if (url.indexOf("?") > -1) - url += "&jsonp=" - else - url += "?jsonp=" - url += callback; - //url += "&" + new Date().getTime().toString(); // prevent caching - - var script = empty("script"); - script.setAttribute("src",url); - script.setAttribute("type","text/javascript"); - document.body.appendChild(script); -} - -var json = {next:0}; - -// Like jsonp, but instead of passing the name of the callback function, you -// pass the callback function directly, making it possible to use anonymous -// functions. -function jsonpf(url,callback,errorcallback) -{ - var name="callback"+(json.next++); - json[name]=function(x) { delete json[name]; callback(x); } - jsonp(url,"json."+name); -} - -/* --- AJAX ----------------------------------------------------------------- */ - -function GetXmlHttpObject(handler) -{ - var objXMLHttp=null - if (window.XMLHttpRequest) - { - // See http://www.w3.org/TR/XMLHttpRequest/ - // https://developer.mozilla.org/en/xmlhttprequest - objXMLHttp=new XMLHttpRequest() - } - else if (window.ActiveXObject) - { - objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") - } - return objXMLHttp -} - -function ajax_http(method,url,body,callback,errorcallback) { - var http=GetXmlHttpObject() - if (!http) { - var errortext="Browser does not support HTTP Request"; - if(errorcallback) errorcallback(errortext,500) - else alert(errortext) - } - else { - function statechange() { - if (http.readyState==4 || http.readyState=="complete") { - if(http.status<300) callback(http.responseText,http.status); - else if(errorcallback) - errorcallback(http.responseText,http.status, - http.getResponseHeader("Content-Type")); - else alert("Request for "+url+" failed: " - +http.status+" "+http.statusText); - } - } - http.onreadystatechange=statechange; - http.open(method,url,true) - http.send(body) - } - return http -} - -function ajax_http_get(url,callback,errorcallback) { - ajax_http("GET",url,null,callback,errorcallback) -} - -function ajax_http_post(url,formdata,callback,errorcallback) { - ajax_http("POST",url,formdata,callback,errorcallback) - // See https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Using_FormData_objects -} - -// JSON via AJAX -function ajax_http_get_json(url,cont,errorcallback) { - ajax_http_get(url,function(txt){cont(eval("("+txt+")"));}, errorcallback); -} - -function sameOrigin(url) { - var a=empty("a"); - a.href=url; // converts to an absolute URL - return hasPrefix(a.href,location.protocol+"//"+location.host+"/"); -} - -// Use AJAX when possible, fallback to JSONP -function http_get_json(url,cont,errorcallback) { - if(sameOrigin(url)) ajax_http_get_json(url,cont,errorcallback); - else jsonpf(url,cont,errorcallback); -} - -/* --- URL construction ----------------------------------------------------- */ - -function encodeArgs(args) { - var q="" - for(var arg in args) - if(args[arg]!=undefined) - q+="&"+arg+"="+encodeURIComponent(args[arg]); - return q; -} - -/* --- HTML construction ---------------------------------------------------- */ -function text(s) { return document.createTextNode(s); } - -function node(tag,as,ds) { - var n=document.createElement(tag); - for(var a in as) n.setAttribute(a,as[a]); - if(ds) for(var i in ds) n.appendChild(ds[i]); - return n; -} - -function empty(tag,name,value) { - var el=node(tag,{},[]) - if(name && value) el.setAttribute(name,value); - return el; -} - -function empty_id(tag,id) { return empty(tag,"id",id); } -function empty_class(tag,cls) { return empty(tag,"class",cls); } - -function div_id(id,cs) { return node("div",{id:id},cs); } -function span_id(id) { return empty_id("span",id); } - -function wrap(tag,contents) { - return node(tag,{},Array.isArray(contents) ? contents : [contents]); -} - -function wrap_class(tag,cls,contents) { - return node(tag,{"class":cls}, - contents ? Array.isArray(contents) ? - contents : [contents] : []) -} - -function span_class(cls,contents) { return wrap_class("span",cls,contents); } -function div_class(cls,contents) { return wrap_class("div",cls,contents); } - -function p(contents) { return wrap("p",contents); } -function dt(contents) { return wrap("dt",contents); } -function dd(contents) { return wrap("dd",contents); } -function li(contents) { return wrap("li",contents); } - -function th(contents) { return wrap("th",contents); } -function td(contents) { return wrap("td",contents); } - -function tr(cells) { return wrap("tr",cells); } - -function button(label,action,key) { - var el=node("input",{"type":"button","value":label},[]); - if(typeof action=="string") el.setAttribute("onclick",action); - else el.onclick=action; - if(key) el.setAttribute("accesskey",key); - return el; -} - -function option(label,value) { - return node("option",{"value":value},[text(label)]); -} - -function hidden(name,value) { - return node("input",{type:"hidden",name:name,value:value},[]) -} - -function tda(cs) { return node("td",{},cs); } - -function img(src) { return empty("img","src",src); } - -/* --- Document modification ------------------------------------------------ */ - -function clear(el) { replaceInnerHTML(el,""); } -function replaceInnerHTML(el,html) { if(el) el.innerHTML=html; } -function replaceChildren(el,newchild) { clear(el); el.appendChild(newchild); } - -function appendChildren(el,ds) { - for(var i in ds) el.appendChild(ds[i]); - return el; -} - -function insertFirst(parent,child) { - parent.insertBefore(child,parent.firstChild); -} - -function insertBefore(el,ref) { ref.parentNode.insertBefore(el,ref); } - -function insertAfter(el,ref) { - ref.parentNode.insertBefore(el,ref.nextSibling); -} - -/* --- Debug ---------------------------------------------------------------- */ - -function debug(s) { - var d=element("debug"); - if(d) d.appendChild(text(s+"\n")) -} - -function show_props(obj, objName) { - var result = ""; - for (var i in obj) { - result += objName + "." + i + " = " + obj[i] + "<br>"; - } - return result; -} - -function field_names(obj) { - var result = ""; - for (var i in obj) { - result += " " + i; - } - return result; -} - -/* --- Data manipulation ---------------------------------------------------- */ -function swap(a,i,j) { // Note: this doesn't work on strings. - var tmp=a[i]; - a[i]=a[j]; - a[j]=tmp; - return a; -} - -function sort(a) { -// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort - return a.sort(); - /* // Note: this doesn't work on strings. - for(var i=0;i<a.length-1;i++) { - var min=i; - for(var j=i+1;j<a.length;j++) - if(a[j]<a[min]) min=j; - if(min!=i) swap(a,i,min); - } - return a; - */ -} - -function filter(p,xs) { - var ys=[]; - for(var i=0;i<xs.length;i++) - if(p(xs[i])) ys[ys.length]=xs[i]; - return ys; -} - -function implode(cs) { // array of strings to string - /* - var s=""; - for(var i=0;i<cs.length;i++) - s+=cs[i]; - return s; - */ - return cs.join(""); -} - -function hasPrefix(s,pre) { return s.substr(0,pre.length)==pre; } - -function commonPrefix(s1,s2) { - for(var i=0;i<s1.length && i<s2.length && s1[i]==s2[i];i++); - return s1.substr(0,i); -} - -/* -function all(p,xs) { - for(var i=0;i<xs.length;i++) - if(!p(xs[i])) return false; - return true; -} -*/ - -function map(f,xs) { - var ys=[]; - for(var i=0;i<xs.length;i++) ys[i]=f(xs[i]); - return ys; -} - -// map in continuation passing style -function mapc(f,xs,cont) { mapc_from(f,xs,0,[],cont); } - -function mapc_from(f,xs,i,ys,cont) { - if(i<xs.length) - f(xs[i],function(y){ys[i]=y;mapc_from(f,xs,i+1,ys,cont)}); - else - cont(ys); -} - -function overlaps(as,bs) { - for(var i=0;i<as.length;i++) - if(elem(as[i],bs)) return true; - return false; -} - -function elem(a,as) { - for(var i=0;i<as.length;i++) - if(a==as[i]) return true; - return false; -} - -function shuffle(a) { - for(i=0;i<a.length;i++) swap(a,i,Math.floor(Math.random()*a.length)) - return a; -} |
