/
_storage.js
98 lines (97 loc) · 2.86 KB
/
_storage.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
96
97
98
import _ from 'underscore';
export const storage = (params, $rootScope, utils) => {
var Storage = function() {
this.data = {heat: {}};
const self = this;
this.counter = 0;
var scope = $rootScope.$new();
//TODO change to emitter and broadcasters
scope.params = params;
this.defaults = {};
scope.$watch("params.get('data')", function(newValue, oldValue) {
console.log("watch - params.get('data')");
self.extend(newValue);
}, true);
};
Storage.prototype = {
get: function(name) {
return this.data[name];
},
refreshCounter: function(){
this.set("counter", parseInt(this.get("counter") || 0, 10) + 1);
},
set: function(name, value) {
this.data[name] = JSON.parse(JSON.stringify(value));
},
setInHash: function(hashName, name, value) {
this.data[hashName][name] = angular.copy(value);
},
extend: function(data) {
_.extend(this.data, angular.copy(data));
},
update: function(name) {
var obj = {};
if(name == 'time') {
var timeObject = this.get('time');
_(timeObject).each(function(value, key){
timeObject[key] = parseInt(value, 10);
});
obj[name] = timeObject;
} else {
obj[name] = this.get(name);
}
params.update({data: obj});
},
updateCrowdMapDataInParams: function() {
params.update({
data: {
...this.data,
gridResolution: this.get('gridResolution'),
crowdMap: this.get('crowdMap')
}
});
},
updateWithRefresh: function(name) {
//be carefull when using this - currently it is only for location data
this.refreshCounter();
var obj = {};
obj[name] = this.get(name);
obj.counter = this.get("counter");
params.update({data: obj});
},
reset: function(name) {
if(_(this.defaults).has(name)){
this.data[name] = angular.copy(this.defaults[name]);
this.update(name);
}
},
resetAddress: function() {
this.data.location = { ...this.data.location, address: '' };
this.update('location');
},
updateDefaults: function(newData) {
this.defaults = utils.merge(this.defaults, newData);
},
updateFromDefaults: function() {
var notUsedDefaults = {};
_(this.defaults).each(function(value, key){
if(!params.get("data")[key]){
notUsedDefaults[key] = value;
}
});
params.update({data: notUsedDefaults});
},
isCrowdMapLayerOn: function() {
return this.data.crowdMap;
},
toggleCrowdMapData: function() {
this.set("crowdMap",!this.isCrowdMapLayerOn());
this.updateCrowdMapDataInParams();
},
updateCrowdMapResolution: function(newResolution) {
this.set("gridResolution", newResolution);
this.updateCrowdMapDataInParams();
}
};
return new Storage();
}