summaryrefslogtreecommitdiffstats
path: root/basicsuite/webengine/content/rubiks/js/oz.js
blob: b821c04a3df01d7575dd3d1f86b31ed3c38646c7 (plain)
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
/* (c) 2007 - now() Ondrej Zara, 1.7 */
var OZ = {
    $:function(x) { return typeof(x) == "string" ? document.getElementById(x) : x; },
    select: function(x) { return document.querySelectorAll(x); },
    opera:!!window.opera,
    ie:!!document.attachEvent && !window.opera,
    gecko:!!document.getAnonymousElementByAttribute,
    webkit:!!navigator.userAgent.match(/webkit/i),
    khtml:!!navigator.userAgent.match(/khtml/i) || !!navigator.userAgent.match(/konqueror/i),
    Event:{
        _id:0,
        _byName:{},
        _byID:{},
        add:function(elm,event,cb) {
            var id = OZ.Event._id++;
            var element = OZ.$(elm);
            var fnc = (element && element.attachEvent ? function() { return cb.apply(element,arguments); } : cb);
            var rec = [element,event,fnc];
            var parts = event.split(" ");
            while (parts.length) {
                var e = parts.pop();
                if (element) {
                    if (element.addEventListener) {
                        element.addEventListener(e,fnc,false);
                    } else if (element.attachEvent) {
                        element.attachEvent("on"+e,fnc);
                    }
                }
                if (!(e in OZ.Event._byName)) { OZ.Event._byName[e] = {}; }
                OZ.Event._byName[e][id] = rec;
            }
            OZ.Event._byID[id] = rec;
            return id;
        },
        remove:function(id) {
            var rec = OZ.Event._byID[id];
            if (!rec) { return; }
            var elm = rec[0];
            var parts = rec[1].split(" ");
            while (parts.length) {
                var e = parts.pop();
                if (elm) {
                    if (elm.removeEventListener) {
                        elm.removeEventListener(e,rec[2],false);
                    } else if (elm.detachEvent) {
                        elm.detachEvent("on"+e,rec[2]);
                    }
                }
                delete OZ.Event._byName[e][id];
            }
            delete OZ.Event._byID[id];
        },
        stop:function(e) { e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true; },
        prevent:function(e) { e.preventDefault ? e.preventDefault() : e.returnValue = false; },
        target:function(e) { return e.target || e.srcElement; }
    },
    Class:function() {
        var c = function() {
            var init = arguments.callee.prototype.init;
            if (init) { init.apply(this,arguments); }
        };
        c.implement = function(parent) {
            for (var p in parent.prototype) { this.prototype[p] = parent.prototype[p]; }
            return this;
        };
        c.extend = function(parent) {
            var tmp = function(){};
            tmp.prototype = parent.prototype;
            this.prototype = new tmp();
            this.prototype.constructor = this;
            return this;
        };
        c.prototype.bind = function(fnc) { return fnc.bind(this); };
        c.prototype.dispatch = function(type, data) {
            var obj = {
                type:type,
                target:this,
                timeStamp:(new Date()).getTime(),
                data:data
            }
            var tocall = [];
            var list = OZ.Event._byName[type];
            for (var id in list) {
                var item = list[id];
                if (!item[0] || item[0] == this) { tocall.push(item[2]); }
            }
            var len = tocall.length;
            for (var i=0;i<len;i++) { tocall[i](obj); }
        }
        return c;
    },
    DOM:{
        elm:function(name, opts) {
            var elm = document.createElement(name);
            for (var p in opts) {
                var val = opts[p];
                if (p == "class") { p = "className"; }
                if (p in elm) { elm[p] = val; }
            }
            OZ.Style.set(elm, opts);
            return elm;
        },
        text:function(str) { return document.createTextNode(str); },
        clear:function(node) { while (node.firstChild) {node.removeChild(node.firstChild);} },
        pos:function(elm) { /* relative to _viewport_ */
            var cur = OZ.$(elm);
            var html = cur.ownerDocument.documentElement;
            var parent = cur.parentNode;
            var x = y = 0;
            if (cur == html) { return [x,y]; }
            while (1) {
                if (OZ.Style.get(cur,"position") == "fixed") {
                    x += cur.offsetLeft;
                    y += cur.offsetTop;
                    return [x,y];
                }

                if (OZ.opera && (parent == html || OZ.Style.get(cur,"display") != "block")) { } else {
                    x -= parent.scrollLeft;
                    y -= parent.scrollTop;
                }
                if (parent == cur.offsetParent || cur.parentNode == html) {
                    x += cur.offsetLeft;
                    y += cur.offsetTop;
                    cur = parent;
                }

                if (parent == html) { return [x,y]; }
                parent = parent.parentNode;
            }
        },
        scroll:function() {
            var x = document.documentElement.scrollLeft || document.body.scrollLeft || 0;
            var y = document.documentElement.scrollTop || document.body.scrollTop || 0;
            return [x,y];
        },
        win:function(avail) {
            return (avail ? [window.innerWidth,window.innerHeight] : [document.documentElement.clientWidth,document.documentElement.clientHeight]);
        },
        hasClass:function(node, className) {
            var cn = OZ.$(node).className;
            var arr = (cn ? cn.split(" ") : []);
            return (arr.indexOf(className) != -1);
        },
        addClass:function(node,className) {
            if (OZ.DOM.hasClass(node, className)) { return; }
            var cn = OZ.$(node).className;
            var arr = (cn ? cn.split(" ") : []);
            arr.push(className);
            OZ.$(node).className = arr.join(" ");
        },
        removeClass:function(node, className) {
            if (!OZ.DOM.hasClass(node, className)) { return; }
            var cn = OZ.$(node).className;
            var arr = (cn ? cn.split(" ") : []);
            var arr = arr.filter(function($){ return $ != className; });
            OZ.$(node).className = arr.join(" ");
        },
        append:function() {
            if (arguments.length == 1) {
                var arr = arguments[0];
                var root = OZ.$(arr[0]);
                for (var i=1;i<arr.length;i++) { root.appendChild(OZ.$(arr[i])); }
            } else for (var i=0;i<arguments.length;i++) { OZ.DOM.append(arguments[i]); }
        }
    },
    Style:{
        get:function(elm, prop) {
            if (document.defaultView && document.defaultView.getComputedStyle) {
                try {
                    var cs = elm.ownerDocument.defaultView.getComputedStyle(elm,"");
                } catch(e) {
                    return false;
                }
                if (!cs) { return false; }
                return cs[prop];
            } else {
                return elm.currentStyle[prop];
            }
        },
        set:function(elm, obj) {
            for (var p in obj) {
                var val = obj[p];
                if (p == "opacity" && OZ.ie) {
                    p = "filter";
                    val = "alpha(opacity="+Math.round(100*val)+")";
                    elm.style.zoom = 1;
                } else if (p == "float") {
                    p = (OZ.ie ? "styleFloat" : "cssFloat");
                }
                if (p in elm.style) { elm.style[p] = val; }
            }
        }
    },
    Request:function(url, callback, options) {
        var o = {data:false, method:"get", headers:{}, xml:false}
        for (var p in options) { o[p] = options[p]; }
        o.method = o.method.toUpperCase();

        var xhr = false;
        if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); }
        else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
        else { return false; }
        xhr.open(o.method, url, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState != 4) { return; }
            if (!callback) { return; }
            var data = (o.xml ? xhr.responseXML : xhr.responseText);
            var headers = {};
            var h = xhr.getAllResponseHeaders();
            if (h) {
                h = h.split(/[\r\n]/);
                for (var i=0;i<h.length;i++) if (h[i]) {
                    var v = h[i].match(/^([^:]+): *(.*)$/);
                    headers[v[1]] = v[2];
                }
            }
            callback(data,xhr.status,headers);
        };
        if (o.method == "POST") { xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); }
        for (var p in o.headers) { xhr.setRequestHeader(p,o.headers[p]); }
        xhr.send(o.data || null);
        return xhr;
    }
}

if (!Function.prototype.bind) {
    Function.prototype.bind = function(thisObj) {
        var fn = this;
        var args = Array.prototype.slice.call(arguments, 1);
        return function() {
            return fn.apply(thisObj, args.concat(Array.prototype.slice.call(arguments)));
        }
    }
};

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(item, from) {
        var len = this.length;
        var i = from || 0;
        if (i < 0) { i += len; }
        for (;i<len;i++) {
            if (i in this && this[i] === item) { return i; }
        }
        return -1;
    }
}
if (!Array.indexOf) {
    Array.indexOf = function(obj, item, from) { return Array.prototype.indexOf.call(obj, item, from); }
}

if (!Array.prototype.lastIndexOf) {
    Array.prototype.lastIndexOf = function(item, from) {
        var len = this.length;
        var i = from || len-1;
        if (i < 0) { i += len; }
        for (;i>-1;i--) {
            if (i in this && this[i] === item) { return i; }
        }
        return -1;
    }
}
if (!Array.lastIndexOf) {
    Array.lastIndexOf = function(obj, item, from) { return Array.prototype.lastIndexOf.call(obj, item, from); }
}

if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(cb, _this) {
        var len = this.length;
        for (var i=0;i<len;i++) {
            if (i in this) { cb.call(_this, this[i], i, this); }
        }
    }
}
if (!Array.forEach) {
    Array.forEach = function(obj, cb, _this) { Array.prototype.forEach.call(obj, cb, _this); }
}

if (!Array.prototype.every) {
    Array.prototype.every = function(cb, _this) {
        var len = this.length;
        for (var i=0;i<len;i++) {
            if (i in this && !cb.call(_this, this[i], i, this)) { return false; }
        }
        return true;
    }
}
if (!Array.every) {
    Array.every = function(obj, cb, _this) { return Array.prototype.every.call(obj, cb, _this); }
}

if (!Array.prototype.some) {
    Array.prototype.some = function(cb, _this) {
        var len = this.length;
        for (var i=0;i<len;i++) {
            if (i in this && cb.call(_this, this[i], i, this)) { return true; }
        }
        return false;
    }
}
if (!Array.some) {
    Array.some = function(obj, cb, _this) { return Array.prototype.some.call(obj, cb, _this); }
}

if (!Array.prototype.map) {
    Array.prototype.map = function(cb, _this) {
        var len = this.length;
        var res = new Array(len);
        for (var i=0;i<len;i++) {
            if (i in this) { res[i] = cb.call(_this, this[i], i, this); }
        }
        return res;
    }
}
if (!Array.map) {
    Array.map = function(obj, cb, _this) { return Array.prototype.map.call(obj, cb, _this); }
}

if (!Array.prototype.filter) {
    Array.prototype.filter = function(cb, _this) {
        var len = this.length;
        var res = [];
            for (var i=0;i<len;i++) {
                if (i in this) {
                    var val = this[i];
                    if (cb.call(_this, val, i, this)) { res.push(val); }
                }
            }
        return res;
    }
}
if (!Array.filter) {
    Array.filter = function(obj, cb, _this) { return Array.prototype.filter.call(obj, cb, _this); }
}