@alter-eco/choropleth
Advanced tools
Comparing version 0.0.5 to 0.0.6
263
dist/main.js
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Choropleth = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var Choropleth; | ||
var eventablejs = require('eventablejs'); | ||
module.exports = Choropleth = { | ||
create: function() { | ||
var instance = Object.assign({}, this.prototype); | ||
var instance = Object.assign({}, eventablejs, this.prototype); | ||
this.init.apply(instance, arguments); | ||
Object.keys(this.prototype).forEach(function(methodName) { | ||
var placeholder = instance[methodName]; | ||
var methodNameCaps = methodName.charAt(0).toUpperCase() + methodName.slice(1); | ||
instance[methodName] = function() { | ||
instance.trigger('before' + methodNameCaps); | ||
var result = placeholder.apply(instance, arguments); | ||
instance.trigger('after' + methodNameCaps, result); | ||
return result; | ||
}; | ||
}.bind(instance)); | ||
return instance; | ||
@@ -29,6 +47,11 @@ }, | ||
this.config.legend = Object.assign({ | ||
orientation: 'vertical', | ||
}, config.legend); | ||
if (config.legend) { | ||
this.config.legend = Object.assign({ | ||
orientation: 'vertical', | ||
}, config.legend); | ||
this.on('beforeDraw', this.updateLegend); | ||
this.on('afterSetScale', this.updateLegend); | ||
} | ||
this.config.scale = Object.assign({ | ||
@@ -50,4 +73,4 @@ type: 'linear', | ||
this.width = rawWidth - margin.left - margin.right; | ||
this.height = rawHeight - margin.top - margin.bottom; | ||
this.width = this.drawWidth = rawWidth - margin.left - margin.right; | ||
this.height = this.drawHeight = rawHeight - margin.top - margin.bottom; | ||
@@ -75,3 +98,3 @@ this.map = d3.select(config.elem) | ||
if (config.tooltip && config.tooltip.enabled !== false) { | ||
if (config.tooltip) { | ||
this.config.tooltip = Object.assign({ | ||
@@ -145,2 +168,6 @@ prefix: '', | ||
updateLegend: function() { | ||
if (!this.config.legend) { | ||
return false; | ||
} | ||
var legend = d3.legendColor() | ||
@@ -155,3 +182,2 @@ .labelFormat(d3.format(this.config.legend.format)) | ||
var legendSelection = this.map.select('.key'); | ||
@@ -161,3 +187,9 @@ | ||
legendSelection.attr('transform', 'translate(0,' + (this.height - legendSelection.node().getBBox().height) + ')') | ||
var legendHeight = legendSelection.node().getBBox().height; | ||
if (this.config.legend.reserveSpace) { | ||
this.drawHeight = this.drawHeight - legendHeight; | ||
} | ||
legendSelection.attr('transform', 'translate(0,' + (this.height - legendHeight) + ')'); | ||
}, | ||
@@ -206,3 +238,3 @@ | ||
this.updateLegend(); | ||
}.bind(this), 0) | ||
}.bind(this), 0); | ||
}, | ||
@@ -215,3 +247,3 @@ | ||
[20, 20], | ||
[this.width, this.height] | ||
[this.drawWidth, this.drawHeight] | ||
], this.geojson)); | ||
@@ -259,3 +291,212 @@ | ||
},{"eventablejs":2}],2:[function(require,module,exports){ | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as a module. | ||
define('eventable', function() { | ||
return (root.Eventable = factory()); | ||
}); | ||
} else if (typeof exports !== 'undefined') { | ||
// Node. Does not work with strict CommonJS, but only CommonJS-like | ||
// enviroments that support module.exports, like Node. | ||
module.exports = factory(); | ||
} else { | ||
// Browser globals | ||
root.Eventable = factory(); | ||
} | ||
}(this, function() { | ||
// Copy and pasted straight out of Backbone 1.0.0 | ||
// We'll try and keep this updated to the latest | ||
var array = []; | ||
var slice = array.slice; | ||
function once(func) { | ||
var memo, times = 2; | ||
return function() { | ||
if (--times > 0) { | ||
memo = func.apply(this, arguments); | ||
} else { | ||
func = null; | ||
} | ||
return memo; | ||
}; | ||
} | ||
// Backbone.Events | ||
// --------------- | ||
// A module that can be mixed in to *any object* in order to provide it with | ||
// custom events. You may bind with `on` or remove with `off` callback | ||
// functions to an event; `trigger`-ing an event fires all callbacks in | ||
// succession. | ||
// | ||
// var object = {}; | ||
// extend(object, Backbone.Events); | ||
// object.on('expand', function(){ alert('expanded'); }); | ||
// object.trigger('expand'); | ||
// | ||
var Eventable = { | ||
// Bind an event to a `callback` function. Passing `"all"` will bind | ||
// the callback to all events fired. | ||
on: function(name, callback, context) { | ||
if (!eventsApi(this, 'on', name, [callback, context]) || !callback) return this; | ||
this._events || (this._events = {}); | ||
var events = this._events[name] || (this._events[name] = []); | ||
events.push({callback: callback, context: context, ctx: context || this}); | ||
return this; | ||
}, | ||
// Bind an event to only be triggered a single time. After the first time | ||
// the callback is invoked, it will be removed. | ||
once: function(name, callback, context) { | ||
if (!eventsApi(this, 'once', name, [callback, context]) || !callback) return this; | ||
var self = this; | ||
var func = once(function() { | ||
self.off(name, func); | ||
callback.apply(this, arguments); | ||
}); | ||
func._callback = callback; | ||
return this.on(name, func, context); | ||
}, | ||
// Remove one or many callbacks. If `context` is null, removes all | ||
// callbacks with that function. If `callback` is null, removes all | ||
// callbacks for the event. If `name` is null, removes all bound | ||
// callbacks for all events. | ||
off: function(name, callback, context) { | ||
var retain, ev, events, names, i, l, j, k; | ||
if (!this._events || !eventsApi(this, 'off', name, [callback, context])) return this; | ||
if (!name && !callback && !context) { | ||
this._events = {}; | ||
return this; | ||
} | ||
names = name ? [name] : Object.keys(this._events); | ||
for (i = 0, l = names.length; i < l; i++) { | ||
name = names[i]; | ||
if (events = this._events[name]) { | ||
this._events[name] = retain = []; | ||
if (callback || context) { | ||
for (j = 0, k = events.length; j < k; j++) { | ||
ev = events[j]; | ||
if ((callback && callback !== ev.callback && callback !== ev.callback._callback) || | ||
(context && context !== ev.context)) { | ||
retain.push(ev); | ||
} | ||
} | ||
} | ||
if (!retain.length) delete this._events[name]; | ||
} | ||
} | ||
return this; | ||
}, | ||
// Trigger one or many events, firing all bound callbacks. Callbacks are | ||
// passed the same arguments as `trigger` is, apart from the event name | ||
// (unless you're listening on `"all"`, which will cause your callback to | ||
// receive the true name of the event as the first argument). | ||
trigger: function(name) { | ||
if (!this._events) return this; | ||
var args = slice.call(arguments, 1); | ||
if (!eventsApi(this, 'trigger', name, args)) return this; | ||
var events = this._events[name]; | ||
var allEvents = this._events.all; | ||
if (events) triggerEvents(events, args); | ||
if (allEvents) triggerEvents(allEvents, arguments); | ||
return this; | ||
}, | ||
// Tell this object to stop listening to either specific events ... or | ||
// to every object it's currently listening to. | ||
stopListening: function(obj, name, callback) { | ||
var listeners = this._listeners; | ||
if (!listeners) return this; | ||
var deleteListener = !name && !callback; | ||
if (typeof name === 'object') callback = this; | ||
if (obj) (listeners = {})[obj._listenerId] = obj; | ||
for (var id in listeners) { | ||
listeners[id].off(name, callback, this); | ||
if (deleteListener) delete this._listeners[id]; | ||
} | ||
return this; | ||
} | ||
}; | ||
// Regular expression used to split event strings. | ||
var eventSplitter = /\s+/; | ||
// Implement fancy features of the Events API such as multiple event | ||
// names `"change blur"` and jQuery-style event maps `{change: action}` | ||
// in terms of the existing API. | ||
var eventsApi = function(obj, action, name, rest) { | ||
if (!name) return true; | ||
// Handle event maps. | ||
if (typeof name === 'object') { | ||
for (var key in name) { | ||
obj[action].apply(obj, [key, name[key]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
// Handle space separated event names. | ||
if (eventSplitter.test(name)) { | ||
var names = name.split(eventSplitter); | ||
for (var i = 0, l = names.length; i < l; i++) { | ||
obj[action].apply(obj, [names[i]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
return true; | ||
}; | ||
// A difficult-to-believe, but optimized internal dispatch function for | ||
// triggering events. Tries to keep the usual cases speedy (most internal | ||
// Backbone events have 3 arguments). | ||
var triggerEvents = function(events, args) { | ||
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2]; | ||
switch (args.length) { | ||
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return; | ||
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return; | ||
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return; | ||
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return; | ||
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); | ||
} | ||
}; | ||
var listenMethods = {listenTo: 'on', listenToOnce: 'once'}; | ||
// Inversion-of-control versions of `on` and `once`. Tell *this* object to | ||
// listen to an event in another object ... keeping track of what it's | ||
// listening to. | ||
function addListenMethod(method, implementation) { | ||
Eventable[method] = function(obj, name, callback) { | ||
var listeners = this._listeners || (this._listeners = {}); | ||
var id = obj._listenerId || (obj._listenerId = (new Date()).getTime()); | ||
listeners[id] = obj; | ||
if (typeof name === 'object') callback = this; | ||
obj[implementation](name, callback, this); | ||
return this; | ||
}; | ||
} | ||
addListenMethod('listenTo', 'on'); | ||
addListenMethod('listenToOnce', 'once'); | ||
// Aliases for backwards compatibility. | ||
Eventable.bind = Eventable.on; | ||
Eventable.unbind = Eventable.off; | ||
return Eventable; | ||
})); | ||
},{}]},{},[1])(1) | ||
}); |
54
index.js
var Choropleth; | ||
var eventablejs = require('eventablejs'); | ||
module.exports = Choropleth = { | ||
create: function() { | ||
var instance = Object.assign({}, this.prototype); | ||
var instance = Object.assign({}, eventablejs, this.prototype); | ||
this.init.apply(instance, arguments); | ||
Object.keys(this.prototype).forEach(function(methodName) { | ||
var placeholder = instance[methodName]; | ||
var methodNameCaps = methodName.charAt(0).toUpperCase() + methodName.slice(1); | ||
instance[methodName] = function() { | ||
instance.trigger('before' + methodNameCaps); | ||
var result = placeholder.apply(instance, arguments); | ||
instance.trigger('after' + methodNameCaps, result); | ||
return result; | ||
}; | ||
}.bind(instance)); | ||
return instance; | ||
@@ -28,6 +46,11 @@ }, | ||
this.config.legend = Object.assign({ | ||
orientation: 'vertical', | ||
}, config.legend); | ||
if (config.legend) { | ||
this.config.legend = Object.assign({ | ||
orientation: 'vertical', | ||
}, config.legend); | ||
this.on('beforeDraw', this.updateLegend); | ||
this.on('afterSetScale', this.updateLegend); | ||
} | ||
this.config.scale = Object.assign({ | ||
@@ -49,4 +72,4 @@ type: 'linear', | ||
this.width = rawWidth - margin.left - margin.right; | ||
this.height = rawHeight - margin.top - margin.bottom; | ||
this.width = this.drawWidth = rawWidth - margin.left - margin.right; | ||
this.height = this.drawHeight = rawHeight - margin.top - margin.bottom; | ||
@@ -74,3 +97,3 @@ this.map = d3.select(config.elem) | ||
if (config.tooltip && config.tooltip.enabled !== false) { | ||
if (config.tooltip) { | ||
this.config.tooltip = Object.assign({ | ||
@@ -144,2 +167,6 @@ prefix: '', | ||
updateLegend: function() { | ||
if (!this.config.legend) { | ||
return false; | ||
} | ||
var legend = d3.legendColor() | ||
@@ -154,3 +181,2 @@ .labelFormat(d3.format(this.config.legend.format)) | ||
var legendSelection = this.map.select('.key'); | ||
@@ -160,3 +186,9 @@ | ||
legendSelection.attr('transform', 'translate(0,' + (this.height - legendSelection.node().getBBox().height) + ')') | ||
var legendHeight = legendSelection.node().getBBox().height; | ||
if (this.config.legend.reserveSpace) { | ||
this.drawHeight = this.drawHeight - legendHeight; | ||
} | ||
legendSelection.attr('transform', 'translate(0,' + (this.height - legendHeight) + ')'); | ||
}, | ||
@@ -205,3 +237,3 @@ | ||
this.updateLegend(); | ||
}.bind(this), 0) | ||
}.bind(this), 0); | ||
}, | ||
@@ -214,3 +246,3 @@ | ||
[20, 20], | ||
[this.width, this.height] | ||
[this.drawWidth, this.drawHeight] | ||
], this.geojson)); | ||
@@ -217,0 +249,0 @@ |
{ | ||
"name": "@alter-eco/choropleth", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "", | ||
@@ -18,3 +18,6 @@ "main": "index.js", | ||
"browserify": "^15.1.0" | ||
}, | ||
"dependencies": { | ||
"eventablejs": "^1.0.5" | ||
} | ||
} |
22916
617
1
+ Addedeventablejs@^1.0.5
+ Addedeventablejs@1.0.5(transitive)