forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquerypp.js
More file actions
707 lines (628 loc) · 18.8 KB
/
jquerypp.js
File metadata and controls
707 lines (628 loc) · 18.8 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
(function( $ ) {
var getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
rupper = /([A-Z])/g,
rdashAlpha = /-([a-z])/ig,
fcamelCase = function( all, letter ) {
return letter.toUpperCase();
},
getStyle = function( elem ) {
if ( getComputedStyle ) {
return getComputedStyle(elem, null);
}
else if ( elem.currentStyle ) {
return elem.currentStyle;
}
},
rfloat = /float/i,
rnumpx = /^-?\d+(?:px)?$/i,
rnum = /^-?\d/;
$.styles = function( el, styles ) {
if (!el ) {
return null;
}
var currentS = getStyle(el),
oldName, val, style = el.style,
results = {},
i = 0,
left, rsLeft, camelCase, name;
for (; i < styles.length; i++ ) {
name = styles[i];
oldName = name.replace(rdashAlpha, fcamelCase);
if ( rfloat.test(name) ) {
name = jQuery.support.cssFloat ? "float" : "styleFloat";
oldName = "cssFloat";
}
if ( getComputedStyle ) {
name = name.replace(rupper, "-$1").toLowerCase();
val = currentS.getPropertyValue(name);
if ( name === "opacity" && val === "" ) {
val = "1";
}
results[oldName] = val;
} else {
camelCase = name.replace(rdashAlpha, fcamelCase);
results[oldName] = currentS[name] || currentS[camelCase];
if (!rnumpx.test(results[oldName]) && rnum.test(results[oldName]) ) { //convert to px
// Remember the original values
left = style.left;
rsLeft = el.runtimeStyle.left;
// Put in the new values to get a computed value out
el.runtimeStyle.left = el.currentStyle.left;
style.left = camelCase === "fontSize" ? "1em" : (results[oldName] || 0);
results[oldName] = style.pixelLeft + "px";
// Revert the changed values
style.left = left;
el.runtimeStyle.left = rsLeft;
}
}
}
return results;
};
/**
* @function jQuery.fn.styles
* @parent jQuery.styles
* @plugin jQuery.styles
*
* Returns a set of computed styles. Pass the names of the styles you want to
* retrieve as arguments:
*
* $("div").styles('float','display')
* // -> { cssFloat: "left", display: "block" }
*
* @param {String} style pass the names of the styles to retrieve as the argument list
* @return {Object} an object of `style` : `value` pairs
*/
$.fn.styles = function() {
return $.styles(this[0], $.makeArray(arguments));
};
})(jQuery);
(function ($) {
var animationNum = 0,
//Animation events implies animations right?
//gets the last editable stylesheet or creates one
getLastStyleSheet = function () {
var sheets = document.styleSheets,
x = sheets.length - 1,
foundSheet = null,
style;
while (x >= 0 && !foundSheet) {
if (sheets[x].cssRules || sheets[x].rules) {
//any stylesheet which we can access cssRules is good
foundSheet = sheets[x];
}
x -= 1;
}
if (!foundSheet) {
style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(document.createTextNode(''));
}
foundSheet = sheets[sheets.length - 1];
}
return foundSheet;
},
//removes an animation rule from a sheet
removeAnimation = function (sheet, name) {
for (var j = sheet.cssRules.length - 1; j >= 0; j--) {
var rule = sheet.cssRules[j];
// 7 means the keyframe rule
if (rule.type === 7 && rule.name == name) {
sheet.deleteRule(j)
return;
}
}
},
/**
* Returns whether the animation should be passed to the original
* jQuery.fn.animate.
*/
passThrough = function (props, ops) {
var nonElement = !(this[0] && this[0].nodeType),
isInline = !nonElement && $(this).css("display") === "inline" && $(this).css("float") === "none";
for (var name in props) {
if (props[name] == 'show' || props[name] == 'hide' // jQuery does something with these two values
|| jQuery.isArray(props[name]) // Arrays for individual easing
|| props[name] < 0 // Negative values not handled the same
|| name == 'zIndex' || name == 'z-index'
// Firefox doesn't animate 'auto' properties
// https://bugzilla.mozilla.org/show_bug.cgi?id=571344
// || (browser.prefix == '-moz-' && (name == 'font-size' || name == 'fontSize'))
) { // unit-less value
return true;
}
}
return props.jquery === true || browser === null ||
jQuery.isEmptyObject(props) || // Animating empty properties
jQuery.isPlainObject(ops) || // Second parameter is an object - anifast only handles numbers
typeof ops == 'string' || // Second parameter is a string like 'slow' TODO: remove
isInline || nonElement;
},
/**
* Return the CSS number (with px added as the default unit if the value is a number)
*/
cssNumber = function(origName, value) {
if (typeof value === "number" && !jQuery.cssNumber[ origName ]) {
return value += "px";
}
return value;
},
/**
* Feature detection borrowed by Modernizr
*/
getBrowserProperties = function(){
var t,
el = document.createElement('fakeelement'),
transitions = {
'transition': {
transitionEnd : 'transitionEnd',
prefix : ''
},
/*
'OTransition': {
transitionEnd : 'oTransitionEnd',
prefix : '-o-'
},
'MSTransition': {
transitionEnd : 'msTransitionEnd',
prefix : '-ms-'
},
*/
'MozTransition': {
transitionEnd : 'animationend',
prefix : '-moz-'
},
'WebkitTransition': {
transitionEnd : 'webkitAnimationEnd',
prefix : '-webkit-'
}
}
for(t in transitions){
if( el.style[t] !== undefined ){
return transitions[t];
}
}
return null;
},
// Properties that Firefox can't animate if set to 'auto'
// https://bugzilla.mozilla.org/show_bug.cgi?id=571344
ffProps = {
top : function(el) {
return el.position().top;
},
left : function(el) {
return el.position().left;
},
width : function(el) {
return el.width();
},
height : function(el) {
return el.height();
},
fontSize : function(el) {
return '1em';
}
},
browser = getBrowserProperties(),
/**
* Add browser specific prefix
*/
addPrefix = function(properties) {
var result = {};
jQuery.each(properties, function(name, value) {
result[browser.prefix + name] = value;
});
return result;
},
// The animation cache
cache = [],
/**
* Returns the animation name for a given style. It either uses a cached
* version or adds it to the stylesheet, removing the oldest style if the
* cache has reached a certain size.
*/
getAnimation = function(style) {
var lastSheet, name, last;
// Look up the cached style, set it to that name and reset age if found
// increment the age for any other animation
$.each(cache, function(i, animation) {
if(style === animation.style) {
name = animation.name;
animation.age = 0;
} else {
animation.age += 1;
}
});
if(!name) { // Add a new style
lastSheet = getLastStyleSheet()
name = "animate" + (animationNum++);
// get the last sheet and insert this rule into it
lastSheet.insertRule("@" + browser.prefix + "keyframes " + name + ' ' + style,
lastSheet.cssRules.length);
cache.push({
name : name,
style : style,
age : 0
});
// Sort the cache by age
cache.sort(function(first, second) {
return first.age - second.age;
});
// Remove the last (oldest) item from the cache if it has more than 20 items
if(cache.length > 20) {
last = cache.pop();
removeAnimation(lastSheet, last.name);
}
}
return name;
},
oldanimate = jQuery.fn.animate;
/**
* @function jQuery.fn.animate
* @parent jQuery.animate
*
* Animate CSS properties using native CSS animations, if possible.
* Uses the original [jQuery.fn.animate()](http://api.jquery.com/animate/) otherwise.
*
* @param {Object} props The CSS properties to animate
* @param {Integer|String|Object} [speed=400] The animation duration in ms.
* Will use jQuery.fn.animate if a string or object is passed
* @param {Function} [callback] A callback to execute once the animation is complete
* @return {jQuery} The jQuery element
*/
jQuery.fn.animate = function (props, speed, callback) {
//default to normal animations if browser doesn't support them
if (passThrough.apply(this, arguments)) {
return oldanimate.apply(this, arguments);
}
if (jQuery.isFunction(speed)) {
callback = speed;
}
this.queue('fx', function(done) {
// Add everything to the animation queue
// Most of of these calls need to happen once per element
var current, //current CSS values
properties = [], // The list of properties passed
to = "",
prop,
self = $(this),
duration = jQuery.fx.speeds[speed] || speed || jQuery.fx.speeds._default,
//the animation keyframe name
animationName,
// The key used to store the animation hook
dataKey = animationName + '.run',
//the text for the keyframe
style = "{ from {",
// The animation end event handler.
// Will be called both on animation end and after calling .stop()
animationEnd = function (currentCSS, exec) {
self.css(currentCSS);
self.css(addPrefix({
"animation-duration" : "",
"animation-name" : "",
"animation-fill-mode" : ""
}));
if (callback && exec) {
// Call success, pass the DOM element as the this reference
callback.call(self[0], true)
}
jQuery.removeData(self, dataKey, true);
}
for(prop in props) {
properties.push(prop);
}
if(browser.prefix === '-moz-') {
// Normalize 'auto' properties in FF
$.each(properties, function(i, prop) {
var converter = ffProps[jQuery.camelCase(prop)];
if(converter && self.css(prop) == 'auto') {
self.css(prop, converter(self));
}
});
}
// Use jQuery.styles
current = self.styles.apply(self, properties);
jQuery.each(properties, function(i, cur) {
// Convert a camelcased property name
var name = cur.replace(/([A-Z]|^ms)/g, "-$1" ).toLowerCase();
style += name + " : " + cssNumber(cur, current[cur]) + "; ";
to += name + " : " + cssNumber(cur, props[cur]) + "; ";
});
style += "} to {" + to + " }}";
animationName = getAnimation(style);
// Add a hook which will be called when the animation stops
jQuery._data(this, dataKey, {
stop : function(gotoEnd) {
// Pause the animation
self.css(addPrefix({
'animation-play-state' : 'paused'
}));
// Unbind the animation end handler
self.off(browser.transitionEnd, animationEnd);
if(!gotoEnd) { // We were told not to finish the animation
// Call animationEnd but set the CSS to the current computed style
animationEnd(self.styles.apply(self, properties), false);
} else {
// Finish animaion
animationEnd(props, true);
}
}
});
// set this element to point to that animation
self.css(addPrefix({
"animation-duration" : duration + "ms",
"animation-name" : animationName,
"animation-fill-mode": "forwards"
}));
self.one(browser.transitionEnd, function() {
// Call animationEnd using the current properties
animationEnd(props, true);
done();
});
});
return this;
};
})(jQuery);
(function() {
var event = jQuery.event,
//helper that finds handlers by type and calls back a function, this is basically handle
// events - the events object
// types - an array of event types to look for
// callback(type, handlerFunc, selector) - a callback
// selector - an optional selector to filter with, if there, matches by selector
// if null, matches anything, otherwise, matches with no selector
findHelper = function( events, types, callback, selector ) {
var t, type, typeHandlers, all, h, handle,
namespaces, namespace,
match;
for ( t = 0; t < types.length; t++ ) {
type = types[t];
all = type.indexOf(".") < 0;
if (!all ) {
namespaces = type.split(".");
type = namespaces.shift();
namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join("\\.(?:.*\\.)?") + "(\\.|$)");
}
typeHandlers = (events[type] || []).slice(0);
for ( h = 0; h < typeHandlers.length; h++ ) {
handle = typeHandlers[h];
match = (all || namespace.test(handle.namespace));
if(match){
if(selector){
if (handle.selector === selector ) {
callback(type, handle.origHandler || handle.handler);
}
} else if (selector === null){
callback(type, handle.origHandler || handle.handler, handle.selector);
}
else if (!handle.selector ) {
callback(type, handle.origHandler || handle.handler);
}
}
}
}
};
/**
* Finds event handlers of a given type on an element.
* @param {HTMLElement} el
* @param {Array} types an array of event names
* @param {String} [selector] optional selector
* @return {Array} an array of event handlers
*/
event.find = function( el, types, selector ) {
var events = ( $._data(el) || {} ).events,
handlers = [],
t, liver, live;
if (!events ) {
return handlers;
}
findHelper(events, types, function( type, handler ) {
handlers.push(handler);
}, selector);
return handlers;
};
/**
* Finds all events. Group by selector.
* @param {HTMLElement} el the element
* @param {Array} types event types
*/
event.findBySelector = function( el, types ) {
var events = $._data(el).events,
selectors = {},
//adds a handler for a given selector and event
add = function( selector, event, handler ) {
var select = selectors[selector] || (selectors[selector] = {}),
events = select[event] || (select[event] = []);
events.push(handler);
};
if (!events ) {
return selectors;
}
//first check live:
/*$.each(events.live || [], function( i, live ) {
if ( $.inArray(live.origType, types) !== -1 ) {
add(live.selector, live.origType, live.origHandler || live.handler);
}
});*/
//then check straight binds
findHelper(events, types, function( type, handler, selector ) {
add(selector || "", type, handler);
}, null);
return selectors;
};
event.supportTouch = "ontouchend" in document;
$.fn.respondsTo = function( events ) {
if (!this.length ) {
return false;
} else {
//add default ?
return event.find(this[0], $.isArray(events) ? events : [events]).length > 0;
}
};
$.fn.triggerHandled = function( event, data ) {
event = (typeof event == "string" ? $.Event(event) : event);
this.trigger(event, data);
return event.handled;
};
/**
* Only attaches one event handler for all types ...
* @param {Array} types llist of types that will delegate here
* @param {Object} startingEvent the first event to start listening to
* @param {Object} onFirst a function to call
*/
event.setupHelper = function( types, startingEvent, onFirst ) {
if (!onFirst ) {
onFirst = startingEvent;
startingEvent = null;
}
var add = function( handleObj ) {
var bySelector, selector = handleObj.selector || "";
if ( selector ) {
bySelector = event.find(this, types, selector);
if (!bySelector.length ) {
$(this).delegate(selector, startingEvent, onFirst);
}
}
else {
//var bySelector = event.find(this, types, selector);
if (!event.find(this, types, selector).length ) {
event.add(this, startingEvent, onFirst, {
selector: selector,
delegate: this
});
}
}
},
remove = function( handleObj ) {
var bySelector, selector = handleObj.selector || "";
if ( selector ) {
bySelector = event.find(this, types, selector);
if (!bySelector.length ) {
$(this).undelegate(selector, startingEvent, onFirst);
}
}
else {
if (!event.find(this, types, selector).length ) {
event.remove(this, startingEvent, onFirst, {
selector: selector,
delegate: this
});
}
}
};
$.each(types, function() {
event.special[this] = {
add: add,
remove: remove,
setup: function() {},
teardown: function() {}
};
});
};
})(jQuery);
(function($){
// TODO remove this, phantom supports touch AND click, but need to make funcunit support touch so its testable
var isPhantom = /Phantom/.test(navigator.userAgent),
supportTouch = !isPhantom && "ontouchend" in document,
scrollEvent = "touchmove scroll",
touchStartEvent = supportTouch ? "touchstart" : "mousedown",
touchStopEvent = supportTouch ? "touchend" : "mouseup",
touchMoveEvent = supportTouch ? "touchmove" : "mousemove",
data = function(event){
var d = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event;
return {
time: (new Date).getTime(),
coords: [ d.pageX, d.pageY ],
origin: $( event.target )
};
};
/**
* @add jQuery.event.swipe
*/
var swipe = $.event.swipe = {
/**
* @attribute delay
* Delay is the upper limit of time the swipe motion can take in milliseconds. This defaults to 500.
*
* A user must perform the swipe motion in this much time.
*/
delay : 500,
/**
* @attribute max
* The maximum distance the pointer must travel in pixels. The default is 75 pixels.
*/
max : 75,
/**
* @attribute min
* The minimum distance the pointer must travel in pixels. The default is 30 pixels.
*/
min : 30
};
$.event.setupHelper( [
/**
* @hide
* @attribute swipe
*/
"swipe",
/**
* @hide
* @attribute swipeleft
*/
'swipeleft',
/**
* @hide
* @attribute swiperight
*/
'swiperight',
/**
* @hide
* @attribute swipeup
*/
'swipeup',
/**
* @hide
* @attribute swipedown
*/
'swipedown'], touchStartEvent, function(ev){
//listen to mouseup
var start = data(ev),
stop,
delegate = ev.delegateTarget || ev.currentTarget,
selector = ev.handleObj.selector,
entered = this;
function moveHandler(event){
if ( !start ) {
return;
}
stop = data(event);
// prevent scrolling
if ( Math.abs( start.coords[0] - stop.coords[0] ) > 10 ) {
event.preventDefault();
}
};
$(document.documentElement).bind(touchMoveEvent,moveHandler )
.one(touchStopEvent, function(event){
$(this).unbind( touchMoveEvent, moveHandler );
if ( start && stop ) {
var deltaX = Math.abs(start.coords[0] - stop.coords[0]),
deltaY = Math.abs(start.coords[1] - stop.coords[1]),
distance = Math.sqrt(deltaX*deltaX+deltaY*deltaY);
if ( stop.time - start.time < swipe.delay && distance >= swipe.min ) {
var events = ['swipe']
if( deltaX >= swipe.min && deltaY < swipe.min) {
events.push( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" );
}else if(deltaY >= swipe.min && deltaX < swipe.min){
events.push( start.coords[1] < stop.coords[1] ? "swipedown" : "swipeup" );
}
//trigger swipe events on this guy
$.each($.event.find(delegate, events, selector), function(){
this.call(entered, ev, {start : start, end: stop})
})
}
}
start = stop = undefined;
})
});
})(jQuery)