forked from beedesk/jQTouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqtouch.js
1376 lines (1228 loc) · 51.1 KB
/
jqtouch.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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
_/ _/_/ _/_/_/_/_/ _/
_/ _/ _/ _/_/ _/ _/ _/_/_/ _/_/_/
_/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/ _/_/ _/ _/ _/_/ _/_/_/ _/_/_/ _/ _/
_/
_/
Created by David Kaneda <http://www.davidkaneda.com>
Documentation and issue tracking on GitHub <http://wiki.github.com/senchalabs/jQTouch/>
Special thanks to Jonathan Stark <http://jonathanstark.com/>
and pinch/zoom <http://www.pinchzoom.com/>
Contributor: Thomas Yip <http://beedesk.com/>
(c) 2009-2010 by jQTouch project members and contributors
See LICENSE.txt for license.
$Revision: $
$Date: $
$LastChangedBy: $
*/
(function($) {
$.jQTouch = function(options) {
var SUPPORT_TOUCH = (typeof Touch != "undefined");
var START_EVENT = SUPPORT_TOUCH? 'touchstart' : 'mousedown';
var MOVE_EVENT = SUPPORT_TOUCH? 'touchmove' : 'mousemove';
var END_EVENT = SUPPORT_TOUCH? 'touchend' : 'mouseup';
var CANCEL_EVENT = SUPPORT_TOUCH? 'touchcancel' : 'mouseout'; // mouseout on document
// Initialize internal variables
var $body,
$head=$('head'),
hist=[],
newPageCount=0,
bogomark=1,
jQTSettings={},
hashCheckInterval,
currentPage,
currentAside=$(''),
orientation,
isMobileWebKit = RegExp(" Mobile/").test(navigator.userAgent),
tapReady=true,
lastTime=0,
lastAnimationTime=0,
actionSelectors=[],
publicObj={},
tapBuffer=351,
extensions=$.jQTouch.prototype.extensions,
actionNodeTypes=['anchor', 'area', 'back'];
behaviorModifier=['toggle'];
standardAnimations=['slide', 'flip', 'slideup', 'swap', 'cube', 'pop', 'dissolve', 'fade', 'notransition'],
animationModifiers=['smokedglass', 'clearglass'];
defaultSection=null,
animations=[],
modifiers=[],
splitscreenmode=false,
hairExtensions='';
var defaults = {
addGlossToIcon: true,
cacheGetRequests: true,
fixedViewport: true,
fullScreen: true,
fullScreenClass: 'fullscreen',
icon: null,
icon4: null, // experimental
preloadImages: false,
startupScreen: null,
statusBar: 'default', // other options: black-translucent, black
useAnimations: true,
defaultAnimation: 'slide',
defaultModifier: '',
useFastTouch: false, // Experimental.
// animation selectors
notransitionSelector: '',
cubeSelector: '.cube',
dissolveSelector: '.dissolve',
fadeSelector: '.fade',
flipSelector: '.flip',
popSelector: '.pop',
slideSelector: '.slide',
slideupSelector: '.slideup',
swapSelector: '.swap',
smokedglassSelector: '.smokedglass',
clearglassSelector: '.clearglass',
notransitionSelector: '.notransition',
// node type selectors
anchorSelector: '#jqt a',
areaSelector: '#jqt area',
backSelector: '#jqt .back, #jqt .cancel, #jqt .goback, #jqt .done, #jqt .toolbar input[type=\'reset\']',
formSelector: '#jqt form',
submitSelector: '#jqt .submit, input[type=\'submit\']',
// behavior selectors (experimental)
toggleSelector: '#jqt .tog',
// special selectors
activableSelector: '#jqt ul > li, #jqt ol > li',
swipeableSelector: '#jqt .swipe',
tapableSelector: '#jqt .tap'
};
function _debug(message) {
var now = (new Date).getTime();
var delta = now - lastTime;
lastTime = now;
if (jQTSettings.debug) {
if (message) {
console.log(delta + ': ' + message);
} else {
console.log(delta + ': ' + 'Called ' + arguments.callee.caller.name);
}
}
}
/* -- tag for code merge --
function addAnimation(animation) {
*/
function initAnimations() {
// Add animations and each selector
for (var i in standardAnimations) {
var name = standardAnimations[i];
var selector = jQTSettings[name + 'Selector'];
if (typeof(selector) == 'string' && selector.length > 0) {
var selector = jQTSettings[name + 'Selector'];
animations.push({name: name, selector: selector});
} else {
console.warn('invalid selector for animation: ' + name);
}
}
// Add animations and each selector
for (var i in animationModifiers) {
var name = animationModifiers[i];
var selector = jQTSettings[name + 'Selector'];
if (typeof(selector) == 'string' && selector.length > 0) {
var selector = jQTSettings[name + 'Selector'];
modifiers.push({name: name, selector: selector});
} else {
console.warn('invalid selector for animation: ' + name);
}
}
}
function findAnimation(search) {
var result;
var name = jQTSettings.defaultAnimation;
var modifier = jQTSettings.defaultModifier;
var matcher = function(candidate) { return false; };
if (typeof(search) === 'string') {
if (!!search) {
matcher = function(candidate) { return hasWord(search, candidate.name); };
}
} else if ($.isFunction(search)) {
matcher = search;
} else {
console.warn("Null fn for animation.");
}
for (var i = animations.length - 1; i >= 0; i--) {
if (matcher(animations[i]) === true) {
name = animations[i].name;
break;
}
}
for (var i = modifiers.length - 1; i >= 0; i--) {
if (matcher(modifiers[i]) === true) {
modifier = modifiers[i].name;
break;
}
}
result = name + " " + modifier;
return result;
}
function adjustAnimation(name, reverse) {
var result;
if (!name) {
name = jQTSettings.defaultAnimation;
}
if (reverse === true) {
var KEY = 'reverse';
var splitted = name.split(' ');
var i = $.inArray(KEY, splitted);
if (name.indexOf(KEY) >= 0 && i < 0) {
console.error('check didn\'t work');
}
if (i >= 0) {
splitted.splice(i, 1);
result = splitted.join(' ');
} else {
result = name + ' ' + KEY;
}
if (result === 'reverse') {
console.error('check failed. input: ' + name + ' output: ' + result + 'i: ' + i + ' joined: ' + splitted.join('-'));
}
} else {
result = name;
}
return result;
}
function addPageToHistory(page, search, animation) {
// Grab some info
var pageId = page.attr('id');
var page = $('#' + pageId); // normalize to actual page
var section = page.attr('section');
// Prepend info to page history
hist.unshift({
page: page,
search: search,
animation: animation,
section: section,
id: pageId
});
// update hash
if (section === defaultSection) {
location.hash = '#' + pageId + getSearchString(search);
}
startHashCheck();
}
function findPageFromHistory(search, start) {
var result;
var matcher;
if (!start) {
start = 0;
}
var number = Math.min(parseInt(search || start, 10), hist.length-1);
if (!isNaN(number)) {
matcher = function(candidate, i) { return i === number; };
} else if (typeof(search) === 'string') {
if (search === '') {
matcher = function(candidate) { return true; };
} else {
matcher = function(candidate) { return candidate.id === search; };
}
} else if ($.isFunction(search)) {
matcher = search;
} else {
matcher = function(candidate) {
var matched = true;
for (var key in search) {
if (search[key] !== candidate[key]) {
matched = false;
break;
}
}
return matched;
};
}
for (var i=start, len=hist.length; i < len; i++) {
if (matcher(hist[i], i)) {
result = $.extend({i: i}, hist[i]);
break;
}
}
return result;
}
function removePageInHistory(to, from, cond) {
if (!from) {
from = 0;
}
var fromPage = hist[from];
var section = fromPage.section;
for (var i=(to-1); i >= from; i--) {
var matched = true;
var candidate = hist[i];
if (!!cond) {
for (var key in cond) {
if (cond[key] !== candidate[key]) {
matched = false;
break;
}
}
}
if (matched) {
hist.splice(i, 1);
}
}
}
function hasWord(string, fullname) {
var result = false;
var names = fullname;
if (!$.isArray(fullname)) {
names = [];
names.push(fullname);
}
for (var i=0, len=names.length; i<len; i++) {
if ((new RegExp('(^|\\s)' + names[i] + '(\\s|$)')).test(string)) {
result = true;
break;
}
}
return result;
};
function findWord(string, fullname) {
var result = "";
var names = fullname;
if (!$.isArray(fullname)) {
names = [];
names.push(fullname);
}
for (var i=0, len=names.length; i<len; i++) {
if ((new RegExp('(^|\\s)' + names[i] + '(\\s|$)')).test(string)) {
result = names[i];
break;
}
}
return result;
};
function parseSearch(q) {
// Andy E and community @ http://stackoverflow.com/posts/2880929/revisions
var results = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); };
while (e = r.exec(q)) {
results[d(e[1])] = d(e[2]);
}
return results;
};
function getSearchString(search) {
var result = '';
for (var item in search) {
if (result.length !== 0) {
result += '&';
}
result += item + '=' + encodeURIComponent(search[item]);
}
if (result.length > 0) {
result = '?' + result;
}
return result;
};
/* -- tag for code merge --
function doNavigation(fromPage, toPage, animation, backwards) {
*/
function animatePages(params) {
var toPage, fromPage, animation, pagecallback;
toPage = params.to;
fromPage = params.from;
animation = params.animation;
pagecallback = params.pagecallback;
// Error check for target page
if (!toPage || !fromPage || toPage.length == 0 || fromPage.length == 0) {
$.fn.unselect();
console.error('Target element is missing. Dest: ' + toPage + ' Source: ' + fromPage);
tapReady = true;
return false;
}
// Error check for fromPage=toPage
if (toPage.hasClass('current')) {
$.fn.unselect();
console.error('Target element is the current page.');
tapReady = true;
return false;
}
// Collapse the keyboard
$(':focus').blur();
// Make sure we are scrolled up to hide location bar
toPage.css('top', window.pageYOffset);
// animation settings
var backwards = !!animation? $.inArray('reverse', animation.split(' ')) >= 0: false;
var animation = !backwards? animation: adjustAnimation(animation, true);
var main = toPage.attr('section') === defaultSection;
fromPage.trigger('pageAnimationStart', { direction: 'out' });
toPage.trigger('pageAnimationStart', { direction: 'in' });
if ($.support.WebKitAnimationEvent && animation && jQTSettings.useAnimations && animation !== 'notransition') {
tapReady = false;
if (main) {
currentAside.addClass('front');
} else {
currentPage.addClass('front');
}
if (backwards) {
toPage.toggleClass('reverse');
fromPage.toggleClass('reverse');
}
// Support both transitions and animations
fromPage[0].addEventListener('webkitTransitionEnd', callback, false);
fromPage[0].addEventListener('webkitAnimationEnd', callback, false);
fromPage.addClass(animation + ' out');
toPage.addClass(animation + ' in current');
if (hasWord(animation, animationModifiers)) {
var modifier = findWord(animation, animationModifiers);
if (!backwards) {
fromPage.addClass(modifier);
toPage.removeClass(modifier);
} else {
fromPage.removeClass(modifier);
toPage.removeClass(modifier);
}
}
var darkart = 30;
if (params.heavy && bogomark > 0.1) {
darkart = Math.floor(350 * bogomark);
}
setTimeout(function() {
fromPage.addClass('start');
toPage.addClass('start');
setTimeout(function() {
if ($.isFunction(pagecallback)) {
pagecallback();
}
}, 10);
}, darkart);
} else {
toPage.addClass('current');
callback();
if ($.isFunction(pagecallback)) {
pagecallback();
}
}
// Define callback to run after animation completes
function callback(event) {
if($.support.WebKitAnimationEvent && animation !== 'notransition') {
fromPage[0].removeEventListener('webkitTransitionEnd', callback);
fromPage[0].removeEventListener('webkitAnimationEnd', callback);
}
if (animation && animation !== 'notransition') {
toPage.removeClass('start in ' + animation);
fromPage.removeClass('start out current ' + animation);
if (hasWord(animation, animationModifiers)) {
var modifier = findWord(animation, animationModifiers);
if (!backwards) {
fromPage.addClass(modifier);
toPage.removeClass(modifier);
} else {
fromPage.removeClass(modifier);
toPage.removeClass(modifier);
}
}
if (backwards) {
toPage.toggleClass('reverse');
fromPage.toggleClass('reverse');
}
toPage.css('top', 0);
} else {
fromPage.removeClass('current active');
if (hasWord(animation, animationModifiers)) {
var modifier = findWord(animation, animationModifiers);
if (!backwards) {
fromPage.addClass(modifier);
toPage.removeClass(modifier);
} else {
fromPage.removeClass(modifier);
toPage.removeClass(modifier);
}
}
}
toPage.trigger('pageAnimationEnd', { direction: 'in', reverse: backwards });
fromPage.trigger('pageAnimationEnd', { direction: 'out', reverse: backwards });
clearInterval(hashCheckInterval);
if (main) {
currentPage = toPage;
currentAside.removeClass('front');
} else {
currentAside = toPage;
currentPage.removeClass('front');
}
var $originallink = toPage.data('referrer');
if ($originallink) {
$originallink.unselect();
}
lastAnimationTime = (new Date()).getTime();
tapReady = true;
};
return true;
}
function getOrientation() {
return orientation;
}
function goBack(param) {
var to, from;
if (typeof(param) === 'string' || param instanceof jQuery) {
/* back compat param */
to = param;
from = null;
} else if (!!param) {
to = param.to;
from = param.from;
}
// init the param
if (hist.length <= 1) {
window.history.go(-2);
}
var fromPage;
var toPage;
if (!!from) {
fromPage = findPageFromHistory(from, 0);
} else {
fromPage = $.extend({i: 0}, hist[0]);
}
if (!fromPage) {
console.error('History in invalid state or goback is called at the home page.');
return false;
}
if (hist.length > 1) {
if (!!to) {
var myto = to.substring(1); // remove #
toPage = findPageFromHistory(myto, fromPage.i+1);
if (!toPage) {
console.error('Cannot find page "' + myto + '" in the history.');
to = null; // reset to to null, trying to recover
}
}
if (!to) {
to = {section: fromPage.section};
toPage = findPageFromHistory(to, fromPage.i+1);
if (!toPage) {
console.error('Cannot find history to go back to. The specified "from" or "to" parameters might be invalid. Or, it has already back to the beginning.');
return false;
}
}
if (toPage.id !== fromPage.id) {
var adjustedName = adjustAnimation(fromPage.animation, true);
var animationstarted = animatePages({
to: toPage.page,
from: fromPage.page,
animation: adjustedName,
pagecallback: function() {
$('#' + fromPage.id).trigger('pageout', {hash: '#' + fromPage.id, search: fromPage.search});
$('#' + toPage.id).trigger('pageresume', {hash: '#' + toPage.id, search: toPage.search});
}});
if (animationstarted) {
// Remove all pages in front of the target page
removePageInHistory(toPage.i, fromPage.i, {section: fromPage.section});
$(toPage.id).trigger('pagein', {});
$(fromPage.id).trigger('pagesuspense', {});
} else {
console.error('Could not go back.');
return;
}
} else {
// branch on href.search (ie, ..?abc=1&ijk=2)
if (!match(fromPage.search, toPage.search)) {
$('#' + fromPage.id).trigger('pageout', {hash: '#' + fromPage.id, search: fromPage.search});
$('#' + toPage.id).trigger('pagein', {hash: '#' + toPage.id, search: toPage.search});
removePageInHistory(toPage.i, fromPage.i, {section: fromPage.section});
} else {
$.fn.unselect();
console.error('Target element is the current page.');
return false;
}
}
} else {
location.hash = '#' + hist[0].id + getSearchString(param.search);
}
return publicObj;
}
function goTo(param, p2, p3) {
var toPage, search, animation, reverse;
if (typeof(param) === 'string' || param instanceof jQuery) { /* back compt hack */
/* back compat param */
toPage = param;
animation = p2;
reverse = p3;
} else {
toPage = param.to;
search = param.search;
animation = param.animation;
reverse = param.reverse;
}
if (typeof(toPage) === 'string') {
nextPage = $(toPage);
if (nextPage.length < 1) {
showPageByHref(toPage, {
'animation': animation
});
return;
} else {
toPage = nextPage;
}
}
var section = toPage.attr('section');
var criteria = !!section? {section: section}: {section: defaultSection};
var fromPage = findPageFromHistory(criteria, 0);
var adjustedName = adjustAnimation(animation, reverse);
if (!fromPage) {
console.error('Cannot find source page.');
return false;
} else if (toPage[0].id !== fromPage.id) {
var animationstarted = animatePages({
to: toPage,
from: fromPage.page,
animation: adjustedName,
heavy: param.heavy,
pagecallback: function() {
$('#' + fromPage.id).trigger('pagesuspense', {hash: '#' + fromPage.id, search: fromPage.search});
toPage.trigger('pagein', {hash: '#' + toPage.attr('id'), search: param.search});
}});
if (animationstarted) {
addPageToHistory(toPage, search, adjustedName);
return publicObj;
} else {
console.error('Could not animate pages.');
return false;
}
} else {
// branch on href.search (ie, ..?abc=1&ijk=2)
if (!match(fromPage.search, search)) {
$('#' + fromPage.id).trigger('pageout', {hash: '#' + fromPage.id, search: fromPage.search});
toPage.trigger('pagein', {hash: '#' + toPage.attr('id'), search: param.search});
removePageInHistory(fromPage.i, 0, {section: fromPage.section});
addPageToHistory(toPage, search, adjustedName);
} else {
$.fn.unselect();
console.error('Target element is the current page.');
return false;
}
}
}
function match(apple, orange) {
var matched = true;
for(var field in apple) {
if (apple[field] !== null) {
if (apple[field] !== orange[field]) {
matched = false;
break;
}
} else {
if (orange[field] !== null) {
matched = false;
break;
}
}
}
if (matched) {
for(var field in orange) {
if(orange[field] === undefined) {
matched = false;
break;
}
}
}
return matched; // debugger friendliness: give a single exit
}
function hashCheck() {
var curid = currentPage.attr('id');
if (location.hash != '#' + curid) {
clearInterval(hashCheckInterval);
// goBack(location.hash);
}
else if (location.hash == '') {
location.hash = '#' + curid;
}
}
function startHashCheck() {
clearInterval(hashCheckInterval);
hashCheckInterval = setInterval(hashCheck, 100);
}
function init(options) {
jQTSettings = $.extend({}, defaults, options);
// Preload images
if (jQTSettings.preloadImages) {
for (var i = jQTSettings.preloadImages.length - 1; i >= 0; i--) {
(new Image()).src = jQTSettings.preloadImages[i];
};
}
// Set appropriate icon (retina display stuff is experimental)
if (jQTSettings.icon || jQTSettings.icon4) {
var precomposed, appropriateIcon;
if (jQTSettings.icon4 && window.devicePixelRatio && window.devicePixelRatio === 2) {
appropriateIcon = jQTSettings.icon4;
} else if (jQTSettings.icon) {
appropriateIcon = jQTSettings.icon;
}
if (appropriateIcon) {
precomposed = (jQTSettings.addGlossToIcon) ? '' : '-precomposed';
hairExtensions += '<link rel="apple-touch-icon' + precomposed + '" href="' + appropriateIcon + '" />';
}
}
// Set startup screen
if (jQTSettings.startupScreen) {
hairExtensions += '<link rel="apple-touch-startup-image" href="' + jQTSettings.startupScreen + '" />';
}
// Set viewport
if (jQTSettings.fixedViewport) {
hairExtensions += '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"/>';
}
// Set full-screen
if (jQTSettings.fullScreen) {
hairExtensions += '<meta name="apple-mobile-web-app-capable" content="yes" />';
if (jQTSettings.statusBar) {
hairExtensions += '<meta name="apple-mobile-web-app-status-bar-style" content="' + jQTSettings.statusBar + '" />';
}
}
// Attach hair extensions
if (hairExtensions) {
$head.prepend(hairExtensions);
}
}
function insertPages(nodes, animation) {
var targetPage = null;
$(nodes).each(function(index, node) {
var $node = $(this);
if (!$node.attr('id')) {
$node.attr('id', 'page-' + (++newPageCount));
}
var section = $node.attr('section');
if (!section) {
$node.attr('section', defaultSection);
}
$node.children().find('[section~="' + section + '"]').removeClass('missection');
$node.children().find('[section]:not([section~="' + section + '"])').addClass('missection');
$body.trigger('pageInserted', {page: $node.appendTo($body)});
if ($node.hasClass('current') || !targetPage) {
targetPage = $node;
}
});
if (targetPage !== null) {
goTo({ /* do i need to set referrer? */
to: '#' + targetPage[0].id,
animation: animation
});
return targetPage;
} else {
return false;
}
}
function updateOrientation() {
orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
$body.removeClass('portrait landscape').addClass(orientation).trigger('turn', {orientation: orientation});
}
function showPageByHref(href, options) {
var defaults = {
data: null,
method: 'GET',
animation: null,
callback: null,
$referrer: null
};
var settings = $.extend({}, defaults, options);
if (href != '#') {
tapReady = false;
$.ajax({
url: href,
data: settings.data,
type: settings.method,
success: function (data, textStatus) {
var firstPage = insertPages(data, settings.animation);
if (firstPage) {
if (settings.method == 'GET' && jQTSettings.cacheGetRequests === true && settings.$referrer) {
settings.$referrer.attr('href', '#' + firstPage.attr('id'));
}
if (settings.callback) {
settings.callback(true);
}
} else {
tapReady = true;
}
},
error: function (data) {
if (settings.$referrer) {
settings.$referrer.unselect();
}
if (settings.callback) {
settings.callback(false);
}
tapReady = true;
}
});
} else if (settings.$referrer) {
settings.$referrer.unselect();
}
}
function submitHandler(e, callback) {
$(':focus').blur();
e.preventDefault();
var $form = (typeof(e)==='string') ? $(e).eq(0) : (e.target ? $(e.target) : $(e));
if (!$form.length) return false;
// someone else will handle this event
if (!$form.is(jQTSettings.formSelector) && !$form.is(jQTSettings.selectSelector)) {
return true;
}
if ($form.attr('action')) {
showPageByHref($form.attr('action'), {
data: $form.serialize(),
method: $form.attr('method') || "POST",
animation: animations[0] || null,
callback: callback
});
}
return false;
}
function submitParentForm($el) {
var $form = $el.closest('form');
if ($form.length) {
var evt = $.Event("submit");
evt.preventDefault();
$form.trigger(evt);
return false;
}
return true;
}
/* -- tag for code merge --
function supportForAnimationEvents() {
function supportForCssMatrix() {
function supportForTouchEvents() {
function supportForTransform3d() {
};
*/
function clickHandler(e) {
_debug();
if (!tapReady) {
_debug('ClickHandler handler aborted because tap is not ready');
e.preventDefault();
return false;
}
// Figure out whether to prevent default
var $el = $(e.target);
// Find the nearest tappable ancestor
if (!$el.is(actionSelectors.join(', '))) {
var $el = $(e.target).closest(actionSelectors.join(', '));
}
// Prevent default if we found an internal link (relative or absolute)
if ($el && $el.attr('href') && !$el.isExternalLink()) {
_debug('Need to prevent default click behavior');
e.preventDefault();
} else {
_debug('No need to prevent default click behavior');
}
// Trigger a tap event if touchstart is not on the job
if ($.support.touch) {
_debug('Not converting click to a tap event because touch handler is on the job');
} else {
_debug('Converting click event to a tap event');
$(e.target).trigger('tap', e);
}
}
function tapHandler(e){
// Grab the clicked element
var $el = $(e.currentTarget);
var anyActionSelectors = actionSelectors.join(', ');
if (!$el.is(anyActionSelectors)) {
var $link = $(e.target).closest(anyActionSelectors);
if ($link.length) {
$el = $link;
} else {
console.warn('Not a known node type. type: ' + e.target.nodeName + ' id: ' + e.target.id);
return;
}
}
var target = $el.attr('target'),
hash = $el.attr('hash'),
search = parseSearch($el.attr('search'));
if (tapReady == false || !$el.length) {
console.warn('Not able to tap element.');
return false;
}
if ($el.isExternalLink()) {
$el.removeClass('active');
return true;
}
// Figure out the animation to use
var animation = findAnimation(function(candidate) {
return $el.is(candidate.selector);
});
var reverse = $el.hasClass('reverse');
// Handle supported action type
if ($el.is(jQTSettings.backSelector)) {
// User clicked a back button
// find out the from page
var from;
var cur = e.currentTarget;
while (!!cur.parentNode) {
// $.parents('#jqt > *') matchs random, but parents: need to roll our own loop
if (cur.parentNode.id === 'jqt') { // found
from = cur.id;
break;
}
cur = cur.parentNode;
}
goBack({
to: hash,
from: from
});
} else if ($el.is(jQTSettings.submitSelector)) {
// User clicked or tapped a submit element
submitParentForm($el);
} else if (target === '_webapp') {
// User clicked an internal link, fullscreen mode
window.location = $el.attr('href');
} else if ($el.attr('href') === '#' || !$el.attr('href')) {
// Allow tap on item with no href
$el.unselect();
return true;
} else if (hash && hash !== '#') {
if ($el.is(jQTSettings.toggleSelector)) {
if ($(hash).hasClass('current')) {
goBack({
to: null,
from: hash.substring(1)
});
} else {
goTo({
to: $(hash).data('referrer', $el),
search: search,
animation: animation,
reverse: reverse
});
}
} else {
// Branch on internal or external href
goTo({
to: $(hash).data('referrer', $el),
search: search,
animation: animation,
reverse: reverse
});
}