Skip to content

Commit

Permalink
Fire data and dataend events
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Apr 20, 2016
1 parent 78de357 commit be2a101
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 2 additions & 1 deletion debug/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@
'zoomstart', 'zoom', 'zoomend',
'rotatestart', 'rotate', 'rotateend',
'pitchstart', 'pitch', 'pitchend',
'boxzoomstart', 'boxzoomend', 'boxzoomcancel'
'boxzoomstart', 'boxzoomend', 'boxzoomcancel',
'dataend', 'data'
].forEach(function (type) {
var name = 'show-' + type;
var input = document.createElement('input');
Expand Down
2 changes: 1 addition & 1 deletion js/style/image_sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ImageSprite.prototype.toJSON = function() {
return this.base;
};

ImageSprite.prototype.loaded = function() {
ImageSprite.prototype.isDataStable = function() {
return !!(this.data && this.img);
};

Expand Down
43 changes: 36 additions & 7 deletions js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ var Map = module.exports = function(options) {

this.on('move', this._update.bind(this, false));
this.on('zoom', this._update.bind(this, true));
this.on('moveend', function() {
this.animationLoop.set(300); // text fading
this._rerender();
}.bind(this));
this.on('moveend', this._onMoveend.bind(this));
this.on('data', this._onData.bind(this));


if (typeof window !== 'undefined') {
window.addEventListener('resize', this._onWindowResize, false);
Expand Down Expand Up @@ -847,9 +846,23 @@ util.extend(Map.prototype, /** @lends Map.prototype */{

this.fire('render');

if (this.isDataStable() && !this._loaded) {
this._loaded = true;
this.fire('load');
if (this._willFireData) {
this.fire('data');
this._willFireData = false;
}

if (this.isDataStable()) {
if (!this._wasLoaded) {
this._wasLoaded = true;
this.fire('load');
}

if (!this._wasDataStable) {
this._wasDataStable = true;
this.fire('dataend');
}
} else {
this._wasDataStable = false;
}

this._frameId = null;
Expand Down Expand Up @@ -906,6 +919,7 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
},

_forwardStyleEvent: function(e) {
if (e.type === 'load' || e.type === 'change') this._willFireData = true;
this.fire('style.' + e.type, util.extend({style: e.target}, e));
},

Expand All @@ -918,6 +932,9 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
},

_forwardTileEvent: function(e) {
if (e.type === 'tile.load' || e.type === 'tile.add') {
this._willFireData = true;
}
this.fire(e.type, util.extend({style: e.target}, e));
},

Expand All @@ -939,22 +956,34 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
if (source.onAdd)
source.onAdd(this);
this._forwardSourceEvent(e);
this._willFireData = true;
},

_onSourceRemove: function(e) {
var source = e.source;
if (source.onRemove)
source.onRemove(this);
this._forwardSourceEvent(e);
this._willFireData = true;
},

_onSourceUpdate: function(e) {
this._update();
this._forwardSourceEvent(e);
this._willFireData = true;
},

_onWindowResize: function() {
this.stop().resize()._update();
},

_onMoveend: function() {
this.animationLoop.set(300); // text fading
this._rerender();
},

_onData: function() {
this._wasDataStable = false;
}
});

Expand Down

0 comments on commit be2a101

Please sign in to comment.