/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
function DIALOG_DRAGGER(dialog) {
312 by Suren A. Chilingaryan
Resizing of sidebar popups
2
    if (typeof dialog.node == "undefined") {
3
        this.dialog = new Object;
4
        this.dialog.node = dialog;
5
        this.dialog.maximize_node = null;
6
        this.dialog.titlebar = null;
7
        this.dialog.content = null;
8
        
9
        this.float_mode = false;
10
    } else {
11
        this.dialog = dialog;
12
        
13
        this.float_mode = true;
14
    }
1 by Suren A. Chilingaryan
Initial import
15
    
16
    this.resizeCornerSize = 16;
17
	/* If set to small, we will hide scrollbars of content window and
18
	resizing will not be operable any more */
19
    this.minimumWidth = 60;
20
    this.minimumHeight = 15;
21
    
22
    this.MouseMoveBind = this.MouseMove.bindAsEventListener( this );
23
    this.MouseOutBind = this.MouseOut.bindAsEventListener( this );
24
    this.MaximizeBind = this.Maximize.bindAsEventListener( this );
25
    this.ResizeBind = this.Resize.bindAsEventListener( this );
26
    this.MoveBind = this.Move.bindAsEventListener( this );
27
    
28
    this.TrackBind = this.Track.bindAsEventListener( this );
29
    this.DoneBind = this.Done.bindAsEventListener( this );
30
    
31
    this.label_displayed = true;
32
    
33
    this.in_resize = false;
34
    this.in_move = false;
35
    this.maximized = false;
36
    
37
    this.is_moved = false;
38
    this.is_resized = false;
312 by Suren A. Chilingaryan
Resizing of sidebar popups
39
    
40
    this.cb_start = null;
41
    this.cb_step = null;
42
    this.cb_end = null;
43
    this.cb_attr = null;
44
    
45
    this.ctrl_min = false;
46
    
47
    this.allow_move = true;
48
    this.allow_resize = true;
49
    this.allow_resize_e = true;
50
    this.allow_resize_w = true;
51
    this.allow_resize_n = true;
52
    this.allow_resize_s = true;
53
}
54
55
DIALOG_DRAGGER.prototype.RegisterCallbacks = function(cb_start, cb_step, cb_done, cbattr) {
56
    this.cb_start = cb_start;
57
    this.cb_step = cb_step;
58
    this.cb_end = cb_done;
59
    this.cb_attr = cbattr;
60
}
61
62
DIALOG_DRAGGER.prototype.ControlMinimumSize = function(min_width, min_height) {
63
    if ((Prototype.Browser.Gecko)&&(domGetStyleProp(this.dialog.node, this.dialog.node.style.minWidth, "min-width"))) {
64
        this.ctrl_min = true;
65
        this.min_width = min_width;
66
        this.min_height = min_height;
67
    }
68
}
69
70
DIALOG_DRAGGER.prototype.Disable = function(move, resize) {
71
    if (!move) this.allow_move = false;
72
    if (typeof resize == "string") {
73
        if (resize.indexOf('n') >= 0) this.allow_resize_n = false;
74
        if (resize.indexOf('s') >= 0) this.allow_resize_s = false;
75
        if (resize.indexOf('w') >= 0) this.allow_resize_w = false;
76
        if (resize.indexOf('e') >= 0) this.allow_resize_e = false;
77
    } else if (!resize) this.allow_resize = false;
78
}
1 by Suren A. Chilingaryan
Initial import
79
80
DIALOG_DRAGGER.prototype.Setup = function() {
312 by Suren A. Chilingaryan
Resizing of sidebar popups
81
    if (this.dialog.content) {
82
	// IE7 and not only :) needs it to adjust widths of sub-divs
83
        domSetWidth(this.dialog.node, this.dialog.node.offsetWidth);
314 by Suren A. Chilingaryan
Resizing fixes for IE and Opera
84
    }
1 by Suren A. Chilingaryan
Initial import
85
	// IE6 :) fixing size for iframes
314 by Suren A. Chilingaryan
Resizing fixes for IE and Opera
86
    if (this.dialog.iframe) {
87
    	this.SaveDimenssions();
88
	this.Restore();
312 by Suren A. Chilingaryan
Resizing of sidebar popups
89
    }
90
91
    if (this.dialog.maximize_node)
92
        Event.observe(this.dialog.maximize_node, 'click', this.MaximizeBind );
93
94
    if ((this.allow_move)&&(this.dialog.titlebar))
95
        Event.observe(this.dialog.titlebar, 'mousedown', this.MoveBind );
96
97
    if (this.allow_resize)
98
        Event.observe(this.dialog.node, 'mousedown', this.ResizeBind );
99
1 by Suren A. Chilingaryan
Initial import
100
    Event.observe(this.dialog.node, 'mousemove', this.MouseMoveBind );
101
    Event.observe(this.dialog.node, 'mouseout', this.MouseOutBind );
102
}
103
104
DIALOG_DRAGGER.prototype.Clean = function() {
312 by Suren A. Chilingaryan
Resizing of sidebar popups
105
    if (this.dialog.maximize_node)
106
        Event.stopObserving(this.dialog.maximize_node, 'click', this.MaximizeBind );
107
108
    if ((this.allow_move)&&(this.dialog.titlebar))
109
        Event.stopObserving(this.dialog.titlebar, 'mousedown', this.MoveBind );
110
111
    if (this.allow_resize)
112
        Event.stopObserving(this.dialog.node, 'mousedown', this.ResizeBind );
113
1 by Suren A. Chilingaryan
Initial import
114
    Event.stopObserving(this.dialog.node, 'mousemove', this.MouseMoveBind );
115
    Event.stopObserving(this.dialog.node, 'mouseout', this.MouseOutBind );
116
}
117
118
DIALOG_DRAGGER.prototype.MouseMove = function(ev) {
119
    if ((this.in_resize)||(this.in_move)) return;
120
    
121
    var target = domGetEventTarget(ev);
122
    if (target != this.dialog.node) {
123
	this.resizeDirection = "";
124
	this.dialog.node.style.cursor = "";
125
	return;
126
    }
312 by Suren A. Chilingaryan
Resizing of sidebar popups
127
1 by Suren A. Chilingaryan
Initial import
128
    var width = this.dialog.node.offsetWidth;
129
    var height = this.dialog.node.offsetHeight;
312 by Suren A. Chilingaryan
Resizing of sidebar popups
130
1 by Suren A. Chilingaryan
Initial import
131
    var offset = domGetEventLayerOffset(ev);
312 by Suren A. Chilingaryan
Resizing of sidebar popups
132
    
1 by Suren A. Chilingaryan
Initial import
133
    var xOff = offset[0];
134
    var yOff = offset[1];
312 by Suren A. Chilingaryan
Resizing of sidebar popups
135
    
136
    if (!this.float_mode) {
137
        var node_offset = domGetNodeOffset(this.dialog.node);
138
        xOff -= node_offset[0];
139
        yOff -= node_offset[1];
140
    }
141
    
1 by Suren A. Chilingaryan
Initial import
142
    var resizeDirection = ""
312 by Suren A. Chilingaryan
Resizing of sidebar popups
143
    if ((this.allow_resize_n)&&(yOff < this.resizeCornerSize))
1 by Suren A. Chilingaryan
Initial import
144
	resizeDirection += "n";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
145
    else if ((this.allow_resize_s)&&(yOff > (height - this.resizeCornerSize)))
1 by Suren A. Chilingaryan
Initial import
146
	resizeDirection += "s";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
147
    if ((this.allow_resize_w)&&(xOff < this.resizeCornerSize))
1 by Suren A. Chilingaryan
Initial import
148
	resizeDirection += "w";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
149
    else if ((this.allow_resize_e)&&(xOff > (width - this.resizeCornerSize)))
1 by Suren A. Chilingaryan
Initial import
150
	resizeDirection += "e";
151
152
    if (resizeDirection) {
153
	this.resizeDirection = resizeDirection;
154
	this.dialog.node.style.cursor = resizeDirection + "-resize";
155
    } else {
156
	this.resizeDirection = "";
157
	this.dialog.node.style.cursor = "";
158
    }
159
}
160
161
DIALOG_DRAGGER.prototype.MouseOut = function(ev) {
162
/* Causes problems in Opera
163
    this.dialog.node.style.cursor = "";
164
*/
165
}
166
167
168
DIALOG_DRAGGER.prototype.Move = function(ev) {
169
    var target = domGetEventTarget(ev);
170
    if ((target != this.dialog.titlebar)&&(target != this.dialog.label)) return;
171
172
    Event.observe(document, 'mousemove', this.TrackBind );
173
    Event.observe(document, 'mouseup', this.DoneBind );
174
175
    var node = this.dialog.node;
176
    var offset = domGetEventPageOffset(ev);
177
    this.xOffset = parseInt(domGetStyleProp(node, node.style.left, "left") ,10) - offset[0];
178
    this.yOffset = parseInt(domGetStyleProp(node, node.style.top, "top"), 10) - offset[1];
316 by Suren A. Chilingaryan
Prevent text selection while resizing
179
1 by Suren A. Chilingaryan
Initial import
180
    this.in_move = true;
181
}
182
183
DIALOG_DRAGGER.prototype.SaveDimenssions = function() {
184
    var node = this.dialog.node;
185
186
    this.oldLeft = parseInt(domGetStyleProp(node, node.style.left, "left") ,10);
187
    this.oldTop = parseInt(domGetStyleProp(node, node.style.top, "top"), 10);
188
189
	// Obtaining original window size
190
    var size = domGetNodeSize(node);
191
    node.style.width = size[0] + "px";
192
    node.style.height = size[1] + "px";
193
194
    var csize = domGetNodeSize(node);
195
    this.corr_x = csize[0] - size[0];
196
    this.corr_y = csize[1] - size[1];
197
    
198
    this.oldWidth = size[0] - this.corr_x;//parseInt(domGetStyleProp(node, node.style.width, "width"), 10);
199
    this.oldHeight = size[1] - this.corr_y;//parseInt(domGetStyleProp(node, node.style.height, "height"), 10);
200
201
    node.style.width = this.oldWidth + "px";
202
    node.style.height = this.oldHeight + "px";
203
312 by Suren A. Chilingaryan
Resizing of sidebar popups
204
    if (this.ctrl_min) {
205
        node.style.minWidth = this.min_width + "px";
206
        node.style.minHeight = this.min_height + "px";
207
    }
208
209
    if (this.dialog.content) {
210
        var mnode = this.dialog.content;
211
	    // Obtaining content element size
212
        size = domGetNodeSize(mnode);
1 by Suren A. Chilingaryan
Initial import
213
214
//    mnode.style.width = size[0] + "px";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
215
        mnode.style.height = size[1] + "px";
1 by Suren A. Chilingaryan
Initial import
216
312 by Suren A. Chilingaryan
Resizing of sidebar popups
217
        var csize = domGetNodeSize(mnode);
1 by Suren A. Chilingaryan
Initial import
218
//    this.corr_cx = csize[0] - size[0];
312 by Suren A. Chilingaryan
Resizing of sidebar popups
219
        this.corr_cy = csize[1] - size[1];
1 by Suren A. Chilingaryan
Initial import
220
221
//    this.cWidth = size[0] - this.corr_cx;
312 by Suren A. Chilingaryan
Resizing of sidebar popups
222
        this.cHeight = size[1] - this.corr_cy;
1 by Suren A. Chilingaryan
Initial import
223
    
224
//    mnode.style.width = this.cWidth + "px";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
225
        mnode.style.height = this.cHeight + "px";
226
    }
1 by Suren A. Chilingaryan
Initial import
227
}
228
229
DIALOG_DRAGGER.prototype.Resize = function(ev) {
230
    var target = domGetEventTarget(ev);
231
    if (target != this.dialog.node) return;
312 by Suren A. Chilingaryan
Resizing of sidebar popups
232
1 by Suren A. Chilingaryan
Initial import
233
    var node = this.dialog.node;
312 by Suren A. Chilingaryan
Resizing of sidebar popups
234
1 by Suren A. Chilingaryan
Initial import
235
    Event.observe(document, 'mousemove', this.TrackBind );
236
    Event.observe(document, 'mouseup', this.DoneBind );
237
238
    this.in_resize = true;
239
240
	// If max-height not supported set titlebar height
312 by Suren A. Chilingaryan
Resizing of sidebar popups
241
    if (this.dialog.titlebar) {
242
        var tb = this.dialog.titlebar;
243
        var mh = parseInt(domGetStyleProp(tb, tb.style.maxHeight, "max-height"),10);
244
        if ((!mh)||((tb.offsetHeight - mh)>10)) {
245
	    domSetWidth(node, node.offsetWidth);
246
	    if (mh>0) domSetHeight(tb, mh);
247
	    else domSetHeight(tb, 21);
248
        }
1 by Suren A. Chilingaryan
Initial import
249
    }
250
    
312 by Suren A. Chilingaryan
Resizing of sidebar popups
251
    if (this.dialog.label) {
252
        if (!this.label_width) {
253
	    this.label_width = this.dialog.label.offsetWidth + this.dialog.label.offsetLeft;
254
        }
1 by Suren A. Chilingaryan
Initial import
255
    }
256
257
    var offset = domGetEventPageOffset(ev);
258
    this.xPosition = offset[0];
259
    this.yPosition = offset[1];
312 by Suren A. Chilingaryan
Resizing of sidebar popups
260
1 by Suren A. Chilingaryan
Initial import
261
    this.SaveDimenssions();    
312 by Suren A. Chilingaryan
Resizing of sidebar popups
262
1 by Suren A. Chilingaryan
Initial import
263
	// Obtaining titlebar size (no correction, since preciese size is not important
312 by Suren A. Chilingaryan
Resizing of sidebar popups
264
    if (this.dialog.titlebar) {
265
        var lnode = this.dialog.titlebar;
266
        var size = domGetNodeSize(lnode);
267
        size[1] += this.corr_y;
268
        if ((size[1] + 10) > this.minimumHeight) this.minimumHeight = size[1] + 10;
269
    }
316 by Suren A. Chilingaryan
Prevent text selection while resizing
270
271
    if (ev && ev.preventDefault) ev.preventDefault();
272
    else window.event.returnValue = false;
273
    return false;
1 by Suren A. Chilingaryan
Initial import
274
}
275
276
DIALOG_DRAGGER.prototype.Restore = function(ev) {
277
    var node = this.dialog.node;
278
    var mnode = this.dialog.content;
279
280
    node.style.left = this.oldLeft + "px";
281
    node.style.top = this.oldTop + "px";
282
    node.style.width = this.oldWidth + "px";
283
    node.style.height = this.oldHeight + "px";
284
    mnode.style.height = this.cHeight + "px";
285
286
    cssSetClass(this.dialog.maximize_node, "maximize");
287
    this.maximized = false;
288
}
289
290
DIALOG_DRAGGER.prototype.Maximize = function(ev) {
291
    var node = this.dialog.node;
292
    var mnode = this.dialog.content;
293
294
    if (this.maximized) {
295
	this.Restore();
296
    } else {
297
	this.SaveDimenssions();
298
	
299
	node.style.left = 0;
300
	node.style.top = 0;
301
	
302
	var ww = windowGetWidth();
303
    	var wh = windowGetHeight();
304
305
	node.style.width =  (ww - this.corr_x) + "px";
306
	
307
	if ((wh - this.corr_y) > this.oldHeight) {
308
	    node.style.height = (wh - this.corr_y) + "px";
309
	    mnode.style.height = (this.cHeight + (wh - this.corr_y - this.oldHeight)) + "px";
310
	}
311
312
	cssSetClass(this.dialog.maximize_node, "restore");
313
	this.maximized = true;
314
    }
315
}
316
317
DIALOG_DRAGGER.prototype.Track = function(ev) {
318
    var node = this.dialog.node;
319
320
    if (this.in_resize) {
321
	var north = false;
322
	var south = false;
323
	var east  = false;
324
	var west  = false;
325
	
312 by Suren A. Chilingaryan
Resizing of sidebar popups
326
	if (this.resizeDirection.charAt(0) == "n") north = this.allow_resize_n;
327
	if (this.resizeDirection.charAt(0) == "s") south = this.allow_resize_s;
328
	if (this.resizeDirection.charAt(0) == "e" || this.resizeDirection.charAt(1) == "e") east = this.allow_resize_e;
329
	if (this.resizeDirection.charAt(0) == "w" || this.resizeDirection.charAt(1) == "w") west = this.allow_resize_w;
1 by Suren A. Chilingaryan
Initial import
330
331
        var offset = domGetEventPageOffset(ev);
332
	var dx = offset[0] - this.xPosition;
333
	var dy = offset[1] - this.yPosition;
334
335
	if (west) dx = -dx;
336
	if (north) dy = -dy;
337
338
	var w = this.oldWidth  + dx;
339
	var h = this.oldHeight + dy;
340
	
341
	if (w < this.minimumWidth) {
342
	    w = this.minimumWidth;
343
	    dx = w - this.oldWidth;
344
	}
345
	
346
	if (h < this.minimumHeight) {
347
	    h = this.minimumHeight;
348
	    dy = h - this.oldHeight;
349
	}
350
	
351
	if (east || west) {
352
	    if (w < this.label_width) {
353
		if (this.label_displayed) {
354
		    this.dialog.titlebar.removeChild(this.dialog.label);
355
		    this.label_displayed = false;
356
		}
357
	    } else {
358
		if (!this.label_displayed) {
359
		    this.dialog.titlebar.appendChild(this.dialog.label);
360
		    this.label_displayed = true;
361
		}
362
	    }
363
	    
364
	    node.style.width = w + "px";
365
	    if (west) node.style.left = (this.oldLeft - dx) + "px";
366
	}
312 by Suren A. Chilingaryan
Resizing of sidebar popups
367
1 by Suren A. Chilingaryan
Initial import
368
	if (north || south) {
369
	    node.style.height = h + "px";
312 by Suren A. Chilingaryan
Resizing of sidebar popups
370
371
	    if (this.dialog.content) {
372
	        var mnode = this.dialog.content;
373
	        if ((this.cHeight + dy) < 0) mnode.style.height = 0 + "px";
374
	        else mnode.style.height = (this.cHeight  + dy) + "px";
375
	        if (dy < 0) {
376
		    var size = domGetNodeSize(mnode);
377
		    var rdy = size[1] - this.corr_cy - this.cHeight;
378
		    if (rdy > dy) {
379
	    	        node.style.height = (this.oldHeight + rdy) + "px";
380
		    }
381
	        }
1 by Suren A. Chilingaryan
Initial import
382
	    }
312 by Suren A. Chilingaryan
Resizing of sidebar popups
383
1 by Suren A. Chilingaryan
Initial import
384
	    if (north) node.style.top  = (this.oldTop  - dy) + "px";
385
	}
386
	
387
	this.is_resized = true;
388
    } else if (this.in_move) {
389
        var offset = domGetEventPageOffset(ev);
390
	node.style.left = (offset[0] + this.xOffset) + "px";
391
	node.style.top  = (offset[1] + this.yOffset) + "px";
392
	
393
	this.is_moved = true;
394
    }
395
    
396
    Event.stop(ev);
397
}
398
399
DIALOG_DRAGGER.prototype.Done = function(ev) {
400
    if (this.in_resize) {
401
	this.in_resize = false;
402
    }
403
404
    if (this.in_move) {
405
	this.in_move = false;
406
    }
312 by Suren A. Chilingaryan
Resizing of sidebar popups
407
1 by Suren A. Chilingaryan
Initial import
408
    Event.stopObserving(document, 'mousemove', this.TrackBind );
409
    Event.stopObserving(document, 'mouseup', this.DoneBind );
410
411
    Event.stop(ev);
312 by Suren A. Chilingaryan
Resizing of sidebar popups
412
413
    if (this.cb_end) {
414
        this.cb_end(this.cb_attr, this);
415
    }
1 by Suren A. Chilingaryan
Initial import
416
}