-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
289 lines (245 loc) · 8.8 KB
/
app.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*jshint maxcomplexity:10 */
//<debug>
Ext.Loader.setPath({
'Ext': 'touch/src',
'patch': 'patch',
'Ext.i18n': 'i18n',
'Ext.ux': 'ux',
'Kort': 'app'
});
//</debug>
Ext.application({
name: 'Kort',
requires: [
'patch.AjaxProxy',
'Ext.MessageBox',
'Ext.i18n.Bundle',
'Kort.util.Config',
'Kort.util.Geolocation'
],
views: [
'Main',
'overlay.login.Panel',
'overlay.geolocationerror.Panel',
'overlay.firststeps.Panel'
],
controllers: [
'About',
'Bugmap',
'Firststeps',
'Fix',
'GeolocationError',
'Highscore',
'Login',
'Main',
'MarkerMap',
'OsmMap',
'Profile',
'Validation',
'Vote'
],
models: [
'Badge',
'Bug',
'Fix',
'HighscoreEntry',
'HighscoreUserBadge',
'Reward',
'SelectAnswer',
'User',
'UserBadge',
'UserLocal',
'Validation',
'Vote'
],
stores: [
'Bugs',
'Highscore',
'HighscoreUserBadges',
'SelectAnswers',
'UserBadges',
'UserLocal',
'Validations'
],
icon: './resources/images/kort-icon.png',
startupImage: {
// Non-retina iPhone, iPod touch, and all Android devices
'320x460': './resources/images/kort-startup-320x460.jpg',
// Retina iPhone and iPod touch
'640x920': './resources/images/kort-startup-640x920.png'
},
viewport: {
// attempt to stop zooming when you double tap on the screen on mobile devices, typically HTC devices with HTC Sense UI
preventZooming: true,
// hide navigation bar of iOS browsers
autoMaximize: (navigator.userAgent.search("Safari") !== -1 && navigator.userAgent.search("CriOS") === -1 && (!Ext.browser.is.Standalone && Ext.os.is.iOS && Ext.browser.version.isGreaterThan(3) ) ? true : false)
},
// launch function is called as soon as app is ready
launch: function() {
var mainPanel;
this.prepareI18n();
this.configureMessageBox();
// create main panel
// this has to be done in launch method so routes can work properly
mainPanel = Ext.create('Kort.view.Main');
Ext.Viewport.add(mainPanel);
mainPanel.hide();
this.loadGeolocation(mainPanel);
},
loadGeolocation: function(mainPanel) {
var me = this;
Kort.geolocation = Ext.create('Kort.util.Geolocation');
Kort.geolocation.updateLocation(function(geo) {
// Destroy the #appStartscreen element
Ext.fly('appStartscreen').destroy();
if(geo) {
me.loadUserClientSecret(geo, mainPanel);
} else {
me.showGeolocationErrorOverlay();
}
});
},
loadUserClientSecret: function(geo, mainPanel) {
var me = this,
userLocalStore = Ext.getStore('UserLocal');
userLocalStore.load(function(records, operation, success) {
console.log('userLocalStore loaded');
if(records.length === 0) {
console.log('no client secret record found in localstorage');
me.loadUser(geo, mainPanel);
} else {
console.log('client secret found in localstorage');
me.loadUser(geo, mainPanel, records[0].get('secret'));
}
}, me);
},
loadUser: function(geo, mainPanel, clientSecret) {
var me = this,
userLocalStore = Ext.getStore('UserLocal');
if(!clientSecret) {
clientSecret = 0;
}
Kort.model.User.load(clientSecret, {
success: function(record, operation) {
console.log('user loaded');
// set global accessor to user
Kort.user = record;
// check if user is logged in
if (!Kort.user.get('logged_in')) {
if(clientSecret && clientSecret !== 0) {
console.log('remove wrong client secret form local store');
userLocalStore.removeAll();
}
me.showLoginOverlay();
} else {
if(!clientSecret) {
console.log('clientSecret not passed -> write client secret to localstore');
me.writeUserClientSecret(Kort.user.get('secret'));
}
me.loadStores(mainPanel, geo);
// enable auto update on geolocation
geo.setAutoUpdate(true);
}
}
});
},
writeUserClientSecret: function(clientSecret) {
var userLocalStore = Ext.getStore('UserLocal'),
userLocal;
if(clientSecret) {
console.log('writing userClientSecret to localstore');
userLocal = Ext.create('Kort.model.UserLocal', { 'secret': clientSecret });
userLocalStore.add(userLocal);
} else {
console.log('Error: no client secret passed');
}
},
showLoginOverlay: function() {
var loginPanel;
console.log('user not logged in -> show login panel');
loginPanel = Ext.create('Kort.view.overlay.login.Panel');
Ext.Viewport.add(loginPanel);
loginPanel.show();
},
showGeolocationErrorOverlay: function() {
var geolocationerrorPanel;
console.log('geolocation error');
geolocationerrorPanel = Ext.create('Kort.view.overlay.geolocationerror.Panel');
Ext.Viewport.add(geolocationerrorPanel);
geolocationerrorPanel.show();
},
loadStores: function(mainPanel, geo) {
var me = this,
userBadges = Ext.getStore('UserBadges'),
selectAnswersStore = Ext.getStore('SelectAnswers'),
highscoreStore = Ext.getStore('Highscore');
// wait until correct position is found
Ext.defer(me.fireEvent, 500, me, ['geolocationready', geo]);
// load select answers
selectAnswersStore.load();
// load badges of user
userBadges.getProxy().setUrl(Kort.util.Config.getWebservices().userBadges.getUrl(Kort.user.get('id')));
userBadges.load();
// load highscore
highscoreStore.load();
this.showMainPanel(mainPanel);
},
showMainPanel: function(mainPanel) {
mainPanel.show();
if(!Kort.user.get('username')) {
this.showFirstStepsPanel();
}
},
showFirstStepsPanel: function() {
var firststepsPanel = Ext.create('Kort.view.overlay.firststeps.Panel');
console.log('no username given -> show first steps panel');
Ext.Viewport.add(firststepsPanel);
firststepsPanel.show();
},
prepareI18n: function() {
Ext.i18n.Bundle.configure({
bundle: 'Kort',
path: 'resources/i18n',
language: Kort.util.Config.getLanguage(),
noCache: true
});
},
configureMessageBox: function() {
// Override MessageBox default messages
Ext.define('Kort.MessageBox', {
override: 'Ext.MessageBox',
statics: {
YES : {text: Ext.i18n.Bundle.message('messagebox.yes'), itemId: 'yes', ui: 'action'},
NO : {text: Ext.i18n.Bundle.message('messagebox.no'), itemId: 'no'},
CANCEL: {text: Ext.i18n.Bundle.message('messagebox.cancel'), itemId: 'cancel'},
OKCANCEL: [
{text: Ext.i18n.Bundle.message('messagebox.ok'), itemId: 'ok', ui: 'action'},
{text: Ext.i18n.Bundle.message('messagebox.cancel'), itemId: 'cancel'}
],
YESNOCANCEL: [
{text: Ext.i18n.Bundle.message('messagebox.yes'), itemId: 'yes', ui: 'action'},
{text: Ext.i18n.Bundle.message('messagebox.no'), itemId: 'no'},
{text: Ext.i18n.Bundle.message('messagebox.cancel'), itemId: 'cancel'}
],
YESNO: [
{text: Ext.i18n.Bundle.message('messagebox.yes'), itemId: 'yes', ui: 'action'},
{text: Ext.i18n.Bundle.message('messagebox.no'), itemId: 'no'}
]
}
});
},
onUpdated: function() {
Kort.app.configureMessageBox();
Kort.app.prepareI18n();
Ext.Msg.defaultAllowedConfig.zIndex = Kort.util.Config.getZIndex().overlayOverlayPanel;
Ext.Msg.confirm(
Ext.i18n.Bundle.message('update.title'),
Ext.i18n.Bundle.message('update.message'),
function(buttonId) {
if (buttonId === 'yes') {
window.location.reload();
}
}
);
}
});