PluginProbe
WP Directory Kit / 1.3.6
WP Directory Kit v1.3.6
1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 All 36 releases
wpdirectorykit / public / js / wdk-scroll-mobile-swipe.js

wdk-scroll-mobile-swipe.js in WP Directory Kit 1.3.6, at public/js/wdk-scroll-mobile-swipe.js

388 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ; (function (factory) {
2 if ((typeof jQuery === 'undefined' || !jQuery) && typeof define === "function" && define.amd) {
3 define(["jquery"], function (jQuery) {
4 return factory(jQuery, document, window, navigator);
5 });
6 } else if ((typeof jQuery === 'undefined' || !jQuery) && typeof exports === "object") {
7 factory(require("jquery"), document, window, navigator);
8 } else {
9 factory(jQuery, document, window, navigator);
10 }
11 }(function ($, document, window, navigator, undefined) {
12 "use strict";
13
14 // =================================================================================================================
15 // Service
16
17 var plugin_count = 0;
18
19 if (!Function.prototype.bind) {
20 Function.prototype.bind = function bind(that) {
21
22 var target = this;
23 var slice = [].slice;
24
25 if (typeof target != "function") {
26 throw new TypeError();
27 }
28
29 var args = slice.call(arguments, 1),
30 bound = function () {
31
32 if (this instanceof bound) {
33
34 var F = function () { };
35 F.prototype = target.prototype;
36 var self = new F();
37
38 var result = target.apply(
39 self,
40 args.concat(slice.call(arguments))
41 );
42 if (Object(result) === result) {
43 return result;
44 }
45 return self;
46
47 } else {
48
49 return target.apply(
50 that,
51 args.concat(slice.call(arguments))
52 );
53
54 }
55
56 };
57
58 return bound;
59 };
60 }
61 if (!Array.prototype.indexOf) {
62 Array.prototype.indexOf = function (searchElement, fromIndex) {
63 var k;
64 if (this == null) {
65 throw new TypeError('"this" is null or not defined');
66 }
67 var O = Object(this);
68 var len = O.length >>> 0;
69 if (len === 0) {
70 return -1;
71 }
72 var n = +fromIndex || 0;
73 if (Math.abs(n) === Infinity) {
74 n = 0;
75 }
76 if (n >= len) {
77 return -1;
78 }
79 k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
80 while (k < len) {
81 if (k in O && O[k] === searchElement) {
82 return k;
83 }
84 k++;
85 }
86 return -1;
87 };
88 }
89
90
91
92 // =================================================================================================================
93 // Template
94
95
96 // =================================================================================================================
97 // Core
98
99 /**
100 * Main plugin constructor
101 *
102 * @param object {Object} link to base input element
103 * @param options {Object} slider config
104 * @param plugin_count {Number}
105 * @constructor
106 */
107 var WdkScrollMobileSwipe = function (object, options, plugin_count) {
108 this.VERSION = "1";
109 this.plugin_count = plugin_count;
110 this.plugin_count = plugin_count;
111 this.object = $(object);
112
113 this.is_key = false;
114 this.is_update = false;
115 this.is_start = true;
116 this.is_finish = false;
117 this.is_active = false;
118 this.is_resize = false;
119 this.is_click = false;
120 var conf_data = this.object.find('.config') || '';
121 var config;
122 if (conf_data)
123 var config = {
124 'onChange': '',
125 'slideMoveType': 'scroll', // scroll - similar scroll, slide - based on cart size detect
126 }
127
128 options = options || {};
129
130
131 this.animate = {
132 'duration': 800
133 };
134
135 this.scrollallowwidth = 20; // in %
136 this.touchstartX = 0;
137 this.touchendX = 0;
138 this.childs_positions = {};
139 this.child_width = 0;
140 this.childrens = null;
141
142 // js config extends default config
143 $.extend(config, options);
144
145 this.options = config;
146 // validate config, to be sure that all data types are correct
147 this.update_check = {};
148
149 this.init();
150 };
151
152 WdkScrollMobileSwipe.prototype = {
153 /**
154 * Starts or updates the plugin instance
155 *
156 * @param [is_update] {boolean}
157 */
158
159 init: function (is_update) {
160 if (!this.is_mobile())
161 return false;
162
163 var that = this;
164 that.object.addClass('WdkScrollMobileSwipe');
165
166 switch (that.options.slideMoveType) {
167 case 'scroll':
168 that.slideMoveTypeScroll();
169 break;
170 case 'slide':
171 that.slideMoveTypeSlide();
172 break;
173
174 default:
175 that.slideMoveTypeScroll();
176 break;
177 }
178
179 return that;
180 },
181
182 slideMoveTypeScroll: function() {
183 var that = this;
184 var clicked = false, startX, startScrollLeft;
185 that.object.on({
186 'mousemove': function(e) {
187 clicked && updateScrollPos(e);
188 },
189 'mousedown': function(e) {
190 e.preventDefault(); // Prevent default mousedown action
191 clicked = true;
192 startX = e.pageX;
193 startScrollLeft = wrapper.scrollLeft(); // Store the initial scrollLeft position
194 },
195 'mouseup': function() {
196 clicked = false;
197 // wrapper.css('cursor', 'auto');
198 }
199 });
200
201 var updateScrollPos = function(e) {
202 e.preventDefault(); // Prevent default mousemove action
203 wrapper.scrollLeft(startScrollLeft - (e.pageX - startX)); // Change the scrollLeft position
204 }
205 },
206
207 slideMoveTypeSlide: function() {
208 var that = this;
209 that.childrens = that.object.children();
210 that.childrens.each(function (i, v) {
211 if ($(this).offset().left < 0) {
212 that.childs_positions[0] = i;
213 } else {
214 that.childs_positions[$(this).offset().left] = i;
215 }
216 });
217
218 that.object.off('touchstart').on("touchstart", function (event) { that.touchstart(event, that) });
219 that.object.off('touchend').on("touchend", function (event) { that.touchend(event, that) });
220
221 jQuery(window).on("resize", function (event) { that.resized(event, that) });
222 },
223
224 touchstart: function (e, that) {
225 e.stopPropagation();
226 that.object.stop();
227
228 if (!that.is_allow())
229 return false;
230
231 e = e.originalEvent || e;
232 that.touchstartX = that.touchendX = 0;
233 if (typeof e.changedTouches[0] != 'undefined') {
234 that.touchstartX = e.changedTouches[0].screenX
235 } else if (typeof e.targetTouches[0] != 'undefined') {
236 that.touchstartX = e.targetTouches[0].screenX
237 }
238 },
239
240 touchend: function (e, that) {
241 if (!that.is_allow())
242 return false;
243
244 e = e.originalEvent || e;
245 if (typeof e.changedTouches[0] != 'undefined') {
246 that.touchendX = e.changedTouches[0].screenX
247 } else if (typeof e.targetTouches[0] != 'undefined') {
248 that.touchendX = e.targetTouches[0].screenX
249 }
250
251 that.checkDirection();
252 },
253
254 checkDirection: function () {
255 if (this.touchendX < this.touchstartX) {
256 //alert('swiped left!');
257 this.swipeleft();
258 }
259 if (this.touchendX > this.touchstartX) {
260 //alert('swiped right!');
261 this.swiperight()
262 }
263 },
264
265 swiperight: function () {
266 var that = this;
267 var scrollRight_current = that.object.scrollLeft();
268 var slide_position_first = 0;
269 var slide_position_next = 0;
270
271 for (let position in that.childs_positions) {
272 if (scrollRight_current > position) {
273 slide_position_first = position;
274 }
275
276 if (scrollRight_current <= position) {
277 slide_position_next = position;
278 break;
279 }
280 }
281
282 if (slide_position_next == 0) {
283 slide_position_next = Object.keys(that.childs_positions).pop();
284 }
285
286 var d = slide_position_next - slide_position_first;
287
288 if (slide_position_next == 0) {
289 this.object.stop().animate({ scrollLeft: slide_position_first }, that.animate.duration);
290 } else if (((slide_position_next - scrollRight_current) / d) * 100 > that.scrollallowwidth) {
291 this.object.stop().animate({ scrollLeft: slide_position_first }, that.animate.duration);
292 } else {
293 this.object.stop().animate({ scrollLeft: slide_position_next }, that.animate.duration);
294 }
295 },
296
297 swipeleft: function () {
298 var that = this;
299 var scrollRight_current = that.object.scrollLeft();
300 var slide_position_first = 0;
301 var slide_position_next = 0;
302
303 for (let position in that.childs_positions) {
304 if (scrollRight_current > position) {
305 slide_position_first = position;
306 }
307
308 if (scrollRight_current <= position) {
309 slide_position_next = position;
310 break;
311 }
312 }
313
314 if (slide_position_next == 0) {
315 slide_position_next = Object.keys(that.childs_positions).pop();
316 }
317
318 var d = slide_position_next - slide_position_first;
319
320 if (slide_position_next == 0) {
321 this.object.stop().animate({ scrollLeft: slide_position_first }, that.animate.duration);
322 } else if (((scrollRight_current - slide_position_first) / d) * 100 > that.scrollallowwidth) {
323 this.object.stop().animate({ scrollLeft: slide_position_next }, that.animate.duration);
324 } else {
325 this.object.stop().animate({ scrollLeft: slide_position_first }, that.animate.duration);
326 }
327 },
328
329 /**
330 * Allow events
331 */
332 is_allow: function () {
333 if ($(window).width() > 767) {
334 return false;
335 }
336
337 return true;
338 },
339
340 /**
341 * Is mobile
342 */
343 is_mobile: function () {
344 let check = false;
345 (function (a) { if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true; })(navigator.userAgent || navigator.vendor || window.opera);
346
347 if (!check) {
348 check = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);
349 }
350
351 return check;
352 },
353
354 /**
355 * Resized
356 */
357 resized: function (event, that) {
358 that.childs_positions = {};
359 that.childrens.each(function (i, v) {
360 if ($(this).offset().left < 0) {
361 that.childs_positions[0] = i;
362 } else {
363 that.childs_positions[$(this).offset().left] = i;
364 }
365 });
366
367 },
368
369 /**
370 * Remove slider instance
371 * and unbind all events
372 */
373 remove: function () {
374 this.remove();
375 }
376
377 };
378
379 $.fn.WdkScrollMobileSwipe = function (options) {
380 return this.each(function () {
381 if (!$.data(this, "WdkScrollMobileSwipe")) {
382 $.data(this, "WdkScrollMobileSwipe", new WdkScrollMobileSwipe(this, options, plugin_count++));
383 }
384 });
385 };
386
387 }));
388