From c8f7eb27450b52e88820c9cd8f5d70f53cab22ed Mon Sep 17 00:00:00 2001 From: "Michael A. Salgado" Date: Thu, 13 Aug 2015 09:29:31 -0500 Subject: [PATCH] Add support for arraybuffer in workers --- .editorconfig | 28 +++++++++++++++ .jshintrc | 24 +++++++++++++ leaflet.shpfile.js | 85 +++++++++++++++++++++++++++++----------------- 3 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 .editorconfig create mode 100644 .jshintrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ae8f390 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,28 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] + +# Change these settings to your own preference +indent_style = space +indent_size = 4 + +# We recommend you to keep these unchanged +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[{package,bower}.json] +indent_style = space +indent_size = 2 + +[*.{js,less}] +indent_style = space +indent_size = 2 diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..fa82a85 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,24 @@ +{ + "node": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 4, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true, + "white": true, + "jquery": true, + "browser": true, + "predef": ["shp", "define", "cw", "L"] +} diff --git a/leaflet.shpfile.js b/leaflet.shpfile.js index 3753a62..e5915d5 100644 --- a/leaflet.shpfile.js +++ b/leaflet.shpfile.js @@ -1,36 +1,59 @@ +'use strict'; + +/* global cw, shp */ L.Shapefile =L.GeoJSON.extend({ - initialize: function (file, options, importUrl) { - if(typeof cw !== 'undefined'){ - importUrl = importUrl || 'shp.js'; - this.worker = cw(new Function('data', 'cb', 'importScripts("' + importUrl + '");shp(data).then(cb);')); - } - L.GeoJSON.prototype.initialize.call(this,{features:[]},options); - this.addFileData(file); - }, - addFileData:function(file){ - var self = this; - self.fire('data:loading'); - if(typeof file !== 'string' && !('byteLength' in file)){ - var data = self.addData(file); - self.fire('data:loaded'); - return data; - } - if(self.worker){ - self.worker.data(cw.makeUrl(file)).then(function(data){ - self.addData(data); - self.fire('data:loaded'); - self.worker.close(); - }); - }else{ - shp(file).then(function(data){ - self.addData(data); - self.fire('data:loaded'); - }); - } - return this; + options: { + isArrayBufer: false, + importUrl: 'shp.js' + }, + + initialize: function (file, options) { + L.Util.setOptions(this, options); + if(typeof cw !== 'undefined'){ + /*jslint evil: true */ + if(!options.isArrayBufer) { + this.worker = cw(new Function('data', 'cb', 'importScripts("' + this.options.importUrl + '");shp(data).then(cb);')); + } else { + this.worker = cw(new Function('data', 'importScripts("' + this.options.importUrl + '"); return shp.parseZip(data);')); + } + } + L.GeoJSON.prototype.initialize.call(this, {features:[]}, options); + this.addFileData(file); + }, + + addFileData:function(file) { + var self = this; + self.fire('data:loading'); + if(typeof file !== 'string' && !('byteLength' in file)) { + var data = self.addData(file); + self.fire('data:loaded'); + return data; + } + + if(self.worker) { + if(!self.options.isArrayBufer) { + self.worker.data(cw.makeUrl(file)).then(function(data){ + self.addData(data); + self.fire('data:loaded'); + self.worker.close(); + }); + } else { + self.worker.data(file, [file]).then(function(data){ + self.addData(data); + self.fire('data:loaded'); + self.worker.close(); + }); + } + } else{ + shp(file).then(function(data){ + self.addData(data); + self.fire('data:loaded'); + }); } + return this; + } }); L.shapefile= function(a,b,c){ - return new L.Shapefile(a,b,c); -} + return new L.Shapefile(a,b,c); +};