mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Syntax editor: improvements and fixes with grammar manager
This commit is contained in:
@@ -20,7 +20,7 @@ function GrammarManager(server,opts) {
|
||||
this.options = {
|
||||
initial: {}
|
||||
};
|
||||
this.hooks = {
|
||||
this.actions = {
|
||||
onload: [
|
||||
function(gm){ debug("default action: onload"); }
|
||||
],
|
||||
@@ -36,13 +36,13 @@ function GrammarManager(server,opts) {
|
||||
}
|
||||
|
||||
// Apply supplied options
|
||||
// if(opts) for(var o in opts) this.options[o]=opts[o];
|
||||
if(opts) for(var o in opts) this.options[o]=opts[o];
|
||||
|
||||
/* --- Client state initialisation -------------------------------------- */
|
||||
this.server = server;
|
||||
this.grammar = null; // current grammar
|
||||
// this.grammars=[];
|
||||
// this.grammar_dirs=[];
|
||||
this.grammars=[];
|
||||
this.grammar_dirs=[];
|
||||
this.startcat = null; // current startcat
|
||||
this.languages = []; // current languages (empty means all langs)
|
||||
|
||||
@@ -71,17 +71,23 @@ GrammarManager.prototype.onload=function(dir,grammar_names,dir_count) {
|
||||
/* --- Registering / unregistering actions to hooks ------------------------- */
|
||||
|
||||
GrammarManager.prototype.register_action=function(hook,action) {
|
||||
this.hooks[hook].push(action);
|
||||
var hookring = this.actions[hook];
|
||||
hookring.push(action);
|
||||
}
|
||||
|
||||
GrammarManager.prototype.unregister_action=function(hook,action) {
|
||||
// TODO!
|
||||
var hookring = this.actions[hook];
|
||||
for (var f=0; f < hookring.length; f++) {
|
||||
if (hookring[f] == action) {
|
||||
hookring = Array.remove(hookring, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Execute actions for a given hook
|
||||
// TODO: any number of arguments
|
||||
GrammarManager.prototype.run_actions=function(hook,arg1,arg2,arg3) {
|
||||
var acts = this.hooks[hook];
|
||||
var acts = this.actions[hook];
|
||||
for (f in acts) {
|
||||
acts[f](arg1,arg2,arg3);
|
||||
}
|
||||
@@ -147,11 +153,11 @@ GrammarManager.prototype.update_language_list=function(grammar) {
|
||||
// Replace the options in the menu with the languages in the grammar
|
||||
var langs=grammar.languages;
|
||||
for(var i=0; i<langs.length; i++) {
|
||||
var ln=langs[i].name;
|
||||
var ln=langs[i].name; // "PhrasebookEng"
|
||||
if(!hasPrefix(ln,"Disamb")) {
|
||||
var lp=langpart(ln,grammar.name);
|
||||
var lp=langpart(ln,grammar.name); // "Eng"
|
||||
if (elem(lp, t.options.initial.languages)) {
|
||||
t.languages.push(ln); // or lp?
|
||||
t.languages.push(ln);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,21 @@ 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);
|
||||
// };
|
||||
// }
|
||||
if(!Array.clone) {
|
||||
Array.clone = function(arg) {
|
||||
return arg.slice(0);
|
||||
};
|
||||
}
|
||||
|
||||
// Array Remove - By John Resig (MIT Licensed)
|
||||
// http://ejohn.org/blog/javascript-array-remove/if(!Array.remove) {
|
||||
if(!Array.remove) {
|
||||
Array.remove = function(arg, from, to) {
|
||||
var rest = arg.slice((to || from) + 1 || arg.length);
|
||||
arg.length = from < 0 ? arg.length + from : from;
|
||||
return arg.push.apply(arg, rest);
|
||||
};
|
||||
}
|
||||
|
||||
/* --- JSONP ---------------------------------------------------------------- */
|
||||
|
||||
|
||||
@@ -13,21 +13,21 @@ An improved version of the [old syntax editor][1].
|
||||
|
||||
## Available startup options
|
||||
|
||||
var editor_options = {
|
||||
target: "editor",
|
||||
initial: {
|
||||
grammar: "http://localhost:41296/grammars/Foods.pgf",
|
||||
startcat: "Kind",
|
||||
languages: ["Eng","Swe","Mlt"],
|
||||
abstr: "Pred (That Fish) Expensive"
|
||||
},
|
||||
show: {
|
||||
grammar_menu: true,
|
||||
startcat_menu: true,
|
||||
to_menu: true,
|
||||
random_button: true
|
||||
}
|
||||
}
|
||||
|Options|Description|Default|
|
||||
|:------|:----------|:------|
|
||||
|target | |"editor"|
|
||||
|initial.grammar|Initial grammar URL, e.g. `"http://localhost:41296/grammars/Foods.pgf"`|-|
|
||||
|initial.startcat|Initial startcat|-|
|
||||
|initial.languages|Initial linearisation languages, e.g. `["Eng","Swe","Mlt"]`|-|
|
||||
|initial.abstr|Initial abstract tree (as string), e.g. `"Pred (That Fish) Expensive"`|-|
|
||||
|show.grammar_menu|Show grammar menu?|true|
|
||||
|show.startcat_menu|Show start category menu?|true|
|
||||
|show.to_menu|Show languages menu?|true|
|
||||
|show.random_button|Show random button?|true|
|
||||
|
||||
## Example usage
|
||||
|
||||
See `editor.html` and `editor_online.js`.
|
||||
|
||||
## TODO
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ function NodeID(x) {
|
||||
switch (typeof x) {
|
||||
case "number": this.id = [x]; break;
|
||||
case "string": this.id = map(function(s){return parseInt(s)}, x.split(",")); break;
|
||||
case "object": this.id = x.get().slice(); break; // another NodeID
|
||||
case "object": this.id = Array.clone(x.get()); break; // another NodeID
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ function AST(fun, cat) {
|
||||
|
||||
// set tree at given id
|
||||
this._setSubtree = function(id, subtree) {
|
||||
var lid = id.get().slice(); // clone NodeID array
|
||||
var lid = Array.clone(id.get()); // clone NodeID array
|
||||
var node = this.root;
|
||||
|
||||
if (lid.length==1)
|
||||
@@ -124,7 +124,7 @@ function AST(fun, cat) {
|
||||
switch (typeof id) {
|
||||
case "number": lid = [id]; break;
|
||||
case "string": lid = id.split(","); break;
|
||||
case "object": lid = id.get().slice(); break; // clone NodeID array
|
||||
case "object": lid = Array.clone(id.get()); break; // clone NodeID array
|
||||
}
|
||||
var node = this.root;
|
||||
if (lid[0] == 0) lid.shift();
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<h2>Syntax Editor</h2>
|
||||
<div id="minibar"></div>
|
||||
<div id="editor"></div>
|
||||
<noscript>This page doesn't works unless JavaScript is enabled.</noscript>
|
||||
<noscript>This page doesn't work unless JavaScript is enabled.</noscript>
|
||||
|
||||
<hr />
|
||||
<small class="modtime">
|
||||
|
||||
@@ -1,20 +1,4 @@
|
||||
/* --- Main Editor object --------------------------------------------------- */
|
||||
/* When creating the object, stuff gets called in this order:
|
||||
new EditorMenu
|
||||
server.get_grammarlists
|
||||
EditorMenu.show_grammarlist
|
||||
EditorMenu.change_grammar
|
||||
server.switch_to_other_grammar
|
||||
server.get_grammar_info
|
||||
EditorMenu.update_startcat_menu
|
||||
EditorMenu.update_language_menu
|
||||
Editor.change_grammar
|
||||
Editor.get_grammar_constructors
|
||||
Editor.start_fresh
|
||||
Editor.update_current_node();
|
||||
Editor.redraw_tree();
|
||||
Editor.get_refinements();
|
||||
*/
|
||||
function Editor(gm,opts) {
|
||||
var t = this;
|
||||
/* --- Configuration ---------------------------------------------------- */
|
||||
@@ -64,11 +48,6 @@ function Editor(gm,opts) {
|
||||
/* --- Register Grammar Manager hooks ----------------------------------- */
|
||||
this.gm.register_action("change_grammar",function(grammar){
|
||||
debug("Editor: change grammar");
|
||||
var startcat0 = t.options.initial.startcat;
|
||||
if (elem(startcat0, grammar.categories))
|
||||
t.startcat = startcat0;
|
||||
else
|
||||
t.startcat = null;
|
||||
t.get_grammar_constructors(bind(t.start_fresh,t));
|
||||
});
|
||||
this.gm.register_action("change_startcat",function(startcat){
|
||||
@@ -124,19 +103,19 @@ Editor.prototype.get_startcat=function() {
|
||||
}
|
||||
|
||||
// TODO
|
||||
Editor.prototype.initialize_from=function(opts) {
|
||||
var t=this;
|
||||
if (opts.startcat)
|
||||
t.options.initial_startcat=opts.startcat;
|
||||
t.change_grammar();
|
||||
if (opts.abstr)
|
||||
t.import_ast(opts.abstr);
|
||||
}
|
||||
// Editor.prototype.initialize_from=function(opts) {
|
||||
// var t=this;
|
||||
// if (opts.startcat)
|
||||
// t.options.initial_startcat=opts.startcat;
|
||||
// t.change_grammar();
|
||||
// if (opts.abstr)
|
||||
// t.import_ast(opts.abstr);
|
||||
// }
|
||||
|
||||
// Called after changing grammar or startcat
|
||||
Editor.prototype.start_fresh=function () {
|
||||
var t = this;
|
||||
t.ast = new AST(null, t.get_startcat());
|
||||
t.ast = new AST(null, t.get_startcat());
|
||||
if (t.options.initial.abstr) {
|
||||
t.import_ast(t.options.initial.abstr);
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ EditorMenu.prototype.hook_onload=function(dir,grammar_names,dir_count) {
|
||||
appendChildren(t.ui.grammar_menu, map(opt, grammar_names));
|
||||
function pick_first_grammar() {
|
||||
if(t.timeout) clearTimeout(t.timeout),t.timeout=null;
|
||||
var grammar0=t.options.initial.grammar;
|
||||
var grammar0=t.gm.options.initial.grammar;
|
||||
if(!grammar0) grammar0=t.grammars[0];
|
||||
t.ui.grammar_menu.value=grammar0;
|
||||
// t.change_grammar();
|
||||
@@ -139,16 +139,11 @@ EditorMenu.prototype.update_startcat_menu=function(grammar) {
|
||||
menu.innerHTML="";
|
||||
var cats=grammar.categories;
|
||||
for(var cat in cats) menu.appendChild(option(cats[cat],cats[cat]))
|
||||
// var startcat=this.local.get("startcat") || grammar.startcat;
|
||||
var startcat0 = t.options.initial.startcat;
|
||||
var startcat0 = t.gm.options.initial.startcat;
|
||||
if (elem(startcat0, cats))
|
||||
menu.value = startcat0;
|
||||
else
|
||||
menu.value = grammar.startcat;
|
||||
// else {
|
||||
// insertFirst(menu,option("Default",""));
|
||||
// menu.value="";
|
||||
// }
|
||||
}
|
||||
|
||||
/* --- Langugage (to) menu -------------------------------------------------- */
|
||||
@@ -168,9 +163,8 @@ EditorMenu.prototype.update_language_menu=function(menu,grammar) {
|
||||
if(!hasPrefix(ln,"Disamb")) {
|
||||
var lp=langpart(ln,grammar.name);
|
||||
var opt=option(lp,ln);
|
||||
if (elem(lp, t.options.initial.languages)) {
|
||||
if (elem(lp, t.gm.options.initial.languages)) {
|
||||
opt.selected=true;
|
||||
t.editor.languages.push(opt.value);
|
||||
}
|
||||
menu.appendChild(opt);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,9 @@ var server_options = {
|
||||
}
|
||||
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"
|
||||
// },
|
||||
initial: {
|
||||
// abstr: "PropOpenDate (SuperlPlace TheMostExpensive School) Tomorrow"
|
||||
},
|
||||
show: {
|
||||
grammar_menu: true,
|
||||
startcat_menu: true,
|
||||
|
||||
Reference in New Issue
Block a user