-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
226 lines (203 loc) · 6.58 KB
/
index.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
require('mapbox.js');
require('leaflet-osm');
var $ = require('jquery');
var fs = require('fs');
var qs = require('querystring').parse(window.location.search.slice(1));
var osmAuth = require('osm-auth');
var store = require('store');
var _ = require('underscore');
var route = require('./lib/route');
var Raven = require('raven-js');
L.mapbox.accessToken = 'pk.eyJ1IjoiYWFyb25saWRtYW4iLCJhIjoiNTVucTd0TSJ9.wVh5WkYXWJSBgwnScLupiQ';
Raven.config('https://e45a6671d39447fda045d873eba13840@app.getsentry.com/35914').install();
/* TODO is there anyway to keep the loaders completely seperate and only import
* them at runtime?
*
* Would this allow us to import external loaders, for example via a gist how to simplify
* - Don't declare any loaders in index.js
* - Pull them in when needed, they are fully bundled themselves
* - If they're missing, catch and display an error
* - Map titles and descriptions get put in the loaders themselves
*/
var keepright = require('./lib/loaders/keepright.js');
var osmigeom = require('./lib/loaders/osmigeom.js');
var unconnected = require('./lib/loaders/unconnected.js');
var tigerdelta = require('./lib/loaders/tigerdelta.js');
var templates = {
app: _(fs.readFileSync('./templates/app.html', 'utf8')).template(),
sidebar: _(fs.readFileSync('./templates/sidebar.html', 'utf8')).template(),
settings: _(fs.readFileSync('./templates/settings.html', 'utf8')).template()
};
var auth = osmAuth({
url: 'https://www.openstreetmap.org',
oauth_consumer_key: 'KcVfjQsvIdd7dPd1IFsYwrxIUd73cekN1QkqtSMd',
oauth_secret: 'K7dFg6rfIhMyvS8cPDVkKVi50XWyX0ibajHnbH8S',
landing: 'land.html'
});
// just use another qs
// loader=keepright&error=deadendoneway
// then all the possible titles are in the loader?
// do we just have a description.json ?
var tasks = {
'deadendoneway': {
title: 'Impossible one-ways',
loader: keepright
},
'impossibleangle': {
title: 'Kinks',
loader: keepright
},
'mixedlayer': {
title: 'Mixed layers',
loader: keepright
},
'nonclosedways': {
title: 'Broken polygons',
loader: keepright
},
'loopings': {
title: 'Loopings',
loader: keepright
},
'strangelayer': {
title: 'Strange layer',
loader: keepright
},
'highwayhighway': {
title: 'Highway intersects highway',
loader: keepright
},
'highwayfootpath': {
title: 'Highway intersects footpath',
loader: keepright
},
'highwayriverbank': {
title: 'Highway intersects water',
loader: keepright
},
'mispelledtags': {
title: 'Misspelled tags',
loader: keepright
},
'unconnectedmajor': {
title: 'Unconnected major',
loader: unconnected
},
'unconnectedminor1': {
title: 'Unconnected minor',
loader: unconnected
},
// 'duplicateways': {
// title: 'Duplicate Ways',
// loader: osmigeom
// },
// 'tigerdeltanamed': {
// title: 'Missing/misaligned TIGER',
// loader: tigerdelta
// }
};
var DEFAULT = 'deadendoneway';
window.current = {};
$('body').append(templates.app({
sidebar: store.get('sidebar')
}));
$('#sidebar').html(templates.sidebar({
tasks: tasks,
current: qs.error,
authed: isAuthenticated(),
avatar: store.get('avatar'),
username: store.get('username')
}));
$('#settings').html(templates.settings());
$('.js-login').on('click', function() {
auth.authenticate(function(err) {
auth.xhr({
method: 'GET',
path: '/api/0.6/user/details'
}, function(err, details) {
if (err) return console.log(err);
if (auth.authenticated()) {
details = details.getElementsByTagName('user')[0];
store.set('username', details.getAttribute('display_name'));
store.set('userid', details.getAttribute('id'));
store.set('avatar', details.getElementsByTagName('img')[0].getAttribute('href'));
$('#sidebar').html(templates.sidebar({
tasks: tasks,
current: qs.error,
authed: isAuthenticated(),
username: store.get('username'),
avatar: store.get('avatar')
}));
// this is a bit hacky, just refresh?
if ($('#editbar').length) $('#editbar').remove();
load();
}
});
});
return false;
});
$('.js-logout').on('click', function() {
auth.logout();
window.location.href = '';
return false;
});
$('.js-sidebar-toggle').on('click', function() {
var $el = $(this);
var $sidebar = $('#sidebar');
if ($el.is('.active')) {
$el.removeClass('active');
$sidebar.removeClass('active');
store.remove('sidebar');
} else {
$sidebar.addClass('active');
$el.addClass('active');
store.set('sidebar', true);
}
return false;
});
var $modeControls = $('.js-mode-controls').find('a');
$modeControls.on('click', function() {
var $el = $(this);
if (!$el.is('.active')) {
var mode = $el.data('mode');
$modeControls.removeClass('active');
$('.js-mode').removeClass('active');
$el.addClass('active');
$('#' + mode).addClass('active');
}
return false;
});
function isAuthenticated() {
return (auth.authenticated() && store.get('username') && store.get('userid'));
}
if (qs.error === undefined) {
var prepend = (Object.keys(qs).length) ? '&' : '?';
window.location.href = window.location.href + prepend + 'error=' + DEFAULT;
}
function load() {
/* TODO This task hash should be it's own module that can be called by core
* for core.mark('done')
*
* eventually, remove everything '.loader.'
* the loader will have everything in it, so calling tasks.smthng will take
* care of it
*
* allow all tasks reguardless of auth for now, demo purposes and such
* if (!current.loader.auth || (current.loader.auth && isAuthenticated())) {'
* // eventually task.auth will be an array with the different types of allowable authentication
* // this will correspond with details in localstorage
* current.loader.next();
* } else {
* return;
* }
*/
window.current.auth = isAuthenticated();
window.current.error = tasks[qs.error].title;
window.current.loader = tasks[qs.error].loader;
window.current.loader.next();
}
route({
auth: isAuthenticated(),
qs: qs,
callback: load
});