/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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
function WINDOW_CONFIG() {
    this.width = 0;
/*    
    this.start = 0;
    this.end = 0;
    this.miny = 0;
    this.maxy = 0;
*/
}


function WINDOW(win_sel_id, start_id, end_id, ymin_id, ymax_id) {
    this.cfg = new WINDOW_CONFIG;
    this.props =  null;
    this.source_winsel = new SELECT(win_sel_id,  windowUpdateWidth, this);

    this.source_winsel.SetupSource(adei.GetSelectService("window_modes"), "window_width");

    this.sin = document.getElementById(start_id);
    if (!this.sin) alert("Custom window start time input field (\"" + start_id + "\") is not found");
    this.ein = document.getElementById(end_id);
    if (!this.ein) alert("Custom window end time input field (\"" + end_id + "\") is not found");
    this.minin = document.getElementById(ymin_id);
    if (!this.minin) alert("Custom window ymin input field (\"" + ymin_id + "\") is not found");
    this.maxin = document.getElementById(ymax_id);
    if (!this.maxin) alert("Custom window ymax input field (\"" + ymax_id + "\") is not found");
}

WINDOW.prototype.DisableControls = function() {
    this.source_winsel.Disable();
}

WINDOW.prototype.EnableControls = function() {
    this.source_winsel.Enable();
}


WINDOW.prototype.SetConfig = function(config) {
    this.config = config;
}

WINDOW.prototype.SetUpdater = function(updater) {
    this.updater = updater;
}


WINDOW.prototype.RegisterGraph = function(graph) {
    // DS. Provide support for multiple attached graphs
    if (!this.graph) {
	this.graph = graph;
	if (!graph.window) graph.AttachWindow(this);
    }
}

WINDOW.prototype.Update = function(props, opts) {
    if (opts) {
	if (opts.reload) {
	    this.minval = false;
	    this.source_winsel.Update(props, opts, opts?opts.window_width:null);
	} else if ((opts.window_width)||(opts.reset)) {
	    this.UpdateWidth(opts.window_width, opts);
	}
    }
}

WINDOW.prototype.UpdateWidth = function (value, opts) {
    if (typeof value == "undefined") {
	value = this.source_winsel.GetValue();
    }

    if ((opts)&&(opts.confirmed)) {
        if (!this.minval) {
	    // Setting minimal standard window
	    var idx = this.source_winsel.GetLastIndex();
	    this.minval = this.source_winsel.GetValue(idx - 1);
	}
    
        var reset_custom = true;
    
	if ((opts)&&(opts.window == "auto")) {
	    this.SetCustomWindow(0,0,0,0);
	    reset_custom = false;
	}
    
        var css;
	var show = false;
    
    
	if (value < 0) {
	    if (this.cfg.width >= 0) {
		if ((reset_custom)&&((!opts)||(!opts.keep_custom))) {
		    this.sin.value = '';
		    this.ein.value = '';
		    this.minin.value = '';
		    this.maxin.value = '';
		}
	    
		cssShowClass('.hide_window_custom');
		
		if (!this.module_on_display) this.fix_hidden = true;


		if (typeof this.geometry_cb == "function") {
		    this.geometry_cb(this.geometry_cbattr);
		}
	    }
	} else {
	    if (this.cfg.width < 0) {
		cssHideClass('.hide_window_custom');

		if (!this.module_on_display) this.fix_hidden = true;
	    }
	    
	
	    if (this.cfg.width != value) show = true;
	}

	this.cfg.width = value;

	if ((show)&&(opts.apply)) this.Apply();
    } else if (((opts)&&(opts.reload))||(this.cfg.width != value)||(opts.window)) {
	if (typeof opts == "undefined") opts = { apply: true };
	this.source_winsel.UpdateChilds(null, opts, value, this.cfg.width);
    }
}

WINDOW.prototype.UpdateRange = function() {
    if (this.graph) {
	this.graph.Clear();
    }
}

WINDOW.prototype.ApplyConfig = function(subcall) {
    if (this.config) {
	var sin, ein;
	var min, max;
	
	var width = this.cfg.width;
	
	if (width<0) {
	    var tmp = this.sin.value;
	    if (tmp) sin = adeiDateParse(tmp);
	    else sin = 0;
	    
	    tmp = this.ein.value;
	    if (tmp) ein = adeiDateParse(tmp);
	    else ein = 0;

	    tmp = this.minin.value;
	    if (tmp) minin = tmp;
	    else minin = 0;
	    
	    tmp = this.maxin.value;
	    if (tmp) maxin = tmp;
	    else maxin = 0;
	} else {
	    sin = 0; ein = 0;
	    minin = 0; maxin = 0;
	}
	
	this.config.SetWindow(width, sin, ein, minin, maxin);

	if (typeof subcall == "undefined") {
//	    alert('save');
	    this.config.Save();
	}
    }

    if (this.graph) this.graph.Clear();
}

WINDOW.prototype.ReadConfig = function(opts) {
    opts.window = "auto";
    opts.window_width = this.config.win_width;
//    this.SetCustomWindow(0,0,0,0);
}

WINDOW.prototype.Apply = function() {
    this.ApplyConfig();
    if (this.updater) this.updater.Update();
}

WINDOW.prototype.ResetX = function() {
	// We are reseting Y as well!
    this.source_winsel.SetValue(0);
    this.UpdateWidth(0);
}

WINDOW.prototype.ResetY = function() {
    this.config.win_min = 0;
    this.config.win_max = 0;

    this.SetCustomWindow(0,0,0,0);
    this.Apply();
}

WINDOW.prototype.SetCustomWindow = function (from, to, miny, maxy) {
    if ((from)||(to)||(miny)||(maxy)) {
	if ((from)||(to)) {
	    this.config.SetSelection(from, to);

	    this.sin.value = adeiDateFormat(from, adeiMathPreciseSubstract(to, from));
	    this.ein.value = adeiDateFormat(to, adeiMathPreciseSubstract(to, from));
	    
	} else {
	    this.config.SetSelection();
	    
	    if ((this.config.win_from)&&(this.config.win_to)&&(this.config.win_from!=this.config.win_to)) {
		this.sin.value = adeiDateFormat(this.config.win_from, this.config.win_to - this.config.win_from);
		this.ein.value = adeiDateFormat(this.config.win_to, this.config.win_to - this.config.win_from);
	    } else /*if (this.cfg.width >= 0)*/ {
		this.sin.value = adeiDateFormat(this.graph.xmin, this.graph.xmax - this.graph.xmin);
		this.ein.value = adeiDateFormat(this.graph.xmax, this.graph.xmax - this.graph.xmin);
	    } /*else {
	        this.sin.value = '';
		this.ein.value = '';
	    }*/
	}

	if ((miny)||(maxy)) {
	    this.minin.value = isNaN(miny)?miny:miny.toPrecision(7);
	    this.maxin.value = isNaN(maxy)?maxy:maxy.toPrecision(7);
	} else {
	    if (/*(this.config.win_min)&&(this.config.win_max)&&*/(this.config.win_min!=this.config.win_max)) {
	        this.minin.value = this.config.win_min;
		this.maxin.value = this.config.win_max;
	    } else {
	        this.minin.value = '';
		this.maxin.value = '';
	    }
	}

	this.UpdateWidth(-1, new Object({keep_custom: true}));
	this.source_winsel.SetValue(-1);
    } else {
	if (!this.config) return;
	
	this.config.SetSelection();

	if (this.config.win_width<0) {
	    if ((this.config.win_from)&&(this.config.win_to)&&(this.config.win_from!=this.config.win_to)) {
		this.sin.value = adeiDateFormat(this.config.win_from, this.config.win_to - this.config.win_from);
		this.ein.value = adeiDateFormat(this.config.win_to, this.config.win_to - this.config.win_from);
	    } else {
	        this.sin.value = '';
		this.ein.value = '';
	    }
	    
	    if (/*(this.config.win_min)&&(this.config.win_max)&&*/(this.config.win_min!=this.config.win_max)) {
	        this.minin.value = this.config.win_min;
		this.maxin.value = this.config.win_max;
	    } else {
	        this.minin.value = '';
		this.maxin.value = '';
	    }
	} else {
	    this.sin.value = '';
	    this.ein.value = '';
	    this.minin.value = '';
	    this.maxin.value = '';

	    this.UpdateWidth(this.config.win_width, new Object({keep_custom: true}));
	    this.source_winsel.SetValue(this.config.win_width);
	}
    }
}

WINDOW.prototype.Lock = function() {
    if ((this.graph)&&(this.cfg.width >= 0)) {
	var xmin = this.graph.xmin;
	var xmax = this.graph.xmax;
	if ((xmin)&&(xmax)) {
	    this.SetCustomWindow(xmin, xmax, 0, 0);
	    this.Apply();
	}
    }
}


WINDOW.prototype.MoveUp = function() {
    if (this.graph.yzoom) {
	var step_size = this.graph.ysize / adei.cfg.step_ratio;
	var min = this.graph.ymin + step_size;
	var max = this.graph.ymax + step_size;

	this.SetCustomWindow(0, 0, min, max);
    }
    this.Apply();    
}

WINDOW.prototype.MoveDown = function() {
    if (this.graph.yzoom) {
	var step_size = this.graph.ysize / adei.cfg.step_ratio;
	var min = this.graph.ymin - step_size;
	var max = this.graph.ymax - step_size;

	this.SetCustomWindow(0, 0, min, max);
    }
    this.Apply();    
}


WINDOW.prototype.MoveLeft = function() {
    if (this.graph.xmin > this.graph.imin) {
	var step_size = this.graph.xsize /  adei.cfg.step_ratio;
	var from = adeiMathPreciseSubstract(this.graph.xmin, step_size);

	if (from < this.graph.imin) {
	    step_size = adeiMathPreciseSubstract(this.graph.xmin, this.graph.imin);
	    from = this.graph.imin;
	}

	var to = adeiMathPreciseSubstract(this.graph.xmax, step_size);

	this.SetCustomWindow(from, to, 0, 0);
    }
    
    this.Apply();    
}

WINDOW.prototype.MoveRight = function() {
    if (this.graph.xmax < this.graph.imax) {
	var step_size = this.graph.xsize /  adei.cfg.step_ratio;
	var to = adeiMathPreciseAdd(this.graph.xmax, step_size);

	if ((this.graph.imax)&&(to > this.graph.imax)) {
	    step_size = adeiMathPreciseSubstract(this.graph.imax, this.graph.xmax);
	    to = this.graph.imax;
	}

	var from = adeiMathPreciseAdd(this.graph.xmin, step_size);

	this.SetCustomWindow(from, to, 0, 0);
    }

    this.Apply();    
}


WINDOW.prototype.Center = function(x, y) {
    var min = 0, max = 0;
    
    if (this.graph.yzoom) {
	var size = this.graph.ysize / 2;
	min = y - size;
	max = y + size;
    }

    var size = this.graph.xsize / 2;
    from = adeiMathPreciseSubstract(x, size);
    to = adeiMathPreciseAdd(x, size);
    
    if (from < this.graph.imin) {
	from = this.graph.imin;
	to = adeiMathPreciseAdd(x, adeiMathPreciseSubstract(x, this.graph.imin));
    } else if ((this.graph.imax)&&(to > this.graph.imax)) {
	from = adeiMathPreciseSubstract(x, adeiMathPreciseSubstract(this.graph.imax, x));
	to = this.graph.imax;
    }

    this.SetCustomWindow(from, to, min, max);
    this.Apply();    

}

WINDOW.prototype.IncreaseWidth = function() {
    var width = this.config.win_width;
    if (width < 0) {
	this.source_winsel.SetValue(0);
    } else {
	this.source_winsel.Prev(false);

/*	
	Updating All is not so bad idea 
	if (this.source_winsel.GetValue() == width) return;
*/
    }

//    this.cfg.width = this.source_winsel.GetValue();
    this.UpdateWidth(this.source_winsel.GetValue());
}

WINDOW.prototype.DecreaseWidth = function() {
    var width = this.config.win_width;
    if (width < 0) {
	var idx = this.source_winsel.GetLastIndex();
	if (idx > 0) this.source_winsel.SetIndex(idx - 1);
    } else {
	this.source_winsel.Next(false, function (idx, value) {
	    if (parseInt(value) < 0) return true;
	    return false;
	});

/*	
	Updating is not so bad idea
	    // Already minimum
	if (this.source_winsel.GetValue() == width) return;
*/

    }

//    this.cfg.width = this.source_winsel.GetValue();
    this.UpdateWidth(this.source_winsel.GetValue());
}

WINDOW.prototype.YZoomIn = function(y) {
    var new_size = this.graph.ysize / adei.cfg.zoom_ratio;

    if (typeof y == "undefined") {
	var diff = (this.graph.ysize - new_size) / 2;

	var min = this.graph.ymin + diff;
	var max = this.graph.ymax - diff;
    } else {
	var min = y - new_size/2;
	var max = y + new_size/2;
    }

    this.SetCustomWindow(0, 0, min, max);

    this.Apply();    
}

WINDOW.prototype.YZoomOut = function(y) {
    if (typeof y == "undefined") {
	var diff = (adei.cfg.zoom_ratio - 1) * this.graph.ysize / 2;

	var min = this.graph.ymin - diff;
	var max = this.graph.ymax + diff;
    } else {
	var new_size = this.graph.ysize * adei.cfg.zoom_ratio / 2;
	
	var min = y - new_size;
	var max = y + new_size;
    }

    this.SetCustomWindow(0, 0, min, max);

    this.Apply();    
}

WINDOW.prototype.CenterZoomIn = function() {
    var new_size = this.graph.xsize /  adei.cfg.zoom_ratio;
    var diff = adeiMathPreciseSubstract(this.graph.xsize, new_size) / 2;
    
    var from = adeiMathPreciseAdd(this.graph.xmin, diff);
    var to = adeiMathPreciseSubstract(this.graph.xmax, diff);
    
    this.SetCustomWindow(from, to, 0, 0);
    this.Apply();    
}

WINDOW.prototype.CenterZoomOut = function() {
    if ((this.graph.xmin)&&(this.graph.xmax)) {
	var diff = (adei.cfg.zoom_ratio - 1) * this.graph.xsize / 2;

	var from = adeiMathPreciseSubstract(this.graph.xmin, diff);
	var to = adeiMathPreciseAdd(this.graph.xmax, diff);
    } else {
	var xmin = this.config.win_from;
	var xmax = this.config.win_to;
	var xsize = adeiMathPreciseSubstract(xmax, xmin);

	var diff = (adei.cfg.zoom_ratio - 1) * xsize / 2;
	var from = adeiMathPreciseSubstract(xmin, diff);
	var to = adeiMathPreciseAdd(xmax, diff);
    }
    
    this.SetCustomWindow(from, to, 0, 0);
    this.Apply();    
}

WINDOW.prototype.DeepZoom = function(x) {
    var prec = this.graph.GetPrecision( adei.cfg.deepzoom_area);
    var from = adeiMathPreciseSubstract(x, prec);
    var to = adeiMathPreciseAdd(x, prec);
    
    this.SetCustomWindow(from, to, 0, 0);
    this.Apply();    
}

WINDOW.prototype.LocalZoomIn = function(x) {
    var new_size = this.graph.xsize /  (2 * adei.cfg.zoom_ratio);
    var from = adeiMathPreciseSubstract(x, new_size);
    var to = adeiMathPreciseAdd(x, new_size);
    
    this.SetCustomWindow(from, to, 0, 0);
    this.Apply();    
}

WINDOW.prototype.LocalZoomOut = function(x) {
    var diff = adei.cfg.zoom_ratio  * this.graph.xsize / 2;

    var from = adeiMathPreciseSubstract(x, diff);
    var to = adeiMathPreciseAdd(x, diff);
    
    this.SetCustomWindow(from, to, 0, 0);
    this.Apply();    
}


WINDOW.prototype.ZoomIn = function(x,y) {
    var width = this.config.win_width;
    if (width > 0) {
	var idx = this.source_winsel.GetLastIndex();
	if (this.source_winsel.GetIndex() == (idx - 1)) {
		// Minimal zoom is reached
	    this.LocalZoomIn(this.graph.xmax);
	    this.sub_edge_mode = true;
	} else {
    	    this.DecreaseWidth();
	}
	return;
    } else if (width == 0) {
	if (((this.graph.xmax - x)<=0)||((this.graph.xsize / (this.graph.xmax - x)) > adei.cfg.edge_ratio)) {
	    this.DecreaseWidth();
	    return;
	}
    }
    this.LocalZoomIn(x);    
}

WINDOW.prototype.ZoomOut = function(x,y) {
    var width = this.config.win_width;

    if (!width) return;

    if (width > 0) {
	this.IncreaseWidth();
	return;
    } else if (width < 0) {
	if ((this.sub_edge_mode)&&(((this.graph.xmax - x)<=0)||((this.graph.xsize / (this.graph.xmax - x)) > adei.cfg.edge_ratio))) {
	    if (this.graph.xsize < this.minval) {
		var new_size = adei.cfg.zoom_ratio  * this.graph.xsize;
		if (new_size > this.minval) {
			// Setting minmal width
		    this.DecreaseWidth();
		    return;
		}
	    }
	}

	var new_size = adei.cfg.zoom_ratio  * this.graph.xsize;
	if (new_size > Math.ceil(this.graph.imax - this.graph.imin)) {
	    this.source_winsel.SetValue(0);
	    this.UpdateWidth(0);
	
	    return ;
	}
    }

    
    this.CenterZoomOut();

/*
    Double operation is a bed idea, moved up

    if ((!this.graph.yzoom)&&(this.graph.xmin <= this.graph.imin)&&(this.graph.xmax >= this.graph.imax)) {
	this.source_winsel.SetValue(0);
	this.UpdateWidth(0);
	this.Apply();
    }
*/
}


WINDOW.prototype.NewGraph = function(xmin, xmax) {
    if (this.graph.xsize > this.minval) {
	this.sub_edge_mode = false;
    }
}


WINDOW.prototype.RegisterGeometryCallback = function(cb, cbattr) {
    this.geometry_cb = cb;
    this.geometry_cbattr = cbattr;
}

WINDOW.prototype.FixHidden = function() {
    if ((this.fix_hidden)&&(this.module_on_display)) {
	if (this.cfg.width<0) {
	    cssHideClass('.hide_window_custom');
	    cssShowClass('.hide_window_custom');
	} else {
	    cssShowClass('.hide_window_custom');
	    cssHideClass('.hide_window_custom');
	}
	this.fix_hidden = false;
    }
}

function windowUpdateWidth(window, value, opts) {
    window.UpdateWidth(value, opts);
}

function windowUpdateRange(window) {
    window.UpdateRange();
}

function windowUpdateRangeFunction(window) { 
    return function() { window.UpdateRange(); } 
}