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

Add AMD support #161

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
5 changes: 5 additions & 0 deletions AMD_footer
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
PruneCluster: PruneCluster,
PruneClusterForLeaflet: PruneClusterForLeaflet
};
}));
15 changes: 15 additions & 0 deletions AMD_header
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function(root, factory) {
if(typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
} else if (typeof module !== 'undefined') {
module.exports = factory(require('leaflet'));
} else {
if (!root.L) {
throw 'Leaflet must be loaded previously';
}

var PruneClusterModule = factory(root.L);
root.PruneCluster = PruneClusterModule.PruneCluster;
root.PruneClusterForLeaflet = PruneClusterModule.PruneClusterForLeaflet;
}
}(this, function(L) {
18 changes: 13 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = function (grunt) {
dist: {
files: [{
src: ["dist/*.js", "dist/*.d.ts", "dist/*.css"]
}],
}]
},
dev: {
files: [{
src: ["build/*.js", "build/*.d.ts", "build/*.map", "build/*.css"]
}],
}]
}
},
ts: {
options: {
target: 'es5',
module: 'amd',
declaration: true,
declaration: true
},
dist: {
src: ["PruneCluster.ts", "LeafletAdapter.ts", "LeafletSpiderfier.ts"],
Expand All @@ -37,13 +37,20 @@ module.exports = function (grunt) {
}
}
},
concat: {
dist: {
src: ['./AMD_header', './dist/PruneCluster.js', './AMD_footer'],
dest: './dist/PruneCluster.amd.js'
}
},
uglify: {
ts: {
options: {
sourceMap: false
},
files: {
'dist/PruneCluster.min.js': ['dist/PruneCluster.js']
'dist/PruneCluster.min.js': ['dist/PruneCluster.js'],
'dist/PruneCluster.amd.min.js': ['dist/PruneCluster.amd.js']
}
}
},
Expand Down Expand Up @@ -86,14 +93,15 @@ module.exports = function (grunt) {
'clean:dist',
'copy:dist',
'ts:dist',
'concat:dist',
'uglify'
]);

grunt.registerTask('build:dev', [
'clean:dev',
'copy:dev',
'ts:dev'
])
]);

grunt.registerTask('build', ['build:dev']);
grunt.registerTask('default', ['build:dev']);
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ pruneCluster.RegisterMarker(marker);
leafletMap.addLayer(pruneCluster);
```

#### AMD

```
npm install
npm install -g grunt-cli
grunt build:dist --force
```

Then, you will have files ready to use in AMD style under `dist` directory.

#### Example

```javascript
// First 'PruneCluster' must be declared in your module loader, pointing to file 'dist/PruneCluster.amd.min.js'
// (or use path to file instead)

define(['PruneCluster', function(PruneClusterModule) {

var pruneCluster = new PruneClusterModule.PruneClusterForLeaflet();

...
var marker = new PruneClusterModule.PruneCluster.Marker(59.8717, 11.1909);
pruneCluster.RegisterMarker(marker);
...

leafletMap.addLayer(pruneCluster);
});
```

### PruneClusterForLeaflet constructor

```javascript
Expand Down
4 changes: 2 additions & 2 deletions dist/PruneCluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,14 +928,14 @@ var PruneClusterLeafletSpiderfier = (L.Layer ? L.Layer : L.Class).extend({
return res;
},
Unspiderfy: function () {
var _this = this;
for (var i = 0, l = this._currentMarkers.length; i < l; ++i) {
this._currentMarkers[i].setLatLng(this._currentCenter).setOpacity(0);
}
var map = this._map;
var markers = this._currentMarkers;
window.setTimeout(function () {
for (i = 0, l = markers.length; i < l; ++i) {
_this._map.removeLayer(markers[i]);
map.removeLayer(markers[i]);
}
}, 300);
this._currentMarkers = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-clean": "~1.1.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-copy": "~1.0.0",
"grunt-contrib-uglify": "~3.0.1",
"grunt-exec": "^2.0.0",
Expand Down