/
toolbelt.js
134 lines (120 loc) · 3.73 KB
/
toolbelt.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
/*!
* Toolbelt JavaScript Library v0.0.1
* https://github.com/brimanning/Toolbelt
*
* Uses jQuery.js
* http://jquery.com/
*
* Copyright 2014 Brian Manning
* Released under the MIT license
*
* Date: 1/21/2014
*/
(function (w, $, s) {
w.toolbelt = {};
w.toolbelt.resizeFunctions = [];
w.toolbelt.addEventOnWindowResize = function (f) {
var eventsToBindTo = 'resize fullscreenchange webkitfullscreenchange mozfullscreenchange orientationchange';
$(window).unbind(eventsToBindTo).bind(eventsToBindTo, w.toolbelt.runWindowResizeEvents);
w.toolbelt.resizeFunctions.push(f);
f();
};
w.toolbelt.runWindowResizeEvents = function () {
for (var i = 0, l = w.toolbelt.resizeFunctions.length; i < l; i++) {
w.toolbelt.resizeFunctions[i]();
}
};
w.toolbelt.checkSWFObject = function (cb, preferredUrl) {
if (typeof swfobject === 'undefined' || swfobject === null) {
$.getScript(w.toolbelt.exists(preferredUrl) ? preferredUrl : window.location.protocol + '//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js', function () {
if (!!cb) {
cb();
}
});
} else {
if (!!cb) {
cb();
}
}
};
w.toolbelt.exists = function(obj) {
return typeof obj !== 'undefined' && !!obj;
};
w.toolbelt.reRender = function() {
w.scrollTo(0, w.pageYOffset);
$(w).trigger('scroll');
};
//Sometimes you just gotta know if it's iOS, IE, Android, we live in a sad, imperfect world
w.toolbelt.checkBrowser = function(browser) {
return navigator.userAgent.toLowerCase().match(new RegExp(browser)) != null;
};
w.toolbelt.isMobile = function() {
return w.toolbelt.checkBrowser('iphone|ipod|ipad|mobile safari|mobile|android|iemobile|Windows Phone');
};
w.toolbelt.isIOS = function() {
return w.toolbelt.checkBrowser('iphone|ipod|ipad|mobile safari');
};
w.toolbelt.isIPad = function() {
return w.toolbelt.checkBrowser('ipad');
};
w.toolbelt.isIPod = function() {
return w.toolbelt.checkBrowser('ipod');
};
w.toolbelt.isIPhone = function() {
return w.toolbelt.checkBrowser('iphone');
};
w.toolbelt.isAndroid = function() {
return w.toolbelt.checkBrowser('android');
};
w.toolbelt.isWindowsPhone = function() {
return w.toolbelt.checkBrowser('iemobile|Windows Phone');
};
w.toolbelt.isVisible = function(obj) {
if (typeof obj === 'string') {
obj = $(obj);
}
if (obj.is(':hidden')) {
return false;
}
var $window = $(w),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = obj.offset().top,
elemBottom = elemTop + obj.height();
return elemBottom >= docViewTop && elemTop <= docViewBottom;
};
w.toolbelt.cachedAjax = function(options) {
var val = null;
options = options || {};
if (!w.toolbelt.exists(options.storageType)) {
s.setType(options.storageType);
}
val = s.get(options.url);
if (!!val && !!val.expiration && val.expiration < new Date().getTime()) {
val = null;
s.remove(options.url);
}
if (val === null) {
var successPromise = options.success,
success = function(val) {
if (w.toolbelt.exists(options.expires) && options.expires > 0) {
var now = new Date();
val.expiration = now.setSeconds(now.getSeconds() + options.expires);
}
s.set(options.url, val);
successPromise(val);
};
options.success = success;
$.ajax(options);
} else if (w.toolbelt.exists(options.success)) {
options.success(val);
}
};
w.toolbelt.ajax = function(options) {
if (options.cached) {
w.toolbelt.cachedAjax(options);
} else {
$.ajax(options);
}
};
}(window, jQuery, snap));