From 68cb039d9f44a820797ba2027b8108c834716809 Mon Sep 17 00:00:00 2001 From: mathisloge Date: Thu, 14 Sep 2017 20:43:06 +0200 Subject: [PATCH 01/10] Update index.d.ts --- index.d.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index d55156e..382dcd8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,10 @@ /// - -declare namespace L { - +import { PolylineOptions, LatLng, MarkerOptions, FeatureGroup } from 'leaflet'; +declare module 'Leaflet.MultiOptionsPolyline' { export type PolylineOptionsFn = (optionIdx: number) => PolylineOptions; export interface MultiOptions { - optionIdxFn: (latLng, prevLatLng, index, allLatlngs) => number; + optionIdxFn: (latLng: LatLng, prevLatLng: LatLng, index: number, allLatlngs: Array) => number; // options for the index returned by optionIdxFn. If supplied with a function then it will be called with the index options: PolylineOptions[] | PolylineOptionsFn; // the context to call optionIdxFn (optional) @@ -26,7 +25,3 @@ declare namespace L { export function multiOptionsPolyline(latlng: LatLng[], options?: MultiOptionsPolylineOptions): MultiOptionsPolyline; } - -declare module 'Leaflet.MultiOptionsPolyline' { - export = L; -} From fe8667115efc67704ef53ff31eebbcb13e523ca2 Mon Sep 17 00:00:00 2001 From: mathisloge Date: Thu, 14 Sep 2017 20:45:13 +0200 Subject: [PATCH 02/10] Rewrite with typescript Make it compitable with leaflet 1.2.0 --- Leaflet.MultiOptionsPolyline.ts | 110 ++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Leaflet.MultiOptionsPolyline.ts diff --git a/Leaflet.MultiOptionsPolyline.ts b/Leaflet.MultiOptionsPolyline.ts new file mode 100644 index 0000000..bf08d1a --- /dev/null +++ b/Leaflet.MultiOptionsPolyline.ts @@ -0,0 +1,110 @@ +import { Polyline, FeatureGroup, Util, LatLng, PolylineOptions, MarkerOptions } from 'leaflet'; + +type PolylineOptionsFn = (optionIdx: number) => PolylineOptions; + +interface MultiOptions { + optionIdxFn: (latLng: LatLng, prevLatLng: LatLng, index: number, allLatlngs: Array) => number; + // options for the index returned by optionIdxFn. If supplied with a function then it will be called with the index + options: PolylineOptions[] | PolylineOptionsFn; + // the context to call optionIdxFn (optional) + fnContext?: any; + copyBaseOptions?: boolean; +} + +export interface MultiOptionsPolylineOptions extends MarkerOptions { + multiOptions: MultiOptions; +} + +class MultiOptionsPolyline extends FeatureGroup { + _layers: object; + _options: any; + _originalLatlngs: Array; + _inLatLngs: Array; + constructor(latlngs: Array, options: MultiOptionsPolylineOptions){ + super(); + this._options = options; + this._originalLatlngs = latlngs; + + let copyBaseOptions = this._options.multiOptions.copyBaseOptions; + + this._layers = {}; + if (copyBaseOptions === undefined || copyBaseOptions) { + this._copyBaseOptions(); + } + + this.setLatLngs(this._originalLatlngs); + } + _copyBaseOptions () { + let multiOptions = this._options.multiOptions, + baseOptions, + optionsArray = multiOptions.options, + i, len = optionsArray.length; + + baseOptions = Util.extend({}, this._options); + delete baseOptions.multiOptions; + + for (i = 0; i < len; ++i) { + optionsArray[i] = Util.extend(baseOptions, optionsArray[i]); + } + } + + setLatLngs (latlngs) { + let i, len = latlngs.length, + multiOptions = this._options.multiOptions, + optionIdxFn = multiOptions.optionIdxFn, + fnContext = multiOptions.fnContext || this, + prevOptionIdx, optionIdx, + segmentLatlngs; + + this.eachLayer(function (layer) { + this.removeLayer(layer); + }, this); + + for (i = 1; i < len; ++i) { + optionIdx = optionIdxFn.call( + fnContext, latlngs[i], latlngs[i - 1], i, latlngs); + + if (i === 1) { + segmentLatlngs = [latlngs[0]]; + prevOptionIdx = optionIdxFn.call(fnContext, latlngs[0], latlngs[0], 0, latlngs); + } + + segmentLatlngs.push(latlngs[i]); + + // is there a change in options or is it the last point? + if (prevOptionIdx !== optionIdx || i === len - 1) { + // Check if options is a function or an array + if (typeof multiOptions.options === "function") { + this.addLayer(new Polyline(segmentLatlngs, multiOptions.options(prevOptionIdx))); + } else { + this.addLayer(new Polyline(segmentLatlngs, multiOptions.options[prevOptionIdx])); + } + + prevOptionIdx = optionIdx; + segmentLatlngs = [latlngs[i]]; + } + } + + return this; + } + + getLatLngs() { + return this._originalLatlngs; + } + + getLatLngsSegments() { + let latlngs = []; + + this.eachLayer(function (layer) { + if(layer instanceof Polyline){ + latlngs.push(layer.getLatLngs()); + } + }); + + return latlngs; + } +} + +export function multiOptionsPolyline(latlngs:Array, options: MultiOptionsPolylineOptions): MultiOptionsPolyline{ + return new MultiOptionsPolyline(latlngs, options); +} From f884892363b2f7ee02613a00b8f878968eba2606 Mon Sep 17 00:00:00 2001 From: mathisloge Date: Thu, 14 Sep 2017 20:45:26 +0200 Subject: [PATCH 03/10] Delete Leaflet.MultiOptionsPolyline.min.js --- Leaflet.MultiOptionsPolyline.min.js | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Leaflet.MultiOptionsPolyline.min.js diff --git a/Leaflet.MultiOptionsPolyline.min.js b/Leaflet.MultiOptionsPolyline.min.js deleted file mode 100644 index cfc727a..0000000 --- a/Leaflet.MultiOptionsPolyline.min.js +++ /dev/null @@ -1,2 +0,0 @@ -/*! Leaflet.MultiOptionsPolyline v1.0.0 26-11-2016 */ -!function(a,b){"function"==typeof define&&define.amd?define(["leaflet"],b):b("object"==typeof module&&module.exports?require("leaflet"):a.L)}(this,function(a){"use strict";var b=a.FeatureGroup.extend({initialize:function(a,b){var c=b.multiOptions.copyBaseOptions;this._layers={},this._options=b,(void 0===c||c)&&this._copyBaseOptions(),this.setLatLngs(a)},_copyBaseOptions:function(){var b,c,d=this._options.multiOptions,e=d.options,f=e.length;for(b=a.extend({},this._options),delete b.multiOptions,c=0;c Date: Thu, 14 Sep 2017 20:45:47 +0200 Subject: [PATCH 04/10] Update Leaflet.MultiOptionsPolyline.js --- Leaflet.MultiOptionsPolyline.js | 134 ++++++++++++-------------------- 1 file changed, 50 insertions(+), 84 deletions(-) diff --git a/Leaflet.MultiOptionsPolyline.js b/Leaflet.MultiOptionsPolyline.js index 01f3644..e1ae143 100644 --- a/Leaflet.MultiOptionsPolyline.js +++ b/Leaflet.MultiOptionsPolyline.js @@ -1,114 +1,80 @@ -(function (root, factory) { - if (typeof define === "function" && define.amd) { - define(["leaflet"], factory); - } else if (typeof module === "object" && module.exports) { - factory(require("leaflet")); - } else { - factory(root.L); - } -} (this, function (L) { - "use strict"; - -/* - * L.MultiOptionsPolyline is a MultiPolyLine which parts can be styled differently. - * options: { - * multiOptions: { - * optionIdxFn: function (latLng, prevLatLng, index, allLatlngs), - * fnContext: ctx, // the context to call optionIdxFn (optional) - * options: [{}, {}, {}] or function, // options for the index returned by optionIdxFn. If supplied with a function then it will be called with the index - * copyBaseOptions: true - * }, - * // other options from Polyline - * } - */ -var MultiOptionsPolyline = L.FeatureGroup.extend({ - - initialize: function (latlngs, options) { - var copyBaseOptions = options.multiOptions.copyBaseOptions; - - this._layers = {}; - this._options = options; +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +exports.__esModule = true; +var leaflet_1 = require("leaflet"); +var MultiOptionsPolyline = /** @class */ (function (_super) { + __extends(MultiOptionsPolyline, _super); + function MultiOptionsPolyline(latlngs, options) { + var _this = _super.call(this) || this; + _this._options = options; + _this._originalLatlngs = latlngs; + var copyBaseOptions = _this._options.multiOptions.copyBaseOptions; + _this._layers = {}; if (copyBaseOptions === undefined || copyBaseOptions) { - this._copyBaseOptions(); + _this._copyBaseOptions(); } - - this.setLatLngs(latlngs); - }, - - _copyBaseOptions: function () { - var multiOptions = this._options.multiOptions, - baseOptions, - optionsArray = multiOptions.options, - i, len = optionsArray.length; - - baseOptions = L.extend({}, this._options); + _this.setLatLngs(_this._originalLatlngs); + return _this; + } + MultiOptionsPolyline.prototype._copyBaseOptions = function () { + var multiOptions = this._options.multiOptions, baseOptions, optionsArray = multiOptions.options, i, len = optionsArray.length; + baseOptions = leaflet_1.Util.extend({}, this._options); delete baseOptions.multiOptions; - for (i = 0; i < len; ++i) { - optionsArray[i] = L.extend({}, baseOptions, optionsArray[i]); + optionsArray[i] = leaflet_1.Util.extend(baseOptions, optionsArray[i]); } - }, - - setLatLngs: function (latlngs) { - var i, len = latlngs.length, - multiOptions = this._options.multiOptions, - optionIdxFn = multiOptions.optionIdxFn, - fnContext = multiOptions.fnContext || this, - prevOptionIdx, optionIdx, - segmentLatlngs; - - this._originalLatlngs = latlngs; - + }; + MultiOptionsPolyline.prototype.setLatLngs = function (latlngs) { + var i, len = latlngs.length, multiOptions = this._options.multiOptions, optionIdxFn = multiOptions.optionIdxFn, fnContext = multiOptions.fnContext || this, prevOptionIdx, optionIdx, segmentLatlngs; this.eachLayer(function (layer) { this.removeLayer(layer); }, this); - for (i = 1; i < len; ++i) { - optionIdx = optionIdxFn.call( - fnContext, latlngs[i], latlngs[i - 1], i, latlngs); - + optionIdx = optionIdxFn.call(fnContext, latlngs[i], latlngs[i - 1], i, latlngs); if (i === 1) { segmentLatlngs = [latlngs[0]]; prevOptionIdx = optionIdxFn.call(fnContext, latlngs[0], latlngs[0], 0, latlngs); } - segmentLatlngs.push(latlngs[i]); - // is there a change in options or is it the last point? if (prevOptionIdx !== optionIdx || i === len - 1) { // Check if options is a function or an array if (typeof multiOptions.options === "function") { - this.addLayer(L.polyline(segmentLatlngs, multiOptions.options(prevOptionIdx))); - } else { - this.addLayer(L.polyline(segmentLatlngs, multiOptions.options[prevOptionIdx])); + this.addLayer(new leaflet_1.Polyline(segmentLatlngs, multiOptions.options(prevOptionIdx))); + } + else { + this.addLayer(new leaflet_1.Polyline(segmentLatlngs, multiOptions.options[prevOptionIdx])); } - prevOptionIdx = optionIdx; segmentLatlngs = [latlngs[i]]; } } - return this; - }, - - getLatLngs: function () { + }; + MultiOptionsPolyline.prototype.getLatLngs = function () { return this._originalLatlngs; - }, - - getLatLngsSegments: function () { + }; + MultiOptionsPolyline.prototype.getLatLngsSegments = function () { var latlngs = []; - this.eachLayer(function (layer) { - latlngs.push(layer.getLatLngs()); + if (layer instanceof leaflet_1.Polyline) { + latlngs.push(layer.getLatLngs()); + } }); - return latlngs; - } -}); - -L.MultiOptionsPolyline = MultiOptionsPolyline; -L.multiOptionsPolyline = function (latlngs, options) { + }; + return MultiOptionsPolyline; +}(leaflet_1.FeatureGroup)); +function multiOptionsPolyline(latlngs, options) { return new MultiOptionsPolyline(latlngs, options); -}; - -})); +} +exports.multiOptionsPolyline = multiOptionsPolyline; From bd465619e86c96c5a20b10567c00f4c04ff33664 Mon Sep 17 00:00:00 2001 From: mathisloge Date: Thu, 14 Sep 2017 20:47:43 +0200 Subject: [PATCH 05/10] Fixed MarkerOptions --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 382dcd8..cd34653 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ /// -import { PolylineOptions, LatLng, MarkerOptions, FeatureGroup } from 'leaflet'; +import { PolylineOptions, LatLng, FeatureGroup } from 'leaflet'; declare module 'Leaflet.MultiOptionsPolyline' { export type PolylineOptionsFn = (optionIdx: number) => PolylineOptions; @@ -12,7 +12,7 @@ declare module 'Leaflet.MultiOptionsPolyline' { copyBaseOptions?: boolean; } - export interface MultiOptionsPolylineOptions extends MarkerOptions { + export interface MultiOptionsPolylineOptions extends PolylineOptions { multiOptions: MultiOptions; } From dbbf63d989783341f7a6d4772fe8da15c8f4237c Mon Sep 17 00:00:00 2001 From: Mathis Logemann Date: Sat, 14 Oct 2017 23:15:51 +0200 Subject: [PATCH 06/10] some file changes... not ready yet --- .gitignore | 1 + Gruntfile.js | 40 --------- demo/index.html | 6 +- .../js/Leaflet.MultiOptionsPolyline.js | 46 +++++----- demo/js/demo.js | 2 +- dist/Leaflet.MultiOptionsPolyline.js | 84 +++++++++++++++++++ index.d.ts | 27 ------ package.json | 22 +++-- rollup.config.js | 15 ++++ .../Leaflet.MultiOptionsPolyline.ts | 12 +-- tsconfig.json | 18 ++++ 11 files changed, 170 insertions(+), 103 deletions(-) delete mode 100755 Gruntfile.js rename Leaflet.MultiOptionsPolyline.js => demo/js/Leaflet.MultiOptionsPolyline.js (69%) create mode 100644 dist/Leaflet.MultiOptionsPolyline.js delete mode 100644 index.d.ts create mode 100644 rollup.config.js rename Leaflet.MultiOptionsPolyline.ts => src/Leaflet.MultiOptionsPolyline.ts (92%) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index ff7dfd2..7badfec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea *.iml node_modules +.vs diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100755 index 33f18b3..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -module.exports = function(grunt) { - - grunt.initConfig({ - ghPagesDir: '../Leaflet.MultiOptionsPolyline@gh-pages/', - pkg: grunt.file.readJSON('package.json'), - uglify: { - options: { - banner: '/*! <%= pkg.name + " v" + pkg.version %> <%= grunt.template.today("dd-mm-yyyy") %> */\n' - }, - dist: { - files: { - '<%= pkg.name %>.min.js': ['<%= pkg.name %>.js'] - } - } - }, - jshint: { - files: ['Leaflet.MultiOptionsPolyline.js', 'demo/js/*.js'], - options: { - } - }, - copy: { - "gh-pages": { - files: [ - {expand: true, src: ['demo/**'], dest: '<%= ghPagesDir %>'}, - {expand: true, src: ['Leaflet.MultiOptionsPolyline.js'], dest: '<%= ghPagesDir %>'} - ] - } - } - - }); - - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-contrib-copy'); - - grunt.registerTask('default', ['jshint', 'uglify']); - -}; \ No newline at end of file diff --git a/demo/index.html b/demo/index.html index 243e0f1..581bc88 100644 --- a/demo/index.html +++ b/demo/index.html @@ -5,7 +5,7 @@ Leaflet.MultiOptionsPolyline Demo - +