forked from intel/appframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq.appframework.js
445 lines (420 loc) · 14.8 KB
/
jq.appframework.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* jq.appframework.js
* @copyright Intel 2013
* @author Ian Maffett
* @description A plugin to allow jQuery developers to use App Framework UI
*/
/* jshint eqeqeq:false */
(function($,window){
"use strict";
jQuery.event.props.push("touches");
jQuery.event.props.push("originalTouches");
jQuery.event.props.push("changedTouches");
var nundefined, document = window.document,classCache = {},isWin8=(typeof(MSApp)==="object"),_jsonPID = 1;
function classRE(name) {
return name in classCache ? classCache[name] : (classCache[name] = new RegExp("(^|\\s)" + name + "(\\s|$)"));
}
function _shimNodes(nodes, obj) {
if (!nodes)
return;
if (nodes.nodeType) {
obj[obj.length++] = nodes;
return;
}
for (var i = 0, iz = nodes.length; i < iz; i++)
obj[obj.length++] = nodes[i];
}
$.extend($.fn,{
/* @param {String} attribute to get
* @param {String} value to set as
* @return {Object} an appframework object
* @title $().css(attribute,[value])
*/
vendorCss: function (attribute, value, obj) {
return this.css($.feat.cssPrefix + attribute, value, obj);
},
/**
* Performs a css vendor specific transform:translate operation on the collection.
*
```
$("#main").cssTranslate('200px,0,0');
```
* @param {String} Transform values
* @return {Object} an appframework object
* @title $().vendorCss(value)
*/
cssTranslate: function (val) {
return this.vendorCss("Transform", "translate" + $.feat.cssTransformStart + val + $.feat.cssTransformEnd);
},
/**
* Gets the computed style of CSS values
*
```
$("#main").computedStyle('display');
```
* @param {String} css property
* @return {Int|String|Float|} css vlaue
* @title $().computedStyle()
*/
computedStyle:function(val){
if(this.length===0||val==nundefined) return;
return window.getComputedStyle(this[0],"")[val];
},
replaceClass: function(name, newName) {
if (name == nundefined || newName == nundefined) return this;
var replaceClassFn=function(cname) {
classList = classList.replace(classRE(cname), " ");
};
for (var i = 0; i < this.length; i++) {
if (name == nundefined) {
this[i].className = newName;
continue;
}
var classList = this[i].className;
name.split(/\s+/g).concat(newName.split(/\s+/g)).forEach(replaceClassFn);
classList = classList.trim();
if (classList.length > 0) {
this[i].className = (classList + " " + newName).trim();
} else
this[i].className = newName;
}
return this;
}
});
function detectUA($, userAgent) {
$.os = {};
$.os.webkit = userAgent.match(/WebKit\/([\d.]+)/) ? true : false;
$.os.android = userAgent.match(/(Android)\s+([\d.]+)/) || userAgent.match(/Silk-Accelerated/) ? true : false;
$.os.androidICS = $.os.android && userAgent.match(/(Android)\s4/) ? true : false;
$.os.ipad = userAgent.match(/(iPad).*OS\s([\d_]+)/) ? true : false;
$.os.iphone = !$.os.ipad && userAgent.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
$.os.ios7 = ($.os.ipad||$.os.iphone)&&userAgent.match(/7_/)||($.os.ipad||$.os.iphone)&&userAgent.match(/8_/) ? true : false;
$.os.webos = userAgent.match(/(webOS|hpwOS)[\s\/]([\d.]+)/) ? true : false;
$.os.touchpad = $.os.webos && userAgent.match(/TouchPad/) ? true : false;
$.os.ios = $.os.ipad || $.os.iphone;
$.os.playbook = userAgent.match(/PlayBook/) ? true : false;
$.os.blackberry10 = userAgent.match(/BB10/) ? true : false;
$.os.blackberry = $.os.playbook || $.os.blackberry10|| userAgent.match(/BlackBerry/) ? true : false;
$.os.chrome = userAgent.match(/Chrome/) ? true : false;
$.os.opera = userAgent.match(/Opera/) ? true : false;
$.os.fennec = userAgent.match(/fennec/i) ? true : userAgent.match(/Firefox/) ? true : false;
$.os.ie = userAgent.match(/MSIE 10.0/i)||userAgent.match(/Trident\/7/i) ? true : false;
$.os.ieTouch = $.os.ie && userAgent.toLowerCase().match(/touch/i) ? true : false;
$.os.tizen = userAgent.match(/Tizen/i)?true:false;
$.os.supportsTouch = ((window.DocumentTouch && document instanceof window.DocumentTouch) || "ontouchstart" in window);
$.os.kindle=userAgent.match(/Silk-Accelerated/)?true:false;
//features
$.feat = {};
var head = document.documentElement.getElementsByTagName("head")[0];
$.feat.nativeTouchScroll = typeof(head.style["-webkit-overflow-scrolling"]) !== "undefined" && ($.os.ios||$.os.blackberry10);
$.feat.cssPrefix = $.os.webkit ? "Webkit" : $.os.fennec ? "Moz" : $.os.ie ? "ms" : $.os.opera ? "O" : "";
$.feat.cssTransformStart = !$.os.opera ? "3d(" : "(";
$.feat.cssTransformEnd = !$.os.opera ? ",0)" : ")";
if ($.os.android && !$.os.webkit)
$.os.android = false;
}
detectUA($, navigator.userAgent);
$.__detectUA = detectUA; //needed for unit tests
/**
* Utility function to create a psuedo GUID
```
var id= $.uuid();
```
* @title $.uuid
*/
$.uuid = function () {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
};
/**
* Gets the css matrix, or creates a fake one
```
$.getCssMatrix(domElement)
```
@returns matrix with postion
*/
$.getCssMatrix = function(ele) {
if ($.is$(ele)) ele = ele.get(0);
var matrixFn = window.WebKitCSSMatrix || window.MSCSSMatrix;
if (ele === nundefined) {
if (matrixFn) {
return new matrixFn();
}
else {
return {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0
};
}
}
var computedStyle = window.getComputedStyle(ele);
var transform = computedStyle.webkitTransform ||
computedStyle.transform ||
computedStyle[$.feat.cssPrefix + "Transform"];
if (matrixFn)
return new matrixFn(transform);
else if (transform) {
//fake css matrix
var mat = transform.replace(/[^0-9\-.,]/g, "").split(",");
return {
a: +mat[0],
b: +mat[1],
c: +mat[2],
d: +mat[3],
e: +mat[4],
f: +mat[5]
};
}
else {
return {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0
};
}
};
/**
* $.create - a faster alertnative to $("<div id='main'>this is some text</div>");
```
$.create("div",{id:'main',innerHTML:'this is some text'});
$.create("<div id='main'>this is some text</div>");
```
* @param {String} DOM Element type or html
* @param [{Object}] properties to apply to the element
* @return {Object} Returns an appframework object
* @title $.create(type,[params])
*/
$.create = function(type, props) {
var elem;
var f = new $();
if (props || type[0] !== "<") {
if (props.html){
props.innerHTML = props.html;
delete props.html;
}
elem = document.createElement(type);
for (var j in props) {
elem[j] = props[j];
}
f[f.length++] = elem;
} else {
elem = document.createElement("div");
if (isWin8) {
MSApp.execUnsafeLocalFunction(function() {
elem.innerHTML = type.trim();
});
} else
elem.innerHTML = type;
_shimNodes(elem.childNodes, f);
}
return f;
};
/**
* $.query - a faster alertnative to $("div");
```
$.query(".panel");
```
* @param {String} selector
* @param {Object} [context]
* @return {Object} Returns an appframework object
* @title $.query(selector,[context])
*/
$.query = function (sel, what) {
try {
return $(sel,what);
}
catch(e) {
return $();
}
};
/* The following are for events on objects */
/**
* Bind an event to an object instead of a DOM Node
```
$.bind(this,'event',function(){});
```
* @param {Object} object
* @param {String} event name
* @param {Function} function to execute
* @title $.bind(object,event,function);
*/
$.bind = function (obj, ev, f) {
if (!obj.__events) obj.__events = {};
if (!$.isArray(ev)) ev = [ev];
for (var i = 0; i < ev.length; i++) {
if (!obj.__events[ev[i]]) obj.__events[ev[i]] = [];
obj.__events[ev[i]].push(f);
}
};
/**
* Trigger an event to an object instead of a DOM Node
```
$.trigger(this,'event',arguments);
```
* @param {Object} object
* @param {String} event name
* @param {Array} arguments
* @title $.trigger(object,event,argments);
*/
$.trigger = function (obj, ev, args) {
var ret = true;
if (!obj.__events) return ret;
if (!$.isArray(ev)) ev = [ev];
if (!$.isArray(args)) args = [];
for (var i = 0; i < ev.length; i++) {
if (obj.__events[ev[i]]) {
var evts = obj.__events[ev[i]];
for (var j = 0; j < evts.length; j++)
if ($.isFunction(evts[j]) && evts[j].apply(obj, args) === false)
ret = false;
}
}
return ret;
};
/**
* Unbind an event to an object instead of a DOM Node
```
$.unbind(this,'event',function(){});
```
* @param {Object} object
* @param {String} event name
* @param {Function} function to execute
* @title $.unbind(object,event,function);
*/
$.unbind = function (obj, ev, f) {
if (!obj.__events) return;
if(ev==nundefined) {
delete obj.__events;
return;
}
if (!$.isArray(ev)) ev = [ev];
for (var i = 0; i < ev.length; i++) {
if (obj.__events[ev[i]]) {
var evts = obj.__events[ev[i]];
for (var j = 0; j < evts.length; j++) {
if (f ==nundefined)
delete evts[j];
if (evts[j] === f) {
evts.splice(j, 1);
break;
}
}
}
}
};
$.cleanUpContent = function(){};
$.isObject = function (obj) {
return typeof obj === "object";
};
$.asap = function (fn, context, args) {
if (!$.isFunction(fn)) throw "$.asap - argument is not a valid function";
setTimeout(function(){
fn.apply(context,args);
});
};
/**
* this function executes javascript in HTML.
```
$.parseJS(content)
```
* @param {String|DOM} content
* @title $.parseJS(content);
*/
var remoteJSPages = {};
$.parseJS = function (div) {
if (!div)
return;
if (typeof (div) === "string") {
var elem = document.createElement("div");
if(isWin8){
MSApp.execUnsafeLocalFunction(function(){
elem.innerHTML = div;
});
}
else
elem.innerHTML = div;
div = elem;
}
var scripts = div.getElementsByTagName("script");
div = null;
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].src.length > 0 && !remoteJSPages[scripts[i].src]&&!isWin8) {
var doc = document.createElement("script");
doc.type = scripts[i].type;
doc.src = scripts[i].src;
document.getElementsByTagName("head")[0].appendChild(doc);
remoteJSPages[scripts[i].src] = 1;
doc = null;
} else {
window["eval"](scripts[i].innerHTML);
}
}
};
$.is$ = function (obj) {
return obj instanceof $;
};
$.jsonP = function(options) {
if (isWin8) {
options.type = "get";
options.dataType = null;
return $.get(options);
}
var callbackName = "jsonp_callback" + (++_jsonPID);
var abortTimeout = "",
context, callback;
var script = document.createElement("script");
window[callbackName] = function(data) {
clearTimeout(abortTimeout);
$(script).remove();
delete window[callbackName];
options.success.call(context, data);
};
if (options.url.indexOf("callback=?") !== -1) {
script.src = options.url.replace(/=\?/, "=" + callbackName);
} else {
callback = options.jsonp ? options.jsonp : "callback";
if (options.url.indexOf("?") === -1) {
options.url += ("?" + callback + "=" + callbackName);
}
else {
options.url += ("&" + callback + "=" + callbackName);
}
script.src = options.url;
}
if (options.error) {
script.onerror = function() {
clearTimeout(abortTimeout);
options.error.call(context, "", "error");
};
}
$("head").append(script);
if (options.timeout > 0)
abortTimeout = setTimeout(function() {
options.error.call(context, "", "timeout");
}, options.timeout);
return {};
};
//Shim to put touch events on the jQuery special event
window.$afm=$;
if (!window.numOnly) {
window.numOnly = function numOnly(val) {
if (val ===undefined || val === "") return 0;
if (isNaN(parseFloat(val))) {
if (val.replace) {
val = val.replace(/[^0-9.-]/g, "");
} else return 0;
}
return parseFloat(val);
};
}
})(jQuery,window);
window.af=window.jq=jQuery;