diff --git a/src/www/js/support.js b/src/www/js/support.js index 9687ea8a1..41b736855 100644 --- a/src/www/js/support.js +++ b/src/www/js/support.js @@ -20,6 +20,15 @@ if(!Array.isArray) { }; } +// Create a clone of an array +// http://davidwalsh.name/javascript-clone-array +// Note that iterating over an array with for...in will include "clone" as a key! +// if(!Array.clone) { +// Array.prototype.clone = function() { +// return this.slice(0); +// }; +// } + /* --- JSONP ---------------------------------------------------------------- */ // Inspired by the function jsonp from diff --git a/src/www/syntax-editor/editor.css b/src/www/syntax-editor/editor.css index 29f45a7e7..2b5d5a6d1 100644 --- a/src/www/syntax-editor/editor.css +++ b/src/www/syntax-editor/editor.css @@ -7,7 +7,7 @@ body.syntax_editor { display:none; } -#editor select#to_menu +.editor select#to_menu { height: 10em; position: absolute; diff --git a/src/www/syntax-editor/editor.html b/src/www/syntax-editor/editor.html index 36045957c..1d4cc401c 100644 --- a/src/www/syntax-editor/editor.html +++ b/src/www/syntax-editor/editor.html @@ -9,7 +9,7 @@

Syntax Editor

-
+

@@ -17,26 +17,23 @@ John J. Camilleri, November 2012 + + + - + + + + + + + + + diff --git a/src/www/syntax-editor/editor.js b/src/www/syntax-editor/editor.js index df2a49da8..70dbab8e9 100644 --- a/src/www/syntax-editor/editor.js +++ b/src/www/syntax-editor/editor.js @@ -1,11 +1,3 @@ - -/* --- Some enhancements --------------------------------------------------- */ - -// http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays -// Array.prototype.clone = function(){ -// return this.slice(0); -// } - /* --- Main Editor object --------------------------------------------------- */ /* When creating the object, stuff gets called in this order: new EditorMenu @@ -49,16 +41,20 @@ function Editor(server,opts) { if(opts) for(var o in opts) this.options[o]=opts[o]; /* --- Creating UI components ------------------------------------------- */ - var main = document.getElementById(this.options.target); + this.container = document.getElementById(this.options.target); + this.container.classList.add("editor"); this.ui = { menubar: div_class("menu"), tree: div_id("tree"), refinements: div_id("refinements"), lin: div_id("linearisations") }; - with(this.ui) { - appendChildren(main, [menubar, tree, refinements, lin]); - } + appendChildren(this.container, [ + this.ui.menubar, + this.ui.tree, + this.ui.refinements, + this.ui.lin + ]); /* --- Client state initialisation -------------------------------------- */ this.server = server; @@ -70,8 +66,11 @@ function Editor(server,opts) { /* --- Main program, this gets things going ----------------------------- */ this.menu = new EditorMenu(this, this.options); - /* --- Apply supplied initial settings (if any) ------------------------- */ -// if (this.options.initial.grammar) + /* --- Shutdown the editor nicely --------------------------------------- */ + this.shutdown = function() { + clear(this.container); + this.container.classList.remove("editor"); + } } @@ -237,32 +236,34 @@ Editor.prototype.redraw_tree=function() { } Editor.prototype.update_linearisation=function(){ - + var t = this; function langpart(conc,abs) { // langpart("FoodsEng","Foods") == "Eng" return hasPrefix(conc,abs) ? conc.substr(abs.length) : conc; } - - var t = this; - with (this) { - var args = { - tree: ast.toString() - }; - server.linearize(args, function(data){ - clear(t.ui.lin); - for (i in data) { - var lang = data[i].to; - var langname = langpart(lang, t.grammar.name); - if (t.languages.length < 1 || elem(lang, t.languages)) { - var div_lang = empty("div"); - div_lang.appendChild(span_class("lang", text(langname))); - div_lang.appendChild( - span_class("lin", [text(data[i].text)]) - ); - t.ui.lin.appendChild(div_lang); - } - } + function row(lang, lin) { + var langname = langpart(lang, t.grammar.name); + var btn = button(langname, function(){ + t.options.lin_action(lin,lang); }); + var c1 = th(btn); + var c2 = td(text(lin)); + var row = tr([c1,c2]); + return row; } + var args = { + tree: t.ast.toString() + }; + t.server.linearize(args, function(data){ + clear(t.ui.lin); + var tbody=empty("tbody"); + for (i in data) { + var lang = data[i].to; + if (t.languages.length < 1 || elem(lang, t.languages)) { + tbody.appendChild(row(lang, data[i].text)) + } + } + t.ui.lin.appendChild(wrap("table",tbody)); + }); } // diff --git a/src/www/syntax-editor/editor_online.js b/src/www/syntax-editor/editor_online.js new file mode 100644 index 000000000..8b7c48acd --- /dev/null +++ b/src/www/syntax-editor/editor_online.js @@ -0,0 +1,41 @@ +var server_options = { + // grammars_url: "http://www.grammaticalframework.org/grammars/", + // grammars_url: "http://localhost:41296/grammars/", +} +var editor_options = { + target: "editor", + // initial: { + // grammar: "http://localhost:41296/grammars/Phrasebook.pgf", + // startcat: "Proposition", + // languages: ["Eng","Swe","Ita"], + // abstr: "PropOpenDate (SuperlPlace TheMostExpensive School) Tomorrow", + // node_id: null + // }, + show: { + grammar_menu: true, + startcat_menu: true, + to_menu: true, + random_button: true + } +} +if(window.Minibar) // Minibar loaded? + editor_options.lin_action=function(s,langFrom) { + editor.shutdown(); + var minibar_options = { + target: "editor", + initial_grammar: editor.menu.ui.grammar_menu.value, // hmm + initial: { + from: langFrom, + input: s.split(" ") // is it that easy? + } + } + editor.minibar=new Minibar(server,minibar_options); + } +if(/^\?\/tmp\//.test(location.search)) { + var args=decodeURIComponent(location.search.substr(1)).split(" ") + if(args[0]) server_options.grammars_url=args[0]; +} +var server = pgf_online(server_options); +var editor = new Editor(server, editor_options); + +