Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added removing of subscriptions from document and window on destroy #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/js/fotorama.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jQuery.Fotorama = function ($fotorama, opts) {
stageWheelTail = {},
navShaftTouchTail = {},
navWheelTail = {},
smartClickTails = [],

scrollTop,
scrollLeft,
Expand Down Expand Up @@ -1319,6 +1320,12 @@ jQuery.Fotorama = function ($fotorama, opts) {
that.cancelFullScreen();
that.stopAutoplay();

stageShaftTouchTail.destroy();
navShaftTouchTail.destroy();
$.each(smartClickTails, function (i, item) {
item.destroy();
});

data = that.data = null;

appendElements();
Expand Down Expand Up @@ -1581,7 +1588,7 @@ jQuery.Fotorama = function ($fotorama, opts) {
clickToShow({index: $arrs.index(this) ? '>' : '<', slow: e.altKey, user: true});
}

smartClick($arrs, function (e) {
smartClickTails = smartClick($arrs, function (e) {
stopEvent(e);
onArrClick.call(this, e);
}, {
Expand Down
29 changes: 24 additions & 5 deletions src/js/touch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var lastEvent,
moveEventType,
preventEvent,
preventEventTimeout;
preventEventTimeout,
nsCounter = 0;

function extendEvent (e) {
var touch = (e.touches || [])[0] || e;
Expand All @@ -21,7 +22,8 @@ function touch ($el, options) {
targetIsSelectFLAG,
targetIsLinkFlag,
tolerance,
moved;
moved,
ns = ".fotoramatouch_" + (nsCounter++);

function onStart (e) {
$target = $(e.target);
Expand Down Expand Up @@ -127,6 +129,22 @@ function touch ($el, options) {
}, TOUCH_TIMEOUT);
}

function destroy(){
$DOCUMENT.off(ns);
$WINDOW.off(ns);

if (MS_POINTER) {
removeEvent(document, 'MSPointerMove', onMove);
removeEvent(document,'MSPointerCancel', onEnd);
removeEvent(document, 'MSPointerUp', onEnd);
}
else {
removeEvent(document, 'touchstart', onOtherStart);
removeEvent(document, 'touchend', onOtherEnd);
removeEvent(document, 'touchcancel', onOtherEnd);
}
}

if (MS_POINTER) {
addEvent(el, 'MSPointerDown', onStart);
addEvent(document, 'MSPointerMove', onMove);
Expand All @@ -141,17 +159,18 @@ function touch ($el, options) {
addEvent(document, 'touchend', onOtherEnd);
addEvent(document, 'touchcancel', onOtherEnd);

$WINDOW.on('scroll', onOtherEnd);
$WINDOW.on('scroll' + ns, onOtherEnd);

$el.on('mousedown', onStart);
$DOCUMENT
.on('mousemove', onMove)
.on('mouseup', onEnd);
.on('mousemove' + ns, onMove)
.on('mouseup' + ns, onEnd);
}

$el.on('click', 'a', function (e) {
tail.checked && stopEvent(e);
});

tail.destroy = destroy;
return tail;
}
13 changes: 9 additions & 4 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ function getIndexFromHash (hash, data, ok, startindex) {

function smartClick ($el, fn, _options) {
_options = _options || {};

var tails = [];
$el.each(function () {
var $this = $(this),
thisData = $this.data(),
Expand All @@ -423,8 +423,7 @@ function smartClick ($el, fn, _options) {
if (thisData.clickOn) return;

thisData.clickOn = true;

$.extend(touch($this, {
tails.push($.extend(touch($this, {
onStart: function (e) {
startEvent = e;
(_options.onStart || noop).call(this, e);
Expand All @@ -436,8 +435,9 @@ function smartClick ($el, fn, _options) {
if (result.moved) return;
fn.call(this, startEvent);
}
}), {noMove: true});
}), {noMove: true}));
});
return tails;
}

function div (classes, child) {
Expand Down Expand Up @@ -502,6 +502,11 @@ function addEvent (el, e, fn, bool) {
el.addEventListener ? el.addEventListener(e, fn, !!bool) : el.attachEvent('on'+e, fn);
}

function removeEvent(el, e, fn, bool) {
if (!e) return;
el.removeEventListener ? el.removeEventListener(e, fn, !!bool) : el.detachEvent('on'+e, fn);
}

function elIsDisabled (el) {
return !!el.getAttribute('disabled');
}
Expand Down