From 9f136e404d2f09e0cc6d47c695d348f0b1ffb597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Aydo=C4=9Fdu?= Date: Fri, 24 Mar 2017 16:19:23 +0300 Subject: [PATCH 1/4] Added unregister trigger support --- src/moveTo.js | 9 +++++++-- tests/moveTo-spec.js | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/moveTo.js b/src/moveTo.js index a763564..a9b42e5 100644 --- a/src/moveTo.js +++ b/src/moveTo.js @@ -87,6 +87,7 @@ const MoveTo = (() => { * Register a dom element as trigger * @param {HTMLElement} dom Dom trigger element * @param {function} callback Callback function + * @return {function|void} unregister function */ MoveTo.prototype.registerTrigger = function(dom, callback) { if (!dom) { @@ -104,10 +105,14 @@ const MoveTo = (() => { options.callback = callback; } - dom.addEventListener('click', (e) => { + const listener = (e) => { e.preventDefault(); this.move(target, options); - }); + }; + + dom.addEventListener('click', listener, false); + + return () => dom.removeEventListener('click', listener, false); }; /** diff --git a/tests/moveTo-spec.js b/tests/moveTo-spec.js index 47ad104..be1c8e0 100644 --- a/tests/moveTo-spec.js +++ b/tests/moveTo-spec.js @@ -18,6 +18,9 @@ function createMockDomElement(options) { offsetParent: null, addEventListener: function(event, cb) { this._listeners[event] = cb; + }, + removeEventListener: function(event, cb) { + delete this._listeners[event]; } }; } @@ -65,15 +68,23 @@ test('It should change options', (t) => { test('It should register trigger', (t) => { const instance = new MoveTo(); - const elem = createMockDomElement({}); - t.is(elem._listeners['click'], undefined); instance.registerTrigger(elem); t.true(typeof elem._listeners['click'] === 'function'); }); +test('It should unregister trigger', (t) => { + const instance = new MoveTo(); + const elem = createMockDomElement({}); + const unregister = instance.registerTrigger(elem); + + t.true(typeof elem._listeners['click'] === 'function'); + unregister(); + t.true(typeof elem._listeners['click'] === 'undefined'); +}); + test('It should add custom ease function', (t) => { const inst = new MoveTo(); const elem = createMockDomElement({}); From 9a5fe39d2702a01d4e850da2ee202a4f58889682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Aydo=C4=9Fdu?= Date: Fri, 24 Mar 2017 16:29:14 +0300 Subject: [PATCH 2/4] Added example of unregistering a trigger --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index ca448ec..e99c00c 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,24 @@ Ease function. See [Easing Equations](http://gizma.com/easing/) for more ease fu ``` +
+ Unregister a trigger + + ```js + document.addEventListener('DOMContentLoaded', function () { + const moveTo = new MoveTo(); + + const trigger = document.getElementsByClassName('js-trigger')[0]; + + // Registered a trigger + const unregister = moveTo.registerTrigger(trigger, { duration: 500 }); + + // Unregister a trigger + unregister(); + }); +``` +
+ ## Development setup ```sh From 9af18d76264e0e13aac0e0e3f64707bc3ac39da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Aydo=C4=9Fdu?= Date: Fri, 24 Mar 2017 16:30:41 +0300 Subject: [PATCH 3/4] typo fix --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e99c00c..9503c63 100644 --- a/README.md +++ b/README.md @@ -181,10 +181,10 @@ Ease function. See [Easing Equations](http://gizma.com/easing/) for more ease fu const moveTo = new MoveTo(); const trigger = document.getElementsByClassName('js-trigger')[0]; - - // Registered a trigger + + // Register a trigger const unregister = moveTo.registerTrigger(trigger, { duration: 500 }); - + // Unregister a trigger unregister(); }); From e126bf47c228f858964d536f8790029fed842e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Aydo=C4=9Fdu?= Date: Mon, 27 Mar 2017 10:56:28 +0300 Subject: [PATCH 4/4] Bump version --- dist/moveTo.js | 11 ++++++++--- dist/moveTo.min.js | 4 ++-- package.json | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/dist/moveTo.js b/dist/moveTo.js index e872bd6..c7a116f 100644 --- a/dist/moveTo.js +++ b/dist/moveTo.js @@ -1,6 +1,6 @@ /*! * MoveTo - A lightweight scroll animation javascript library without any dependency. - * Version 1.5.3 (24-03-2017 14:41) + * Version 1.6.0 (27-03-2017 10:55) * Licensed under MIT * Copyright 2017 Hasan Aydoğdu */ @@ -94,6 +94,7 @@ var MoveTo = function () { * Register a dom element as trigger * @param {HTMLElement} dom Dom trigger element * @param {function} callback Callback function + * @return {function|void} unregister function */ MoveTo.prototype.registerTrigger = function (dom, callback) {var _this = this; if (!dom) { @@ -111,10 +112,14 @@ var MoveTo = function () { options.callback = callback; } - dom.addEventListener('click', function (e) { + var listener = function listener(e) { e.preventDefault(); _this.move(target, options); - }); + }; + + dom.addEventListener('click', listener, false); + + return function () {return dom.removeEventListener('click', listener, false);}; }; /** diff --git a/dist/moveTo.min.js b/dist/moveTo.min.js index 8b4940e..ed0202b 100644 --- a/dist/moveTo.min.js +++ b/dist/moveTo.min.js @@ -1,8 +1,8 @@ /*! * MoveTo - A lightweight scroll animation javascript library without any dependency. - * Version 1.5.3 (24-03-2017 14:41) + * Version 1.6.0 (27-03-2017 10:55) * Licensed under MIT * Copyright 2017 Hasan Aydoğdu */ -"use strict";var MoveTo=function(){function t(t,e,n,o){return t/=o,t--,-n*(t*t*t*t-1)+e}function e(t){for(var e=0,n=0;t;)e+=t.offsetTop,n+=t.offsetLeft,t=t.offsetParent;return{top:e,left:n}}function n(t,e){var n={};return Object.keys(t).forEach(function(e){n[e]=t[e]}),Object.keys(e).forEach(function(t){n[t]=e[t]}),n}function o(t){return t.replace(/([A-Z])/g,function(t){return"-"+t.toLowerCase()})}function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.options=n(a,e),this.easeFunctions=n({easeOutQuart:t},o)}function i(t,e){var n={};return Object.keys(e).forEach(function(e){var r=t.getAttribute("data-mt-"+o(e));r&&(n[e]=isNaN(r)?r:parseInt(r,10))}),n}var a={tolerance:0,duration:800,easing:"easeOutQuart",callback:function(){}};return r.prototype.registerTrigger=function(t,e){var o=this;if(t){var r=t.getAttribute("href")||t.getAttribute("data-target"),a=r&&"#"!==r?document.getElementById(r.substring(1)):0,u=n(this.options,i(t,this.options));"function"==typeof e&&(u.callback=e),t.addEventListener("click",function(t){t.preventDefault(),o.move(a,u)})}},r.prototype.move=function(t){var o=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(0===t||t){r=n(this.options,r);var i="number"==typeof t?t:e(t).top,a=window.pageYOffset;i-=r.tolerance;var u=i-a,c=null,s=void 0,f=function e(n){var f=window.pageYOffset;c||(c=n-1);var v=n-c;if(s&&(s===f||u>0&&s>f||u<0&&s0&&void 0!==arguments[0]?arguments[0]:{},o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.options=n(a,e),this.easeFunctions=n({easeOutQuart:t},o)}function i(t,e){var n={};return Object.keys(e).forEach(function(e){var r=t.getAttribute("data-mt-"+o(e));r&&(n[e]=isNaN(r)?r:parseInt(r,10))}),n}var a={tolerance:0,duration:800,easing:"easeOutQuart",callback:function(){}};return r.prototype.registerTrigger=function(t,e){var o=this;if(t){var r=t.getAttribute("href")||t.getAttribute("data-target"),a=r&&"#"!==r?document.getElementById(r.substring(1)):0,u=n(this.options,i(t,this.options));"function"==typeof e&&(u.callback=e);var c=function(t){t.preventDefault(),o.move(a,u)};return t.addEventListener("click",c,!1),function(){return t.removeEventListener("click",c,!1)}}},r.prototype.move=function(t){var o=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(0===t||t){r=n(this.options,r);var i="number"==typeof t?t:e(t).top,a=window.pageYOffset;i-=r.tolerance;var u=i-a,c=null,s=void 0,f=function e(n){var f=window.pageYOffset;c||(c=n-1);var v=n-c;if(s&&(s===f||u>0&&s>f||u<0&&s