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

fix: fix "load" actions in dynamically added components #11077

Merged
merged 4 commits into from
Mar 26, 2018
Merged
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
2 changes: 1 addition & 1 deletion gulp/tasks/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gulp.task('javascript', ['javascript:foundation', 'javascript:deps', 'javascript
var pluginsAsExternals = {
'jquery': 'jQuery',
'./foundation.core': '{Foundation: window.Foundation}',
'./foundation.util.core' : '{rtl: window.Foundation.rtl, GetYoDigits: window.Foundation.GetYoDigits, transitionend: window.Foundation.transitionend, RegExpEscape: window.Foundation.RegExpEscape}',
'./foundation.util.core' : '{rtl: window.Foundation.rtl, GetYoDigits: window.Foundation.GetYoDigits, transitionend: window.Foundation.transitionend, RegExpEscape: window.Foundation.RegExpEscape, onLoad: window.Foundation.onLoad}',
'./foundation.util.imageLoader' : '{onImagesLoaded: window.Foundation.onImagesLoaded}',
'./foundation.util.keyboard' : '{Keyboard: window.Foundation.Keyboard}',
'./foundation.util.mediaQuery' : '{MediaQuery: window.Foundation.MediaQuery}',
Expand Down
1 change: 1 addition & 0 deletions js/entries/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Foundation.rtl = CoreUtils.rtl;
Foundation.GetYoDigits = CoreUtils.GetYoDigits;
Foundation.transitionend = CoreUtils.transitionend;
Foundation.RegExpEscape = CoreUtils.RegExpEscape;
Foundation.onLoad = CoreUtils.onLoad;

Foundation.Box = Box;
Foundation.onImagesLoaded = onImagesLoaded;
Expand Down
3 changes: 2 additions & 1 deletion js/entries/plugins/foundation.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ Foundation.addToJquery($);
// These are now separated out, but historically were a part of this module,
// and since this is here for backwards compatibility we include them in
// this entry.
import {rtl, GetYoDigits, transitionend, RegExpEscape} from '../../foundation.util.core';
import {rtl, GetYoDigits, transitionend, RegExpEscape, onLoad} from '../../foundation.util.core';
Foundation.rtl = rtl;
Foundation.GetYoDigits = GetYoDigits;
Foundation.transitionend = transitionend;
Foundation.RegExpEscape = RegExpEscape;
Foundation.onLoad = onLoad;

// Every plugin depends on plugin now, we can include that on the core for the
// script inclusion path.
Expand Down
4 changes: 2 additions & 2 deletions js/foundation.accordion.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import $ from 'jquery';
import { onLoad, GetYoDigits } from './foundation.util.core';
import { Keyboard } from './foundation.util.keyboard';
import { GetYoDigits } from './foundation.util.core';
import { Plugin } from './foundation.plugin';

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ class Accordion extends Plugin {
//roll up a little to show the titles
if (this.options.deepLinkSmudge) {
var _this = this;
$(window).on('load', function() {
onLoad($(window), function() {
var offset = _this.$element.offset();
$('html, body').animate({ scrollTop: offset.top }, _this.options.deepLinkSmudgeDelay);
});
Expand Down
31 changes: 13 additions & 18 deletions js/foundation.magellan.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import $ from 'jquery';
import { GetYoDigits } from './foundation.util.core';
import { onLoad, GetYoDigits } from './foundation.util.core';
import { Plugin } from './foundation.plugin';
import { SmoothScroll } from './foundation.smoothScroll';

Expand Down Expand Up @@ -83,6 +83,7 @@ class Magellan extends Plugin {
duration: _this.options.animationDuration,
easing: _this.options.animationEasing
};

$(window).one('load', function(){
if(_this.options.deepLinking){
if(location.hash){
Expand All @@ -93,28 +94,19 @@ class Magellan extends Plugin {
_this._updateActive();
});

if (document.readyState === "complete") {
_this.$element.on({
'resizeme.zf.trigger': _this.reflow.bind(_this),
'scrollme.zf.trigger': _this._updateActive.bind(_this)
}).on('click.zf.magellan', 'a[href^="#"]', function(e) {
e.preventDefault();
var arrival = this.getAttribute('href');
_this.scrollToLoc(arrival);
});
} else {
$(window).one('load', function(){
_this.$element.on({
_this.onLoadListener = onLoad($(window), function () {
_this.$element
.on({
'resizeme.zf.trigger': _this.reflow.bind(_this),
'scrollme.zf.trigger': _this._updateActive.bind(_this)
}).on('click.zf.magellan', 'a[href^="#"]', function(e) {
})
.on('click.zf.magellan', 'a[href^="#"]', function (e) {
e.preventDefault();
var arrival = this.getAttribute('href');
_this.scrollToLoc(arrival);
});
});
}

});

this._deepLinkScroll = function(e) {
if(_this.options.deepLinking) {
_this.scrollToLoc(window.location.hash);
Expand Down Expand Up @@ -234,7 +226,10 @@ class Magellan extends Plugin {
var hash = this.$active[0].getAttribute('href');
window.location.hash.replace(hash, '');
}
$(window).off('hashchange', this._deepLinkScroll);

$(window)
.off('hashchange', this._deepLinkScroll)
.off(this.onLoadListener);
}
}

Expand Down
13 changes: 8 additions & 5 deletions js/foundation.offcanvas.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

import $ from 'jquery';
import { onLoad, transitionend, RegExpEscape } from './foundation.util.core';
import { Keyboard } from './foundation.util.keyboard';
import { MediaQuery } from './foundation.util.mediaQuery';
import { transitionend, RegExpEscape } from './foundation.util.core';
import { Plugin } from './foundation.plugin';

import { Triggers } from './foundation.util.triggers';
Expand Down Expand Up @@ -166,15 +166,17 @@ class OffCanvas extends Plugin {
_setMQChecker() {
var _this = this;

$(window).on('changed.zf.mediaquery', function() {
this.onLoadListener = onLoad($(window), function () {
if (MediaQuery.atLeast(_this.options.revealOn)) {
_this.reveal(true);
} else {
_this.reveal(false);
}
}).one('load.zf.offCanvas', function() {
});

$(window).on('changed.zf.mediaquery', function () {
if (MediaQuery.atLeast(_this.options.revealOn)) {
_this.reveal(true);
} else {
_this.reveal(false);
}
});
}
Expand Down Expand Up @@ -445,6 +447,7 @@ class OffCanvas extends Plugin {
this.close();
this.$element.off('.zf.trigger .zf.offCanvas');
this.$overlay.off('.zf.offCanvas');
$(window).off(this.onLoadListener);
}
}

Expand Down
7 changes: 5 additions & 2 deletions js/foundation.reveal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import $ from 'jquery';
import { onLoad } from './foundation.util.core';
import { Keyboard } from './foundation.util.keyboard';
import { MediaQuery } from './foundation.util.mediaQuery';
import { Motion } from './foundation.util.motion';
Expand Down Expand Up @@ -78,7 +79,7 @@ class Reveal extends Plugin {
}
this._events();
if (this.options.deepLink && window.location.hash === ( `#${this.id}`)) {
$(window).one('load.zf.reveal', this.open.bind(this));
this.onLoadListener = onLoad($(window), () => this.open());
}
}

Expand Down Expand Up @@ -471,7 +472,9 @@ class Reveal extends Plugin {
}
this.$element.hide().off();
this.$anchor.off('.zf');
$(window).off(`.zf.reveal:${this.id}`);
$(window)
.off(`.zf.reveal:${this.id}`)
.off(this.onLoadListener);

if ($('.reveal:visible').length === 0) {
this._removeRevealOpenClasses(); // also remove .is-reveal-open from the html element when there is no opened reveal
Expand Down
14 changes: 8 additions & 6 deletions js/foundation.sticky.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import $ from 'jquery';
import { GetYoDigits } from './foundation.util.core';
import { onLoad, GetYoDigits } from './foundation.util.core';
import { MediaQuery } from './foundation.util.mediaQuery';
import { Plugin } from './foundation.plugin';
import { Triggers } from './foundation.util.triggers';
Expand Down Expand Up @@ -60,18 +60,18 @@ class Sticky extends Plugin {

this.scrollCount = this.options.checkEvery;
this.isStuck = false;
$(window).one('load.zf.sticky', function(){
this.onLoadListener = onLoad($(window), function () {
//We calculate the container height to have correct values for anchor points offset calculation.
_this.containerHeight = _this.$element.css("display") == "none" ? 0 : _this.$element[0].getBoundingClientRect().height;
_this.$container.css('height', _this.containerHeight);
_this.elemHeight = _this.containerHeight;
if(_this.options.anchor !== ''){
if (_this.options.anchor !== '') {
_this.$anchor = $('#' + _this.options.anchor);
}else{
} else {
_this._parsePoints();
}

_this._setSizes(function(){
_this._setSizes(function () {
var scroll = window.pageYOffset;
_this._calc(false, scroll);
//Unstick the element will ensure that proper classes are set.
Expand Down Expand Up @@ -403,7 +403,9 @@ class Sticky extends Plugin {
if (this.$anchor && this.$anchor.length) {
this.$anchor.off('change.zf.sticky');
}
$(window).off(this.scrollListener);
$(window)
.off(this.scrollListener)
.off(this.onLoadListener);

if (this.wasWrapped) {
this.$element.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion js/foundation.tabs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import $ from 'jquery';
import { onLoad } from './foundation.util.core';
import { Keyboard } from './foundation.util.keyboard';
import { onImagesLoaded } from './foundation.util.imageLoader';
import { Plugin } from './foundation.plugin';
Expand Down Expand Up @@ -77,7 +78,7 @@ class Tabs extends Plugin {
}

if(isActive && _this.options.autoFocus){
$(window).on('load', function() {
_this.onLoadListener = onLoad($(window), function() {
$('html, body').animate({ scrollTop: $elem.offset().top }, _this.options.deepLinkSmudgeDelay, () => {
$link.focus();
});
Expand Down Expand Up @@ -397,6 +398,7 @@ class Tabs extends Plugin {
$(window).off('hashchange', this._checkDeepLink);
}

$(window).off(this.onLoadListener);
}
}

Expand Down
31 changes: 30 additions & 1 deletion js/foundation.util.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,33 @@ function transitionend($elem){
}
}

export {rtl, GetYoDigits, RegExpEscape, transitionend};
/**
* Return an event type to listen for window load.
*
* If `$elem` is passed, an event will be triggered on `$elem`. If window is already loaded, the event will still be triggered.
* If `handler` is passed, attach it to the event on `$elem`.
* Calling `onLoad` without handler allows you to get the event type that will be triggered before attaching the handler by yourself.
* @function
*
* @param {Object} [] $elem - jQuery element on which the event will be triggered if passed.
* @param {Function} [] handler - function to attach to the event.
* @returns {String} - event type that should or will be triggered.
*/
function onLoad($elem, handler) {
const didLoad = document.readyState === 'complete';
const eventType = (didLoad ? '_didLoad' : 'load') + '.zf.util.onLoad';
const cb = () => $elem.triggerHandler(eventType);

if ($elem) {
if (handler) $elem.one(eventType, handler);

if (didLoad)
setTimeout(cb);
else
$(window).one('load', cb);
}

return eventType;
}

export {rtl, GetYoDigits, RegExpEscape, transitionend, onLoad};
11 changes: 3 additions & 8 deletions js/foundation.util.triggers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import $ from 'jquery';
import { onLoad } from './foundation.util.core';
import { Motion } from './foundation.util.motion';

const MutationObserver = (function () {
Expand Down Expand Up @@ -244,16 +245,10 @@ Triggers.init = function($, Foundation) {
if (typeof($.triggersInitialized) === 'undefined') {
let $document = $(document);

if(document.readyState === "complete") {
onLoad($(window), function () {
Triggers.Initializers.addSimpleListeners();
Triggers.Initializers.addGlobalListeners();
} else {
$(window).on('load', () => {
Triggers.Initializers.addSimpleListeners();
Triggers.Initializers.addGlobalListeners();
});
}

});

$.triggersInitialized = true;
}
Expand Down
7 changes: 5 additions & 2 deletions test/javascript/util/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Foundation core', function() {
name.should.be.a('string');
name.should.be.equal('');
});

it('should handle a named function expression', function() {
var D = function foo(){};
var name = Foundation.getFnName(D);
Expand All @@ -109,10 +109,13 @@ describe('Foundation core', function() {
name.should.be.equal('foo');
});
});

describe('transitionEnd()', function() {
});

describe('onLoad()', function (done) {
});

describe('throttle()', function() {
});
});