-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraformer-geostore-localstorage.js
95 lines (80 loc) · 2.49 KB
/
terraformer-geostore-localstorage.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(function (root, factory) {
// Browser Global.
if(typeof root.navigator === "object") {
if (!root.Terraformer){
throw new Error("Terraformer.GeoStore.LocalStorage requires the core Terraformer library. https://github.com/esri/Terraformer");
}
if (!root.Terraformer.GeoStore){
throw new Error("Terraformer.GeoStore.LocalStorage requires the Terraformer GeoStore library. https://github.com/esri/terraformer-geostore");
}
root.Terraformer.GeoStore.LocalStorage = factory(root.Terraformer).LocalStorage;
}
}(this, function() {
var exports = { };
var callback;
// These methods get called in context of the geostore
function LocalStorage(){
var opts = arguments[0] || {};
this._key = opts.key || "_terraformer";
}
// store the data at id returns true if stored successfully
LocalStorage.prototype.add = function(geojson, callback){
if(geojson.type === "FeatureCollection"){
for (var i = 0; i < geojson.features.length; i++) {
this.set(geojson.features[i]);
}
} else {
this.set(geojson);
}
if (callback) {
callback( null, geojson );
}
};
LocalStorage.prototype.key = function(id){
return this._key +"_"+id;
};
// remove the data from the index and data with id returns true if removed successfully.
LocalStorage.prototype.remove = function( id, callback ){
localStorage.removeItem( this.key( id ) );
if (callback) {
callback( null, id );
}
};
// return the data stored at id
LocalStorage.prototype.get = function(id, callback){
if (callback) {
callback( null, JSON.parse(localStorage.getItem(this.key(id))));
}
};
LocalStorage.prototype.set = function(feature){
localStorage.setItem(this.key(feature.id), JSON.stringify(feature));
};
LocalStorage.prototype.update = function(geojson, callback){
this.set(geojson);
if (callback) {
callback( null, geojson );
}
};
LocalStorage.prototype.serialize = function(callback){
var objs = [];
for (var key in localStorage){
if(key.match(this._key)){
objs.push(JSON.parse(localStorage.getItem(key)));
}
}
if (callback) {
callback(null, JSON.stringify(objs));
}
};
LocalStorage.prototype.deserialize = function(serial, callback){
var data = JSON.parse(serial);
for (var i = data.length - 1; i >= 0; i--) {
this.set(data[i]);
}
if (callback) {
callback();
}
};
exports.LocalStorage = LocalStorage;
return exports;
}));