-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (63 loc) · 2.4 KB
/
index.js
File metadata and controls
70 lines (63 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var createClassName = require('data-field-classname')
var Emitter = require('component-emitter')
var extend = require('xtend')
var createMap = require('./map')
/**
Create a virtual-dom geojson data-field for use with [data-ui](https://github.com/editdata/data-ui).
* @name createGeoJSONField
* @param {Object} options an options object, including any properties you can pass to leaflet & virtual-dom/h
* @param {String} options.accessToken mapbox access token for using their API
* @param {Object} options.tileLayer Leaflet tilelayer, default is osm tiles
* @param {String} options.imagePath path to leaflet images
* @param {Boolean} options.display true for display mode, default is false for input mode
* @returns field
* @example
* var createGeoJSONField = require('data-field-geojson')
* var field = createGeoJSONField(options)
* field.render(h, {}, geojsonObject)
*/
module.exports = function createGeoJSONField (options) {
options = extend({
tagName: 'div',
display: false,
size: 'normal',
attributes: {}
}, options)
var field = {}
Emitter(field)
options.dataType = 'geojson'
options.fieldType = options.display ? 'display' : 'input'
options.onclick = function (e) {
field.emit('click', e)
}
options.ondraw = function (e, value) {
field.emit('draw', e, value)
field.emit('update', e, value)
}
options.onedit = function (e, value) {
field.emit('edit', e, value)
field.emit('update', e, value)
}
/**
Render the virtual-dom geojson data-field.
* @param {function} h virtual-dom `h` function
* @param {Object} properties an options object, including any properties you can pass to leaflet & virtual-dom/h
* @param {Boolean} properties.display true for display mode, default is false for input mode
* @param {Object} properties.value a geojson Feature or Featurecollection
* @param {Object} value a geojson Feature or Featurecollection
* @returns virtual-dom tree
* @name field.render
* @example
* var createGeoJSONField = require('data-field-geojson')
* var field = createGeoJSONField(options)
* field.render(h, properties, geojsonObject)
*/
field.render = function (h, properties, value) {
properties = extend(options, properties)
value = value || properties.value
var map = field.map = createMap(value, properties)
properties.className = createClassName(properties)
return h(properties.tagName, properties, map)
}
return field
}