Browse Source

build: single-file deploy

Include extlibs into the built chiq{,.unmin}.js file.
master
Chimo 6 years ago
parent
commit
27078b2f45
9 changed files with 496 additions and 286 deletions
  1. +4
    -9
      build.sh
  2. +244
    -0
      chiq.js
  3. +1
    -0
      chiq.map.js
  4. +246
    -28
      chiq.unmin.js
  5. +0
    -1
      dist/chiq.map.js
  6. +0
    -2
      dist/chiq.min.js
  7. +0
    -66
      dist/extlib/latlon-spherical.js
  8. +0
    -177
      dist/extlib/xr.js
  9. +1
    -3
      examples/pois/pois.html

+ 4
- 9
build.sh View File

@ -1,14 +1,9 @@
#!/usr/bin/bash
DIR=dist
mkdir -p "${DIR}/extlib"
# min
uglifyjs --source-map "${DIR}"/chiq.map.js --screw-ie8 --mangle --compress --lint src/*.js > "${DIR}"/chiq.min.js
uglifyjs --source-map chiq.map.js --screw-ie8 --mangle --compress --lint src/*.js > chiq.js
printf "\n\n" >> chiq.js
cat src/extlib/*.js >> chiq.js
# unmin
cat src/*.js > "${DIR}"/chiq.unmin.js
# extlibs
cp src/extlib/*.js dist/extlib/
cat src/extlib/*.js src/*.js > chiq.unmin.js

+ 244
- 0
chiq.js View File

@ -0,0 +1,244 @@
!function(){"use strict";var e,t,n=window.Pullet||{};n.map={create:function(e,n){var o,r=t(n),a=new ol.layer.Tile({source:new ol.source.OSM}),i=[a,r],s=r.getSource().getExtent();return o=new ol.Map({target:e,layers:i,view:new ol.View({center:[0,0],zoom:1})}),o.getView().fit(s,o.getSize(),{maxZoom:18}),o}},t=function(t){var n,o,r,a,i,s=t.length,u=[];for(n=0;s>n;n+=1)a=t[n],i=a.tags.name,o=parseFloat(a.lat),r=parseFloat(a.lon),isNaN(o)||isNaN(r)||u.push(new ol.Feature({geometry:new ol.geom.Point(ol.proj.transform([r,o],"EPSG:4326","EPSG:3857"))}));return new ol.layer.Vector({source:new ol.source.Vector({features:u}),style:e("/images/marker-green.png")})},e=function(e){return new ol.style.Style({image:new ol.style.Icon({anchor:[.5,46],anchorXUnits:"fraction",anchorYUnits:"pixels",opacity:.75,src:e})})},window.Pullet=n}(),function(){"use strict";var e,t=window.Pullet||{};t.nominatim={lookup:function(t){var n,o,r,a=window.Pullet.endpoint.nominatim+"/lookup",i=t.length,s=[],u=[],l=0,c=[],y=t.slice();return r=function(t){var n,o,r,a,s;if(l+=1,l===i){for(o=c.length,n=0;o>n;n+=1)r=c[n],a=e(r.osm_id,y),a!==!1&&(s=y[a],s.fill(r),y[a]=s);t(y)}},new Promise(function(e){for(Array.isArray(t)&&0!==t.length||e([]);t.length>0;){for(u=[],o=t.splice(0,50),i=o.length,n=0;i>n;n+=1)u.push(o[n].fid);s.push(u)}for(n=0,i=s.length;i>n;n+=1)xr.get(a,{format:"json",osm_ids:s[n].join()}).then(function(t){c=c.concat(t),r(e)}).catch(function(){r(e)})})}},e=function(e,t){var n,o,r=t.length;for(n=0;r>n;n+=1)if(o=t[n],o.osm_id===parseInt(e,10))return n;return!1},window.Pullet=t}(),function(){"use strict";var e,t=window.Pullet||{};e=function(e){this.osm_id=e.id,this.name=e.name,this.tags=e.tags,this.lat=e.lat,this.lon=e.lon,this.osm_type=e.type,this.fid=e.type.slice(0,1).toUpperCase()+e.id},e.prototype.fill=function(e){this.address=e.address,this.class=e.class,this.name||(this.name=e.display_name),this.display_name=e.display_name,this.lat=e.lat,this.lon=e.lon,this.nom_id=e.place_id,this.nom_type=e.type},t.Place=e,window.Pullet=t}(),function(){"use strict";var e,t,n=window.Pullet||{};n.places={nearby:function(){return Pullet.getLocation().then(function(e){var t=e.coords;return Pullet.places.near(t.latitude,t.longitude)}).catch(function(e){return Promise.reject(e)})},near:function(e,t){var n,o=Pullet.endpoint.overpass,r=300,a="<around lon='"+t+"' lat='"+e+"' radius='"+r+"'/>",i="<has-kv k='name'/>";return n="<osm-script output='json' timeout='25'><union><query type='node'><has-kv k='amenity'/>"+a+i+"</query><query type='way'><has-kv k='amenity'/>"+a+i+"</query><query type='node'><has-kv k='shop'/>"+a+i+"</query><query type='way'><has-kv k='shop'/>"+a+i+"</query><query type='node'><has-kv k='office'/>"+a+i+"</query><query type='way'><has-kv k='office'/>"+a+i+"</query><query type='node'><has-kv k='craft'/>"+a+i+"</query><query type='way'><has-kv k='craft'/>"+a+i+"</query><query type='node'><has-kv k='leisure'/>"+a+i+"</query><query type='way'><has-kv k='leisure'/>"+a+i+"</query><query type='node'><has-kv k='public_transport' v='platform'/>"+a+i+"</query><query type='way'><has-kv k='public_transport' v='platform'/>"+a+i+"</query><query type='node'><has-kv k='highway' v='bus_stop'/>"+a+i+"</query><query type='way'><has-kv k='highway' v='bus_stop'/>"+a+i+"</query></union><print mode='body' order='quadtile'/></osm-script>",xr.post(o,n).then(function(e){var t,n=e.elements,o=n.length,r=[];for(t=0;o>t;t+=1)r.push(new Pullet.Place(n[t]));return r})},fillDistance:function(e,t){var n,o,r,a=t.length,i=new LatLon(e.lat,e.lon);for(n=0;a>n;n+=1)o=t[n],r=new LatLon(o.lat,o.lon),t[n].distance=i.distanceTo(r);return t},sortByAlpha:function(t){return t.sort(e)},sortByDistance:function(e,o){return o=n.places.fillDistance(e,o),o.sort(t)}},t=function(e,t){var n=e.distance,o=t.distance;return o>n?-1:n>o?1:0},e=function(e,t){var n=e.tags.name||"",o=t.tags.name||"";return n=n.toLowerCase(),o=o.toLowerCase(),o>n?-1:n>o?1:0},window.Pullet=n}(),function(){"use strict";var e=window.Pullet||{};e.getLocation=function(){return new Promise(function(e,t){navigator.geolocation.getCurrentPosition(function(t){e(t)},function(e){t(e)})})},window.Pullet=e}();
//# sourceMappingURL=chiq.map.js
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2015 */
/* - www.movable-type.co.uk/scripts/latlong.html MIT Licence */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
'use strict';
if (typeof module!='undefined' && module.exports) var Dms = require('./dms'); // CommonJS (Node)
/**
* Creates a LatLon point on the earth's surface at the specified latitude / longitude.
*
* @classdesc Tools for geodetic calculations
* @requires Dms from 'dms.js'
*
* @constructor
* @param {number} lat - Latitude in degrees.
* @param {number} lon - Longitude in degrees.
*
* @example
* var p1 = new LatLon(52.205, 0.119);
*/
function LatLon(lat, lon) {
// allow instantiation without 'new'
if (!(this instanceof LatLon)) return new LatLon(lat, lon);
this.lat = Number(lat);
this.lon = Number(lon);
}
/**
* Returns the distance from 'this' point to destination point (using haversine formula).
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres).
* @returns {number} Distance between this point and destination point, in same units as radius.
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var d = p1.distanceTo(p2); // Number(d.toPrecision(4)): 404300
*/
LatLon.prototype.distanceTo = function(point, radius) {
if (!(point instanceof LatLon)) throw new TypeError('point is not LatLon object');
radius = (radius === undefined) ? 6371e3 : Number(radius);
var R = radius;
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
var Δφ = φ2 - φ1;
var Δλ = λ2 - λ1;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
};
/** Extend Number object with method to convert numeric degrees to radians */
if (Number.prototype.toRadians === undefined) {
Number.prototype.toRadians = function() { return this * Math.PI / 180; };
}
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'module'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module);
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod);
global.xr = mod.exports;
}
})(this, function (exports, module) {
/**
* xr (c) James Cleveland 2015
* URL: https://github.com/radiosilence/xr
* License: BSD
*/
'use strict';
var Methods = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
PATCH: 'PATCH',
OPTIONS: 'OPTIONS'
};
var Events = {
READY_STATE_CHANGE: 'readystatechange',
LOAD_START: 'loadstart',
PROGRESS: 'progress',
ABORT: 'abort',
ERROR: 'error',
LOAD: 'load',
TIMEOUT: 'timeout',
LOAD_END: 'loadend'
};
var defaults = {
method: Methods.GET,
data: undefined,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dump: JSON.stringify,
load: JSON.parse,
xmlHttpRequest: function xmlHttpRequest() {
return new XMLHttpRequest();
},
promise: function promise(fn) {
return new Promise(fn);
}
};
function res(xhr) {
return {
status: xhr.status,
response: xhr.response,
xhr: xhr
};
}
function assign(l) {
for (var _len = arguments.length, rs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rs[_key - 1] = arguments[_key];
}
for (var i in rs) {
if (!({}).hasOwnProperty.call(rs, i)) continue;
var r = rs[i];
if (typeof r !== 'object') continue;
for (var k in r) {
if (!({}).hasOwnProperty.call(r, k)) continue;
l[k] = r[k];
}
}
return l;
}
function urlEncode(params) {
var paramStrings = [];
for (var k in params) {
if (!({}).hasOwnProperty.call(params, k)) continue;
paramStrings.push(encodeURIComponent(k) + '=' + encodeURIComponent(params[k]));
}
return paramStrings.join('&');
}
var config = {};
function configure(opts) {
config = assign({}, config, opts);
}
function promise(args, fn) {
return (args && args.promise ? args.promise : config.promise || defaults.promise)(fn);
}
function xr(args) {
return promise(args, function (resolve, reject) {
var opts = assign({}, defaults, config, args);
var xhr = opts.xmlHttpRequest();
xhr.open(opts.method, opts.params ? opts.url.split('?')[0] + '?' + urlEncode(opts.params) : opts.url, true);
xhr.addEventListener(Events.LOAD, function () {
if (xhr.status >= 200 && xhr.status < 300) {
var _data = null;
if (xhr.responseText) {
_data = opts.raw === true ? xhr.responseText : opts.load(xhr.responseText);
}
resolve(_data);
} else {
reject(res(xhr));
}
});
xhr.addEventListener(Events.ABORT, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.ERROR, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.TIMEOUT, function () {
return reject(res(xhr));
});
for (var k in opts.headers) {
if (!({}).hasOwnProperty.call(opts.headers, k)) continue;
xhr.setRequestHeader(k, opts.headers[k]);
}
for (var k in opts.events) {
if (!({}).hasOwnProperty.call(opts.events, k)) continue;
xhr.addEventListener(k, opts.events[k].bind(null, xhr), false);
}
var data = typeof opts.data === 'object' && !opts.raw ? opts.dump(opts.data) : opts.data;
if (data !== undefined) xhr.send(data);else xhr.send();
});
}
xr.assign = assign;
xr.urlEncode = urlEncode;
xr.configure = configure;
xr.Methods = Methods;
xr.Events = Events;
xr.defaults = defaults;
xr.get = function (url, params, args) {
return xr(assign({ url: url, method: Methods.GET, params: params }, args));
};
xr.put = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PUT, data: data }, args));
};
xr.post = function (url, data, args) {
return xr(assign({ url: url, method: Methods.POST, data: data }, args));
};
xr.patch = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PATCH, data: data }, args));
};
xr.del = function (url, args) {
return xr(assign({ url: url, method: Methods.DELETE }, args));
};
xr.options = function (url, args) {
return xr(assign({ url: url, method: Methods.OPTIONS }, args));
};
module.exports = xr;
});

+ 1
- 0
chiq.map.js
File diff suppressed because it is too large
View File


dist/chiq.unmin.js → chiq.unmin.js View File


+ 0
- 1
dist/chiq.map.js
File diff suppressed because it is too large
View File


+ 0
- 2
dist/chiq.min.js View File

@ -1,2 +0,0 @@
!function(){"use strict";var e,t,n=window.Pullet||{};n.map={create:function(e,n){var o,r=t(n),a=new ol.layer.Tile({source:new ol.source.OSM}),i=[a,r],s=r.getSource().getExtent();return o=new ol.Map({target:e,layers:i,view:new ol.View({center:[0,0],zoom:1})}),o.getView().fit(s,o.getSize(),{maxZoom:18}),o}},t=function(t){var n,o,r,a,i,s=t.length,u=[];for(n=0;s>n;n+=1)a=t[n],i=a.tags.name,o=parseFloat(a.lat),r=parseFloat(a.lon),isNaN(o)||isNaN(r)||u.push(new ol.Feature({geometry:new ol.geom.Point(ol.proj.transform([r,o],"EPSG:4326","EPSG:3857"))}));return new ol.layer.Vector({source:new ol.source.Vector({features:u}),style:e("/images/marker-green.png")})},e=function(e){return new ol.style.Style({image:new ol.style.Icon({anchor:[.5,46],anchorXUnits:"fraction",anchorYUnits:"pixels",opacity:.75,src:e})})},$("[data-mapcontainer]").each(function(){var e=$(this),t=$(e.data("mapcontainer")),o=[];e.find(".poi").each(function(){var e=$(this);o.push({tags:{name:e.find(".poi__name").text()},lat:e.find(".poi__lat").text(),lon:e.find(".poi__lon").text()})}),n.map.create(t.get(0),o)}),window.Pullet=n}(),function(){"use strict";var e,t=window.Pullet||{};t.nominatim={lookup:function(t){var n,o,r,a=window.Pullet.endpoint.nominatim+"/lookup",i=t.length,s=[],u=[],l=0,c=[],y=t.slice();return r=function(t){var n,o,r,a,s;if(l+=1,l===i){for(o=c.length,n=0;o>n;n+=1)r=c[n],a=e(r.osm_id,y),a!==!1&&(s=y[a],s.fill(r),y[a]=s);t(y)}},new Promise(function(e){for(Array.isArray(t)&&0!==t.length||e([]);t.length>0;){for(u=[],o=t.splice(0,50),i=o.length,n=0;i>n;n+=1)u.push(o[n].fid);s.push(u)}for(n=0,i=s.length;i>n;n+=1)xr.get(a,{format:"json",osm_ids:s[n].join()}).then(function(t){c=c.concat(t),r(e)}).catch(function(){r(e)})})}},e=function(e,t){var n,o,r=t.length;for(n=0;r>n;n+=1)if(o=t[n],o.osm_id===parseInt(e,10))return n;return!1},window.Pullet=t}(),function(){"use strict";var e,t=window.Pullet||{};e=function(e){this.osm_id=e.id,this.name=e.name,this.tags=e.tags,this.lat=e.lat,this.lon=e.lon,this.osm_type=e.type,this.fid=e.type.slice(0,1).toUpperCase()+e.id},e.prototype.fill=function(e){this.address=e.address,this.class=e.class,this.name||(this.name=e.display_name),this.display_name=e.display_name,this.lat=e.lat,this.lon=e.lon,this.nom_id=e.place_id,this.nom_type=e.type},t.Place=e,window.Pullet=t}(),function(){"use strict";var e,t,n=window.Pullet||{};n.places={nearby:function(){return Pullet.getLocation().then(function(e){var t=e.coords;return Pullet.places.near(t.latitude,t.longitude)}).catch(function(e){return Promise.reject(e)})},near:function(e,t){var n,o=Pullet.endpoint.overpass,r=300,a="<around lon='"+t+"' lat='"+e+"' radius='"+r+"'/>",i="<has-kv k='name'/>";return n="<osm-script output='json' timeout='25'><union><query type='node'><has-kv k='amenity'/>"+a+i+"</query><query type='way'><has-kv k='amenity'/>"+a+i+"</query><query type='node'><has-kv k='shop'/>"+a+i+"</query><query type='way'><has-kv k='shop'/>"+a+i+"</query><query type='node'><has-kv k='office'/>"+a+i+"</query><query type='way'><has-kv k='office'/>"+a+i+"</query><query type='node'><has-kv k='craft'/>"+a+i+"</query><query type='way'><has-kv k='craft'/>"+a+i+"</query><query type='node'><has-kv k='leisure'/>"+a+i+"</query><query type='way'><has-kv k='leisure'/>"+a+i+"</query><query type='node'><has-kv k='public_transport' v='platform'/>"+a+i+"</query><query type='way'><has-kv k='public_transport' v='platform'/>"+a+i+"</query><query type='node'><has-kv k='highway' v='bus_stop'/>"+a+i+"</query><query type='way'><has-kv k='highway' v='bus_stop'/>"+a+i+"</query></union><print mode='body' order='quadtile'/></osm-script>",xr.post(o,n).then(function(e){var t,n=e.elements,o=n.length,r=[];for(t=0;o>t;t+=1)r.push(new Pullet.Place(n[t]));return r})},fillDistance:function(e,t){var n,o,r,a=t.length,i=new LatLon(e.lat,e.lon);for(n=0;a>n;n+=1)o=t[n],r=new LatLon(o.lat,o.lon),t[n].distance=i.distanceTo(r);return t},sortByAlpha:function(t){return t.sort(e)},sortByDistance:function(e,o){return o=n.places.fillDistance(e,o),o.sort(t)}},t=function(e,t){var n=e.distance,o=t.distance;return o>n?-1:n>o?1:0},e=function(e,t){var n=e.tags.name||"",o=t.tags.name||"";return n=n.toLowerCase(),o=o.toLowerCase(),o>n?-1:n>o?1:0},window.Pullet=n}(),function(){"use strict";var e=window.Pullet||{};e.getLocation=function(){return new Promise(function(e,t){navigator.geolocation.getCurrentPosition(function(t){e(t)},function(e){t(e)})})},window.Pullet=e}();
//# sourceMappingURL=dist/chiq.map.js

+ 0
- 66
dist/extlib/latlon-spherical.js View File

@ -1,66 +0,0 @@
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2015 */
/* - www.movable-type.co.uk/scripts/latlong.html MIT Licence */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
'use strict';
if (typeof module!='undefined' && module.exports) var Dms = require('./dms'); // CommonJS (Node)
/**
* Creates a LatLon point on the earth's surface at the specified latitude / longitude.
*
* @classdesc Tools for geodetic calculations
* @requires Dms from 'dms.js'
*
* @constructor
* @param {number} lat - Latitude in degrees.
* @param {number} lon - Longitude in degrees.
*
* @example
* var p1 = new LatLon(52.205, 0.119);
*/
function LatLon(lat, lon) {
// allow instantiation without 'new'
if (!(this instanceof LatLon)) return new LatLon(lat, lon);
this.lat = Number(lat);
this.lon = Number(lon);
}
/**
* Returns the distance from 'this' point to destination point (using haversine formula).
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres).
* @returns {number} Distance between this point and destination point, in same units as radius.
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var d = p1.distanceTo(p2); // Number(d.toPrecision(4)): 404300
*/
LatLon.prototype.distanceTo = function(point, radius) {
if (!(point instanceof LatLon)) throw new TypeError('point is not LatLon object');
radius = (radius === undefined) ? 6371e3 : Number(radius);
var R = radius;
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
var Δφ = φ2 - φ1;
var Δλ = λ2 - λ1;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
};
/** Extend Number object with method to convert numeric degrees to radians */
if (Number.prototype.toRadians === undefined) {
Number.prototype.toRadians = function() { return this * Math.PI / 180; };
}

+ 0
- 177
dist/extlib/xr.js View File

@ -1,177 +0,0 @@
// https://github.com/radiosilence/xr
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'module'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module);
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod);
global.xr = mod.exports;
}
})(this, function (exports, module) {
/**
* xr (c) James Cleveland 2015
* URL: https://github.com/radiosilence/xr
* License: BSD
*/
'use strict';
var Methods = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
PATCH: 'PATCH',
OPTIONS: 'OPTIONS'
};
var Events = {
READY_STATE_CHANGE: 'readystatechange',
LOAD_START: 'loadstart',
PROGRESS: 'progress',
ABORT: 'abort',
ERROR: 'error',
LOAD: 'load',
TIMEOUT: 'timeout',
LOAD_END: 'loadend'
};
var defaults = {
method: Methods.GET,
data: undefined,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dump: JSON.stringify,
load: JSON.parse,
xmlHttpRequest: function xmlHttpRequest() {
return new XMLHttpRequest();
},
promise: function promise(fn) {
return new Promise(fn);
}
};
function res(xhr) {
return {
status: xhr.status,
response: xhr.response,
xhr: xhr
};
}
function assign(l) {
for (var _len = arguments.length, rs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rs[_key - 1] = arguments[_key];
}
for (var i in rs) {
if (!({}).hasOwnProperty.call(rs, i)) continue;
var r = rs[i];
if (typeof r !== 'object') continue;
for (var k in r) {
if (!({}).hasOwnProperty.call(r, k)) continue;
l[k] = r[k];
}
}
return l;
}
function urlEncode(params) {
var paramStrings = [];
for (var k in params) {
if (!({}).hasOwnProperty.call(params, k)) continue;
paramStrings.push(encodeURIComponent(k) + '=' + encodeURIComponent(params[k]));
}
return paramStrings.join('&');
}
var config = {};
function configure(opts) {
config = assign({}, config, opts);
}
function promise(args, fn) {
return (args && args.promise ? args.promise : config.promise || defaults.promise)(fn);
}
function xr(args) {
return promise(args, function (resolve, reject) {
var opts = assign({}, defaults, config, args);
var xhr = opts.xmlHttpRequest();
xhr.open(opts.method, opts.params ? opts.url.split('?')[0] + '?' + urlEncode(opts.params) : opts.url, true);
xhr.addEventListener(Events.LOAD, function () {
if (xhr.status >= 200 && xhr.status < 300) {
var _data = null;
if (xhr.responseText) {
_data = opts.raw === true ? xhr.responseText : opts.load(xhr.responseText);
}
resolve(_data);
} else {
reject(res(xhr));
}
});
xhr.addEventListener(Events.ABORT, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.ERROR, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.TIMEOUT, function () {
return reject(res(xhr));
});
for (var k in opts.headers) {
if (!({}).hasOwnProperty.call(opts.headers, k)) continue;
xhr.setRequestHeader(k, opts.headers[k]);
}
for (var k in opts.events) {
if (!({}).hasOwnProperty.call(opts.events, k)) continue;
xhr.addEventListener(k, opts.events[k].bind(null, xhr), false);
}
var data = typeof opts.data === 'object' && !opts.raw ? opts.dump(opts.data) : opts.data;
if (data !== undefined) xhr.send(data);else xhr.send();
});
}
xr.assign = assign;
xr.urlEncode = urlEncode;
xr.configure = configure;
xr.Methods = Methods;
xr.Events = Events;
xr.defaults = defaults;
xr.get = function (url, params, args) {
return xr(assign({ url: url, method: Methods.GET, params: params }, args));
};
xr.put = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PUT, data: data }, args));
};
xr.post = function (url, data, args) {
return xr(assign({ url: url, method: Methods.POST, data: data }, args));
};
xr.patch = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PATCH, data: data }, args));
};
xr.del = function (url, args) {
return xr(assign({ url: url, method: Methods.DELETE }, args));
};
xr.options = function (url, args) {
return xr(assign({ url: url, method: Methods.OPTIONS }, args));
};
module.exports = xr;
});

+ 1
- 3
examples/pois/pois.html View File

@ -30,9 +30,7 @@
</div>
<!-- libraries/dependencies -->
<script src="../../dist/extlib/xr.js"></script>
<script src="../../dist/extlib/latlon-spherical.js"></script>
<script src="../../dist/chiq.unmin.js"></script>
<script src="../../chiq.js"></script>
<!-- Required JS for this page -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>


Loading…
Cancel
Save