/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/graph.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 GRAPH(id, selid) {
 
2
    this.config = null;
 
3
    this.window = null;
 
4
    this.exporter = null;
 
5
    this.selector = document.getElementById(selid);
 
6
    this.frame = document.getElementById(id);
 
7
    
 
8
    this.start_xy = [ 0, 0 ];
 
9
    this.margins = { left: 0, top: 0, right: 0, bottom: 0 };
 
10
    this.crop_margins = { left: 0, top: 0, right: 0, bottom: 0 };
 
11
    this.allWidth = 10;
 
12
    this.allHeight = 10;
 
13
    
 
14
    this.height = 0;
 
15
    this.width = 0;
 
16
    
 
17
    if (this.frame) {
 
18
        this.img = this.frame.getElementsByTagName('img')[0];
 
19
    } else {
 
20
        this.frame = null;
 
21
    }
 
22
    
 
23
/*    
 
24
    Event.observe(id,'click',this.MouseStart);
 
25
    Event.observe(id,'MouseMove', this.MouseStart);
 
26
    Event.observe(id,'MouseDown', this.MouseStart);
 
27
*/
 
28
 
 
29
    this.crop = null;
 
30
 
 
31
    if (this.img) {
 
32
        Event.observe(this.img, 'load', this.Configure.bindAsEventListener(this));
 
33
    } else {
 
34
        if (this.frame) alert('The "img" element is not found within specified block "' + id + '"');
 
35
        else alert('GRAPH is not able to locate specified ("' + id + '") element');
 
36
    }
 
37
 
 
38
    this.LegendCloseBind = this.LegendClose.bindAsEventListener( this );
 
39
    this.LegendCloseHelperBind = eventCanceler.bindAsEventListener();
 
40
}
 
41
 
 
42
GRAPH.prototype.AttachConfig = function(config) {
 
43
    this.config = config;
 
44
//    config.Register(this);
 
45
}
 
46
 
 
47
GRAPH.prototype.Clear = function() {
 
48
    if (this.crop) {
 
49
        this.crop.clear();
 
50
    }
 
51
}
 
52
 
 
53
 
 
54
GRAPH.prototype.onEndCrop = function (self) {
 
55
    return function( coords, dimensions ) {
 
56
        if (self.window) {
 
57
            if ((dimensions.width < self.allWidth)&&(dimensions.height < self.allHeight)) {
 
58
                self.window.SetCustomWindow(0, 0, 0, 0);
 
59
            } else if (dimensions.height < self.allHeight) {
 
60
                var xmin = self.xmin + (self.xmax - self.xmin)*(coords.x1 - self.margins.left) / self.real_width;
 
61
                var xmax = self.xmin + (self.xmax - self.xmin)*(coords.x2 - self.margins.left) / self.real_width;
 
62
                self.window.SetCustomWindow(xmin, xmax, 0, 0);
 
63
            } else if (dimensions.width < self.allWidth) {
 
64
                var ymin = self.ymax - (self.ymax - self.ymin)*(coords.y2 - self.margins.top) / self.real_height;
 
65
                var ymax = self.ymax - (self.ymax - self.ymin)*(coords.y1 - self.margins.top) / self.real_height;
 
66
                self.window.SetCustomWindow(0, 0, ymin, ymax);
 
67
            } else {
 
68
                var ymin = self.ymax - (self.ymax - self.ymin)*(coords.y2 - self.margins.top) / self.real_height;
 
69
                var ymax = self.ymax - (self.ymax - self.ymin)*(coords.y1 - self.margins.top) / self.real_height;
 
70
                var xmin = self.xmin + (self.xmax - self.xmin)*(coords.x1 - self.margins.left) / self.real_width;
 
71
                var xmax = self.xmin + (self.xmax - self.xmin)*(coords.x2 - self.margins.left) / self.real_width;
 
72
                self.window.SetCustomWindow(xmin, xmax, ymin, ymax);
 
73
            }
 
74
        }
 
75
    }
 
76
            
 
77
}
 
78
 
 
79
GRAPH.prototype.onCancelCrop = function (self) {
 
80
    return function() {
 
81
        if (self.window) {
 
82
            self.window.SetCustomWindow(0,0,0,0);
 
83
//          self.window.SetCustomWindow(self.config.from, self.config.to, self.config.win_min, self.config.win_max);
 
84
        }
 
85
    }
 
86
}
 
87
 
 
88
GRAPH.prototype.onApply = function (self) {
 
89
    return function ( coords, dimensions ) {
 
90
        self.window.Apply();
 
91
        self.crop.clear();
 
92
    }
 
93
}
 
94
 
 
95
GRAPH.prototype.onSave = function (self) {
 
96
    return function ( coords, dimensions ) {
 
97
        if (self.exporter) 
 
98
            self.exporter.Export(true);
 
99
        else
 
100
            adeiReportError("Data Exporter is not registered", "GRAPH");
 
101
    }
 
102
}
 
103
 
 
104
GRAPH.prototype.onMouseScroll = function (self) {
 
105
    return function ( delta, point ) {
 
106
//      alert(adei.key);
 
107
        if (delta > 0)
 
108
            self.window.ZoomOut(point.x, point.y);
 
109
        else if (delta < 0)
 
110
            self.window.ZoomIn(point.x, point.y);
 
111
    }
 
112
}
 
113
 
 
114
 
 
115
 
 
116
GRAPH.prototype.LegendClose = function(self) {
 
117
    return function(ev) {
 
118
        if (self.legend) {
 
119
            self.legend.Hide();
 
120
            self.legend = null;
 
121
        }
 
122
        Event.stop(ev);
 
123
    }
 
124
}
 
125
 
 
126
GRAPH.prototype.ShowLegend = function (self, mouse_x, mouse_y) {
 
127
    return function(transport) {
 
128
        if (transport.responseText.charAt(0) == '{') {
 
129
            var json = transport.responseText.evalJSON(false);
 
130
            if (json.error) {
 
131
                alert(json.error);
 
132
            } else {
 
133
                var html = "";//"<h4>Legend</h4>""<div class=\"window_close\"></div><h4>Legend</h4>";
 
134
                if ((json.legend)&&(json.legend.length>0)) {
 
135
                    html+="<table><tr><th>ID</th><th>Name</th></tr>";
 
136
                    for (var i = 0; i < json.legend.length; i++) {
 
137
                        var item = json.legend[i];
 
138
                        html+="<tr><td>" + item.id + "</td><td>" + item.name + "</td></tr>";
 
139
                    }
 
140
                    html+="</table>";
 
141
                } else {
 
142
                    html+="<p>Nothing selected</p>";
 
143
                }
 
144
                
 
145
                if ((self.legend)&&(self.legend.CheckReusability())) {
 
146
                    self.legend.AlterContent(html);
 
147
                } else {
 
148
                    var legend = new DIALOG(self.LegendClose(self), "Legend", html, "legend");
 
149
                    legend.Show(self.frame, self.legend, mouse_x, mouse_y);
 
150
                    self.legend = legend;
 
151
                }
 
152
            }
 
153
        } else {
 
154
            // XML
 
155
        }
 
156
    }
 
157
}
 
158
 
 
159
GRAPH.prototype.onClick = function (self) {
 
160
    return function ( ev, point ) {
 
161
 
 
162
        var point = {
 
163
            xmin: self.xmin.toString(),
 
164
            xmax: self.xmax.toString(),
 
165
            ymin: self.ymin,
 
166
            ymax: self.ymax,
 
167
            x : adeiMathPreciseAdd(self.xmin, self.xsize*(point.x - self.margins.left) / self.real_width),
 
168
            y : self.ymax - (self.ymax - self.ymin)*(point.y - self.margins.top) / self.real_height
 
169
        };
 
170
        
 
171
//      alert(self.xmax + " - " + self.xmin + "(" + self.xsize + "): " + point.x + "\n");
 
172
        
 
173
        new Ajax.Request(adei.GetServiceURL("legend"), 
 
174
        {
 
175
            method: 'post',
 
176
            requestHeaders: {Accept: 'application/json'},
 
177
            parameters: { props: self.config.GetJSON(point) },
 
178
            onSuccess: self.ShowLegend(self, ev.clientX, ev.clientY),
 
179
            onFailure: function() {
 
180
                alert('GetLegend request is failed');
 
181
            }
 
182
        });
 
183
    
 
184
        //updater.
 
185
    }
 
186
}
 
187
 
 
188
 
 
189
GRAPH.prototype.AttachWindow = function (window) {
 
190
    this.window = window;
 
191
    window.RegisterGraph(this);
 
192
}
 
193
 
 
194
GRAPH.prototype.AttachExporter = function (exporter) {
 
195
    this.exporter = exporter;
 
196
}
 
197
 
 
198
GRAPH.prototype.SetMargins = function (l,t,r,b) {
 
199
    this.margins = { left: l, top: t, right: r, bottom: b };
 
200
    this.crop_margins = { left: l, top: t, right: r, bottom: b };
 
201
}
 
202
 
 
203
GRAPH.prototype.SetAllSizes = function (w,h) {
 
204
    this.allWidth = w;
 
205
    this.allHeight = h;
 
206
}
 
207
 
 
208
GRAPH.prototype.Prepare = function() {
 
209
        // Fixing IE6.
 
210
    var diff = document.body.offsetWidth - windowGetWidth();
 
211
    if (diff > 0) {
 
212
        var new_size = this.frame.offsetWidth - diff;
 
213
        if (new_size > 0) {
 
214
            this.config.SetupGeometry(new_size, this.frame.offsetHeight);
 
215
            return;
 
216
        }
 
217
    }
 
218
    
 
219
    this.config.SetupGeometry(this.frame);
 
220
}
 
221
 
 
222
GRAPH.prototype.Configure = function() {
 
223
                this.real_height = this.img.height - this.margins.top - this.margins.bottom;
 
224
                this.real_width = this.img.width - this.margins.left - this.margins.right;
 
225
            
 
226
                if (this.crop) {
 
227
                    var new_width = this.img.width;
 
228
                    var new_height = this.img.height;
 
229
                    
 
230
                    if ((new_width != this.width)||(new_height != this.height)) {
 
231
                        this.width = new_width;
 
232
                        this.height = new_height;
 
233
        
 
234
                        this.crop.setParams();  // DS. this action slows evertything down after several steps
 
235
                    }
 
236
                } else {
 
237
                    var tooltip;
 
238
                    if (this.exporter) 
 
239
                        tooltip = " (" + this.exporter.GetTooltip() + ")";
 
240
                    else
 
241
                        tooltip = "";
 
242
 
 
243
                    this.crop = new Cropper.Img("graph_image", {
 
244
                        onEndCrop:  this.onEndCrop(this),
 
245
                        onCancelCrop: this.onCancelCrop(this),
 
246
                        onClick: this.onClick(this),
 
247
                        onDblClick: this.onApply(this),
 
248
                        onMouseScroll: this.onMouseScroll(this),
 
249
                        onApplyClick: this.onApply(this),
 
250
                        onSaveClick: this.onSave(this),
 
251
                        margins: this.crop_margins,
 
252
                        allWidth: this.allWidth,
 
253
                        allHeight: this.allHeight,
 
254
                        monitorImage: false,
 
255
                        imageReady: true,
 
256
                        tooltips: new Object({
 
257
                            'apply': translate('Zoom to the Selection'),
 
258
                            'save': translate('Export Selected Data') + tooltip
 
259
                        })
 
260
                    });
 
261
                }
 
262
}
 
263
 
 
264
 
 
265
GRAPH.prototype.Update = function(json, forced) {
 
266
    if (json.draw) {
 
267
        if (this.img) {
 
268
            var doit = 1;
 
269
 
 
270
            if (this.crop) {
 
271
                var crop = this.crop;
 
272
                if ((crop.selected)||(crop.dragging)||(crop.resizing)||(crop.calcW()>this.allWidth)||(crop.calcH()>this.allHeight)) {
 
273
                    if (forced) this.crop.clear();
 
274
                    else doit = 0;
 
275
                }
 
276
            }
 
277
 
 
278
 
 
279
            
 
280
            if (doit) {
 
281
                this.img.src = adei.GetServiceURL("getimage", "id=" + json.image);
 
282
 
 
283
                this.xmin = parseFloat(json.xmin);
 
284
                this.xmax = parseFloat(json.xmax);
 
285
                this.ymin = parseFloat(json.ymin);
 
286
                this.ymax = parseFloat(json.ymax);
 
287
 
 
288
                this.xsize = parseFloat(adeiMathPreciseSubstract(this.xmax, this.xmin));
 
289
//              alert(this.xsize + " - " + (this.xmax - this.xmin));
 
290
                
 
291
//              alert(json.xmin + " - " + this.xmin);
 
292
 
 
293
//          x : self.xmin + (self.xmax - self.xmin)*(point.x - self.margins.left) / self.real_width,
 
294
//          y : self.ymax - (self.ymax - self.ymin)*(point.y - self.margins.top) / self.real_height
 
295
 
 
296
            }
 
297
        }
 
298
    }
 
299
}
 
300
 
 
301
GRAPH.prototype.SetExportTooltip = function () {
 
302
    var tooltip = this.exporter.GetTooltip();
 
303
    if ((this.crop)&&(tooltip)) {
 
304
        this.crop.setTooltip(new Object({'save': translate("Export Selected Data (%s)", tooltip)}));
 
305
    }
 
306
}
 
307
 
 
308
GRAPH.prototype.NotifyExportSettings = function() {
 
309
    this.SetExportTooltip();
 
310
}
 
311
 
 
312
//GRAPH.prototype.NotificationExport
 
313
 
 
314
 
 
315
GRAPH.prototype.AdjustGeometry = function(width, height) {
 
316
    domAdjustGeometry(this.frame, width, height - (this.selector?this.selector.offsetHeight:0) - 5, true);
 
317
}
 
318
 
 
319
 
 
320
/*
 
321
GRAPH.prototype.MouseSelection = function(xy) {
 
322
    alert(this.start_xy[0] - xy[0]);
 
323
}
 
324
 
 
325
GRAPH.prototype.MouseStart = function(evt) {
 
326
    var xy = domGetMouseOffset(evt);
 
327
    this.start_xy = xy;
 
328
    return false;
 
329
}
 
330
 
 
331
GRAPH.prototype.MouseDone = function(evt) {
 
332
    var xy = domGetMouseOffset(evt);
 
333
    this.MouseSelection(xy);
 
334
}
 
335
 
 
336
GRAPH.prototype.MouseVisualize = function(evt) {
 
337
    var xy = domGetMouseOffset(evt);
 
338
    return false;
 
339
}
 
340
*/
 
 
b'\\ No newline at end of file'