1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/* --- Grammar Manager object ----------------------------------------------- */
/*
This object stores the state for:
- grammar
- startcat
- languages
Hooks which actions can be hooked to:
- onload
- change_grammar
- change_startcat
- change_languages
*/
function GrammarManager(server,opts) {
var t = this;
/* --- Configuration ---------------------------------------------------- */
// default values
this.options = {
initial: {}
};
this.actions = {
onload: [
function(gm){ debug("default action: onload"); }
],
change_grammar: [
function(grammar){ debug("default action: change grammar"); }
],
change_startcat: [
function(startcat){ debug("default action: change startcat"); }
],
change_languages: [
function(languages){ debug("default action: change languages"); }
]
}
// Apply supplied options
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.startcat = null; // current startcat
this.languages = this.options.initial.languages || [];
// current languages (empty means all langs)
/* --- Main program, this gets things going ----------------------------- */
this.init=function(){
this.server.get_grammarlists(bind(this.onload,this));
}
this.init();
}
//
//GrammarManager.prototype.update_grammar_list=function(dir,grammar_names,dir_count) {
GrammarManager.prototype.onload=function(dir,grammar_names,dir_count) {
var t=this;
t.grammars=[];
t.grammar_dirs=[];
t.grammar_dirs.push(dir);
t.grammars=t.grammars.concat(grammar_names.map(function(g){return dir+g}));
var grammar0=t.options.initial.grammar || t.grammars[0];
t.change_grammar(grammar0);
// Execute hooked actions
t.run_actions("onload",dir,grammar_names,dir_count);
}
/* --- Registering / unregistering actions to hooks ------------------------- */
GrammarManager.prototype.register_action=function(hook,action) {
var hookring = this.actions[hook];
hookring.push(action);
}
GrammarManager.prototype.unregister_action=function(hook,action) {
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.actions[hook];
for (f in acts) {
acts[f](arg1,arg2,arg3);
}
}
/* --- Grammar -------------------------------------------------------------- */
// API
GrammarManager.prototype.change_grammar=function(grammar_url) {
var t=this;
t.server.switch_to_other_grammar(grammar_url, function() {
t.server.grammar_info(function(grammar){
// Set internal state
t.grammar = grammar;
// Call internal functions
t.update_startcat(grammar);
t.update_language_list(grammar);
// Execute hooked actions
t.run_actions("change_grammar",grammar);
});
});
}
/* --- Start category ------------------------------------------------------- */
// Internal
// Sets default startcat for grammar
GrammarManager.prototype.update_startcat=function(grammar) {
var t=this;
var cats=grammar.categories;
var startcat0 = t.options.initial.startcat;
if (elem(startcat0, cats))
t.startcat = startcat0;
else
t.startcat = grammar.startcat;
}
// API
GrammarManager.prototype.change_startcat=function(startcat) {
var t = this;
// Set internal state
t.startcat = startcat;
// Call internal functions
// ...
// Execute hooked actions
t.run_actions("change_startcat",startcat);
}
/* --- Languages ------------------------------------------------------------ */
// Internal
// Sets default languages for grammar
GrammarManager.prototype.update_language_list=function(grammar) {
var t = this;
function langpart(conc,abs) { // langpart("FoodsEng","Foods") == "Eng"
return hasPrefix(conc,abs) ? conc.substr(abs.length) : conc;
}
// 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; // "PhrasebookEng"
if(!hasPrefix(ln,"Disamb")) {
var lp=langpart(ln,grammar.name); // "Eng"
if (elem(lp, t.options.initial.languages)) {
t.languages.push(ln);
}
}
}
}
// API
GrammarManager.prototype.change_languages=function(languages) {
var t = this;
// Set internal state
t.languages = languages;
// Call internal functions
// ...
// Execute hooked actions
t.run_actions("change_languages",languages);
}
|