/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/dom.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 isIE() {
 
2
    return !!(window.attachEvent && !window.opera);
 
3
}
 
4
 
 
5
function isOpera() {
 
6
    return !!(window.opera);
 
7
}
 
8
 
 
9
function domGetStyleProp(node, jsname, cssname) {
 
10
    if (jsname) return jsname;
 
11
    if ((node.currentStyle)&&(node.currentStyle[cssname])) return node.currentStyle[cssname];
 
12
    if ((document.defaultView)&&(document.defaultView.getComputedStyle)) {
 
13
        var style = document.defaultView.getComputedStyle(node,null);
 
14
        if (style) return style.getPropertyValue(cssname);
 
15
    }
 
16
    return null;
 
17
}
 
18
 
 
19
function domNodeSetText(node, text) {
 
20
    var text_node = document.createTextNode(text);
 
21
    if (node.firstChild) node.replaceChild(text_node, node.firstChild);
 
22
    else node.appendChild(text_node);
 
23
}
 
24
 
 
25
function domHide(id) {
 
26
    var node = document.getElementById(id);
 
27
    if (node) node.style.display = "none";
 
28
    return node;
 
29
}
 
30
 
 
31
function domShow(id, display) {
 
32
    if (typeof display == "undefined") {
 
33
        display = "block";
 
34
    }
 
35
 
 
36
    var node = document.getElementById(id);
 
37
    if (node) node.style.display = display;
 
38
    return node;
 
39
}
 
40
 
 
41
 
 
42
function cssSetClass(node, css_class) {
 
43
    if (typeof node == "string") 
 
44
        node = document.getElementById(node);
 
45
    
 
46
    if (node) {
 
47
        node.className = css_class;
 
48
/*      
 
49
        node.setAttribute("className", css_class);
 
50
        node.setAttribute("class", css_class);
 
51
*/
 
52
    }
 
53
    
 
54
    return node;
 
55
}
 
56
 
 
57
function cssSetProperty(css_class, prop, value) {
 
58
    var css;
 
59
    
 
60
    if (document.styleSheets[0].cssRules)
 
61
        css = document.styleSheets[0].cssRules;
 
62
    else // IE (Bad Billy and standards, part N)
 
63
        css = document.styleSheets[0].rules;
 
64
 
 
65
    for (i=0;i<css.length;i++) {
 
66
        if (css[i].selectorText.toLowerCase()==css_class) {
 
67
            css[i].style[prop]=value;
 
68
            break;
 
69
        }
 
70
    }
 
71
}
 
72
 
 
73
function cssHideClass(css_class) {
 
74
    cssSetProperty(css_class, 'display', 'none');
 
75
}
 
76
 
 
77
function cssShowClass(css_class) {
 
78
    cssSetProperty(css_class, 'display', 'inline');
 
79
}
 
80
 
 
81
function domGetValue(id) {
 
82
    var node = document.getElementById(id);
 
83
    if (node) {
 
84
        return node.value;
 
85
    } else return null;
 
86
}
 
87
 
 
88
function windowGetWidth() {
 
89
    return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
 
90
 
91
 
 
92
function windowGetHeight() {
 
93
    return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
 
94
 
95
                
 
96
function domAdjustGeometry(node, width, height, diminish) {
 
97
    if (typeof diminish == "undefined") diminish = false;
 
98
    if (node) {
 
99
        var cur_value = 0;
 
100
 
 
101
        if (!diminish) cur_value = node.offsetHeight;
 
102
        if (cur_value < height) node.style.height = height + "px";
 
103
 
 
104
        if (!diminish) cur_value = node.offsetWidth;
 
105
        if (cur_value < width) node.style.width = width + "px";
 
106
    }
 
107
}
 
108
 
 
109
function domGetMouseOffset(evt) {
 
110
    if (evt.offsetX != null) return [ evt.offsetX, evt.offsetY ];
 
111
    
 
112
    var obj = evt.target || evt.srcElement;
 
113
    var tmp = obj;
 
114
    var top = 0, left = 0;
 
115
    
 
116
    while (tmp.offsetParent) {
 
117
        left += tmp.offsetLeft;
 
118
        top += tmp.offsetTop;
 
119
        tmp = tmp.offsetParent;
 
120
    }
 
121
    
 
122
    return [ evt.clientX - left, evt.clientY - top ];
 
123
}
 
124
 
 
125
function domGetEventTarget(event) {
 
126
    if (typeof event.target != "undefined")
 
127
        return event.target;
 
128
    else
 
129
        return event.srcElement;
 
130
}
 
131
 
 
132
function domGetEventPageOffset(ev) {
 
133
        /* offset in the document */
 
134
    if (typeof ev.pageX == "undefined") 
 
135
        return [ ev.x, ev.y ];
 
136
    else
 
137
        return [ ev.pageX, ev.pageY ];
 
138
}
 
139
 
 
140
function domGetEventLayerOffset(ev) {
 
141
        /* offset in the target element */
 
142
    if (typeof ev.layerX == "undefined") 
 
143
        return [ ev.offsetX, ev.offsetY ];
 
144
    else
 
145
        return [ ev.layerX, ev.layerY ];
 
146
}
 
147
 
 
148
 
 
149
function domGetScrollEventDelta(ev) {
 
150
    var delta = 0;
 
151
    
 
152
    if (ev.detail) {    /* GECKO */
 
153
        delta = ev.detail;
 
154
    } else if (ev.wheelDelta) { /* IE/Opera */
 
155
        delta = -ev.wheelDelta/40;
 
156
    }
 
157
    
 
158
        // Still, delta absolute value could vary depending on browser version
 
159
    return delta;
 
160
}
 
161
 
 
162
function domGetNodeOffset(obj) {
 
163
    var curleft = curtop = 0;
 
164
    if (obj.offsetParent) {
 
165
        do {
 
166
            curleft += obj.offsetLeft;
 
167
            curtop += obj.offsetTop;
 
168
        } while (obj = obj.offsetParent);
 
169
    } else {
 
170
        alert("Can't calculate node position");
 
171
    }
 
172
    return [curleft,curtop];
 
173
}
 
174
 
 
175
function domGetNodeSize(obj) {
 
176
/*    if ((document.defaultView)&&(document.defaultView.getComputedStyle)) {
 
177
        var style = document.defaultView.getComputedStyle(obj,null);
 
178
        if (style) {
 
179
            var x = parseInt(style.getPropertyValue("width"),10);
 
180
            var y = parseInt(style.getPropertyValue("height"),10);
 
181
            if ((x>0)&&(y>0)) return [x,y];
 
182
        }
 
183
    }*/
 
184
//    if (obj.clientWidth) return [obj.clientWidth, obj.clientHeight];
 
185
    return [obj.offsetWidth, obj.offsetHeight];
 
186
}
 
187
 
 
188
 
 
189
function domAlignNode(node, border, default_x, default_y, resize) {
 
190
    var width = windowGetWidth();
 
191
    var height = windowGetHeight();
 
192
    var size = domGetNodeSize(node);
 
193
 
 
194
    var size_x = 0;
 
195
    var size_y = 0;
 
196
    var pos_x = default_x;
 
197
    var pos_y = default_y;
 
198
 
 
199
    if ((pos_x + size[0] + border) > width) {
 
200
        var move = border + (pos_x + size[0]) - width;
 
201
        if (move > pos_x) {
 
202
            if (pos_x > border) move = pos_x - border;
 
203
            else {
 
204
                if (resize) {
 
205
                    // Using 2/3 of the screen
 
206
                    size_x = (width - 2 * border) * 2 / 3;
 
207
                    move = pos_x - (border + (width - 2 * border) / 6);
 
208
                } else move = 0;
 
209
            }
 
210
        }
 
211
                    
 
212
        if (move) pos_x -= move;
 
213
    }
 
214
 
 
215
    if ((pos_y + size[1] + border) > height) {
 
216
        var move = border + (pos_y + size[1]) - height;
 
217
        if (move > pos_y) {
 
218
            if (resize) {
 
219
                size_y = (height - 2 * border);
 
220
                move = pos_y - border;
 
221
            } else {
 
222
                if (pos_y > border) move = pos_y - border;
 
223
                else move = 0;
 
224
            }
 
225
        }
 
226
                    
 
227
        if (move) pos_y -= move;
 
228
    }
 
229
 
 
230
    if ((size_x)||(size_y)) {
 
231
        if (typeof resize == "function") resize(node, size_x, size_y);
 
232
        else {
 
233
            if (size_x) node.style.width = size_x + "px";
 
234
            if (size_y) node.style.height = size_y + "px";
 
235
        }
 
236
    }
 
237
 
 
238
    node.style.left = pos_x + "px";
 
239
    node.style.top = pos_y + "px";
 
240
 
 
241
        // Checking if everything is in place
 
242
    if ((!size_x)&&(!size_y)) {
 
243
        var new_size = domGetNodeSize(node);
 
244
        if ((new_size[0] != size[0])||(new_size[1] != size[1])) {
 
245
            if (new_size[0] == size[0]) size_x = 0;
 
246
            else size_x = size[0];
 
247
            if (new_size[1] == size[1]) size_y = 0;
 
248
            else size_y = size[1];
 
249
            
 
250
            if (typeof resize == "function") resize(node, size_x, size_y);
 
251
            else {
 
252
                if (size_x) node.style.width = size_x + "px";
 
253
                if (size_y) node.style.height = size_y + "px";
 
254
            }
 
255
        }
 
256
    }
 
257
}
 
258
 
 
259
function domSetMinHeight(node, height, extend, check_node) {
 
260
    if (typeof node.corr_y == "undefined") {
 
261
        if (domGetStyleProp(node, node.style.minHeight, "min-height"))
 
262
            node.style.minHeight = height + "px";
 
263
        else
 
264
            node.style.height = height + "px";
 
265
 
 
266
        if (check_node) node.corr_y = check_node.offsetHeight - height;
 
267
        else node.corr_y = node.offsetHeight - height;
 
268
 
 
269
        if (!node.corr_y) return;
 
270
        else if ((!extend)&&(node.corr_y<0)) {
 
271
            node.corr_y = 0;
 
272
            return;
 
273
        }
 
274
    }
 
275
 
 
276
    if (domGetStyleProp(node, node.style.minHeight, "min-height"))
 
277
        node.style.minHeight = (height - node.corr_y) + "px";
 
278
    else
 
279
        node.style.height = (height - node.corr_y) + "px";
 
280
}
 
281
 
 
282
function domSetHeight(node, height, extend, check_node) {
 
283
    if (typeof node.corr_y == "undefined") {
 
284
        node.style.height = height + "px";
 
285
 
 
286
        if (check_node) node.corr_y = check_node.offsetHeight - height;
 
287
        else node.corr_y = node.offsetHeight - height;
 
288
 
 
289
        if (!node.corr_y) return;
 
290
        else if ((!extend)&&(node.corr_y<0)) {
 
291
            node.corr_y = 0;
 
292
            return;
 
293
        }
 
294
    }
 
295
 
 
296
    node.style.height = (height - node.corr_y) + "px";
 
297
}
 
298
 
 
299
function domSetMinWidth(node, width, extend, check_node) {
 
300
    if (typeof node.corr_x == "undefined") {
 
301
        
 
302
            // In Opera/IE check returns true, but fails. With minHeight everything is OK
 
303
        if ((Prototype.Browser.Gecko)&&(domGetStyleProp(node, node.style.minWidth, "min-width")))
 
304
            node.style.minWidth = width + "px";
 
305
        else
 
306
            node.style.width = width + "px";
 
307
 
 
308
        if (check_node) node.corr_x = check_node.offsetWidth - width;
 
309
        else node.corr_x = node.offsetWidth - width;
 
310
 
 
311
        if (!node.corr_x) return;
 
312
        else if ((!extend)&&(node.corr_x<0)) {
 
313
            node.corr_x = 0;
 
314
            return;
 
315
        }
 
316
    }
 
317
    
 
318
    if ((Prototype.Browser.Gecko)&&(domGetStyleProp(node, node.style.minWidth, "min-width")))
 
319
        node.style.minWidth = (width - node.corr_x) + "px";
 
320
    else
 
321
        node.style.width = (width - node.corr_x) + "px";
 
322
}
 
323
 
 
324
function domSetWidth(node, width, extend, check_node) {
 
325
    if (typeof node.corr_x == "undefined") {
 
326
        node.style.width = width + "px";
 
327
 
 
328
        if (check_node) node.corr_x = check_node.offsetWidth - width;
 
329
        else node.corr_x = node.offsetWidth - width;
 
330
 
 
331
        if (!node.corr_x) return;
 
332
        else if ((!extend)&&(node.corr_x<0)) {
 
333
            node.corr_x = 0;
 
334
            return;
 
335
        }
 
336
    }
 
337
    
 
338
    node.style.width = (width - node.corr_x) + "px";
 
339
}
 
340
 
 
341
 
 
342
function dateFormat(d) {
 
343
    return (d.getUTCMonth()+1) + '/' + d.getUTCDate() + '/' + d.getUTCFullYear() + ' ' +
 
344
        d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds();
 
345
}
 
346
 
 
347
function eventCanceler(ev) {
 
348
    Event.stop(ev);
 
349
}