-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
173 lines (148 loc) · 4.1 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
'use strict'
require( 'babel-polyfill' )
global.config = require( '../json/config.json' )
global.resizeTimer
global.riot = require( 'riot' )
global.route = require( 'riot-route' )
const Slideout = require( 'slideout' )
global.request = require( 'superagent' )
global.L = require( 'leaflet' )
global.slideout = new Slideout( {
'panel': document.getElementById( 'panel' ),
'menu': document.getElementById( 'menu' ),
'padding': 256,
'tolerance': 70,
'side': 'right',
'touch': false
} )
const menu_item = require( '../tags/menu-item.tag' )
const menu_header = require( '../tags/menu-header.tag' )
const home_contents = require( '../tags/home-contents.tag' )
const main_contents = require( '../tags/main-contents.tag' )
const map = require( '../tags/map.tag' )
const error = require( '../tags/error.tag' )
if ( window.navigator.standalone ) {
if ( document.body.clientHeight < document.body.clientWidth ) {
document.querySelector( 'body' ).classList.remove( 'web-app' )
} else {
document.querySelector( 'body' ).classList.add( 'web-app' )
}
window.addEventListener( 'resize', () => {
if ( document.body.clientHeight < document.body.clientWidth ) {
document.querySelector( 'body' ).classList.remove( 'web-app' )
} else {
document.querySelector( 'body' ).classList.add( 'web-app' )
}
} )
}
riot.mount( menu_header )
document.querySelector( '.toggle' ).addEventListener( 'click', () => {
slideout.toggle()
} )
request
.get( config.endpoint + '/menu.json' )
.query( { v: new Date().getTime() } )
.set( 'Accept', 'application/json' )
.end( function( err, res ) {
riot.mount( menu_item, {
json: res.body
} )
} )
document.querySelector( '.back-to-home' ).addEventListener( 'click', () => {
slideout.close()
route( 'home' )
}, false )
if ( ! location.hash ) {
route( 'home' )
} else {
route( location.hash.replace( '#', '/' ) )
}
// All main contents are in this block.
const content_block = document.createElement( 'div' )
route( function( page, id ) {
if ( ! page ) {
return
}
const parent = document.querySelector( '#panel' )
while ( parent.firstChild ) {
parent.removeChild( parent.firstChild )
}
parent.appendChild( content_block )
// to ignore scroll when map is showing
document.body.classList.remove( 'fixed' )
document.querySelector( '#panel' ).style.height = 'auto'
riot.mount( content_block, error, {
message: 'Not Found.'
} )
const event = new CustomEvent( 'router-' + page, { 'detail': {
id: id
} } )
document.dispatchEvent( event )
slideout.close()
window.scrollTo( 0, 0 )
} )
route.start( true )
const router = function( page, callback ) {
document.addEventListener( 'router-' + page, ( e ) => {
const id = e.detail.id
content_block.innerHTML = ''
callback( id )
}, false )
}
const mount = function( tag, opts ) {
riot.mount( content_block, tag, opts )
}
router( 'home', () => {
mount( home_contents )
} )
router( 'data', ( id ) => {
if ( id.match( /^[a-f0-9]{32}$/ ) ) {
request
.get( config.endpoint + '/' + id + '.json' )
.set( 'Accept', 'application/json' )
.end( function( err, res ) {
if ( err ) {
mount( error, {
message: 'Not Found.'
} )
} else {
mount( main_contents, {
id: id,
data: res.body
} )
}
} )
} else {
mount( error, {
message: 'Not Found.'
} )
}
} )
router( 'map', ( id ) => {
if ( id.match( /^[a-f0-9]{32}\:[0-9]+$/ ) ) {
const args = id.split( ':' )
request
.get( config.endpoint + '/' + args[0] + '.json' )
.set( 'Accept', 'application/json' )
.end( function( err, res ) {
if ( err ) {
mount( error, {
message: 'Not Found.'
} )
} else {
const data = res.body[ args[1] ]
mount( map, {
layers: config.map.layers,
zoom: config.map.zoom,
lat: data.lat,
lng: data.lng,
markers: res.body
} )
}
} )
} else {
mount( error, {
message: 'Not Found.'
} )
}
} )