/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
function CONFIG_CONFIG() {
    this.db_server = null;
    this.db_name = null;
    this.db_group = null;
    this.db_mask = null;

    this.experiment = "0-0";
    this.window = "0";
    
    this.frame_width = 0;
    this.frame_height = 0;
    
    this.aggregation = null;
    this.interpolate = null;
    this.show_marks = null;
    this.show_gaps = null;
}


function CONFIG_EXTRA() {
    this.format = null;
    this.resample = null;
    this.mask_mode = null;
}


CONFIG_CONFIG.prototype.Clone = function() {
    var res = new Object;
    for (i in this) {
	res[i] = this[i];
    }
    return res;
}

function CONFIG() {
    this.cfg = new CONFIG_CONFIG;
    this.cfg_extra = new CONFIG_EXTRA;
    
    this.open_module = null;
    
    this.win_width = 0;
    this.win_from = 0;
    this.win_to = 0;
    this.win_min = 0;
    this.win_max = 0;
    
    this.sel_from = 0;
    this.sel_to = 0;

    this.confirmed = 0;
    this.initializing = true;
    this.ready = false;

    this.objlist = new Array;
    
    this.history = new HISTORY(this.HistoryEvent(this));
}

CONFIG.prototype.RegisterUpdater = function(updater) {
    // DS: Multiple updaters support
    this.updater = updater;
}

CONFIG.prototype.Register = function(obj) {
    this.objlist.push(obj);
}

CONFIG.prototype.SetSource = function(server, db, group, mask) {
    this.cfg.db_server  = server;
    this.cfg.db_name = db;
    this.cfg.db_group = group;
    this.cfg.db_mask = mask;
}

CONFIG.prototype.SetInterval = function(from, to) {
    this.cfg.experiment = from + '-' + to;
}

CONFIG.prototype.SetWindow = function(width, from, to, miny, maxy) {
    if (width<0) {
	this.win_width = -1;
	
	if (miny != maxy) {
	    this.win_min = miny;
	    this.win_max = maxy;
	}
	if (from != to) {
	    this.win_from = from;
	    this.win_to = to;
	}
	
    	if ((this.win_from!=this.win_to)&&(this.win_min!=this.win_max)) {
	    this.cfg.window = this.win_from + '-' + this.win_to + ',' + this.win_min + ':' + this.win_max;
	} else if (this.win_min!=this.win_max) {
	    this.cfg.window = this.win_min + ':' + this.win_max;
	} else if (this.win_from!=this.win_to) {
	    this.cfg.window = this.win_from + '-' + this.win_to;
	} else {
	    this.win_width = 0;
	    this.cfg.window = 0;
	}
    } else {
	this.win_width = width;

	this.win_from = 0;
	this.win_to = 0;
	this.win_min = 0;
	this.win_max = 0;

	this.cfg.window = width;
    }
}

CONFIG.prototype.SetSelection = function(from, to) {
    if (typeof from == "undefined") {
	this.sel_from = 0;
	this.sel_to = 0;
    } else {
	this.sel_from = from;
	this.sel_to = to;
    }
}


CONFIG.prototype.SetExportSettings = function(format, resample, mask_mode) {
    if (format) this.cfg_extra.format = format;
    if (resample) this.cfg_extra.resample = resample;
    if (mask_mode) this.cfg_extra.mask_mode = mask_mode;
    
    this.updater.Notify("ExportSettings");
}


CONFIG.prototype.SetAggregationSettings = function(a, i, marks, gaps) {
    if (a) this.cfg.aggregation = a;
    if (i) this.cfg.interpolate = i;
    if (marks) this.cfg.show_marks = marks;
    if (gaps) this.cfg.show_gaps = gaps;
}


CONFIG.prototype.ParseWindow = function(winprm) {
    var win;
    
    if (typeof win == "undefined") win = this.cfg.window;
    else win = winprm;

    if (isNaN(win)) {
	this.win_width = -1;
	
	this.win_from = 0;
	this.win_to = 0;
	this.win_min = 0;
	this.win_max = 0;
	
	var err = false;
	var p = win.split(",");
	
	if ((p.legth < 1)||(p.length > 2)) err = true;
	else {
	    var process = function(self, prm) {

		var t = prm.split(":");

		if (t.length == 2) {
		    if ((isNaN(t[0]))||(isNaN(t[1]))) return true;
		    else {
			self.win_min = t[0];
			self.win_max = t[1];
		    }
		} else {
		    t = prm.split("-");

		    if ((t.length == 2)&&(t[0].length>0)) {
			if ((isNaN(t[0]))||(isNaN(t[1]))) return true;
			else {
			    self.win_from = t[0];
			    self.win_to = t[1];
			}
		    } else return true;
		}
		return false;
	    }
	    
	    err = process(this, p[0]);
	    if ((!err)&&(p.length == 2)) err = process(this, p[1]);
	}
		
	
	if (err) {
	    this.win_width = 0;
	    adeiReportError(translate("Invalid window \"%s\" is specified",win));
	}
    } else {
        this.win_width = win;

	this.win_from = 0;
	this.win_to = 0;
	this.win_min = 0;
	this.win_max = 0;
    }
    
    if (typeof win != "undefined") {
	this.SetWindow(this.win_width, this.win_from, this.win_to, this.min, this.max);
    }
}

CONFIG.prototype.ApplyConfig = function(save_on_apply, filter) {
    this.ParseWindow();

//    this.onConfirmation = this.UpdateAndSaveFunction(this, save_on_apply, filter);

    var len = this.objlist.length;
    for (var i = 0; i < len; i++) {
	var obj = this.objlist[i];
//	if (typeof obj.ReadConifg == "function") {	// DS: Comparison fails in Firefox
	if (obj.ReadConfig) {
		//confirm: true,
	    var opts = new Object({
		reset: true,
		success_cb: this.UpdateAndSaveFunction(this, save_on_apply, filter)
	    });

	    if (!this.ready) opts.reload = true;
	    
	    if (typeof filter != "undefined")
		eval("if (obj instanceof " + filter + ") obj.ReadConfig(opts);");
	    else 
		obj.ReadConfig(opts);
	}
    }

//    if (this.updater) this.updater.Update();
}


CONFIG.prototype.UpdateAndSaveFunction = function(self, save_on_apply, filter) {
    return function() {
	var len = self.objlist.length;
	for (var i = 0; i < len; i++) {
	    var obj = self.objlist[i];
	    if (obj.ApplyConfig) {
		if (typeof filter != "undefined")
		    eval("if (obj instanceof " + filter + ") obj.ApplyConfig(true);");
		else 
		    obj.ApplyConfig(true);
	    }
	}
	

	if (self.updater) self.updater.Update();
	if (save_on_apply) self.Save();
    }
}

/*
CONFIG.prototype.UpdateFunction = function(self) {
    return function() {
	if (self.updater) self.updater.Update();
    }
}
*/

CONFIG.prototype.SetupGeometry = function(node, y) {
	/* correction in IE required */
    if (typeof node == "object") {
	this.cfg.frame_width = node.offsetWidth;
	this.cfg.frame_height = node.offsetHeight;
    } else {
	this.cfg.frame_width = node;
	this.cfg.frame_height = y;
    }
}

/*
CONFIG.prototype.Setup = function(src, ivl, wnd) {
    with (this) {
	SetSource(src.db_server, src.db_name, src.db_group, src.db_mask);
	SetInterval(ivl.start, ivl.end);
	SetWindow(wnd.width, wnd.from, wnd.to, wnd.miny, wnd.maxy);
    }
}
*/


CONFIG.prototype.SetupModule = function(m) {
    this.open_module = m;
}


CONFIG.prototype.GetConfig = function() {
    return this.cfg;
}

CONFIG.prototype.GetJSON = function(addon) {
    if (typeof addon == "undefined") return Object.toJSON(this.cfg);
    else return Object.toJSON(Object.extend(this.cfg.Clone(), addon));
}

CONFIG.prototype.GetProps = function(window_type) {
    var res;

    //if (!(cfg instanceof CONFIG_CONFIG)) cfg = this.cfg;

    with (this.cfg) {
	var win = window;
	if (typeof window_type != "undefined") {
	    if (window_type > 0) {
		win = "0";
	    } else if (window_type < 0) {
		if (this.sel_from != this.sel_to) 
		    win = this.sel_from + "-" + this.sel_to;
		else 
		    adeiReportError("The selection is empty. Storing full window", "CONFIG");
	    }
	}
	
	if (db_server) {
	    res = "db_server=" + db_server + "&db_name=" + db_name + "&db_group=" + db_group + "&db_mask=" + db_mask + "&experiment=" + experiment + "&window=" + win;
	    if (this.cfg.aggregation) res += "&aggregation=" + this.cfg.aggregation;
	    if (this.cfg.interpolate) res += "&interpolate=" + this.cfg.interpolate;
	    if (this.cfg.show_marks) res += "&show_gaps=" + this.cfg.show_marks;
	    if (this.cfg.show_gaps) res += "&show_gaps=" + this.cfg.show_gaps;
	} else {
	    res = "no_source";
	}
    }

    with (this.cfg_extra) {
	if (format) res += "&format=" + format;
	if (resample) res += "&resample=" + resample;
	if (mask_mode) res += "&mask_mode=" + mask_mode;
    }
    
    return res;
}

CONFIG.prototype.ParseProps = function(props) {
    var done = false;
    var cfg = new CONFIG_CONFIG();
    var extra = new CONFIG_EXTRA();
    
    var vars = props.split("&");
    for (var i = 0; i < vars.length; i++) {
	var pair = vars[i].split("=",2);
	if (pair.length != 2) continue;
	
	if (typeof cfg[pair[0]] != "undefined") {
	    cfg[pair[0]] = pair[1];
	    done = true;
	}
	else if (typeof extra[pair[0]] != "undefined") {
	    extra[pair[0]] = pair[1];
	    done = true;
	}
    }
    
    if (done) return new Object({'cfg': cfg, 'extra': extra});
    return false;
}


CONFIG.prototype.Save = function() {
    if ((this.cfg)&&(this.history)) {
	this.history.Add(this.GetProps(), this.cfg.Clone());
    }
}

CONFIG.prototype.FixLocation = function(props, hid) {
    if ((this.cfg)&&(this.history)) {
	if (typeof props == "undefined") {
	    var props = this.history.GetProps();
	    
	    if (typeof hid == "undefined")
		var hid = this.history.GetID();
	}
	    
	var new_props = this.GetProps();
	if (props != new_props) this.history.ReplaceLocation(new_props, props, hid);
    }
}

CONFIG.prototype.ReplaceURL = function() {
    this.FixLocation();
}

CONFIG.prototype.ReplaceConfig = function() {
    if ((this.cfg)&&(this.history)) {
/*	
	history.back(); // prevent complains from history module???
	var props = this.history.GetProps();
	var new_props = this.GetProps();
	
    	this.history.Replace(props, new_props, this.cfg.Clone());*/
	adeiReportError(translate('ReplaceConfig is not supported, yet'));
    }
}

CONFIG.prototype.HistoryEvent = function (self) {
    return function (newLocation, historyData, historyID) {
	if ((!historyData)||(typeof historyData.Clone != "function")) {
	    //return self.Load();
	    return;
	}
	
	self.cfg = historyData.Clone();
        self.FixLocation(newLocation, historyID);
	self.ApplyConfig();
    }
}

CONFIG.prototype.Load = function() {
    var props = this.history.GetProps();
    if (props) {
	var cfg = this.ParseProps(props);
	if (cfg) {
	    this.cfg = cfg.cfg;
	    this.cfg_extra = cfg.extra;

	    adei.SetStatus(translate("Loading..."), 0);
	    this.ApplyConfig(true);
	}
    }
}

CONFIG.prototype.ApplyDataSource = function(srv, db, grp, mask, exp) {
    if (typeof srv != "undefined") {
	this.cfg.db_server = srv;
    } else this.cfg.db_server = null;

    if (typeof db != "undefined") {
	this.cfg.db_name = db;
    } else this.cfg.db_name  = null;
    
    if (typeof grp != "undefined") {
	this.cfg.db_group = grp;
    } else this.cfg.db_group  = null;

    if (typeof mask != "undefined") {
	this.cfg.db_mask = mask;
    } else this.cfg.db_mask  = null;

    if (typeof exp != "undefined") {
	this.cfg.experiment = exp;
	this.cfg.window = "0";
    } /* keeping otherwise */
    

    this.ApplyConfig(true, "SOURCE");
}

/*
CONFIG.prototype.RequestConfirmation = function(onConfirm) {
    if (this.onConfirmation) {
	if (this.onConfirmation == onConfirm) return 1;
	return 2;
    }
    this.onConfirmation = onConfirm;
    return 0;
}
*/

CONFIG.prototype.Confirm = function(me, val) {
/* 
	    BEWARE: 
	    SOURCE can confirm WINDOW with val=2 
	    INTERVAL can confirm WINDOW with val=1

    if (me instanceof SOURCE) this.confirmed |= (1<<val);
    else if (me instanceof INTERVAL) this.confirmed |= 4;
    else if (me instanceof WINDOW) this.confirmed |= 4;
*/

    if (me instanceof SOURCE) this.confirmed |= 1;

    if ((this.confirmed&1)==1) {
	this.confirmed = 0;

	if (this.initializing) {
	    this.ready = true;
	    this.initializing = false;

	    adei.SetStatus(translate("Ready..."), 0);
	    this.Load();
	} else if (this.onConfirmation) {
	    var func = this.onConfirmation;
	    this.onConfirmation = null;
	    func();
	}
    }
}

function configSetupModule(config, m) {
    config.SetupModule(m);
}