-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
400 lines (316 loc) · 12.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// This is a little wonky, but it makes build a lot easier
// If confused, contact John
(function(){
var requireConfig = {
packages: [
{
"name": "utils",
"location": "lib",
"main": "utils/index.js"
}
, {
"name": "troller",
"location": "lib",
"main": "troller.js"
}
, {
"name": "api",
"location": "lib",
"main": "api.js"
}
, {
"name": "geo",
"location": "lib",
"main": "geo-location.js"
}
, {
"name": "user",
"location": "lib",
"main": "user.js"
}
, {
"name": "scrollWatcher",
"location": "lib",
"main": "scrollWatcher.js"
}
, {
"main": "config.js"
}
, {
"name": "components",
"location": "components",
"main": "index.js"
}
, {
"name": "models",
"location": "models",
"main": "index.js"
}
]
, map: {
'*': {
'css': 'jam/require-css/css', // or whatever the path to require-css and require-less are
'less': 'jam/require-less/less'
}
}
};
if (typeof require !== "undefined" && require.config){
require.config(requireConfig);
}
if (typeof exports !== "undefined" && typeof module !== "undefined")
module.exports = requireConfig;
else {
/**********************\
* MODULE STARTS HERE *
\**********************/
define(function(require){
require('lib/segment');
// Styles
require('less!styles/main');
// Helpers
require('./lib/hbt-helpers');
var
utils = require('utils')
, troller = require('troller')
, config = require('config')
, user = require('user')
, Router = require('lib/router')
, Components = require('components')
, scrollWatcher = require('scrollWatcher')
, Models = require('models')
// Pages provided to app-level page manager
, Pages = {
'explore': require('./pages/explore/index')
, 'collections': require('./pages/collections/index')
, 'explore-collection': require('./pages/explore-collection/index')
, 'settings': require('./pages/settings/index')
, 'my-punchcards': require('./pages/punch-cards/index')
, 'legal': require('./pages/legal/index')
, 'privacy': require('./pages/privacy/index')
, 'charities': require('./pages/charities/index')
, 'gb-for-businesses': require('./pages/gb-for-businesses/index')
, 'about-us': require('./pages/about-us/index')
, 'how-it-works': require('./pages/how-it-works/index')
, 'locations': require('./pages/locations/index')
, 'support': require('./pages/support/index')
, 'card-update': require('./pages/card-update/index')
, 'business': require('./pages/business/index')
, '404': require('./pages/404/index')
}
// Modals provided to app-level modal manager
, Modals = {
'product-details': require('./modals/product-details/index')
, 'add-new-collection': require('./modals/add-new-collection/index')
, 'enter-keytag': require('./modals/enter-keytag/index')
, 'edit-collection': require('./modals/edit-collection/index')
, 'update-password': require('./modals/update-password/index')
, 'punchcard': require('./modals/punchcard/index')
, 'location-details': require('./modals/location-details/index')
, 'login': require('./modals/login/index')
, 'register': require('./modals/register/index')
, 'forgot-password': require('./modals/forgot-password/index')
, 'reset-password': require('./modals/reset-password/index')
, 'complete-registration': require('./modals/complete-registration/index')
, 'alert': require('./modals/alert/index')
}
, app = {
init: function(){
// Initial call to session
utils.parallel({
session: function(done){ user.isLoggedIn(done); }
, domready: function(done){ utils.domready(function(){ done() }); }
}, function(error){
if (error) troller.error(error);
document.body.appendChild( utils.dom('<div id="main-loader" />')[0] )
document.body.appendChild( app.appView.el );
utils.startHistory();
troller.analytics.track('App Init');
// Load in File picker
require(['./lib/filepicker'], function(filepicker){});
});
app.appView = new Components.App.Main();
app.appView.providePages(Pages);
app.appView.provideModals(Modals);
app.appView.render();
app.loadTypekit();
// The only browser we support that doesn't support ajax is
// IE, so we can reasonably use this to check for IE
if (!utils.support.cors) app.loadIEModules();
user.on('auth', function(){ troller.analytics.track('Auth'); });
}
, changePage: function(page, options, callback){
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
options.transitionOptions = options.transitionOptions || {};
options.transitionOptions.onViewAAnimationComplete = function(viewA) {
window.scrollTo(0);
}
app.appView.changePage(page, options, callback);
troller.trigger('change-page', {page: page});
if (page != 'explore') app.hideBanner();
var title = app.appView.children.pages.pages[page].title || 'Goodybag'
app.setTitle( title );
var _options = { hash: utils.history.location.hash };
if (typeof options == 'object')
utils.extend( _options, options );
troller.analytics.track( 'Page.Loaded ' + title, _options );
troller.analytics.pageview();
}
, currentPage: function(){
return app.appView.children.pages.current;
}
, setTitle: function(title){
document.title = title;
}
, router: new Router()
, loadTypekit: function(){
var script = document.createElement('script');
script.src = config.typekitUrl;
script.async = true;
script.onload = function(e){
try { Typekit.load(); } catch(e) {}
};
document.getElementsByTagName('head')[0].appendChild(script);
}
, loadIEModules: function(){
require(config.ieOnlyModules, function(){});
}
, confirm: function(msg){
return confirm(msg);
}
, error: function(error, $el, action){
// No filepicker "they cancelled the modal error"
if (error.code && error.code == 104) return;
// No XHR errors - they probably just canceled the request
if (error.hasOwnProperty('status') && error.status == 0) return;
troller.analytics.track('error', error);
if (typeof $el == 'function'){
action = $el;
$el = null;
}
if (!action) action = troller.alert;
if (error){
var msg, detailsAdded = false;
if (typeof error == "object") {
// Filepicker.FPError 101: The user closed the dialog without picking a file
if (error.code == 101)
return;
msg = error.message || (window.JSON ? window.JSON.stringify(error) : error);
}
else
msg = error;
if (error.details){
msg += "\n";
if (typeof error.details == 'string')
msg += error.details
else {
for (var key in error.details){
if ($el) $el.find('.field-' + key).addClass('error');
if (error.details[key]){
msg += "\n" + app.getKeyNiceName(key) + ": " + error.details[key] + ", ";
detailsAdded = true;
}
}
if (detailsAdded) msg = msg.substring(0, msg.length -2);
}
}
action(msg, error);
return msg;
}
}
, alert: function(message, error) {
troller.modals.open('alert', { message: message, error: error });
}
, getKeyNiceName: function(key){
return config.niceNames[key] || key;
}
, spinner: new utils.Spinner(config.spinner)
, spin: function(el){
if (typeof el == 'string')
el = utils.dom(el)[0];
if (!el) el = utils.dom('#main-loader')[0];
if (el.id == 'main-loader') utils.dom(el).css('display', 'block');
app.spinner.spin(el);
}
, stopSpinning: function(){
utils.dom('#main-loader').css('display', 'none');
app.spinner.stop();
}
, openModal: function(modal, options, callback){
app.appView.children.modals.open(modal, options, callback);
var _options = { hash: utils.history.location.hash };
if (typeof options == 'object')
utils.extend( _options, options );
var trackingData = utils.clone(_options);
for (var key in trackingData) {
if (trackingData[key] instanceof utils.Model || trackingData[key] instanceof utils.Collection)
trackingData[key] = trackingData[key].toJSON();
}
troller.analytics.track( 'Modal.Opened ' + modal, trackingData );
}
, closeModal: function(modal, options){
return app.appView.children.modals.close(modal, options);
}
, promptUserLogin: function(options){
options = options || {};
troller.modals.open('login', function(error, modal){
if (error) return troller.error(error);
if (!options.fromUrlChange) return;
modal.once('close', function(){
// They had gone to at least one page before
if (app.currentPage()) utils.history.history.back();
// Just in case
else utils.history.navigate('/explore', { replace: true, trigger: true });
});
});
if (!app.currentPage()) troller.app.changePage('explore', {});
}
, showBanner: function(){
app.appView.showBanner();
app._bannerShown = true;
}
, hideBanner: function(){
app.appView.hideBanner();
}
, bannerShown: function(){
return !!app._bannerShown;
}
}
;
troller.add('app.setTitle', app.setTitle);
troller.add('app.changePage', app.changePage);
troller.add('app.currentPage', app.currentPage);
troller.add('app.showBanner', app.showBanner);
troller.add('app.hideBanner', app.hideBanner);
troller.add('app.bannerShown', app.bannerShown);
troller.add('app.error', app.error);
troller.add('error', app.error);
troller.add('confirm', app.confirm);
troller.add('alert', app.alert);
troller.add('spinner.spin', app.spin);
troller.add('spinner.stop', app.stopSpinning);
troller.add('modals.open', app.openModal);
troller.add('modals.close', app.closeModal);
troller.add('promptUserLogin', app.promptUserLogin);
troller.add('analytics', analytics);
// Make sure analytics reference is correct
var checkAnalytics = function(){
if (utils.isArray( analytics ))
return setTimeout( checkAnalytics, 10 );
// Apply queued events
for (var i = 0, l = troller.analytics.length; i < l; ++i){
analytics[ troller.analytics[i][0] ].apply(
analytics, troller.analytics[i].slice(1)
);
}
// Reset troller reference
troller.analytics = analytics;
};
checkAnalytics();
return app;
});
}
})();