This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
exception.js
231 lines (193 loc) · 6.17 KB
/
exception.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* ALA exception panel.
*
* @module ALAException
* @return {Object}
*/
define([
'panels',
'ala/blur_slider',
'ala/exceptions',
'shared/settings_listener',
'shared/settings_helper'
],
function(panels, BlurSlider, alaExceptions, SettingsListener,
SettingsHelper) {
'use strict';
function ALAException() {
this.itemData = null;
this.currentApp = null;
this.currentAppSettings = null;
this.blurSlider = new BlurSlider();
}
ALAException.prototype = {
/**
* Initialize ALA exception panel.
*
* @method init
* @constructor
*/
init: function() {
this.panel = document.getElementById('ala-exception');
this.appInfoImg = this.panel.querySelector('.app-info img');
this.appInfoSpan = this.panel.querySelector('.app-info span');
this.appType = this.panel.querySelector('.app-type');
this.blurLabel = this.panel.querySelector('.app-blur-label');
this.alert = this.panel.querySelector('.app-custom-location-alert');
this.blurSlider.init(
this.panel.querySelector('.type-blur'),
1,
this.saveExceptions.bind(this)
);
this.events();
},
/**
* Register events.
*/
events: function() {
this.panel.addEventListener('pagerendered', this.onBeforeShow.bind(this));
this.appType.addEventListener('change', function(event) {
this.changeAppType(event.target.value, true);
}.bind(this));
this.panel.querySelector('.set-custom-location').addEventListener('click',
function() {
panels.show({ id: 'ala-custom', options: this });
}.bind(this)
);
this.alert.querySelector('button').addEventListener('click',
function() {
this.alert.setAttribute('hidden', 'hidden');
panels.show({ id: 'ala-custom', options: this });
}.bind(this)
);
},
/**
* Actions before displaying panel.
* @param event
*/
onBeforeShow: function(event) {
this.itemData = event.detail;
this.appInfoImg.src = this.itemData.iconSrc;
this.appInfoSpan.textContent = this.itemData.name;
this.currentApp = this.itemData.origin;
this.currentAppSettings =
alaExceptions.exceptionsList[this.itemData.origin];
if (!this.currentAppSettings) {
// set default value (from general settings)
this.appType.value = 'system-settings';
// change settings type
this.changeAppType('system-settings', false);
} else {
// set checkbox value
this.appType.value = this.currentAppSettings.type;
// change settings type
this.changeAppType(this.currentAppSettings.type, false);
// set slider value
this.blurSlider.setValue(this.currentAppSettings.slider);
}
},
/**
* Change Application type.
* @param {String} value
* @param {Boolean} save
*/
changeAppType: function(value, save) {
// set attribute to section
this.panel.dataset.type = value;
// hide alert
this.alert.setAttribute('hidden', 'hidden');
switch (value) {
case 'user-defined':
/** @TODO: add alert */
if (!(alaExceptions.exceptionsList[this.currentApp] &&
alaExceptions.exceptionsList[this.currentApp].coords)) {
// show alert if geolocation is not set
this.alert.removeAttribute('hidden');
}
break;
case 'system-settings':
// remove application
if (save === true) {
this.removeException();
}
return;
case 'blur':
case 'precise':
case 'no-location':
break;
default:
break;
}
// save current type
save && this.saveExceptions(null);
},
/**
* Save exception list.
* @param {Object|Null} settings
*/
saveExceptions: function(settings) {
var current = this.currentAppSettings || {};
var extraSettings = settings || {};
alaExceptions.exceptionsList[this.currentApp] = {
type: this.appType.value,
slider: this.blurSlider.getValue(),
radius: this.blurSlider.getRadius(this.blurSlider.getValue()),
coords: extraSettings.coords || current.coords || null,
cl_type: extraSettings.cl_type || current.cl_type || null,
cl_region: extraSettings.cl_region || current.cl_region || null,
cl_city: extraSettings.cl_city || current.cl_city || null,
cl_longitude: extraSettings.cl_longitude || current.cl_longitude ||null,
cl_latitude: extraSettings.cl_latitude || current.cl_latitude || null
};
SettingsHelper('geolocation.app_settings')
.set(alaExceptions.exceptionsList);
},
/**
* Remove exception from list.
*/
removeException: function() {
delete alaExceptions.exceptionsList[this.currentApp];
SettingsHelper('geolocation.app_settings')
.set(alaExceptions.exceptionsList);
},
/**
* Get data for Define Custom Location.
* @return {Array}
*/
getDCLData: function() {
this.currentAppSettings =
alaExceptions.exceptionsList[this.currentApp];
return {
type: this.currentAppSettings.cl_type,
region: this.currentAppSettings.cl_region,
city: this.currentAppSettings.cl_city,
longitude: this.currentAppSettings.cl_longitude,
latitude: this.currentAppSettings.cl_latitude
};
},
/**
* Save custom location settings.
* @param {Object} settings
*/
saveDCLData: function(settings) {
var flag = settings.latitude !== '' && settings.longitude !== '';
this.saveExceptions({
coords: flag ? '@'+settings.latitude+','+settings.longitude : '',
cl_type: settings.type,
cl_region: settings.region,
cl_city: settings.city,
cl_longitude: settings.longitude,
cl_latitude: settings.latitude
});
},
/**
* Go back from DCL
*/
goBackFromDCL: function() {
panels.show(
{ id: 'ala-exception', options: this.itemData, back: true}
);
}
};
return new ALAException();
});