summaryrefslogtreecommitdiff
path: root/src/www/js
diff options
context:
space:
mode:
authorjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-11-27 10:09:40 +0000
committerjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-11-27 10:09:40 +0000
commitb3c1177f9ed5a3eaff378671fa119be1c605498e (patch)
tree6c32e5fc86fd45b91a934b8d0fc6da2cf236ab8a /src/www/js
parentdef375e58808ea431ef0d786ae0abb9de0a67155 (diff)
Syntax editor: improvements and fixes with grammar manager
Diffstat (limited to 'src/www/js')
-rw-r--r--src/www/js/grammar_manager.js26
-rw-r--r--src/www/js/support.js21
2 files changed, 31 insertions, 16 deletions
diff --git a/src/www/js/grammar_manager.js b/src/www/js/grammar_manager.js
index a2bf3b830..11d13d71c 100644
--- a/src/www/js/grammar_manager.js
+++ b/src/www/js/grammar_manager.js
@@ -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);
}
}
}
diff --git a/src/www/js/support.js b/src/www/js/support.js
index eefda3f8b..47753e768 100644
--- a/src/www/js/support.js
+++ b/src/www/js/support.js
@@ -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 ---------------------------------------------------------------- */