1 |
62654413
|
Scott Ullrich
|
(function(_scope){
|
2 |
|
|
|
3 |
|
|
/*
|
4 |
|
|
* pi.js
|
5 |
|
|
* 1.0
|
6 |
|
|
* Azer Koçulu <http://azer.kodfabrik.com>
|
7 |
|
|
* http://pi-js.googlecode.com
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
_scope.pi = Object(3.14159265358979323846);
|
11 |
|
|
var pi = _scope.pi;
|
12 |
|
|
pi.version = 1.0;
|
13 |
|
|
|
14 |
|
|
pi.env = {
|
15 |
|
|
ie: /MSIE/i.test(navigator.userAgent),
|
16 |
|
|
ie6: /MSIE 6/i.test(navigator.userAgent),
|
17 |
|
|
ie7: /MSIE 7/i.test(navigator.userAgent),
|
18 |
|
|
ie8: /MSIE 8/i.test(navigator.userAgent),
|
19 |
|
|
firefox: /Firefox/i.test(navigator.userAgent),
|
20 |
|
|
opera: /Opera/i.test(navigator.userAgent),
|
21 |
|
|
webkit: /Webkit/i.test(navigator.userAgent)
|
22 |
|
|
};
|
23 |
|
|
|
24 |
|
|
pi.util = {
|
25 |
|
|
IsArray:function(_object){
|
26 |
|
|
return _object && _object != window && ( _object instanceof Array || ( typeof _object.length == "number" && typeof _object.item =="function" ) )
|
27 |
|
|
},
|
28 |
|
|
IsHash:function(_object){
|
29 |
|
|
return _object && typeof _object=="object"&&(_object==window||_object instanceof Object)&&!_object.nodeName&&!pi.util.IsArray(_object)
|
30 |
|
|
},
|
31 |
|
|
DOMContentLoaded:[],
|
32 |
|
|
AddEvent: function(_element,_eventName,_fn,_useCapture){
|
33 |
|
|
_element[pi.env.ie.toggle("attachEvent","addEventListener")](pi.env.ie.toggle("on","")+_eventName,_fn,_useCapture||false);
|
34 |
|
|
return pi.util.AddEvent.curry(this,_element);
|
35 |
|
|
},
|
36 |
|
|
RemoveEvent: function(_element,_eventName,_fn,_useCapture){
|
37 |
|
|
return _element[pi.env.ie.toggle("detachEvent","removeEventListener")](pi.env.ie.toggle("on","")+_eventName,_fn,_useCapture||false);
|
38 |
|
|
},
|
39 |
|
|
GetWindowSize:function(){
|
40 |
|
|
return {
|
41 |
|
|
height:pi.env.ie?Math.max(document.documentElement.clientHeight,document.body.clientHeight):window.innerHeight,
|
42 |
|
|
width:pi.env.ie?Math.max(document.documentElement.clientWidth,document.body.clientWidth):window.innerWidth
|
43 |
|
|
}
|
44 |
|
|
},
|
45 |
|
|
Include:function(_url,_callback){
|
46 |
|
|
var script = new pi.element("script").attribute.set("src",_url), callback = _callback||new Function, done = false, head = pi.get.byTag("head")[0];
|
47 |
|
|
script.environment.getElement().onload = script.environment.getElement().onreadystatechange = function(){
|
48 |
|
|
if(!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")){
|
49 |
|
|
callback.call(this);
|
50 |
|
|
done = true;
|
51 |
|
|
head.removeChild(script.environment.getElement());
|
52 |
|
|
}
|
53 |
|
|
};
|
54 |
|
|
script.insert(head);
|
55 |
|
|
},
|
56 |
|
|
Element:{
|
57 |
|
|
addClass:function(_element,_class){
|
58 |
|
|
if( !pi.util.Element.hasClass(_element,_class) )
|
59 |
|
|
pi.util.Element.setClass(_element, pi.util.Element.getClass(_element) + " " + _class );
|
60 |
|
|
},
|
61 |
|
|
getClass:function(_element){
|
62 |
|
|
return _element.getAttribute(pi.env.ie.toggle("className","class"))||"";
|
63 |
|
|
},
|
64 |
|
|
hasClass:function(_element,_class){
|
65 |
|
|
return pi.util.Element.getClass(_element).split(" ").indexOf(_class)>-1;
|
66 |
|
|
},
|
67 |
|
|
removeClass:function(_element,_class){
|
68 |
|
|
if( pi.util.Element.hasClass(_element,_class) )
|
69 |
|
|
pi.util.Element.setClass(
|
70 |
|
|
_element,
|
71 |
|
|
pi.util.Element.getClass(_element,_class).split(" ").removeValue(_class).join(" ")
|
72 |
|
|
);
|
73 |
|
|
},
|
74 |
|
|
setClass:function(_element,_value){
|
75 |
|
|
_element.setAttribute( pi.env.ie.toggle("className","class"), _value );
|
76 |
|
|
},
|
77 |
|
|
toggleClass:function(){
|
78 |
|
|
if(pi.util.Element.hasClass.apply(this,arguments))
|
79 |
|
|
pi.util.Element.removeClass.apply(this,arguments);
|
80 |
|
|
else
|
81 |
|
|
pi.util.Element.addClass.apply(this,arguments);
|
82 |
|
|
},
|
83 |
|
|
getOpacity:function(_styleObject){
|
84 |
|
|
var styleObject = _styleObject;
|
85 |
|
|
if(!pi.env.ie)
|
86 |
|
|
return styleObject["opacity"];
|
87 |
|
|
|
88 |
|
|
var alpha = styleObject["filter"].match(/opacity\=(\d+)/i);
|
89 |
|
|
return alpha?alpha[1]/100:1;
|
90 |
|
|
},
|
91 |
|
|
setOpacity:function(_element,_value){
|
92 |
|
|
if(!pi.env.ie)
|
93 |
|
|
return pi.util.Element.addStyle(_element,{ "opacity":_value });
|
94 |
|
|
_value*=100;
|
95 |
|
|
pi.util.Element.addStyle(_element,{ "filter":"alpha(opacity="+_value+")" });
|
96 |
|
|
return this._parent_;
|
97 |
|
|
},
|
98 |
|
|
getPosition:function(_element){
|
99 |
|
|
var parent = _element,offsetLeft = 0, offsetTop = 0, view = pi.util.Element.getView(_element);
|
100 |
|
|
while(parent&&parent!=document.body&&parent!=document.firstChild){
|
101 |
|
|
offsetLeft +=parseInt(parent.offsetLeft);
|
102 |
|
|
offsetTop += parseInt(parent.offsetTop);
|
103 |
|
|
parent = parent.offsetParent;
|
104 |
|
|
};
|
105 |
|
|
return {
|
106 |
|
|
"bottom":view["bottom"],
|
107 |
|
|
"left":view["left"],
|
108 |
|
|
"marginTop":view["marginTop"],
|
109 |
|
|
"marginLeft":view["marginLeft"],
|
110 |
|
|
"offsetLeft":offsetLeft,
|
111 |
|
|
"offsetTop":offsetTop,
|
112 |
|
|
"position":view["position"],
|
113 |
|
|
"right":view["right"],
|
114 |
|
|
"top":view["top"],
|
115 |
|
|
"z-index":view["zIndex"]
|
116 |
|
|
};
|
117 |
|
|
},
|
118 |
|
|
getSize:function(_element){
|
119 |
|
|
var view = pi.util.Element.getView(_element);
|
120 |
|
|
return {
|
121 |
|
|
"height":view["height"],
|
122 |
|
|
"offsetHeight":_element.offsetHeight,
|
123 |
|
|
"offsetWidth":_element.offsetWidth,
|
124 |
|
|
"width":view["width"]
|
125 |
|
|
}
|
126 |
|
|
},
|
127 |
|
|
addStyle:function(_element,_style){
|
128 |
|
|
for(var key in _style){
|
129 |
|
|
key = key=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):key;
|
130 |
|
|
if (key == "opacity" && pi.env.ie) {
|
131 |
|
|
pi.util.Element.setOpacity(_element,_style[key]);
|
132 |
|
|
continue;
|
133 |
|
|
}
|
134 |
|
|
_element.style[key] = _style[key];
|
135 |
|
|
}
|
136 |
|
|
},
|
137 |
|
|
getStyle:function(_element,_property){
|
138 |
|
|
_property = _property=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):_property;
|
139 |
|
|
if(_property=="opacity"&&pi.env.ie)
|
140 |
|
|
return pi.util.Element.getOpacity(_element.style);
|
141 |
|
|
return typeof _property=="string"?_element.style[_property]:_element.style;
|
142 |
|
|
},
|
143 |
|
|
getView:function(_element,_property){
|
144 |
|
|
var view = document.defaultView?document.defaultView.getComputedStyle(_element,null):_element.currentStyle;
|
145 |
|
|
_property = _property=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):_property;
|
146 |
|
|
if(_property=="opacity"&&pi.env.ie)
|
147 |
|
|
return pi.util.Element.getOpacity(_element,view);
|
148 |
|
|
return typeof _property=="string"?view[_property]:view;
|
149 |
|
|
}
|
150 |
|
|
},
|
151 |
|
|
CloneObject:function(_object,_fn){
|
152 |
|
|
var tmp = {};
|
153 |
|
|
for(var key in _object)
|
154 |
|
|
{
|
155 |
|
|
if( pi.util.IsArray( _object[key] ) ){
|
156 |
|
|
tmp[key] = Array.prototype.clone.apply( _object[key] );
|
157 |
|
|
} else
|
158 |
|
|
if( pi.util.IsHash( _object[key] ) ){
|
159 |
|
|
tmp[ key ] = pi.util.CloneObject(_object[key]);
|
160 |
|
|
if(_fn)_fn.call(tmp,key,_object);
|
161 |
|
|
} else
|
162 |
|
|
tmp[key] = _object[key];
|
163 |
|
|
}
|
164 |
|
|
return tmp;
|
165 |
|
|
},
|
166 |
|
|
MergeObjects:function(_object,_source){
|
167 |
|
|
for(var key in _source){
|
168 |
|
|
var value = _source[key];
|
169 |
|
|
if (pi.util.IsArray(_source[key])) {
|
170 |
|
|
if(pi.util.IsArray( _object[key] )){
|
171 |
|
|
Array.prototype.push.apply( _source[key], _object[key] )
|
172 |
|
|
}
|
173 |
|
|
else
|
174 |
|
|
value = _source[key].clone();
|
175 |
|
|
}
|
176 |
|
|
else
|
177 |
|
|
if (pi.util.IsHash(_source[key])) {
|
178 |
|
|
if (pi.util.IsHash(_object[key])) {
|
179 |
|
|
value = pi.util.MergeObjects(_object[key], _source[key]);
|
180 |
|
|
} else {
|
181 |
|
|
value = pi.util.CloneObject( _source[key] );
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
_object[key] = value;
|
185 |
|
|
};
|
186 |
|
|
return _object;
|
187 |
|
|
}
|
188 |
|
|
};
|
189 |
|
|
|
190 |
|
|
pi.get = function(){
|
191 |
|
|
return document.getElementById(arguments[0]);
|
192 |
|
|
};
|
193 |
|
|
pi.get.byTag = function(){
|
194 |
|
|
return document.getElementsByTagName(arguments[0]);
|
195 |
|
|
};
|
196 |
|
|
pi.get.byClass = function(){ return document.getElementsByClassName.apply(document,arguments); };
|
197 |
|
|
|
198 |
|
|
pi.base = function(){
|
199 |
|
|
this.body = {};
|
200 |
|
|
this.constructor = null;
|
201 |
|
|
|
202 |
|
|
this.build = function(_skipClonning){
|
203 |
|
|
var base = this, skipClonning = _skipClonning||false, _private = {},
|
204 |
|
|
fn = function(){
|
205 |
|
|
var _p = pi.util.CloneObject(_private);
|
206 |
|
|
if(!skipClonning){
|
207 |
|
|
for(var key in this){
|
208 |
|
|
|
209 |
|
|
if(pi.util.IsArray( this[ key ] ) ){
|
210 |
|
|
this[key] = Array.prototype.clone.apply( this[key] );
|
211 |
|
|
} else
|
212 |
|
|
if( pi.util.IsHash(this[key]) ){
|
213 |
|
|
this[key] = pi.util.CloneObject(
|
214 |
|
|
this[ key ],
|
215 |
|
|
function(_key,_object){
|
216 |
|
|
this[ _key ]._parent_ = this;
|
217 |
|
|
}
|
218 |
|
|
);
|
219 |
|
|
this[key]._parent_ = this;
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
};
|
223 |
|
|
base.createAccessors( _p, this );
|
224 |
|
|
if(base.constructor)
|
225 |
|
|
return base.constructor.apply(this,arguments);
|
226 |
|
|
return this;
|
227 |
|
|
};
|
228 |
|
|
this.movePrivateMembers(this.body,_private);
|
229 |
|
|
if(this.constructor){
|
230 |
|
|
fn["$Constructor"] = this.constructor;
|
231 |
|
|
}
|
232 |
|
|
fn.prototype = this.body;
|
233 |
|
|
return fn;
|
234 |
|
|
};
|
235 |
|
|
|
236 |
|
|
this.createAccessors = function(_p, _branch){
|
237 |
|
|
var getter = function(_property){ return this[_property]; },
|
238 |
|
|
setter = function(_property,_value){ this[_property] = _value; return _branch._parent_||_branch; };
|
239 |
|
|
|
240 |
|
|
for (var name in _p) {
|
241 |
|
|
var isPrivate = name.substring(0, 1) == "_", title = name.substring(1, 2).toUpperCase() + name.substring(2);
|
242 |
|
|
if (isPrivate) {
|
243 |
|
|
_branch["get" + title] = getter.curry(_p,name);
|
244 |
|
|
_branch["set" + title] = setter.curry(_p,name);
|
245 |
|
|
}
|
246 |
|
|
else
|
247 |
|
|
if (pi.util.IsHash(_p[name])){
|
248 |
|
|
if(!_branch[name])
|
249 |
|
|
_branch[name] = {};
|
250 |
|
|
this.createAccessors(_p[name], _branch[name]);
|
251 |
|
|
}
|
252 |
|
|
};
|
253 |
|
|
};
|
254 |
|
|
|
255 |
|
|
this.movePrivateMembers = function(_object, _branch){
|
256 |
|
|
for (var name in _object) {
|
257 |
|
|
var isPrivate = name.substring(0, 1) == "_";
|
258 |
|
|
|
259 |
|
|
if (isPrivate) {
|
260 |
|
|
_branch[name] = _object[name];
|
261 |
|
|
delete _object[name];
|
262 |
|
|
}
|
263 |
|
|
else
|
264 |
|
|
if (pi.util.IsHash(_object[name])){
|
265 |
|
|
_branch[name] = {};
|
266 |
|
|
this.movePrivateMembers(_object[name], _branch[name]);
|
267 |
|
|
}
|
268 |
|
|
};
|
269 |
|
|
};
|
270 |
|
|
};
|
271 |
|
|
|
272 |
|
|
Function.prototype.extend = function(_prototype,_skipClonning){
|
273 |
|
|
var object = new pi.base, superClass = this;
|
274 |
|
|
if(_prototype["$Constructor"]){
|
275 |
|
|
object.constructor = _prototype["$Constructor"];
|
276 |
|
|
delete _prototype["$Constructor"];
|
277 |
|
|
};
|
278 |
|
|
|
279 |
|
|
object.body = superClass==pi.base?_prototype:pi.util.MergeObjects(_prototype,superClass.prototype,2);
|
280 |
|
|
object.constructor=object.constructor||function(){
|
281 |
|
|
if(superClass!=pi.base)
|
282 |
|
|
superClass.apply(this,arguments);
|
283 |
|
|
};
|
284 |
|
|
|
285 |
|
|
return object.build(_skipClonning);
|
286 |
|
|
};
|
287 |
|
|
|
288 |
|
|
Function.prototype.curry = function(_scope){
|
289 |
|
|
var fn = this, scope = _scope||window, args = Array.prototype.slice.call(arguments,1);
|
290 |
|
|
return function(){
|
291 |
|
|
return fn.apply(scope,args.concat( Array.prototype.slice.call(arguments,0) ));
|
292 |
|
|
};
|
293 |
|
|
};
|
294 |
|
|
|
295 |
|
|
pi.element = pi.base.extend({
|
296 |
|
|
"$Constructor":function(_tag){
|
297 |
|
|
this.environment.setElement(document.createElement(_tag||"DIV"));
|
298 |
|
|
this.environment.getElement().pi = this;
|
299 |
|
|
return this;
|
300 |
|
|
},
|
301 |
|
|
"clean":function(){
|
302 |
|
|
var childs = this.child.get();
|
303 |
|
|
while(childs.length){
|
304 |
|
|
childs[0].parentNode.removeChild(childs[0]);
|
305 |
|
|
}
|
306 |
|
|
},
|
307 |
|
|
"clone":function(_deep){
|
308 |
|
|
return this.environment.getElement().cloneNode(_deep);
|
309 |
|
|
},
|
310 |
|
|
"insert":function(_element){
|
311 |
|
|
_element = _element.environment?_element.environment.getElement():_element;
|
312 |
|
|
_element.appendChild(this.environment.getElement());
|
313 |
|
|
return this;
|
314 |
|
|
},
|
315 |
|
|
"insertAfter":function(_referenceElement){
|
316 |
|
|
_referenceElement = _referenceElement.environment?_referenceElement.environment.getElement():_referenceElement;
|
317 |
|
|
_referenceElement.nextSibling?this.insertBefore(_referenceElement.nextSibling):this.insert(_referenceElement.parentNode);
|
318 |
|
|
return this;
|
319 |
|
|
},
|
320 |
|
|
"insertBefore":function(_referenceElement){
|
321 |
|
|
_referenceElement = _referenceElement.environment?_referenceElement.environment.getElement():_referenceElement;
|
322 |
|
|
_referenceElement.parentNode.insertBefore(this.environment.getElement(),_referenceElement);
|
323 |
|
|
return this;
|
324 |
|
|
},
|
325 |
|
|
"query":function(_expression,_resultType,namespaceResolver,_result){
|
326 |
|
|
return pi.xpath(_expression,_resultType||"ORDERED_NODE_SNAPSHOT_TYPE",this.environment.getElement(),_namespaceResolver,_result);
|
327 |
|
|
},
|
328 |
|
|
"remove":function(){
|
329 |
|
|
this.environment.getParent().removeChild(
|
330 |
|
|
this.environment.getElement()
|
331 |
|
|
);
|
332 |
|
|
},
|
333 |
|
|
"update":function(_value){
|
334 |
|
|
["TEXTAREA","INPUT"].indexOf(this.environment.getName())>-1?
|
335 |
|
|
(this.environment.getElement().value = _value):
|
336 |
|
|
(this.environment.getElement().innerHTML = _value);
|
337 |
|
|
return this;
|
338 |
|
|
},
|
339 |
|
|
"attribute":{
|
340 |
|
|
"getAll":function(_name){
|
341 |
|
|
return this._parent_.environment.getElement().attributes;
|
342 |
|
|
},
|
343 |
|
|
"clear":function(_name){
|
344 |
|
|
this.set(_name,"");
|
345 |
|
|
return this._parent_;
|
346 |
|
|
},
|
347 |
|
|
"get":function(_name){
|
348 |
|
|
return this._parent_.environment.getElement().getAttribute(_name);
|
349 |
|
|
},
|
350 |
|
|
"has":function(_name){
|
351 |
|
|
return pi.env.ie?(this.get(_name)!=null):this._parent_.environment.getElement().hasAttribute(_name);
|
352 |
|
|
},
|
353 |
|
|
"remove":function(_name){
|
354 |
|
|
this._parent_.environment.getElement().removeAttribute(_name);
|
355 |
|
|
return this._parent_;
|
356 |
|
|
},
|
357 |
|
|
"set":function(_name,_value){
|
358 |
|
|
this._parent_.environment.getElement().setAttribute(_name,_value);
|
359 |
|
|
return this._parent_;
|
360 |
|
|
},
|
361 |
|
|
"addClass":function(_classes){
|
362 |
|
|
for (var i = 0; i < arguments.length; i++) {
|
363 |
|
|
pi.util.Element.addClass(this._parent_.environment.getElement(),arguments[i]);
|
364 |
|
|
};
|
365 |
|
|
return this._parent_;
|
366 |
|
|
},
|
367 |
|
|
"clearClass":function(){
|
368 |
|
|
this.setClass("");
|
369 |
|
|
this._parent_;
|
370 |
|
|
},
|
371 |
|
|
"getClass":function(){
|
372 |
|
|
return pi.util.Element.getClass( this._parent_.environment.getElement() );
|
373 |
|
|
},
|
374 |
|
|
"hasClass":function(_class){
|
375 |
|
|
return pi.util.Element.hasClass( this._parent_.environment.getElement(), _class );
|
376 |
|
|
},
|
377 |
|
|
"setClass":function(_value){
|
378 |
|
|
return pi.util.Element.setClass( this._parent_.environment.getElement(), _value );
|
379 |
|
|
},
|
380 |
|
|
"removeClass":function(_class){
|
381 |
|
|
pi.util.Element.removeClass( this._parent_.environment.getElement(), _class );
|
382 |
|
|
return this._parent_;
|
383 |
|
|
},
|
384 |
|
|
"toggleClass":function(_class){
|
385 |
|
|
pi.util.Element.toggleClass( this._parent_.environment.getElement(), _class );
|
386 |
|
|
}
|
387 |
|
|
},
|
388 |
|
|
"child":{
|
389 |
|
|
"get":function(){
|
390 |
|
|
return this._parent_.environment.getElement().childNodes;
|
391 |
|
|
},
|
392 |
|
|
"add":function(_elements){
|
393 |
|
|
for (var i = 0; i < arguments.length; i++) {
|
394 |
|
|
var el = arguments[i];
|
395 |
|
|
this._parent_.environment.getElement().appendChild(
|
396 |
|
|
el.environment ? el.environment.getElement() : el
|
397 |
|
|
);
|
398 |
|
|
}
|
399 |
|
|
return this._parent_;
|
400 |
|
|
},
|
401 |
|
|
"addAfter":function(_element,_referenceElement){
|
402 |
|
|
this.addBefore(
|
403 |
|
|
_element.environment?_element.environment.getElement():_element,
|
404 |
|
|
(_referenceElement.environment?_referenceElement.environment.getElement():_referenceElement).nextSibling
|
405 |
|
|
);
|
406 |
|
|
return this._parent_;
|
407 |
|
|
},
|
408 |
|
|
"addBefore":function(_element,_referenceElement){
|
409 |
|
|
this._parent_.environment.getElement().insertBefore(
|
410 |
|
|
_element.environment?_element.environment.getElement():_element,
|
411 |
|
|
_referenceElement.environment?_referenceElement.environment.getElement():_referenceElement
|
412 |
|
|
);
|
413 |
|
|
return this._parent_;
|
414 |
|
|
},
|
415 |
|
|
"query":function(_tag,_attributeName,_attributeValue){
|
416 |
|
|
return this._parent_.query(
|
417 |
|
|
"{0}{1}".format( (_tag?"{0}".format(_tag):"/*"), _attributeName||_attributeValue?"[contains(concat(' ', @{0}, ' '),' {1} ')]".format(_attributeName||"",_attributeValue||""):"" )
|
418 |
|
|
);
|
419 |
|
|
},
|
420 |
|
|
"remove":function(_element){
|
421 |
|
|
this._parent_.environment.getElement().removeChild(_element.environment?_element.environment.getElement():_element);
|
422 |
|
|
}
|
423 |
|
|
},
|
424 |
|
|
"environment":{
|
425 |
|
|
"_element":null,
|
426 |
|
|
"getParent":function(){
|
427 |
|
|
return this.getElement().parentNode;
|
428 |
|
|
},
|
429 |
|
|
"getPosition":function(){
|
430 |
|
|
return pi.util.Element.getPosition(this.getElement());
|
431 |
|
|
},
|
432 |
|
|
"getSize":function(){
|
433 |
|
|
return pi.util.Element.getSize( this.getElement() );
|
434 |
|
|
},
|
435 |
|
|
"addStyle":function(_styleObject){
|
436 |
|
|
pi.util.Element.addStyle(this.getElement(),_styleObject);
|
437 |
|
|
return this._parent_;
|
438 |
|
|
},
|
439 |
|
|
"getStyle":function(_property){
|
440 |
|
|
return pi.util.Element.getStyle(_property);
|
441 |
|
|
},
|
442 |
|
|
"getName":function(){
|
443 |
|
|
return this.getElement().nodeName;
|
444 |
|
|
},
|
445 |
|
|
"getType":function(){
|
446 |
|
|
return this.getElement().nodeType;
|
447 |
|
|
},
|
448 |
|
|
"getView":function(_property){
|
449 |
|
|
return pi.util.Element.getView(this.getElement(),_property);
|
450 |
|
|
}
|
451 |
|
|
},
|
452 |
|
|
"event":{
|
453 |
|
|
"addListener":function(_event,_fn,_useCapture){
|
454 |
|
|
pi.util.AddEvent(this._parent_.environment.getElement(),_event,_fn,_useCapture);
|
455 |
|
|
return this._parent_;
|
456 |
|
|
},
|
457 |
|
|
"removeListener":function(_event,_fn,_useCapture){
|
458 |
|
|
pi.util.RemoveEvent(this._parent_.environment.getElement(),_event,_fn,_useCapture);
|
459 |
|
|
return this._parent_;
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
});
|
463 |
|
|
|
464 |
|
|
pi.xhr = new pi.base;
|
465 |
|
|
pi.xhr.constructor = function(){
|
466 |
|
|
var api;
|
467 |
|
|
if(!window.XMLHttpRequest){
|
468 |
|
|
var names = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
|
469 |
|
|
for (var i = 0; i < names.length; i++) {
|
470 |
|
|
try {
|
471 |
|
|
this.environment.setApi(new ActiveXObject(names[i]));
|
472 |
|
|
break;
|
473 |
|
|
} catch (e) { continue; }
|
474 |
|
|
}
|
475 |
|
|
}
|
476 |
|
|
else
|
477 |
|
|
this.environment.setApi(new XMLHttpRequest());
|
478 |
|
|
this.environment.getApi().onreadystatechange=this.event.readystatechange.curry(this);
|
479 |
|
|
return this;
|
480 |
|
|
};
|
481 |
|
|
pi.xhr.body = {
|
482 |
|
|
"abort":function(){
|
483 |
|
|
this.environment.getApi().abort();
|
484 |
|
|
},
|
485 |
|
|
"send":function(){
|
486 |
|
|
var url = this.environment.getUrl(), data = this.environment.getData(),dataUrl = "";
|
487 |
|
|
|
488 |
|
|
for (var key in data)
|
489 |
|
|
dataUrl += "{0}={1}&".format(key, data[key]);
|
490 |
|
|
|
491 |
|
|
if (this.environment.getType()=="GET")
|
492 |
|
|
url += (url.search("\\?")==-1?"?":"&")+"{0}".format(dataUrl);
|
493 |
|
|
|
494 |
|
|
this.environment.getApi().open(this.environment.getType(),url,this.environment.getAsync());
|
495 |
|
|
|
496 |
|
|
for(var key in this.environment.getHeader())
|
497 |
|
|
this.environment.getApi().setRequestHeader(key,this.environment.getHeader()[key]);
|
498 |
|
|
|
499 |
|
|
this.environment.getApi().send(this.environment.getType()=="GET"?"":dataUrl);
|
500 |
|
|
}
|
501 |
|
|
};
|
502 |
|
|
pi.xhr.body.environment = {
|
503 |
|
|
"_async":true, "_api":null, "_cache":true, "_callback":[], "_channel":null, "_data":{}, "_header":{}, "_mimeType":null, "_multipart":false, "_type":"GET", "_timeout":0, "_url":"",
|
504 |
|
|
"addCallback": function(_options,_fn){
|
505 |
|
|
this.getCallback().push({ "fn":_fn, "options":_options });
|
506 |
|
|
},
|
507 |
|
|
"addHeader": function(_key,_value){
|
508 |
|
|
this.getHeader()[_key] = _value;
|
509 |
|
|
},
|
510 |
|
|
"addData": function(_key,_value){
|
511 |
|
|
this.getData()[_key] = _value;
|
512 |
|
|
},
|
513 |
|
|
"changeCache":function(_value){
|
514 |
|
|
if(_value==false){
|
515 |
|
|
this.addData("forceCache",Math.round(Math.random()*10000));
|
516 |
|
|
}
|
517 |
|
|
this.setCache(_value);
|
518 |
|
|
},
|
519 |
|
|
"changeType": function(_value){
|
520 |
|
|
if(_value=="POST"){
|
521 |
|
|
this.addHeader("Content-Type","application/x-www-form-urlencoded");
|
522 |
|
|
}
|
523 |
|
|
this.setType(_value);
|
524 |
|
|
}
|
525 |
|
|
};
|
526 |
|
|
pi.xhr.body.event = {
|
527 |
|
|
"readystatechange":function(){
|
528 |
|
|
var readyState = this.environment.getApi().readyState;
|
529 |
|
|
var callback=this.environment.getCallback();
|
530 |
|
|
|
531 |
|
|
for (var i = 0; i < callback.length; i++) {
|
532 |
|
|
if(callback[i].options.readyState.indexOf(readyState)>-1)
|
533 |
|
|
callback[i].fn.apply(this);
|
534 |
|
|
}
|
535 |
|
|
}
|
536 |
|
|
};
|
537 |
|
|
pi.xhr = pi.xhr.build();
|
538 |
|
|
|
539 |
|
|
/*
|
540 |
|
|
* xml.xhr.get
|
541 |
|
|
*/
|
542 |
|
|
|
543 |
|
|
pi.xhr.get = function(_url,_returnPiObject){
|
544 |
|
|
var request = new pi.xhr();
|
545 |
|
|
request.environment.setAsync(false);
|
546 |
|
|
request.environment.setUrl(_url);
|
547 |
|
|
request.send();
|
548 |
|
|
return _returnPiObject?request:request.environment.getApi();
|
549 |
|
|
};
|
550 |
|
|
|
551 |
|
|
/*
|
552 |
|
|
* pi.xpath
|
553 |
|
|
*/
|
554 |
|
|
|
555 |
|
|
pi.xpath = function(_expression,_resultType,_contextNode,_namespaceResolver,_result){
|
556 |
|
|
var contextNode = _contextNode||document,
|
557 |
|
|
expression = _expression||"",
|
558 |
|
|
namespaceResolver = _namespaceResolver||null,
|
559 |
|
|
result=_result||null,
|
560 |
|
|
resultType=_resultType||"ANY_TYPE";
|
561 |
|
|
return document.evaluate(expression, contextNode, namespaceResolver, XPathResult[resultType], result);
|
562 |
|
|
};
|
563 |
|
|
|
564 |
|
|
Array.prototype.clone = function(){
|
565 |
|
|
var tmp = [];
|
566 |
|
|
Array.prototype.push.apply(tmp,this);
|
567 |
|
|
tmp.forEach(function(item,index,object){
|
568 |
|
|
if(item instanceof Array)
|
569 |
|
|
object[index] = object[index].clone();
|
570 |
|
|
});
|
571 |
|
|
return tmp;
|
572 |
|
|
};
|
573 |
|
|
Array.prototype.count = function(_value){
|
574 |
|
|
var count = 0;
|
575 |
|
|
this.forEach(function(){
|
576 |
|
|
count+=Number(arguments[0]==_value);
|
577 |
|
|
});
|
578 |
|
|
return count;
|
579 |
|
|
};
|
580 |
|
|
|
581 |
|
|
Array.prototype.forEach = Array.prototype.forEach||function(_function){
|
582 |
|
|
for(var i=0; i<this.length; i++)
|
583 |
|
|
_function.apply(this,[this[i],i,this]);
|
584 |
|
|
};
|
585 |
|
|
|
586 |
|
|
Array.prototype.getLastItem = function(){
|
587 |
|
|
return this[this.length-1];
|
588 |
|
|
};
|
589 |
|
|
|
590 |
|
|
Array.prototype.indexOf = Array.prototype.indexOf||function(_value){
|
591 |
|
|
var index = -1;
|
592 |
|
|
for(var i=0; i<this.length; i++)
|
593 |
|
|
if(this[i]==_value){
|
594 |
|
|
index = i;
|
595 |
|
|
break;
|
596 |
|
|
}
|
597 |
|
|
return index;
|
598 |
|
|
};
|
599 |
|
|
|
600 |
|
|
Array.prototype.remove = function(_index){
|
601 |
|
|
var array = this.slice(0,_index);
|
602 |
|
|
Array.prototype.push.apply(array,this.slice(_index+1));
|
603 |
|
|
return array;
|
604 |
|
|
};
|
605 |
|
|
|
606 |
|
|
Array.prototype.removeValue = function(_value){
|
607 |
|
|
return this.remove(this.indexOf(_value));
|
608 |
|
|
};
|
609 |
|
|
|
610 |
|
|
Boolean.prototype.toggle = function(){
|
611 |
|
|
return this==true?arguments[0]:arguments[1];
|
612 |
|
|
};
|
613 |
|
|
|
614 |
|
|
Number.prototype.base = function(_system){
|
615 |
|
|
var remain = this%_system;
|
616 |
|
|
if(this==remain)return String.fromCharCode(this+(this>9?87:48));
|
617 |
|
|
return ((this-remain)/_system).base(_system)+String.fromCharCode(remain+(remain>9?87:48));
|
618 |
|
|
};
|
619 |
|
|
Number.prototype.decimal = function(_system){
|
620 |
|
|
var result = 0, digit = String(this).split("");
|
621 |
|
|
for(var i=0; i<digit.length; i++)
|
622 |
|
|
{
|
623 |
|
|
digit[i]=parseInt((digit[i].charCodeAt(0)>58)?digit[i].charCodeAt(0)-87:digit[i]);
|
624 |
|
|
result += digit[i]*(Math.pow(_system,digit.length-1-i));
|
625 |
|
|
}
|
626 |
|
|
return result;
|
627 |
|
|
};
|
628 |
|
|
Number.prototype.range = function(_pattern){
|
629 |
|
|
for(
|
630 |
|
|
var value = String(this), isFloat = /\./i.test(value),
|
631 |
|
|
i = isFloat.toggle(parseInt(value.split(".")[0]),0),
|
632 |
|
|
end = parseInt(value.split(".")[isFloat.toggle(1,0)]),
|
633 |
|
|
array = []; i<end; i++
|
634 |
|
|
){
|
635 |
|
|
array.push(
|
636 |
|
|
Boolean(_pattern)==false?i:(typeof _pattern=="function"?_pattern(i):_pattern[i])
|
637 |
|
|
);
|
638 |
|
|
}
|
639 |
|
|
return array;
|
640 |
|
|
};
|
641 |
|
|
|
642 |
|
|
String.prototype.escape = function(){
|
643 |
|
|
return escape(this);
|
644 |
|
|
};
|
645 |
|
|
|
646 |
|
|
String.prototype.format = function(){
|
647 |
|
|
var values = arguments;
|
648 |
|
|
return this.replace(/\{(\d)\}/g,function(){
|
649 |
|
|
return values[arguments[1]];
|
650 |
|
|
})
|
651 |
|
|
};
|
652 |
|
|
|
653 |
|
|
String.prototype.leftpad = function(_len,_ch){
|
654 |
|
|
var str=this;
|
655 |
|
|
var ch = Boolean(_ch)==false?" ":_ch;
|
656 |
|
|
while(str.length<_len)
|
657 |
|
|
str=ch+str;
|
658 |
|
|
return str;
|
659 |
|
|
};
|
660 |
|
|
|
661 |
|
|
String.prototype.toggle = function(_value,_other){
|
662 |
|
|
return this==_value?_value:_other;
|
663 |
|
|
};
|
664 |
|
|
|
665 |
|
|
String.prototype.unicode = function(){
|
666 |
|
|
var str="", obj = this.split("");
|
667 |
|
|
for(var i=obj.length-1; i>=0; i--)
|
668 |
|
|
str="\\u{0}{1}".format(String(obj[i].charCodeAt(0).base(16)).leftpad(4,"0"),str);
|
669 |
|
|
return str;
|
670 |
|
|
};
|
671 |
|
|
|
672 |
|
|
pi.util.AddEvent(
|
673 |
|
|
pi.env.ie?window:document,
|
674 |
|
|
pi.env.ie?"load":"DOMContentLoaded",
|
675 |
|
|
function(){
|
676 |
|
|
for(var i=0; i<pi.util.DOMContentLoaded.length; i++){
|
677 |
|
|
pi.util.DOMContentLoaded[ i ]();
|
678 |
|
|
}
|
679 |
|
|
}
|
680 |
|
|
);
|
681 |
|
|
|
682 |
|
|
})(window);
|