Skip to content

Commit

Permalink
be module friendy
Browse files Browse the repository at this point in the history
Fix #158 for layer scripts, still have to do it for tiles scripts
  • Loading branch information
brunob committed Jul 24, 2019
1 parent dbeef7a commit aeab9b8
Show file tree
Hide file tree
Showing 8 changed files with 1,582 additions and 1,474 deletions.
58 changes: 36 additions & 22 deletions layer/Icon.Canvas.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
L.Icon.Canvas = L.Icon.extend({
options: {
iconSize: new L.Point(20, 20), // Have to be supplied
/*
iconAnchor: (Point)
popupAnchor: (Point)
*/
className: 'leaflet-canvas-icon'
},
(function (factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.YourPlugin = factory(L);
}
}(function (L) {
L.Icon.Canvas = L.Icon.extend({
options: {
iconSize: new L.Point(20, 20), // Have to be supplied
/*
iconAnchor: (Point)
popupAnchor: (Point)
*/
className: 'leaflet-canvas-icon'
},

createIcon: function () {
var e = document.createElement('canvas');
this._setIconStyles(e, 'icon');
var s = this.options.iconSize;
e.width = s.x;
e.height = s.y;
this.draw(e.getContext('2d'), s.x, s.y);
return e;
},
createIcon: function () {
var e = document.createElement('canvas');
this._setIconStyles(e, 'icon');
var s = this.options.iconSize;
e.width = s.x;
e.height = s.y;
this.draw(e.getContext('2d'), s.x, s.y);
return e;
},

createShadow: function () {
return null;
}
});
createShadow: function () {
return null;
}
});
}, window));
108 changes: 61 additions & 47 deletions layer/Layer.Deferred.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
L.DeferredLayer = L.LayerGroup.extend({
options: {
js: [],
init: null
},
(function (factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.YourPlugin = factory(L);
}
}(function (L) {
L.DeferredLayer = L.LayerGroup.extend({
options: {
js: [],
init: null
},

_script_cache: {},
_script_cache: {},

initialize: function (options) {
L.Util.setOptions(this, options);
L.LayerGroup.prototype.initialize.apply(this);
this._loaded = false;
},
initialize: function (options) {
L.Util.setOptions(this, options);
L.LayerGroup.prototype.initialize.apply(this);
this._loaded = false;
},

onAdd: function (map) {
L.LayerGroup.prototype.onAdd.apply(this, [map]);
if (this._loaded) return;
var loaded = function () {
this._loaded = true;
var l = this.options.init();
if (l)
this.addLayer(l);
};
this._loadScripts(this.options.js.reverse(), L.Util.bind(loaded, this));
},

_loadScripts: function (scripts, cb, args) {
if (!scripts || scripts.length === 0)
return cb(args);
var _this = this, s = scripts.pop(), c;
c = this._script_cache[s];
if (c === undefined) {
c = {url: s, wait: []};
var script = document.createElement('script');
script.src = s;
script.type = 'text/javascript';
script.onload = function () {
c.e.readyState = 'completed';
var i = 0;
for (i = 0; i < c.wait.length; i++)
c.wait[i]();
onAdd: function (map) {
L.LayerGroup.prototype.onAdd.apply(this, [map]);
if (this._loaded) return;
var loaded = function () {
this._loaded = true;
var l = this.options.init();
if (l)
this.addLayer(l);
};
c.e = script;
document.getElementsByTagName('head')[0].appendChild(script);
this._loadScripts(this.options.js.reverse(), L.Util.bind(loaded, this));
},

_loadScripts: function (scripts, cb, args) {
if (!scripts || scripts.length === 0)
return cb(args);
var _this = this, s = scripts.pop(), c;
c = this._script_cache[s];
if (c === undefined) {
c = {url: s, wait: []};
var script = document.createElement('script');
script.src = s;
script.type = 'text/javascript';
script.onload = function () {
c.e.readyState = 'completed';
var i = 0;
for (i = 0; i < c.wait.length; i++)
c.wait[i]();
};
c.e = script;
document.getElementsByTagName('head')[0].appendChild(script);
}
function _cb () { _this._loadScripts(scripts, cb, args); }
c.wait.push(_cb);
if (c.e.readyState === 'completed')
_cb();
this._script_cache[s] = c;
}
function _cb () { _this._loadScripts(scripts, cb, args); }
c.wait.push(_cb);
if (c.e.readyState === 'completed')
_cb();
this._script_cache[s] = c;
}
});
});
}, window));
27 changes: 19 additions & 8 deletions layer/Marker.Rotate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/*
* Based on comments by @runanet and @coomsie
* https://github.com/CloudMade/Leaflet/issues/386
*
* Wrapping function is needed to preserve L.Marker.update function
*/
(function () {
(function (factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.YourPlugin = factory(L);
}
}(function (L) {
/*
* Based on comments by @runanet and @coomsie
* https://github.com/CloudMade/Leaflet/issues/386
*
*/
var _old__setPos = L.Marker.prototype._setPos;
L.Marker.include({
_updateImg: function (i, a, s) {
Expand Down Expand Up @@ -59,4 +70,4 @@
}
}
});
}());
}, window));
110 changes: 62 additions & 48 deletions layer/Marker.Text.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
L.Icon.Text = L.Icon.extend({
initialize: function (text, options) {
this._text = text;
L.Icon.prototype.initialize.apply(this, [options]);
},

createIcon: function () {
var el = document.createElement('div');
el.appendChild(document.createTextNode(this._text));
this._setIconStyles(el, 'icon');
el.style.textShadow = '2px 2px 2px #fff';
return el;
},

createShadow: function () { return null; }

});

L.Marker.Text = L.Marker.extend({
initialize: function (latlng, text, options) {
L.Marker.prototype.initialize.apply(this, [latlng, options]);
this._fakeicon = new L.Icon.Text(text);
},

_initIcon: function () {
L.Marker.prototype._initIcon.apply(this);

var i = this._icon, s = this._shadow, obj = this.options.icon;
this._icon = this._shadow = null;

this.options.icon = this._fakeicon;
L.Marker.prototype._initIcon.apply(this);
this.options.icon = obj;

if (s) {
s.parentNode.removeChild(s);
this._icon.appendChild(s);
(function (factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.YourPlugin = factory(L);
}
}(function (L) {
L.Icon.Text = L.Icon.extend({
initialize: function (text, options) {
this._text = text;
L.Icon.prototype.initialize.apply(this, [options]);
},

createIcon: function () {
var el = document.createElement('div');
el.appendChild(document.createTextNode(this._text));
this._setIconStyles(el, 'icon');
el.style.textShadow = '2px 2px 2px #fff';
return el;
},

createShadow: function () { return null; }

});

L.Marker.Text = L.Marker.extend({
initialize: function (latlng, text, options) {
L.Marker.prototype.initialize.apply(this, [latlng, options]);
this._fakeicon = new L.Icon.Text(text);
},

_initIcon: function () {
L.Marker.prototype._initIcon.apply(this);

var i = this._icon, s = this._shadow, obj = this.options.icon;
this._icon = this._shadow = null;

this.options.icon = this._fakeicon;
L.Marker.prototype._initIcon.apply(this);
this.options.icon = obj;

if (s) {
s.parentNode.removeChild(s);
this._icon.appendChild(s);
}

i.parentNode.removeChild(i);
this._icon.appendChild(i);

var w = this._icon.clientWidth, h = this._icon.clientHeight;
this._icon.style.marginLeft = -w / 2 + 'px';
var off = new L.Point(w/2, 0);
L.DomUtil.setPosition(i, off);
if (s) L.DomUtil.setPosition(s, off);
}

i.parentNode.removeChild(i);
this._icon.appendChild(i);

var w = this._icon.clientWidth, h = this._icon.clientHeight;
this._icon.style.marginLeft = -w / 2 + 'px';
var off = new L.Point(w/2, 0);
L.DomUtil.setPosition(i, off);
if (s) L.DomUtil.setPosition(s, off);
}
});
});
}, window));
Loading

0 comments on commit aeab9b8

Please sign in to comment.