/dev/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/trunk

« back to all changes in this revision

Viewing changes to js/config.js

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function CONFIG_CONFIG() {
 
2
    this.db_server = null;
 
3
    this.db_name = null;
 
4
    this.db_group = null;
 
5
    this.db_mask = null;
 
6
 
 
7
    this.experiment = "0-0";
 
8
    this.window = "0";
 
9
    
 
10
    this.frame_width = 0;
 
11
    this.frame_height = 0;
 
12
}
 
13
 
 
14
 
 
15
function CONFIG_EXTRA() {
 
16
    this.format = null;
 
17
    this.resample = null;
 
18
    this.mask_mode = null;
 
19
}
 
20
 
 
21
 
 
22
CONFIG_CONFIG.prototype.Clone = function() {
 
23
    var res = new Object;
 
24
    for (i in this) {
 
25
        res[i] = this[i];
 
26
    }
 
27
    return res;
 
28
}
 
29
 
 
30
function CONFIG() {
 
31
    this.cfg = new CONFIG_CONFIG;
 
32
    this.cfg_extra = new CONFIG_EXTRA;
 
33
    
 
34
    this.open_module = null;
 
35
    
 
36
    this.win_width = 0;
 
37
    this.win_from = 0;
 
38
    this.win_to = 0;
 
39
    this.win_min = 0;
 
40
    this.win_max = 0;
 
41
    
 
42
    this.sel_from = 0;
 
43
    this.sel_to = 0;
 
44
 
 
45
    this.confirmed = 0;
 
46
    this.initializing = true;
 
47
 
 
48
    this.objlist = new Array;
 
49
    
 
50
    this.history = new HISTORY(this.HistoryEvent(this));
 
51
}
 
52
 
 
53
CONFIG.prototype.RegisterUpdater = function(updater) {
 
54
    // DS: Multiple updaters support
 
55
    this.updater = updater;
 
56
}
 
57
 
 
58
CONFIG.prototype.Register = function(obj) {
 
59
    this.objlist.push(obj);
 
60
}
 
61
 
 
62
CONFIG.prototype.SetSource = function(server, db, group, mask) {
 
63
    this.cfg.db_server  = server;
 
64
    this.cfg.db_name = db;
 
65
    this.cfg.db_group = group;
 
66
    this.cfg.db_mask = mask;
 
67
}
 
68
 
 
69
CONFIG.prototype.SetInterval = function(from, to) {
 
70
    this.cfg.experiment = from + '-' + to;
 
71
}
 
72
 
 
73
CONFIG.prototype.SetWindow = function(width, from, to, miny, maxy) {
 
74
    if (width<0) {
 
75
        this.win_width = -1;
 
76
        
 
77
        if (miny != maxy) {
 
78
            this.win_min = miny;
 
79
            this.win_max = maxy;
 
80
        }
 
81
        if (from != to) {
 
82
            this.win_from = from;
 
83
            this.win_to = to;
 
84
        }
 
85
        
 
86
        if ((this.win_from!=this.win_to)&&(this.win_min!=this.win_max)) {
 
87
            this.cfg.window = this.win_from + '-' + this.win_to + ',' + this.win_min + ':' + this.win_max;
 
88
        } else if (this.win_min!=this.win_max) {
 
89
            this.cfg.window = this.win_min + ':' + this.win_max;
 
90
        } else if (this.win_from!=this.win_to) {
 
91
            this.cfg.window = this.win_from + '-' + this.win_to;
 
92
        } else {
 
93
            this.win_width = 0;
 
94
            this.cfg.window = 0;
 
95
        }
 
96
    } else {
 
97
        this.win_width = width;
 
98
 
 
99
        this.win_from = 0;
 
100
        this.win_to = 0;
 
101
        this.win_min = 0;
 
102
        this.win_max = 0;
 
103
 
 
104
        this.cfg.window = width;
 
105
    }
 
106
}
 
107
 
 
108
CONFIG.prototype.SetSelection = function(from, to) {
 
109
    if (typeof from == "undefined") {
 
110
        this.sel_from = 0;
 
111
        this.sel_to = 0;
 
112
    } else {
 
113
        this.sel_from = from;
 
114
        this.sel_to = to;
 
115
    }
 
116
}
 
117
 
 
118
 
 
119
CONFIG.prototype.SetExportSettings = function(format, resample, mask_mode) {
 
120
    if (format) this.cfg_extra.format = format;
 
121
    if (resample) this.cfg_extra.resample = resample;
 
122
    if (mask_mode) this.cfg_extra.mask_mode = mask_mode;
 
123
    
 
124
    this.updater.Notify("ExportSettings");
 
125
}
 
126
 
 
127
 
 
128
 
 
129
CONFIG.prototype.ParseWindow = function(winprm) {
 
130
    var win;
 
131
    
 
132
    if (typeof win == "undefined") win = this.cfg.window;
 
133
    else win = winprm;
 
134
 
 
135
    if (isNaN(win)) {
 
136
        this.win_width = -1;
 
137
        
 
138
        this.win_from = 0;
 
139
        this.win_to = 0;
 
140
        this.win_min = 0;
 
141
        this.win_max = 0;
 
142
        
 
143
        var err = false;
 
144
        var p = win.split(",");
 
145
        
 
146
        if ((p.legth < 1)||(p.length > 2)) err = true;
 
147
        else {
 
148
            var process = function(self, prm) {
 
149
                var t = prm.split("-");
 
150
 
 
151
                if ((t.length == 2)&&(t[0].length>0)) {
 
152
                    if ((isNaN(t[0]))||(isNaN(t[1]))) return true;
 
153
                    else {
 
154
                        self.win_from = t[0];
 
155
                        self.win_to = t[1];
 
156
                    }
 
157
                } else {
 
158
                    t = prm.split(":");
 
159
 
 
160
                    if (t.length == 2) {
 
161
                        if ((isNaN(t[0]))||(isNaN(t[1]))) return true;
 
162
                        else {
 
163
                            self.win_min = t[0];
 
164
                            self.win_max = t[1];
 
165
                        }
 
166
                    } else return true;
 
167
                }
 
168
                return false;
 
169
            }
 
170
            
 
171
            err = process(this, p[0]);
 
172
            if ((!err)&&(p.length == 2)) err = process(this, p[1]);
 
173
        }
 
174
                
 
175
        
 
176
        if (err) {
 
177
            this.win_width = 0;
 
178
            alert("Invalid window: '" + win + "'");
 
179
        }
 
180
    } else {
 
181
        this.win_width = win;
 
182
 
 
183
        this.win_from = 0;
 
184
        this.win_to = 0;
 
185
        this.win_min = 0;
 
186
        this.win_max = 0;
 
187
    }
 
188
    
 
189
    if (typeof win != "undefined") {
 
190
        this.SetWindow(this.win_width, this.win_from, this.win_to, this.min, this.max);
 
191
    }
 
192
}
 
193
 
 
194
CONFIG.prototype.ApplyConfig = function(save_on_apply, filter) {
 
195
    this.ParseWindow();
 
196
 
 
197
    this.onConfirmation = this.UpdateAndSaveFunction(this, save_on_apply, filter);
 
198
 
 
199
    var len = this.objlist.length;
 
200
    for (var i = 0; i < len; i++) {
 
201
        var obj = this.objlist[i];
 
202
//      if (typeof obj.ReadConifg == "function") {      // DS: Comparison fails in Firefox
 
203
        if (obj.ReadConfig) {
 
204
            var opts = new Object({
 
205
                confirm: true,
 
206
                reset: true
 
207
            });
 
208
            
 
209
            if (typeof filter != "undefined")
 
210
                eval("if (obj instanceof " + filter + ") obj.ReadConfig(opts);");
 
211
            else 
 
212
                obj.ReadConfig(opts);
 
213
        }
 
214
    }
 
215
 
 
216
//    if (this.updater) this.updater.Update();
 
217
}
 
218
 
 
219
CONFIG.prototype.UpdateAndSaveFunction = function(self, save_on_apply, filter) {
 
220
    return function() {
 
221
        //alert(this.cfg.db_server + "-" + this.cfg.db_name);
 
222
 
 
223
        var len = self.objlist.length;
 
224
        for (var i = 0; i < len; i++) {
 
225
            var obj = self.objlist[i];
 
226
            if (obj.ApplyConfig) {
 
227
                if (typeof filter != "undefined")
 
228
                    eval("if (obj instanceof " + filter + ") obj.ApplyConfig(true);");
 
229
                else 
 
230
                    obj.ApplyConfig(true);
 
231
            }
 
232
        }
 
233
        
 
234
 
 
235
        if (self.updater) self.updater.Update();
 
236
        if (save_on_apply) self.Save();
 
237
    }
 
238
}
 
239
 
 
240
/*
 
241
CONFIG.prototype.UpdateFunction = function(self) {
 
242
    return function() {
 
243
        if (self.updater) self.updater.Update();
 
244
    }
 
245
}
 
246
*/
 
247
 
 
248
CONFIG.prototype.SetupGeometry = function(node, y) {
 
249
    if (typeof node == "object") {
 
250
        this.cfg.frame_width = node.offsetWidth - 5;
 
251
        this.cfg.frame_height = node.offsetHeight - 5;
 
252
    } else {
 
253
        this.cfg.frame_width = node - 5;
 
254
        this.cfg.frame_height = y - 5;
 
255
    }
 
256
}
 
257
 
 
258
/*
 
259
CONFIG.prototype.Setup = function(src, ivl, wnd) {
 
260
    with (this) {
 
261
        SetSource(src.db_server, src.db_name, src.db_group, src.db_mask);
 
262
        SetInterval(ivl.start, ivl.end);
 
263
        SetWindow(wnd.width, wnd.from, wnd.to, wnd.miny, wnd.maxy);
 
264
    }
 
265
}
 
266
*/
 
267
 
 
268
 
 
269
CONFIG.prototype.SetupModule = function(m) {
 
270
    this.open_module = m;
 
271
}
 
272
 
 
273
 
 
274
CONFIG.prototype.GetConfig = function() {
 
275
    return this.cfg;
 
276
}
 
277
 
 
278
CONFIG.prototype.GetJSON = function(addon) {
 
279
    if (typeof addon == "undefined") return Object.toJSON(this.cfg);
 
280
    else return Object.toJSON(Object.extend(this.cfg.Clone(), addon));
 
281
}
 
282
 
 
283
CONFIG.prototype.GetProps = function(window_type) {
 
284
    var res;
 
285
 
 
286
    //if (!(cfg instanceof CONFIG_CONFIG)) cfg = this.cfg;
 
287
 
 
288
    with (this.cfg) {
 
289
        var win = window;
 
290
        if (typeof window_type != "undefined") {
 
291
            if (window_type > 0) {
 
292
                win = "0";
 
293
            } else if (window_type < 0) {
 
294
                if (this.sel_from != this.sel_to) 
 
295
                    win = this.sel_from + "-" + this.sel_to;
 
296
                else 
 
297
                    adeiReportError("The selection is empty. Storing full window", "CONFIG");
 
298
            }
 
299
        }
 
300
        
 
301
        if (db_server) {
 
302
            res = "db_server=" + db_server + "&db_name=" + db_name + "&db_group=" + db_group + "&db_mask=" + db_mask + "&experiment=" + experiment + "&window=" + win;
 
303
        } else {
 
304
            res = "no_source";
 
305
        }
 
306
    }
 
307
 
 
308
    with (this.cfg_extra) {
 
309
        if (format) res += "&format=" + format;
 
310
        if (resample) res += "&resample=" + resample;
 
311
        if (mask_mode) res += "&mask_mode=" + mask_mode;
 
312
    }
 
313
    
 
314
    return res;
 
315
}
 
316
 
 
317
CONFIG.prototype.ParseProps = function(props) {
 
318
    var done = false;
 
319
    var cfg = new CONFIG_CONFIG();
 
320
    var extra = new CONFIG_EXTRA();
 
321
    
 
322
    var vars = props.split("&");
 
323
    for (var i = 0; i < vars.length; i++) {
 
324
        var pair = vars[i].split("=",2);
 
325
        if (pair.length != 2) continue;
 
326
        
 
327
        if (typeof cfg[pair[0]] != "undefined") {
 
328
            cfg[pair[0]] = pair[1];
 
329
            done = true;
 
330
        }
 
331
        else if (typeof extra[pair[0]] != "undefined") {
 
332
            extra[pair[0]] = pair[1];
 
333
            done = true;
 
334
        }
 
335
    }
 
336
    
 
337
    if (done) return new Object({'cfg': cfg, 'extra': extra});
 
338
    return false;
 
339
}
 
340
 
 
341
 
 
342
CONFIG.prototype.Save = function() {
 
343
    if ((this.cfg)&&(this.history)) {
 
344
        this.history.Add(this.GetProps(), this.cfg.Clone());
 
345
    }
 
346
}
 
347
 
 
348
CONFIG.prototype.FixLocation = function(props) {
 
349
    if ((this.cfg)&&(this.history)) {
 
350
        if (typeof props == "undefined")
 
351
            var props = this.history.GetProps();
 
352
            
 
353
        var new_props = this.GetProps();
 
354
        
 
355
        if (props != new_props) {
 
356
            var orig = window.location.toString();
 
357
            var replacement;
 
358
            if (orig.match('#')) {
 
359
                replacement = orig.replace(/#.*$/, '#' + new_props);
 
360
                this.history.AddSynonim(props, new_props);
 
361
            } else replacement = '#' + new_props;
 
362
            window.location.replace(replacement);
 
363
        }
 
364
    }
 
365
}
 
366
 
 
367
CONFIG.prototype.ReplaceURL = function() {
 
368
    this.FixLocation();
 
369
}
 
370
 
 
371
CONFIG.prototype.ReplaceConfig = function() {
 
372
    if ((this.cfg)&&(this.history)) {
 
373
/*      
 
374
        history.back(); // prevent complains from history module???
 
375
        var props = this.history.GetProps();
 
376
        var new_props = this.GetProps();
 
377
        
 
378
        this.history.Replace(props, new_props, this.cfg.Clone());*/
 
379
        alert('not supported, yet');
 
380
    }
 
381
}
 
382
 
 
383
CONFIG.prototype.HistoryEvent = function (self) {
 
384
    return function (newLocation, historyData) {
 
385
        if ((!historyData)||(typeof historyData.Clone != "function")) {
 
386
            //return self.Load();
 
387
            return;
 
388
        }
 
389
        
 
390
        self.cfg = historyData.Clone();
 
391
        self.FixLocation(newLocation);
 
392
        self.ApplyConfig();
 
393
    }
 
394
}
 
395
 
 
396
CONFIG.prototype.Load = function() {
 
397
    var props = this.history.GetProps();
 
398
    if (props) {
 
399
        var cfg = this.ParseProps(props);
 
400
        if (cfg) {
 
401
            this.cfg = cfg.cfg;
 
402
            this.cfg_extra = cfg.extra;
 
403
//          this.Save();
 
404
            this.ApplyConfig(true);
 
405
        }
 
406
    }
 
407
}
 
408
 
 
409
CONFIG.prototype.ApplyDataSource = function(srv, db, grp, mask, exp) {
 
410
    if (typeof srv != "undefined") {
 
411
        this.cfg.db_server = srv;
 
412
    } else this.cfg.db_server = null;
 
413
 
 
414
    if (typeof db != "undefined") {
 
415
        this.cfg.db_name = db;
 
416
    } else this.cfg.db_name  = null;
 
417
    
 
418
    if (typeof grp != "undefined") {
 
419
        this.cfg.db_group = grp;
 
420
    } else this.cfg.db_group  = null;
 
421
 
 
422
    if (typeof mask != "undefined") {
 
423
        this.cfg.db_mask = mask;
 
424
    } else this.cfg.db_mask  = null;
 
425
 
 
426
    if (typeof exp != "undefined") {
 
427
        this.cfg.experiment = exp;
 
428
        this.cfg.window = "0";
 
429
    } /* keeping otherwise */
 
430
    
 
431
 
 
432
    this.ApplyConfig(true, "SOURCE");
 
433
}
 
434
 
 
435
 
 
436
CONFIG.prototype.Confirm = function(me, val) {
 
437
        /* BEWARE: 
 
438
            SOURCE can confirm WINDOW with val=2 
 
439
            INTERVAL can confirm WINDOW with val=1
 
440
        */
 
441
    if (me instanceof SOURCE) this.confirmed |= (1<<val);
 
442
    else if (me instanceof INTERVAL) this.confirmed |= 4;
 
443
    else if (me instanceof WINDOW) this.confirmed |= 4;
 
444
 
 
445
    if ((this.confirmed&7)==7) {
 
446
        this.confirmed = 0;
 
447
 
 
448
        if (this.initializing) {
 
449
            this.initializing = false;
 
450
            this.Load();
 
451
        } else if (this.onConfirmation) {
 
452
            var func = this.onConfirmation;
 
453
            this.onConfirmation = null;
 
454
            func();
 
455
        }
 
456
    }
 
457
}
 
458
 
 
459
function configSetupModule(config, m) {
 
460
    config.SetupModule(m);
 
461
}
 
462