OSDN Git Service

Version 0.6.73, move ._rawNode from X.Dom.Event to X.EventDispatcher.
[pettanr/clientJs.git] / 0.6.x / js / 00_core / 00_builtin.js
1 /* \r
2  * ビルトインオブジェクトに拡張したい10のメソッド\r
3  * http://d.hatena.ne.jp/ofk/20080922/1222047483\r
4  */\r
5 \r
6 Function.prototype.apply || (Function.prototype.apply = function (x, y) {\r
7         var a, i, r, j;\r
8         x = x || window;\r
9         y = y || [];\r
10         \r
11         // apply 内で apply を呼んだ場合に備える\r
12         if( x === window ){\r
13                 x.__apply = void 0;\r
14         } else {\r
15                 if( x.constructor && x.constructor.prototype.__apply ){\r
16                         delete x.constructor.prototype.__apply;\r
17                 } else\r
18                 if( x.__apply ) delete x.__apply;\r
19         };\r
20         \r
21         x.__apply = this;\r
22         if (!x.__apply) x.constructor.prototype.__apply = this;\r
23         j = y.length;\r
24         switch (j) {\r
25                 case 0: r = x.__apply(); break;\r
26                 case 1: r = x.__apply(y[0]); break;\r
27                 case 2: r = x.__apply(y[0], y[1]); break;\r
28                 case 3: r = x.__apply(y[0], y[1], y[2]); break;\r
29                 case 4: r = x.__apply(y[0], y[1], y[2], y[3]); break;\r
30                 case 5: r = x.__apply(y[0], y[1], y[2], y[3], y[4]); break;\r
31                 case 6: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5]); break;\r
32                 case 7: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6]); break;\r
33                 case 8: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7]); break;\r
34                 case 9: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8]); break;\r
35                 default:\r
36                         a = [];\r
37                         for (i = 0; i < j; ++i)\r
38                                 a[i] = 'y[' + i + ']';\r
39                         //r = eval('x.__apply(' + a.join(',') + ')');\r
40                         // closuer compiler 対策\r
41                         r = (new Function( 'x,y', 'return x.__apply(' + a.join(',') + ')' ))( x, y );\r
42                         break;\r
43         };\r
44         // ie5\r
45         if( x === window ){\r
46                 x.__apply = void 0;\r
47         } else {\r
48                 //alert( typeof x );\r
49                 if( x.constructor && x.constructor.prototype.__apply ){\r
50                         delete x.constructor.prototype.__apply;\r
51                 } else\r
52                 if( x.__apply ) delete x.__apply;\r
53         };\r
54         return r;\r
55 });\r
56 Function.prototype.call || (Function.prototype.call = function () {\r
57         var a = arguments, x = a[0], y = [], i = 1, j = a.length;\r
58         for (; i < j; ++i)\r
59                 y[i - 1] = a[i];\r
60         return this.apply(x, y);\r
61 });\r
62 \r
63 Array.prototype.pop || (Array.prototype.pop = function () {\r
64         var r = this[this.length - 1];\r
65         --this.length;\r
66         return r;\r
67 });\r
68 Array.prototype.push || (Array.prototype.push = function () {\r
69         var a = arguments, i = 0, j = a.length, l = this.length;\r
70         for (; i < j; ++i)\r
71                 this[l + i] = a[i];\r
72         return this.length;\r
73 });\r
74 Array.prototype.shift || (Array.prototype.shift = function () {\r
75         var r = this[0], i = 1, j = this.length;\r
76         for( ; i < j; ++i)\r
77                 this[i - 1] = this[i];\r
78         --this.length;\r
79         return r;\r
80 });\r
81 Array.prototype.unshift || (Array.prototype.unshift = function () {\r
82         var a = arguments, l = a.length, j = this.length += l - 1, i = j;\r
83         for (; i >= l; --i)\r
84                 this[i] = this[i - l];\r
85         for (i = 0; i < l; ++i)\r
86                 this[i] = a[i];\r
87         return j;\r
88 });\r
89 Array.prototype.splice || (Array.prototype.splice = function (x, y) {\r
90         var a = arguments, s = a.length - 2 - y, r = this.slice(x, x + y),i,j;\r
91         if (s > 0) {\r
92                 for (i = this.length - 1, j = x + y; i >= j; --i)\r
93                         this[i + s] = this[i];\r
94         }\r
95         else if (s < 0) {\r
96                 for (i = x + y, j = this.length; i < j; ++i)\r
97                         this[i + s] = this[i];\r
98                 this.length += s;\r
99         }\r
100         for (i = 2, j = a.length; i < j; ++i)\r
101                 this[i - 2 + x] = a[i];\r
102         return r;\r
103 });\r
104 \r
105 /*\r
106  * original\r
107  * JavaScript 1.6, Array.indexOfを下位互換実装する\r
108  * http://www.inazumatv.com/contents/archives/7965\r
109  */\r
110 \r
111 Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, fromIndex ){\r
112         var l = this.length >>> 0, i;\r
113         \r
114         if( l === 0 ) return -1;\r
115         \r
116         if( fromIndex ){\r
117             i = fromIndex || 0;\r
118             i = i === -Infinity ? 0 : ( i < 0 ? -i : i ) | 0; // Math.floor\r
119             if( l <= i ) return -1;\r
120         };\r
121 \r
122         for( i = 0 <= i ? i : 0 < l + i ? l + i : 0; i < l; ++i ){\r
123             if( this[ i ] === searchElement ) return i;\r
124         };\r
125         return -1;\r
126         });\r
127 \r
128 \r
129 /*\r
130  * Window\r
131  * by http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html\r
132  */\r
133 \r
134 /* 正規表現が使われているため、まだ投入しない itozyun\r
135 window.encodeURI || (window.encodeURI = function (x) {\r
136         return ("" + x).replace(/[^!#$&-;=?-Z_a-z~]/g, function (s) {\r
137                 var c = s.charCodeAt(0), p = "%";\r
138                 return (\r
139                         c < 16 ? "%0" + c.toString(16) :\r
140                         c < 128 ? p + c.toString(16) :\r
141                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
142                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
143                 ).toUpperCase();\r
144         });\r
145 });\r
146 \r
147 window.encodeURIComponent || (window.encodeURIComponent = function (x) {\r
148         return ("" + x).replace(/[^!'-*.0-9A-Z_a-z~-]/g, function (s) {\r
149                 var c = s.charCodeAt(0), p = "%";\r
150                 return (\r
151                         c < 16 ? "%0" + c.toString(16) :\r
152                         c < 128 ? p + c.toString(16) :\r
153                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
154                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
155                 ).toUpperCase();\r
156         });\r
157 });\r
158 \r
159 // 手抜き\r
160 window.decodeURI || (window.decodeURI = function (x) {\r
161         return ("" + x).replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig, function (s) {\r
162                 var c = parseInt(s.substring(1), 16);\r
163                 return String.fromCharCode(\r
164                         c < 128 ? c :\r
165                         c < 224 ? (c & 31) << 6 | parseInt(s.substring(4), 16) & 63 :\r
166                         ((c & 15) << 6 | parseInt(s.substring(4), 16) & 63) << 6 | parseInt(s.substring(7), 16) & 63\r
167                 );\r
168         });\r
169 });\r
170 */\r
171 \r
172 \r
173 //window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);\r
174 \r
175 \r
176 /*\r
177  * String\r
178  */\r
179 \r
180 // replace(RegExp, Function)対応\r
181 /*\r
182 if (window.ActiveXObject ? !Number.prototype.toFixed : (!navigator.taintEnabled && !document.createElement("input").setSelectionRange))\r
183         (function () {\r
184                 var g = String.prototype.replace;\r
185                 String.prototype.replace = function (x, y) {\r
186                         var s = this, z = y;\r
187                         // 第二引数が関数\r
188                         if (y instanceof Function) {\r
189                                 // 第一引数が正規表現\r
190                                 if (x instanceof RegExp) {\r
191                                         // その上、グローバルマッチ\r
192                                         if (x.global || /^\/.*g$/.test(x)) {\r
193                                                 var r = [], m;\r
194                                                 while ((m = x.exec(s)) != null) {\r
195                                                         var i = m.index;\r
196                                                         r[r.length] = s.slice(0, i);\r
197                                                         s = s.slice(i + m[0].length);\r
198                                                         r[r.length] = y.apply(null, m.concat(i, this));\r
199                                                 }\r
200                                                 r[r.length] = s;\r
201                                                 return r.join("");\r
202                                         }\r
203                                         var m = x.exec(s);\r
204                                         if (!m)\r
205                                                 return s;\r
206                                         z = y.apply(null, m.concat(m.index, s));\r
207                                 }\r
208                                 else {\r
209                                         var i = s.indexOf(x);\r
210                                         if (i < 0)\r
211                                                 return s;\r
212                                         z = y(x, i, s);\r
213                                 }\r
214                         }\r
215                         return g.call(s, x, z);\r
216                 };\r
217         })(); */\r
218         \r
219 /*\r
220  * Safari の JavaScript の不備 \r
221  * http://nanto.asablo.jp/blog/2006/01/13/209495\r
222  * \r
223  * web.paulownia.jp - JavaScriptとクロージャ\r
224  * https://web.archive.org/web/20070526063400/http://web.paulownia.jp/script/oop/closure.html\r
225  * MacOSX 10.3のsafariにはhasOwnPropertyが実装されていないので、独自に追加する必要があります。\r
226  */\r
227 /*\r
228 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
229                 var proto = this.constructor && this.constructor.prototype,\r
230                         __p__ = proto && proto.__proto__,\r
231                         v     = this[ p ],\r
232                         r     = false;\r
233                 \r
234                 if( __p__ ) proto.__proto__ = null;\r
235                 \r
236                 if( p in this ){\r
237                         if( v !== v ){\r
238                                 if( proto && ( p in proto ) && proto[ p ] !== proto[ p ] ){ // proto[ p ] is NaN\r
239                                         proto[ p ] = 0; // different value\r
240                                         r = this[ p ] !== this[ p ]; // isNaN?\r
241                                         proto[ p ] = v; // set NaN\r
242                                 } else {\r
243                                         r = true;\r
244                                 };\r
245                         } else\r
246                         if( proto && p in proto && proto[ p ] === v ){\r
247                                 // this と proto に同名で同値が書かれている可能性あり\r
248                                 proto[ p ] = v + ' '; // different value\r
249                                 r = v === this[ p ];\r
250                                 proto[ p ] = v;\r
251                         } else {\r
252                                 r = true;\r
253                         };\r
254                 };\r
255                 \r
256                 if( __p__ ) proto.__proto__ = __p__;\r
257                 \r
258                 return r;\r
259   }); */\r
260 /*\r
261 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
262                 var proto = this.constructor && this.constructor.prototype,\r
263                         __p__ = proto && proto.__proto__,\r
264                         r     = false,//!!( __p__ && ( proto.__proto__ = null ) )\r
265                         _pro_, v, isNaN;\r
266                 \r
267                 if( __p__ ) proto.__proto__ = null;\r
268                 if( this.__proto__ ){\r
269                         _pro_ = this.__proto__;\r
270                         this.__proto__ = null;\r
271                 };\r
272                 \r
273                 if( p === '__proto__' ){\r
274                         r = !!_pro_;\r
275                 } else {\r
276                         v     = this[ p ];\r
277                         isNaN = v !== v;                \r
278                         \r
279                         if( p in this ){\r
280                                 if( proto && p in proto && ( proto[ p ] === v ) ^ isNaN ){ //true + false, false + true\r
281                                         // this と proto に同名で同値が書かれている可能性あり\r
282                                         proto[ p ] = v + ' '; // different value\r
283                                         r = ( v === this[ p ] ) ^ isNaN; // true + false, false + true\r
284                                         proto[ p ] = v;\r
285                                 } else {\r
286                                         r = true;\r
287                                 };\r
288                         };                      \r
289                 };\r
290 \r
291                 if( __p__ ) proto.__proto__ = __p__;\r
292                 if( _p_ ) this.__proto__ = _pro_;\r
293                 return r;\r
294   }); */