Project

General

Profile

1
// Document
2
// HTMLDocument is an extension of Document.  If the browser has HTMLDocument but not Document, the former will suffice as an alias for the latter.
3
if (!this.Document){this.Document = this.HTMLDocument; }
4

    
5
// Element
6
if (!window.HTMLElement) { window.HTMLElement = window.Element; }
7

    
8
// Window
9
(function(global) {
10
    if (global.constructor) {
11
        global.Window = global.constructor;
12
    } else {
13
        (global.Window = global.constructor = new Function('return function Window() {}')()).prototype = this;
14
    }
15
}(this));
16

    
17
// Date.now
18
if(!Date.now){ Date.now = function now() { return new Date().getTime(); }; }
19

    
20
// performance.now
21
(function(){
22
    if ("performance" in window == false) {    window.performance = {}; }
23

    
24
    if ("now" in window.performance == false){
25
        var nowOffset = Date.now();
26

    
27
        window.performance.now = function now(){
28
            return Date.now() - nowOffset;
29
        }
30
    }
31
})();
32

    
33

    
34
// Array.prototype.indexOf
35
if (!Array.prototype.indexOf) {
36
    Array.prototype.indexOf = function indexOf(searchElement) {
37
        if (this === undefined || this === null) {
38
            throw new TypeError(this + 'is not an object');
39
        }
40

    
41
        var    arraylike = this instanceof String ? this.split('') : this,
42
            length = Math.max(Math.min(arraylike.length, 9007199254740991), 0) || 0,
43
            index = Number(arguments[1]) || 0;
44

    
45
        index = (index < 0 ? Math.max(length + index, 0) : index) - 1;
46

    
47
        while (++index < length) {
48
            if (index in arraylike && arraylike[index] === searchElement) {
49
                return index;
50
            }
51
        }
52

    
53
        return -1;
54
    };
55
}
56

    
57
// getComputedStyle
58
if (!('getComputedStyle' in window)) {
59
    (function(){
60
        function getComputedStylePixel(element, property, fontSize) {
61

    
62
            // Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed.
63
            var value = element.document && element.currentStyle[property].match(/([\d\.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''],
64
                size = value[1],
65
                suffix = value[2],
66
                rootSize;
67

    
68
            fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16;
69
            rootSize = property == 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
70

    
71
            return suffix == '%' ? size / 100 * rootSize :
72
                suffix == 'cm' ? size * 0.3937 * 96 :
73
                suffix == 'em' ? size * fontSize :
74
                suffix == 'in' ? size * 96 :
75
                suffix == 'mm' ? size * 0.3937 * 96 / 10 :
76
                suffix == 'pc' ? size * 12 * 96 / 72 :
77
                suffix == 'pt' ? size * 96 / 72 :
78
                size;
79
        }
80

    
81
        function setShortStyleProperty(style, property) {
82
            var    borderSuffix = property == 'border' ? 'Width' : '',
83
                t = property + 'Top' + borderSuffix,
84
                r = property + 'Right' + borderSuffix,
85
                b = property + 'Bottom' + borderSuffix,
86
                l = property + 'Left' + borderSuffix;
87

    
88
            style[property] = (style[t] == style[r] && style[t] == style[b] && style[t] == style[l] ? [ style[t] ] :
89
                            style[t] == style[b] && style[l] == style[r] ? [ style[t], style[r] ] :
90
                            style[l] == style[r] ? [ style[t], style[r], style[b] ] :
91
                            [ style[t], style[r], style[b], style[l] ]).join(' ');
92
        }
93

    
94
        // <CSSStyleDeclaration>
95
        function CSSStyleDeclaration(element) {
96
            var style = this,
97
            currentStyle = element.currentStyle,
98
            fontSize = getComputedStylePixel(element, 'fontSize'),
99
            unCamelCase = function (match) {
100
                return '-' + match.toLowerCase();
101
            },
102
            property;
103

    
104
            for (property in currentStyle) {
105
                Array.prototype.push.call(style, property == 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase));
106

    
107
                if (property == 'width') {
108
                    style[property] = element.offsetWidth + 'px';
109
                } else if (property == 'height') {
110
                    style[property] = element.offsetHeight + 'px';
111
                } else if (property == 'styleFloat') {
112
                    style.float = currentStyle[property];
113
                } else if (/margin.|padding.|border.+W/.test(property) && style[property] != 'auto') {
114
                    style[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px';
115
                } else if (/^outline/.test(property)) {
116
                    // errors on checking outline
117
                    try {
118
                        style[property] = currentStyle[property];
119
                    } catch (error) {
120
                        style.outlineColor = currentStyle.color;
121
                        style.outlineStyle = style.outlineStyle || 'none';
122
                        style.outlineWidth = style.outlineWidth || '0px';
123
                        style.outline = [style.outlineColor, style.outlineWidth, style.outlineStyle].join(' ');
124
                    }
125
                } else {
126
                    style[property] = currentStyle[property];
127
                }
128
            }
129

    
130
            setShortStyleProperty(style, 'margin');
131
            setShortStyleProperty(style, 'padding');
132
            setShortStyleProperty(style, 'border');
133

    
134
            style.fontSize = Math.round(fontSize) + 'px';
135
        }
136

    
137
        CSSStyleDeclaration.prototype = {
138
            constructor: CSSStyleDeclaration,
139
            // <CSSStyleDeclaration>.getPropertyPriority
140
            getPropertyPriority: function () {
141
                throw new Error('NotSupportedError: DOM Exception 9');
142
            },
143
            // <CSSStyleDeclaration>.getPropertyValue
144
            getPropertyValue: function (property) {
145
                return this[property.replace(/-\w/g, function (match) {
146
                    return match[1].toUpperCase();
147
                })];
148
            },
149
            // <CSSStyleDeclaration>.item
150
            item: function (index) {
151
                return this[index];
152
            },
153
            // <CSSStyleDeclaration>.removeProperty
154
            removeProperty: function () {
155
                throw new Error('NoModificationAllowedError: DOM Exception 7');
156
            },
157
            // <CSSStyleDeclaration>.setProperty
158
            setProperty: function () {
159
                throw new Error('NoModificationAllowedError: DOM Exception 7');
160
            },
161
            // <CSSStyleDeclaration>.getPropertyCSSValue
162
            getPropertyCSSValue: function () {
163
                throw new Error('NotSupportedError: DOM Exception 9');
164
            }
165
        };
166

    
167
        // <Global>.getComputedStyle
168
        window.getComputedStyle = function getComputedStyle(element) {
169
            return new CSSStyleDeclaration(element);
170
        };
171
    })();
172
}
173

    
174
// Event
175
if (!window.Event||!Window.prototype.Event) {
176
    (function (){
177
        window.Event = Window.prototype.Event = Document.prototype.Event = Element.prototype.Event = function Event(type, eventInitDict) {
178
            if (!type) { throw new Error('Not enough arguments'); }
179
            var event,
180
                bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false,
181
                cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
182
            if ( 'createEvent' in document ) {
183
                event = document.createEvent('Event');
184
                event.initEvent(type, bubbles, cancelable);
185
            } else {
186
                event = document.createEventObject();
187
                event.type = type;
188
                event.bubbles = bubbles;
189
                event.cancelable = cancelable;
190
            }
191
            return event;
192
        };
193
    })();
194
}
195

    
196
// CustomEvent
197
if (!('CustomEvent' in window) || !('CustomEvent' in Window.prototype)) {
198
    (function(){
199
        window.CustomEvent = Window.prototype.CustomEvent = Document.prototype.CustomEvent = Element.prototype.CustomEvent = function CustomEvent(type, eventInitDict) {
200
            if (!type) {
201
                throw Error('TypeError: Failed to construct "CustomEvent": An event name must be provided.');
202
            }
203
            var event = new Event(type, eventInitDict);
204
            event.detail = eventInitDict && eventInitDict.detail || null;
205
            return event;
206
        };
207

    
208
    })()
209
}
210

    
211
// addEventListener
212
if (!window.addEventListener||!Window.prototype.addEventListener) {
213
    (function (){
214
        window.addEventListener = Window.prototype.addEventListener = Document.prototype.addEventListener = Element.prototype.addEventListener = function addEventListener() {
215
            var    element = this,
216
                type = arguments[0],
217
                listener = arguments[1];
218

    
219
            if (!element._events) {    element._events = {}; }
220

    
221
            if (!element._events[type]) {
222
                element._events[type] = function (event) {
223
                    var    list = element._events[event.type].list,
224
                        events = list.slice(),
225
                        index = -1,
226
                        length = events.length,
227
                        eventElement;
228

    
229
                    event.preventDefault = function preventDefault() {
230
                        if (event.cancelable !== false) {
231
                            event.returnValue = false;
232
                        }
233
                    };
234

    
235
                    event.stopPropagation = function stopPropagation() {
236
                        event.cancelBubble = true;
237
                    };
238

    
239
                    event.stopImmediatePropagation = function stopImmediatePropagation() {
240
                        event.cancelBubble = true;
241
                        event.cancelImmediate = true;
242
                    };
243

    
244
                    event.currentTarget = element;
245
                    event.relatedTarget = event.fromElement || null;
246
                    event.target = event.target || event.srcElement || element;
247
                    event.timeStamp = new Date().getTime();
248

    
249
                    if (event.clientX) {
250
                        event.pageX = event.clientX + document.documentElement.scrollLeft;
251
                        event.pageY = event.clientY + document.documentElement.scrollTop;
252
                    }
253

    
254
                    while (++index < length && !event.cancelImmediate) {
255
                        if (index in events) {
256
                            eventElement = events[index];
257

    
258
                            if (list.indexOf(eventElement) !== -1 && typeof eventElement === 'function') {
259
                                eventElement.call(element, event);
260
                            }
261
                        }
262
                    }
263
                };
264

    
265
                element._events[type].list = [];
266

    
267
                if (element.attachEvent) {
268
                    element.attachEvent('on' + type, element._events[type]);
269
                }
270
            }
271

    
272
            element._events[type].list.push(listener);
273
        };
274

    
275
        window.removeEventListener = Window.prototype.removeEventListener = Document.prototype.removeEventListener = Element.prototype.removeEventListener = function removeEventListener() {
276
            var    element = this,
277
                type = arguments[0],
278
                listener = arguments[1],
279
                index;
280

    
281
            if (element._events && element._events[type] && element._events[type].list) {
282
                index = element._events[type].list.indexOf(listener);
283

    
284
                if (index !== -1) {
285
                    element._events[type].list.splice(index, 1);
286

    
287
                    if (!element._events[type].list.length) {
288
                        if (element.detachEvent) {
289
                            element.detachEvent('on' + type, element._events[type]);
290
                        }
291
                        delete element._events[type];
292
                    }
293
                }
294
            }
295
        };
296
    })();
297
}
298

    
299
// Event dispatcher    / trigger
300
if (!window.dispatchEvent||!Window.prototype.dispatchEvent||!Document.prototype.dispatchEvent||!Element.prototype.dispatchEvent) {
301
    (function(){
302
        window.dispatchEvent = Window.prototype.dispatchEvent = Document.prototype.dispatchEvent = Element.prototype.dispatchEvent = function dispatchEvent(event) {
303
            if (!arguments.length) {
304
                throw new Error('Not enough arguments');
305
            }
306

    
307
            if (!event || typeof event.type !== 'string') {
308
                throw new Error('DOM Events Exception 0');
309
            }
310

    
311
            var element = this, type = event.type;
312

    
313
            try {
314
                if (!event.bubbles) {
315
                    event.cancelBubble = true;
316

    
317
                    var cancelBubbleEvent = function (event) {
318
                        event.cancelBubble = true;
319

    
320
                        (element || window).detachEvent('on' + type, cancelBubbleEvent);
321
                    };
322

    
323
                    this.attachEvent('on' + type, cancelBubbleEvent);
324
                }
325

    
326
                this.fireEvent('on' + type, event);
327
            } catch (error) {
328
                event.target = element;
329

    
330
                do {
331
                    event.currentTarget = element;
332

    
333
                    if ('_events' in element && typeof element._events[type] === 'function') {
334
                        element._events[type].call(element, event);
335
                    }
336

    
337
                    if (typeof element['on' + type] === 'function') {
338
                        element['on' + type].call(element, event);
339
                    }
340

    
341
                    element = element.nodeType === 9 ? element.parentWindow : element.parentNode;
342
                } while (element && !event.cancelBubble);
343
            }
344

    
345
            return true;
346
        };
347
    })();
348
}
(8-8/17)