UNPKG

16.5 kBJavaScriptView Raw
1/*!
2 * Copyright (c) 2017 NAVER Corp.
3 * @egjs/jquery-pauseresume project is licensed under the MIT license
4 *
5 * @egjs/jquery-pauseresume JavaScript library
6 *
7 *
8 * @version 2.0.1
9 */
10(function webpackUniversalModuleDefinition(root, factory) {
11 if(typeof exports === 'object' && typeof module === 'object')
12 module.exports = factory(require("jquery"));
13 else if(typeof define === 'function' && define.amd)
14 define(["jquery"], factory);
15 else {
16 var a = typeof exports === 'object' ? factory(require("jquery")) : factory(root["jQuery"]);
17 for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
18 }
19})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
20return /******/ (function(modules) { // webpackBootstrap
21/******/ // The module cache
22/******/ var installedModules = {};
23/******/
24/******/ // The require function
25/******/ function __webpack_require__(moduleId) {
26/******/
27/******/ // Check if module is in cache
28/******/ if(installedModules[moduleId]) {
29/******/ return installedModules[moduleId].exports;
30/******/ }
31/******/ // Create a new module (and put it into the cache)
32/******/ var module = installedModules[moduleId] = {
33/******/ i: moduleId,
34/******/ l: false,
35/******/ exports: {}
36/******/ };
37/******/
38/******/ // Execute the module function
39/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
40/******/
41/******/ // Flag the module as loaded
42/******/ module.l = true;
43/******/
44/******/ // Return the exports of the module
45/******/ return module.exports;
46/******/ }
47/******/
48/******/
49/******/ // expose the modules object (__webpack_modules__)
50/******/ __webpack_require__.m = modules;
51/******/
52/******/ // expose the module cache
53/******/ __webpack_require__.c = installedModules;
54/******/
55/******/ // define getter function for harmony exports
56/******/ __webpack_require__.d = function(exports, name, getter) {
57/******/ if(!__webpack_require__.o(exports, name)) {
58/******/ Object.defineProperty(exports, name, {
59/******/ configurable: false,
60/******/ enumerable: true,
61/******/ get: getter
62/******/ });
63/******/ }
64/******/ };
65/******/
66/******/ // getDefaultExport function for compatibility with non-harmony modules
67/******/ __webpack_require__.n = function(module) {
68/******/ var getter = module && module.__esModule ?
69/******/ function getDefault() { return module['default']; } :
70/******/ function getModuleExports() { return module; };
71/******/ __webpack_require__.d(getter, 'a', getter);
72/******/ return getter;
73/******/ };
74/******/
75/******/ // Object.prototype.hasOwnProperty.call
76/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
77/******/
78/******/ // __webpack_public_path__
79/******/ __webpack_require__.p = "";
80/******/
81/******/ // Load entry module and return exports
82/******/ return __webpack_require__(__webpack_require__.s = 1);
83/******/ })
84/************************************************************************/
85/******/ ([
86/* 0 */
87/***/ (function(module, exports) {
88
89module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
90
91/***/ }),
92/* 1 */
93/***/ (function(module, exports, __webpack_require__) {
94
95"use strict";
96
97
98exports.__esModule = true;
99
100var _jquery = __webpack_require__(0);
101
102var _jquery2 = _interopRequireDefault(_jquery);
103
104var _AniPropertyManager = __webpack_require__(2);
105
106var _AniPropertyManager2 = _interopRequireDefault(_AniPropertyManager);
107
108var _MathUtil = __webpack_require__(4);
109
110var _MathUtil2 = _interopRequireDefault(_MathUtil);
111
112function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
113
114/**
115 * @namespace jQuery
116 */
117exports["default"] = function ($) {
118 var animateFn = $.fn.animate;
119 var stopFn = $.fn.stop;
120 var delayFn = $.fn.delay;
121
122 /**
123 * Generate a new easing function.
124 *
125 * function to avoid JS Hint error "Don't make functions within a loop"
126 */
127 function generateNewEasingFunc(resumePercent, remainPercent, scale, originalEasing) {
128 return function easingFunc(percent) {
129 var newPercent = resumePercent + remainPercent * percent;
130
131 return scale(originalEasing(newPercent));
132 };
133 }
134
135 $.fn.animate = function (prop, speed, easing, callback) {
136 return this.each(function () {
137 // optall should be made for each elements.
138 var optall = $.speed(speed, easing, callback);
139
140 // prepare next animation when current animation completed.
141 optall.complete = function () {
142 _AniPropertyManager2["default"].prepareNextAniProp(this);
143 };
144
145 // Queue animation property to recover the current animation.
146 _AniPropertyManager2["default"].addAniProperty("animate", this, prop, optall);
147 animateFn.call($(this), prop, optall);
148 });
149
150 // TODO: Below code is more reasonable?
151 // return animateFn.call(this, prop, optall); // and declare optall at outside this.each loop.
152 };
153
154 /**
155 * Set a timer to delay execution of subsequent items in the queue.
156 * And it internally manages "fx"queue to support pause/resume if "fx" type.
157 *
158 * @param {Number} An integer indicating the number of milliseconds to delay execution of the next item in the queue.
159 * @param {String} A string containing the name of the queue. Defaults to fx, the standard effects queue.
160 */
161 $.fn.delay = function (time, type) {
162 var t = void 0;
163 var isCallByResume = arguments.length <= 2 ? undefined : arguments[2]; // internal used value.
164
165 if (type && type !== "fx") {
166 return delayFn.call(this, time, type);
167 }
168
169 t = parseInt(time, 10);
170 t = isNaN(t) ? 0 : t;
171
172 return this.each(function () {
173 var _this = this;
174
175 if (!isCallByResume) {
176 // Queue delay property to recover the current animation.
177 // Don't add property when delay is called by resume.
178 _AniPropertyManager2["default"].addAniProperty("delay", this, null, { duration: t });
179 }
180
181 delayFn.call($(this), time).queue(function (next) {
182 next();
183
184 // Remove delay property when delay has been expired.
185 _AniPropertyManager2["default"].removeAniProperty(_this);
186 });
187 });
188 };
189
190 /**
191 * Pauses the animation executed through a call to the jQuery <a href=http://api.jquery.com/animate/>.animate()</a> method.
192 * @ko jQuery의<a href=http://api.jquery.com/animate/>animate() 메서드</a>가 실행한 애니메이션을 일시 정지한다
193 *
194 * @name jQuery#pause
195 * @method
196 * @support {"ie": "10+", "ch" : "latest", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
197 * @example
198 * $("#box").pause(); //paused the current animation
199 */
200 $.fn.pause = function () {
201 return this.each(function () {
202 var p = void 0;
203
204 if (_AniPropertyManager2["default"].getStatus(this) !== "inprogress") {
205 return;
206 }
207 // Clear fx-queue except 1 dummy function
208 // for promise not to be expired when calling stop()
209 $.queue(this, "fx", [$.noop]);
210 stopFn.call($(this));
211
212 // Remember current animation property
213 p = this.__aniProps[0];
214 if (p) {
215 p.elapsed += $.now() - p.start;
216
217 // Complement native timer's inaccuracy (complete timer can be different from your request.)
218 // (eg. your request:400ms -> real :396 ~ 415 ms ))
219 if (p.elapsed >= p.opt.duration) {
220 p = _AniPropertyManager2["default"].prepareNextAniProp(this);
221 }
222
223 p && (p.paused = true);
224 }
225 });
226 };
227
228 /**
229 * Resumes the animation paused through a call to the pause() method.
230 * @ko pause() 메서드가 일시 정지한 애니메이션을 다시 실행한다
231 *
232 * @name jQuery#resume
233 * @alias eg.Pause
234 * @method
235 * @support {"ie": "10+", "ch" : "latest", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
236 * @example
237 * $("#box").resume(); //resume the paused animation
238 */
239 $.fn.resume = function () {
240 return this.each(function () {
241 var type = "fx";
242 var p = void 0;
243 var i = void 0;
244
245 if (_AniPropertyManager2["default"].getStatus(this) !== "paused") {
246 return;
247 }
248
249 // Clear fx-queue,
250 // And this queue will be initialized by animate call.
251 $.queue(this, type || "fx", []);
252
253 // Restore __aniProps
254 i = 0;
255 p = this.__aniProps[i];
256
257 while (p) {
258 // Restore easing status
259 if (p.elapsed > 0 && p.opt.easing) {
260 var resumePercent = p.elapsed / p.opt.duration;
261 var remainPercent = 1 - resumePercent;
262 var originalEasing = $.easing[p.opt.easing];
263 var startEasingValue = originalEasing(resumePercent);
264 var scale = _MathUtil2["default"].scaler([startEasingValue, 1], [0, 1]);
265 var newEasingName = p.opt.easing + "_" + p.uuid;
266
267 // Make new easing function that continues from pause point.
268 $.easing[newEasingName] = generateNewEasingFunc(resumePercent, remainPercent, scale, originalEasing);
269 p.opt.easing = newEasingName;
270
271 // Store new easing function to clear it later.
272 p.addEasingFn(newEasingName);
273 }
274
275 p.paused = false;
276 p.opt.duration -= p.elapsed;
277
278 // If duration remains, request 'animate' with storing aniProps
279 if (p.opt.duration > 0 || p.elapsed === 0) {
280 i === 0 && p.init();
281
282 if (p.type === "delay") {
283 // pass last parameter 'true' not to add an aniProperty.
284 $(this).delay(p.opt.duration, "fx", true);
285 } else {
286 animateFn.call($(this), p.prop, p.opt);
287 }
288 }
289
290 i++;
291 p = this.__aniProps[i];
292 }
293 });
294 };
295
296 $.fn.stop = function () {
297 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
298 args[_key] = arguments[_key];
299 }
300
301 var type = args[0];
302 var clearQ = args[1];
303
304 stopFn.apply(this, args);
305
306 if (typeof type !== "string") {
307 clearQ = type;
308 }
309
310 return this.each(function () {
311 var p = void 0;
312
313 // When this element was not animated properly, do nothing.
314 if (_AniPropertyManager2["default"].getStatus(this) === "empty") {
315 return;
316 }
317
318 if (!clearQ) {
319 p = this.__aniProps.shift();
320 p && p.clearEasingFn();
321 } else {
322 // If clearQueue is requested,
323 // then all properties must be initialized
324 // for element not to be resumed.
325 p = this.__aniProps.shift();
326 while (p) {
327 p.clearEasingFn();
328 p = this.__aniProps.shift();
329 }
330 this.__aniProps = [];
331 }
332 });
333 };
334
335 $.expr.filters.paused = function (elem) {
336 return _AniPropertyManager2["default"].getStatus(elem) === "paused";
337 };
338}(_jquery2["default"]);
339
340module.exports = exports["default"];
341
342/***/ }),
343/* 2 */
344/***/ (function(module, exports, __webpack_require__) {
345
346"use strict";
347
348
349exports.__esModule = true;
350
351var _AniProperty = __webpack_require__(3);
352
353var _AniProperty2 = _interopRequireDefault(_AniProperty);
354
355function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
356
357function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
358
359var AniPropertyManager = function () {
360 function AniPropertyManager() {
361 _classCallCheck(this, AniPropertyManager);
362 }
363
364 AniPropertyManager.addAniProperty = function addAniProperty(type, el, prop, optall) {
365 var newProp = new _AniProperty2["default"](type, el, prop, optall);
366
367 el.__aniProps = el.__aniProps || [];
368
369 // Animation is excuted immediately.
370 if (el.__aniProps.length === 0) {
371 newProp.init();
372 }
373 el.__aniProps.push(newProp);
374 };
375
376 AniPropertyManager.removeAniProperty = function removeAniProperty(el) {
377 var removeProp = el.__aniProps.shift();
378
379 removeProp && removeProp.clearEasingFn();
380 el.__aniProps[0] && el.__aniProps[0].init();
381 };
382
383 AniPropertyManager.prepareNextAniProp = function prepareNextAniProp(el) {
384 // Dequeue animation property that was ended.
385 var removeProp = el.__aniProps.shift();
386 var userCallback = removeProp.opt.old;
387
388 removeProp.clearEasingFn();
389
390 // Callback should be called before aniProps.init()
391 if (userCallback && typeof userCallback === "function") {
392 userCallback.call(el);
393 }
394
395 // If next ani property exists
396 el.__aniProps[0] && el.__aniProps[0].init();
397 return el.__aniProps[0];
398 };
399
400 // Check if this element can be paused/resume.
401
402
403 AniPropertyManager.getStatus = function getStatus(el) {
404 if (!el.__aniProps || el.__aniProps.length === 0) {
405 // Current element doesn't have animation information.
406 // Check 'animate' is applied to this element.
407 return "empty";
408 }
409
410 return el.__aniProps[0].paused ? "paused" : "inprogress";
411 };
412
413 return AniPropertyManager;
414}();
415
416exports["default"] = AniPropertyManager;
417module.exports = exports["default"];
418
419/***/ }),
420/* 3 */
421/***/ (function(module, exports, __webpack_require__) {
422
423"use strict";
424
425
426exports.__esModule = true;
427
428var _jquery = __webpack_require__(0);
429
430var _jquery2 = _interopRequireDefault(_jquery);
431
432function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
433
434function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
435
436var $ = _jquery2["default"];
437var uuid = 1;
438
439var AniProperty = function () {
440 function AniProperty(type, el, prop, optall) {
441 _classCallCheck(this, AniProperty);
442
443 this.el = el;
444 this.opt = optall;
445 this.start = -1;
446 this.elapsed = 0;
447 this.paused = false;
448 this.uuid = uuid++;
449 this.easingNames = [];
450 this.prop = prop;
451 this.type = type;
452 }
453
454 AniProperty.prototype.init = function init() {
455 this.start = $.now();
456 this.elapsed = 0;
457
458 for (var propName in this.prop) {
459 var propValue = this.prop[propName];
460
461 // DO NOT SUPPORT TRANSFORM YET
462 // TODO: convert from relative value to absolute value on transform
463 if (propName === "transform") {
464 continue;
465 }
466
467 if (typeof propValue !== "string") {
468 continue;
469 }
470
471 // If it has a absoulte value.
472 var markIndex = propValue.search(/[+|-]=/);
473
474 if (markIndex < 0) {
475 // this.prop[propName] = propValue;
476 continue;
477 }
478
479 // If it has a relative value
480 var sign = propValue.charAt(markIndex) === "-" ? -1 : 1;
481
482 // Current value
483 var currValue = $.css(this.el, propName);
484
485 // CurrValue + (relativeValue)
486 this.prop[propName] = propValue.replace(/([-|+])*([\d|.])+/g, AniProperty.generateAbsoluteValMaker(currValue, propName, sign)).replace(/[-|+]+=/g, "");
487 }
488 };
489
490 AniProperty.prototype.addEasingFn = function addEasingFn(easingName) {
491 this.easingNames.push(easingName);
492 };
493
494 AniProperty.prototype.clearEasingFn = function clearEasingFn() {
495 var easing = void 0;
496
497 easing = this.easingNames.shift();
498 while (easing) {
499 delete $.easing[easing];
500 easing = this.easingNames.shift();
501 }
502 this.easingNames = [];
503 };
504
505 /**
506 * Generate a new absolute value maker.
507 *
508 * function to avoid JS Hint error "Don't make functions within a loop"
509 */
510
511
512 AniProperty.generateAbsoluteValMaker = function generateAbsoluteValMaker(prevValue, propName, sign) {
513 var prev = prevValue;
514
515 return function absoluteValMaker(match) {
516 if (!prev || prev === "auto") {
517 // Empty strings, null, undefined and "auto" are converted to 0.
518 // This solution is somewhat extracted from jQuery Tween.propHooks._default.get
519 // TODO: Should we consider adopting a Tween.propHooks?
520 prev = 0;
521 } else {
522 prev = parseFloat(prev);
523 }
524 return prev + match * sign;
525 };
526 };
527
528 return AniProperty;
529}();
530
531exports["default"] = AniProperty;
532module.exports = exports["default"];
533
534/***/ }),
535/* 4 */
536/***/ (function(module, exports, __webpack_require__) {
537
538"use strict";
539
540
541exports.__esModule = true;
542
543function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
544
545var MathUtil = function () {
546 function MathUtil() {
547 _classCallCheck(this, MathUtil);
548 }
549
550 MathUtil.interpolateNumber = function interpolateNumber(a, b) {
551 var numA = +a;
552 var numB = +b;
553
554 return function (t) {
555 return numA * (1 - t) + numB * t;
556 };
557 };
558
559 MathUtil.uninterpolateNumber = function uninterpolateNumber(a, b) {
560 var numA = +a;
561 var numB = b - numA;
562
563 numB = numB || 1 / numB;
564
565 return function (x) {
566 return (x - numA) / numB;
567 };
568 };
569
570 // Adopt linear scale from d3
571
572
573 MathUtil.scaler = function scaler(domain, range) {
574 var u = MathUtil.uninterpolateNumber(domain[0], domain[1]);
575 var i = MathUtil.interpolateNumber(range[0], range[1]);
576
577 return function (x) {
578 return i(u(x));
579 };
580 };
581
582 return MathUtil;
583}();
584
585exports["default"] = MathUtil;
586module.exports = exports["default"];
587
588/***/ })
589/******/ ]);
590});
\No newline at end of file