-
Notifications
You must be signed in to change notification settings - Fork 0
/
lungo.js
2830 lines (2419 loc) · 85.5 KB
/
lungo.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
/* lungo v2.1.0 - 2013/4/29
http://lungo.tapquo.com
Copyright (c) 2013 Tapquo S.L. - Licensed GPLv3, Commercial */
var Lungo = Lungo || {};
// Add commonjs export for component.io support
if ((typeof module !== 'undefined') && module.exports) {
// add Quo as module dependencie
var Quo = require('quojs');
// Export module
module.exports = Lungo;
}
Lungo.VERSION = '2.1';
Lungo.Element || (Lungo.Element = {});
Lungo.Data || (Lungo.Data = {});
Lungo.Sugar || (Lungo.Sugar = {});
Lungo.View || (Lungo.View = {});
Lungo.Boot || (Lungo.Boot = {});
Lungo.Device || (Lungo.Device = {});
Lungo.ready || (Lungo.ready = Quo().ready);
/**
* Object with data-attributes (HTML5) with a special <markup>
*
* @namespace Lungo
* @class Attributes
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
* @author Guillermo Pascual <[email protected]> || @pasku1
*/
Lungo.Attributes = {
count: {
selector: '*',
html: '<span class="tag theme count">{{value}}</span>'
},
pull: {
selector: 'section',
html: '<div class="{{value}}" data-control="pull" data-icon="down" data-loading="black">\
<strong>title</strong>\
</div>'
},
progress: {
selector: '*',
html: '<div class="progress">\
<span class="bar"><span class="value" style="width:{{value}};"></span></span>\
</div>'
},
label: {
selector: '*',
html: '<abbr>{{value}}</abbr>'
},
icon: {
selector: '*',
html: '<span class="icon {{value}}"></span>'
},
image: {
selector: '*',
html: '<img src="{{value}}" class="icon" />'
},
title: {
selector: 'header',
html: '<span class="title centered">{{value}}</span>'
},
loading: {
selector: '*',
html: '<div class="loading {{value}}">\
<span class="top"></span>\
<span class="right"></span>\
<span class="bottom"></span>\
<span class="left"></span>\
</div>'
},
back: {
selector: 'header',
html: '<nav class="left"><a href="#back" data-router="section" class="left"><span class="icon {{value}}"></span></a></nav>'
}
};
/**
* Object with data-attributes (HTML5) with a special <markup>
*
* @namespace Lungo
* @class Constants
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.Constants = {
ELEMENT: {
SECTION: 'section',
ARTICLE: 'article',
ASIDE: 'aside',
BODY: 'body',
DIV: 'div',
LIST: '<ul></ul>',
LI: 'li'
},
QUERY: {
LIST_IN_ELEMENT: 'article.list, aside.list',
ELEMENT_SCROLLABLE: 'aside.scroll, article.scroll'
},
CLASS: {
ACTIVE: 'active',
ASIDE: 'aside',
SHOW: 'show',
HIDE: 'hide',
HIDING: 'hiding',
RIGHT: 'right',
LEFT: 'left',
HORIZONTAL: 'horizontal',
SMALL: 'small'
},
TRIGGER: {
LOAD: 'load',
UNLOAD: 'unload'
},
TRANSITION: {
DURATION: 350
},
ATTRIBUTE: {
ID: 'id',
HREF: 'href',
TITLE: 'title',
ARTICLE: 'article',
CLASS: 'class',
WIDTH: 'width',
HEIGHT: 'height',
PIXEL: 'px',
PERCENT: '%',
ROUTER: 'router',
FIRST: 'first',
LAST: 'last',
EMPTY: ''
},
BINDING: {
START: '{{',
END: '}}',
KEY: 'value',
SELECTOR: '{{value}}'
},
ERROR: {
DATABASE: 'ERROR: Connecting to Data.Sql.',
DATABASE_TRANSACTION: 'ERROR: Data.Sql >> ',
ROUTER: 'ERROR: The target does not exists >>',
LOADING_RESOURCE: 'ERROR: Loading resource: '
}
};
/**
* Contains all the common functions used in Lungo.
*
* @namespace Lungo
* @class Core
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
* @author Guillermo Pascual <[email protected]> || @pasku1
*/
Lungo.Core = (function(lng, $$, undefined) {
var ARRAY_PROTO = Array.prototype;
var HASHTAG_CHARACTER = '#';
/**
* Console system to display messages when you are in debug mode.
*
* @method log
*
* @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
* @param {string} Message to show in console
*/
var log = function(severity, message) {
if (!lng.Core.isMobile()) {
console[(severity === 1) ? 'log' : (severity === 2) ? 'warn' : 'error'](message);
} else {
// @todo : send to the server
}
};
/**
* Executes callbacks based on the parameters received.
*
* @method execute
*
* @param {Function} callback to execute
*/
var execute = function() {
var args = toArray(arguments);
var callback = args.shift();
if (toType(callback) === 'function') {
callback.apply(null, args);
}
};
/**
* Creates a new function that, when called, itself calls this function in
* the context of the provided this value, with a given sequence of arguments
* preceding any provided when the new function was called.
*
* @method bind
*
* @param {object} object to which the 'this' can refer in the new function when the new function is called.
* @param {Function} method A function object.
*/
var bind = function(object, method) {
return function() {
return method.apply(object, toArray(arguments));
};
};
/**
* Copy from any number of objects and mix them all into a new object.
* The implementation is simple; just loop through arguments and
* copy every property of every object passed to the function.
*
* @method mix
*
* @param {object} arguments to mix them all into a new object.
* @return {object} child a new object with all the objects from the arguments mixed.
*/
var mix = function() {
var child = child || {};
for (var arg = 0, len = arguments.length; arg < len; arg++) {
var argument = arguments[arg];
for (var prop in argument) {
if (isOwnProperty(argument, prop)) {
child[prop] = argument[prop];
}
}
}
return child;
};
/**
* Every object descended from Object inherits the hasOwnProperty method.
* This method can be used to determine whether an object has the specified property
* as a direct property of that object.
*
* @param {object} object to test for a property's existence inside itself.
* @param {string} property the name of the property to test.
* @return {boolean} indicating whether the object has the specified property.
*/
var isOwnProperty = function(object, property) {
return $$.isOwnProperty(object, property);
};
/**
* Determine the internal JavaScript [[Class]] of an object.
*
* @param {object} obj to get the real type of itself.
* @return {string} with the internal JavaScript [[Class]] of itself.
*/
var toType = function(obj) {
return $$.toType(obj);
};
/**
* Convert an array-like object into a true JavaScript array.
*
* @param {object} obj Any object to turn into a native Array.
* @return {object} The object is now a plain array.
*/
var toArray = function(obj) {
return ARRAY_PROTO.slice.call(obj, 0);
};
/**
* Determine if the current environment is a mobile environment
*
* @method isMobile
*
* @return {boolean} true if is mobile environment, false if not.
*/
var isMobile = function() {
return $$.isMobile();
};
/**
* Returns information of execute environment
*
* @method environment
*
* @return {object} Environment information
*/
var environment = function() {
return $$.environment();
};
/**
* Returns a ordered list of objects by a property
*
* @method orderByProperty
*
* @param {list} List of objects
* @param {string} Name of property
* @param {string} Type of order: asc (ascendent) or desc (descendent)
* @return {list} Ordered list
*/
var orderByProperty = function(data, property, order) {
var order_operator = (order === 'desc') ? -1 : 1;
return data.sort(function(a, b) {
return (a[property] < b[property]) ? - order_operator :
(a[property] > b[property]) ? order_operator : 0;
}
);
};
/**
* Returns a correct URL using hashtag character
*
* @method parseUrl
*
* @param {string} Url
* @return {string} Url parsed
*/
var parseUrl = function(href) {
var href_hashtag = href.lastIndexOf(HASHTAG_CHARACTER);
if (href_hashtag > 0) {
href = href.substring(href_hashtag);
} else if (href_hashtag === -1) {
href = HASHTAG_CHARACTER + href ;
}
return href;
};
/**
* Returns a Object in a list by a property value
*
* @method objectInListByProperty
*
* @param {list} List of objects
* @param {string} Name of property
* @param {var} Value for comparision
* @return {object} Instance of object founded (if exists)
*/
var findByProperty = function(list, property, value) {
var search = null;
for (var i = 0, len = list.length; i < len; i++) {
var element = list[i];
if (element[property] == value) {
search = element;
break;
}
}
return search;
};
return {
log: log,
execute: execute,
bind: bind,
mix: mix,
isOwnProperty: isOwnProperty,
toType: toType,
toArray: toArray,
isMobile: isMobile,
environment: environment,
orderByProperty: orderByProperty,
parseUrl: parseUrl,
findByProperty: findByProperty
};
})(Lungo, Quo);
/**
* LungoJS Dom Handler
*
* @namespace Lungo
* @class Dom
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
* @author Guillermo Pascual <[email protected]> || @pasku1
*/
/**
* Add an event listener
*
* @method dom
*
* @param {string} <Markup> element selector
* @return {Object} QuoJS <element> instance
*/
Lungo.dom = function(selector) {
return $$(selector);
};
/**
* ?
*
* @namespace Lungo
* @class Fallback
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.Events = (function(lng, undefined) {
var SPACE_CHAR = ' ';
var init = function(events) {
for (event in events) {
var index_of = event.indexOf(SPACE_CHAR);
if (index_of > 0) {
var event_name = event.substring(0, index_of);
var element = event.substring(index_of + 1);
lng.dom(element).on(event_name, events[event]);
}
}
};
return {
init: init
};
})(Lungo);
/**
* ?
*
* @namespace Lungo
* @class Fallback
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.Fallback = (function(lng, undefined) {
var fixPositionInAndroid = function() {
env = lng.Core.environment();
if (env.isMobile && env.os.name === 'Android' && env.os.version < "3") {
lng.dom(document.body).data("position", "absolute");
} else {
lng.dom(document.body).data("position", "fixed");
}
};
return {
fixPositionInAndroid: fixPositionInAndroid
};
})(Lungo);
/**
* Instance initializer
*
* @namespace Lungo
* @class Init
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.init = function(config) {
if (config && config.resources) {
Lungo.Resource.load(config.resources);
}
Lungo.Boot.Events.init();
Lungo.Boot.Data.init();
Lungo.Boot.Layout.init();
};
/**
* Notification system in CSS3
*
* @namespace Lungo
* @class Notification
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.Notification = (function(lng, undefined) {
var _options = [];
var _el = null;
var _window = null;
var DELAY_TIME = 1;
var ANIMATION_MILISECONDS = 200;
var ATTRIBUTE = lng.Constants.ATTRIBUTE;
var BINDING = lng.Constants.BINDING;
var SELECTOR = {
BODY: 'body',
NOTIFICATION: '.notification',
MODAL: '.notification .window',
MODAL_HREF: '.notification .window a',
WINDOW_CLOSABLE: '.notification [data-action=close], .notification > .error, .notification > .success',
CONFIRM_BUTTONS: '.notification .confirm a.button'
};
var STYLE = {
MODAL: 'modal',
VISIBLE: 'visible',
SHOW: 'show',
WORKING: 'working',
INPUT: 'input'
};
var CALLBACK_HIDE = 'Lungo.Notification.hide()';
var MARKUP_NOTIFICATION = '<div class="notification"><div class="window"></div></div>';
/**
*
*/
var show = function(title, icon, seconds, callback) {
var markup;
if (title !== undefined) {
markup = _markup(title, null, icon);
} else {
var data_loading = lng.Attributes.loading.html;
markup = data_loading.replace(BINDING.START + BINDING.KEY + BINDING.END, 'icon white');
}
_show(markup, 'growl');
_hide(seconds, callback);
};
/**
*
*/
var hide = function() {
_window.removeClass('show');
setTimeout(function() {
_el.style('display', 'none').removeClass('html').removeClass('confirm').removeClass('notify').removeClass('growl');
}, ANIMATION_MILISECONDS - 50);
};
/**
*
*/
var confirm = function(options) {
_options = options;
var markup = _markup(options.title, options.description, options.icon);
markup += _button_markup(options.accept, 'accept');
markup += _button_markup(options.cancel, 'cancel');
_show(markup, 'confirm');
};
/**
*
*/
var success = function(title, description, icon, seconds, callback) {
_notify(title, description, icon, 'success', seconds, callback);
};
/**
*
*/
var error = function(title, description, icon, seconds, callback) {
_notify(title, description, icon, 'error', seconds, callback);
};
/**
*
*/
var _notify = function(title, description, icon, stylesheet, seconds, callback) {
_show(_markup(title, description, icon), stylesheet);
if (seconds) {
_hide(seconds, callback);
}
};
/**
*
*/
var html = function(markup, button) {
markup += (button) ? '<a href="#" class="button large anchor" data-action="close">' + button + '</a>' : '';
_show(markup, 'html');
};
var _init = function() {
lng.dom(SELECTOR.BODY).append(MARKUP_NOTIFICATION);
_el = lng.dom(SELECTOR.NOTIFICATION);
_window = _el.children('.window');
_subscribeEvents();
};
var _show = function(html, stylesheet) {
_el.show();
_window.removeClass(STYLE.SHOW);
_window.removeClass('error').removeClass('success').removeClass('html').removeClass('growl');
_window.addClass(stylesheet);
_window.html(html);
setTimeout(function() {
_window.addClass(STYLE.SHOW);
}, DELAY_TIME);
};
var _hide = function(seconds, callback) {
if (seconds !== undefined && seconds !== 0) {
var miliseconds = seconds * 1000;
setTimeout(function() {
hide();
// if (callback) callback.apply(callback);
if (callback) setTimeout(callback, ANIMATION_MILISECONDS);
}, miliseconds);
}
};
var _markup = function(title, description, icon) {
description = !description ? " " : description;
return '<span class="icon ' + icon + '"></span><strong class="text bold">' + title + '</strong><small>' + description + '</small>';
};
var _button_markup = function(options, callback) {
return '<a href="#" data-callback="' + callback + '" class="button anchor large text thin">' + options.label + '</a>';
};
var _subscribeEvents = function() {
lng.dom(SELECTOR.CONFIRM_BUTTONS).tap(function(event) {
var button = lng.dom(this);
var callback = _options[button.data('callback')].callback;
if (callback) callback.call(callback);
hide();
});
lng.dom(SELECTOR.WINDOW_CLOSABLE).tap( hide );
};
_init();
return {
show: show,
hide: hide,
error: error,
success: success,
confirm: confirm,
html: html
};
})(Lungo);
/**
* Load Resources
*
* @namespace Lungo
* @class Resource
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
*/
Lungo.Resource = (function(lng, $$, undefined) {
var ELEMENT = lng.Constants.ELEMENT;
var ERROR = lng.Constants.ERROR;
/**
* Start loading async sections (local & remote)
*
* @method start
*
*/
var load = function(resource) {
if (lng.Core.toType(resource) === 'array') {
for (var i=0, len=resource.length; i < len; i++) {
_load(resource[i]);
}
} else {
_load(resource);
}
};
/**
*
*/
var _load = function(resource) {
try {
var response = _loadSyncResource(resource);
_pushResourceInBody(response);
} catch(error) {
lng.Core.log(3, error.message);
}
};
var _loadSyncResource = function(url) {
return $$.ajax({
url: url,
async: false,
dataType: 'html',
error: function() {
console.error(ERROR.LOADING_RESOURCE + url);
}
});
};
var _pushResourceInBody = function(section) {
if (lng.Core.toType(section) === 'string') {
lng.dom(ELEMENT.BODY).append(section);
}
};
return {
load: load
};
})(Lungo, Quo);
/*! Overthrow v.0.1.0. An overflow:auto polyfill for responsive design. (c) 2012: Scott Jehl, Filament Group, Inc. http://filamentgroup.github.com/Overthrow/license.txt */
(function( w, undefined ){
var doc = w.document,
docElem = doc.documentElement,
classtext = "scroll-enabled",
// Touch events are used in the polyfill, and thus are a prerequisite
canBeFilledWithPoly = "ontouchmove" in doc,
// The following attempts to determine whether the browser has native overflow support
// so we can enable it but not polyfill
overflowProbablyAlreadyWorks =
// Features-first. iOS5 overflow scrolling property check - no UA needed here. thanks Apple :)
"WebkitOverflowScrolling" in docElem.style ||
// Touch events aren't supported and screen width is greater than X
// ...basically, this is a loose "desktop browser" check.
// It may wrongly opt-in very large tablets with no touch support.
( !canBeFilledWithPoly && w.screen.width > 1200 ) ||
// Hang on to your hats.
// Whitelist some popular, overflow-supporting mobile browsers for now and the future
// These browsers are known to get overlow support right, but give us no way of detecting it.
(function(){
var ua = w.navigator.userAgent,
// Webkit crosses platforms, and the browsers on our list run at least version 534
webkit = ua.match( /AppleWebKit\/([0-9]+)/ ),
wkversion = webkit && webkit[1],
wkLte534 = webkit && wkversion >= 534;
return (
/* Android 3+ with webkit gte 534
~: Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13 */
ua.match( /Android ([0-9]+)/ ) && RegExp.$1 >= 3 && wkLte534 ||
/* Blackberry 7+ with webkit gte 534
~: Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+ */
ua.match( / Version\/([0-9]+)/ ) && RegExp.$1 >= 0 && w.blackberry && wkLte534 ||
/* Blackberry Playbook with webkit gte 534
~: Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/0.0.1 Safari/534.8+ */
ua.indexOf( /PlayBook/ ) > -1 && RegExp.$1 >= 0 && wkLte534 ||
/* Firefox Mobile (Fennec) 4 and up
~: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:2.1.1) Gecko/ Firefox/4.0.2pre Fennec/4.0. */
ua.match( /Fennec\/([0-9]+)/ ) && RegExp.$1 >= 4 ||
/* WebOS 3 and up (TouchPad too)
~: Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.48 Safari/534.6 TouchPad/1.0 */
ua.match( /wOSBrowser\/([0-9]+)/ ) && RegExp.$1 >= 233 && wkLte534 ||
/* Nokia Browser N8
~: Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/012.002; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/533.4 (KHTML, like Gecko) NokiaBrowser/7.3.0 Mobile Safari/533.4 3gpp-gba
~: Note: the N9 doesn't have native overflow with one-finger touch. wtf */
ua.match( /NokiaBrowser\/([0-9\.]+)/ ) && parseFloat(RegExp.$1) === 7.3 && webkit && wkversion >= 533
);
})(),
// Easing can use any of Robert Penner's equations (http://www.robertpenner.com/easing_terms_of_use.html). By default, overthrow includes ease-out-cubic
// arguments: t = current iteration, b = initial value, c = end value, d = total iterations
// use w.overthrow.easing to provide a custom function externally, or pass an easing function as a callback to the toss method
defaultEasing = function (t, b, c, d) {
return c*((t=t/d-1)*t*t + 1) + b;
},
enabled = false,
// Keeper of intervals
timeKeeper,
/* toss scrolls and element with easing
// elem is the element to scroll
// options hash:
* left is the desired horizontal scroll. Default is "+0". For relative distances, pass a string with "+" or "-" in front.
* top is the desired vertical scroll. Default is "+0". For relative distances, pass a string with "+" or "-" in front.
* duration is the number of milliseconds the throw will take. Default is 100.
* easing is an optional custom easing function. Default is w.overthrow.easing. Must follow the easing function signature
*/
toss = function( elem, options ){
var i = 0,
sLeft = elem.scrollLeft,
sTop = elem.scrollTop,
// Toss defaults
o = {
top: "+0",
left: "+0",
duration: 100,
easing: w.overthrow.easing
},
endLeft, endTop;
// Mixin based on predefined defaults
if( options ){
for( var j in o ){
if( options[ j ] !== undefined ){
o[ j ] = options[ j ];
}
}
}
// Convert relative values to ints
// First the left val
if( typeof o.left === "string" ){
o.left = parseFloat( o.left );
endLeft = o.left + sLeft;
}
else {
endLeft = o.left;
o.left = o.left - sLeft;
}
// Then the top val
if( typeof o.top === "string" ){
o.top = parseFloat( o.top );
endTop = o.top + sTop;
}
else {
endTop = o.top;
o.top = o.top - sTop;
}
timeKeeper = setInterval(function(){
if( i++ < o.duration ){
elem.scrollLeft = o.easing( i, sLeft, o.left, o.duration );
elem.scrollTop = o.easing( i, sTop, o.top, o.duration );
}
else{
if( endLeft !== elem.scrollLeft ){
elem.scrollLeft = endLeft;
}
if( endTop !== elem.scrollTop ){
elem.scrollTop = endTop;
}
intercept();
}
}, 1 );
// Return the values, post-mixin, with end values specified
return { top: endTop, left: endLeft, duration: o.duration, easing: o.easing };
},
// find closest overthrow (elem or a parent)
closest = function( target, ascend ){
return !ascend && target.className && target.className.indexOf( "scroll" ) > -1 && target || closest( target.parentNode );
},
// Intercept any throw in progress
intercept = function(){
clearInterval( timeKeeper );
},
// Enable and potentially polyfill overflow
enable = function(){
// If it's on,
if( enabled ){
return;
}
// It's on.
enabled = true;
// If overflowProbablyAlreadyWorks or at least the element canBeFilledWithPoly, add a class to cue CSS that assumes overflow scrolling will work (setting height on elements and such)
if( overflowProbablyAlreadyWorks || canBeFilledWithPoly ){
docElem.className += " " + classtext;
}
// Destroy everything later. If you want to.
w.overthrow.forget = function(){
// Strip the class name from docElem
docElem.className = docElem.className.replace( classtext, "" );
// Remove touch binding (check for method support since this part isn't qualified by touch support like the rest)
if( doc.removeEventListener ){
doc.removeEventListener( "touchstart", start, false );
}
// reset easing to default
w.overthrow.easing = defaultEasing;
// Let 'em know
enabled = false;
};
// If overflowProbablyAlreadyWorks or it doesn't look like the browser canBeFilledWithPoly, our job is done here. Exit viewport left.
if( overflowProbablyAlreadyWorks || !canBeFilledWithPoly ){
return;
}
// Fill 'er up!
// From here down, all logic is associated with touch scroll handling
// elem references the overthrow element in use
var elem,
// The last several Y values are kept here
lastTops = [],
// The last several X values are kept here
lastLefts = [],
// lastDown will be true if the last scroll direction was down, false if it was up
lastDown,
// lastRight will be true if the last scroll direction was right, false if it was left
lastRight,
// For a new gesture, or change in direction, reset the values from last scroll
resetVertTracking = function(){
lastTops = [];
lastDown = null;
},
resetHorTracking = function(){
lastLefts = [];
lastRight = null;
},
// After releasing touchend, throw the overthrow element, depending on momentum
finishScroll = function(){
// Come up with a distance and duration based on how
// Multipliers are tweaked to a comfortable balance across platforms
var top = ( lastTops[ 0 ] - lastTops[ lastTops.length -1 ] ) * 8,
left = ( lastLefts[ 0 ] - lastLefts[ lastLefts.length -1 ] ) * 8,
duration = Math.max( Math.abs( left ), Math.abs( top ) ) / 8;
// Make top and left relative-style strings (positive vals need "+" prefix)
top = ( top > 0 ? "+" : "" ) + top;
left = ( left > 0 ? "+" : "" ) + left;
// Make sure there's a significant amount of throw involved, otherwise, just stay still
if( !isNaN( duration ) && duration > 0 && ( Math.abs( left ) > 80 || Math.abs( top ) > 80 ) ){
toss( elem, { left: left, top: top, duration: duration } );
}
},
// On webkit, touch events hardly trickle through textareas and inputs
// Disabling CSS pointer events makes sure they do, but it also makes the controls innaccessible
// Toggling pointer events at the right moments seems to do the trick
// Thanks Thomas Bachem http://stackoverflow.com/a/5798681 for the following
inputs,
setPointers = function( val ){
inputs = elem.querySelectorAll( "textarea, input" );
for( var i = 0, il = inputs.length; i < il; i++ ) {
inputs[ i ].style.pointerEvents = val;
}
},
// For nested overthrows, changeScrollTarget restarts a touch event cycle on a parent or child overthrow
changeScrollTarget = function( startEvent, ascend ){
if( doc.createEvent ){
var newTarget = ( !ascend || ascend === undefined ) && elem.parentNode || elem.touchchild || elem,
tEnd;
if( newTarget !== elem ){
tEnd = doc.createEvent( "HTMLEvents" );
tEnd.initEvent( "touchend", true, true );
elem.dispatchEvent( tEnd );
newTarget.touchchild = elem;
elem = newTarget;
newTarget.dispatchEvent( startEvent );
}
}
},
// Touchstart handler
// On touchstart, touchmove and touchend are freshly bound, and all three share a bunch of vars set by touchstart
// Touchend unbinds them again, until next time
start = function( e ){
// Stop any throw in progress
intercept();
// Reset the distance and direction tracking
resetVertTracking();
resetHorTracking();
elem = closest( e.target );
if( !elem || elem === docElem || e.touches.length > 1 ){
return;
}
setPointers( "none" );
var touchStartE = e,
scrollT = elem.scrollTop,