forked from jordansinger/Hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.js
171 lines (138 loc) · 6.05 KB
/
hook.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
/**
* Hook
* Version: 1.1
* Author: Jordan Singer, Brandon Jacoby, Adam Girton
* Copyright (c) 2013 - Hook. All rights reserved.
* http://www.usehook.com
*/
;(function ( $, window, document, undefined ) {
var win = $(this),
st = win.scrollTop() || window.pageYOffset,
called = false;
var hasTouch = function() {
return !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
};
var methods = {
init: function(options) {
return this.each(function() {
var $this = $(this),
settings = $this.data('hook');
if(typeof(settings) === 'undefined') {
var defaults = {
reloadPage: true, // if false will reload element
dynamic: true, // if false Hook elements already there
textRequired: false, // will input loader text if true
scrollWheelSelected: false, // will use scroll wheel events
swipeDistance: 50, // swipe distance for loader to show on touch devices
loaderClass: 'hook-loader',
spinnerClass: 'hook-spinner',
loaderTextClass: 'hook-text',
loaderText: 'Reloading...',
reloadEl: function() {}
};
settings = $.extend({}, defaults, options);
$this.data('hook', settings);
} else {
settings = $.extend({}, settings, options);
}
if(settings.dynamic === true) {
var loaderElem = '<div class=' + settings.loaderClass + '>';
loaderElem += '<div class='+ settings.spinnerClass + '/>';
loaderElem += '</div>';
var spinnerTextElem = '<span class='+ settings.loaderTextClass + '>' + settings.loaderText + '</span>';
$this
.append(loaderElem);
if (settings.textRequired === true) {
$this.addClass('hook-with-text');
$this.append(spinnerTextElem);
}
}
if(!hasTouch()) {
if(settings.scrollWheelSelected === true){
win.on('mousewheel', function(event, delta) {
methods.onScroll($this, settings, delta);
});
} else {
win.on('scroll', function() {
methods.onScroll($this, settings);
});
}
} else {
var lastY = 0,
swipe = 0;
win.on('touchstart', function(e){
lastY = e.originalEvent.touches[0].pageY;
});
win.on('touchmove', function(e) {
swipe = e.originalEvent.touches[0].pageY + lastY;
st = $(this).scrollTop();
if(swipe < settings.swipeDistance) {
e.preventDefault();
}
if(swipe > settings.swipeDistance && lastY <= 40) {
methods.onSwipe($this, settings);
}
});
win.on('touchend', function(){
swipe = 0;
});
}
});
},
onScroll: function(el, settings, delta) {
st = win.scrollTop();
if(settings.scrollWheelSelected === true && (delta >= 150 && st <= 0)) {
if(called === false) {
methods.reload(el, settings);
called = true;
}
}
if(settings.scrollWheelSelected === false && st <= 0) {
if(called === false) {
methods.reload(el, settings);
called = true;
}
}
},
onSwipe: function(el, settings) {
if(st <= 0) {
methods.reload(el, settings);
}
},
reload: function(el, settings) {
el.show();
el.animate({
"marginTop": "0px"
}, 200);
el.delay(500).slideUp(200, function () {
if(settings.reloadPage) {
window.location.reload(true);
}
called = false;
});
if(!settings.reloadPage) {
settings.reloadEl();
}
},
destroy: function() {
return $(this).each(function(){
var $this = $(this);
$this.empty();
$this.removeData('hook');
});
}
};
$.fn.hook = function () {
var method = arguments[0];
if(methods[method]) {
method = methods[method];
arguments = Array.prototype.slice.call(arguments, 1);
} else if (typeof(method) === 'object' || !method) {
method = methods.init;
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
return method.apply(this, arguments);
};
})( jQuery, window, document );