-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-a-safety-map.js
330 lines (279 loc) · 11.3 KB
/
make-a-safety-map.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
var bboxmap;
$(document).ready(function()
{
$('input.required').each(behaveAsRequired);
$('#emergency-other').each(behaveAsRequired);
prepareEmergencyChoiceInput();
prepareBBoxMapInput();
prepareNoteTextarea();
prepareRecipientsListInput();
});
function behaveAsRequired(index, input)
{
var timeout = false;
function checkValue()
{
clearTimeout(timeout);
if($(input).attr('value')) {
$(input).removeClass('unacceptable');
$(input).addClass('good-to-go');
} else {
$(input).removeClass('good-to-go');
$(input).addClass('unacceptable');
}
}
function eventuallyCheckValue()
{
$(input).removeClass('unacceptable');
$(input).removeClass('good-to-go');
clearTimeout(timeout);
timeout = setTimeout(checkValue, 1000);
}
$(input).blur(checkValue);
$(input).change(checkValue);
$(input).bind('keyup', eventuallyCheckValue);
}
function prepareEmergencyChoiceInput()
{
/**
* Change to #emergency-chooser margin-top: -38px
*/
function chooseOther()
{
$('#emergency-other').show();
$('#emergency-chooser').css({ marginTop: -38 });
$('#emergency-select').css({ top: 38, zIndex: 1000 });
$('#emergency-select').animate({ top: 0 }, { duration: 'fast' });
$('#emergency-select').addClass('other');
$('#emergency-select').attr('name', '');
$('#emergency-other').attr('name', 'place[emergency]');
$('#emergency-other').focus();
}
/**
* Change to #emergency-chooser margin-top: 0
*/
function chooseNormal()
{
function onMoved()
{
$('#emergency-chooser').css({ marginTop: 0 });
$('#emergency-select').css({ top: 0, zIndex: 1000 });
$('#emergency-select').removeClass('other');
$('#emergency-other').hide();
$('#emergency-select').attr('name', 'place[emergency]');
$('#emergency-other').attr('name', '');
}
$('#emergency-select').css({ top: 0, zIndex: 1000 });
$('#emergency-select').animate({ top: 38 }, { duration: 'fast', complete: onMoved });
}
// deal with "Other (please specify)"
$('#emergency-select').change(function()
{
if($('#emergency-select option#otherplace').attr('selected')) {
return chooseOther();
} else if($('#emergency-other').attr('name') == 'place[emergency]') {
return chooseNormal();
}
}
);
}
function prepareBBoxMapInput()
{
var mm = com.modestmaps;
// pull an initial location from the DOM.
var lat0 = parseFloat($('input#loc0').attr('value')),
lon0 = parseFloat($('input#loc1').attr('value')),
initialLocation = (isNaN(lat0) || isNaN(lon0))
? new mm.Location(0, 0)
: new mm.Location(lat0, lon0);
// pull an initial extent from the DOM.
var lat1 = parseFloat($('input#bbox0').attr('value')),
lon1 = parseFloat($('input#bbox1').attr('value')),
lat2 = parseFloat($('input#bbox2').attr('value')),
lon2 = parseFloat($('input#bbox3').attr('value')),
initialExtentA = (isNaN(lat1) || isNaN(lon1))
? undefined
: new mm.Location(lat1, lon1),
initialExtentB = (isNaN(lat2) || isNaN(lon2))
? undefined
: new mm.Location(lat2, lon2),
initialExtent = (initialExtentA && initialExtentB)
? [initialExtentA, initialExtentB]
: undefined;
var provider = new mm.StamenProvider('toner-lite');
// make a map!
bboxmap = new mm.Map('bboxmap', provider, {x: 502, y: 320}, [ new AnyZoomHandler() ]);
if(initialExtent) {
bboxmap.setExtent(initialExtent);
} else {
bboxmap.setCenterZoom(initialLocation, 1);
}
add_roundy_corners(bboxmap);
function onMapChange() {
var extent = bboxmap.getExtent();
if (extent[0].lat-extent[1].lat > 0.001 && extent[1].lon-extent[0].lon > 0.001) {
var pos = $('#mark').position();
var loc;
if (pos) loc = bboxmap.pointLocation(new mm.Point(pos.left, pos.top));
else loc = mm.Location.interpolate(extent[0], extent[1], 0.5);
$("input#loc0").attr('value', loc.lat.toFixed(6));
$("input#loc1").attr('value', loc.lon.toFixed(6));
$("input#bbox0").attr('value', extent[0].lat.toFixed(6));
$("input#bbox1").attr('value', extent[0].lon.toFixed(6));
$("input#bbox2").attr('value', extent[1].lat.toFixed(6));
$("input#bbox3").attr('value', extent[1].lon.toFixed(6));
}
};
bboxmap.addCallback('drawn', onMapChange);
onMapChange();
var point = bboxmap.locationPoint(initialLocation),
left = point.x.toFixed(0) + 'px',
top = point.y.toFixed(0) + 'px',
$mark = $('<img id="mark" style="left: '+left+'; top: '+top+'; margin-left: -29px; margin-top: -29px; cursor: move; position: absolute; z-index: 1000;" src="../images/cross_round_lg.png">');
$mark.bind('mousedown', function(mde) {
var mousePosition = $mark.offset();
var mouseOffset = { left: mde.pageX - mousePosition.left, top: mde.pageY - mousePosition.top }
function onMarkMouseMove(mme) {
var newPos = { left: mme.pageX - mouseOffset.left, top: mme.pageY - mouseOffset.top };
$mark.offset(newPos);
var pos = $mark.position();
var loc = bboxmap.pointLocation(new mm.Point(pos.left, pos.top));
$("input#loc0").attr('value', loc.lat.toFixed(6));
$("input#loc1").attr('value', loc.lon.toFixed(6));
return false;
}
$(document.body).bind('mousemove', onMarkMouseMove);
$(document.body).one('mouseup', function() {
$(document.body).unbind('mousemove', onMarkMouseMove);
return false;
});
return false;
});
var $zoom = $('<p id="zoom" style="position:absolute; margin: 10px; padding: 0; right: 0; top: 0; z-index:2000;"></p>')
.append('<a href="#" id="zoomin" style="padding: 0px; margin-bottom: 5px; text-decoration: none;"><img border="0" src="../images/zoom_in_25px_recent.png"></a><br>')
.append('<a href="#" id="zoomout" style="padding: 0px; text-decoration: none;"><img border="0" src="../images/zoom_out_25px_short.png"></a>');
$('#bboxmap').append($mark);
$('#bboxmap').append($zoom);
$('#zoomin').bind('click', function() { bboxmap.zoomIn(); return false; });
$('#zoomout').bind('click', function() { bboxmap.zoomOut(); return false; });
var $search = $('<p style="position:absolute; margin: 0px; padding: 0px; z-index:2000;"></p>')
.append('<form id="searchform"><input type="text" id="search" name="search"><button type="submit">Search</button></form>');
$(document.body).append($search);
$(document.body).bind('search-needs-adjusting', function() {
var mapOffset = $('#bboxmap').offset();
mapOffset.left += 10;
mapOffset.top += 10;
$search.offset(mapOffset);
});
$(window).load(function() {
$(document.body).trigger('search-needs-adjusting');
});
setTimeout(function() {
$(document.body).trigger('search-needs-adjusting');
}, 100);
$('#searchform').bind('submit', function() {
var q = $('#search').attr('value');
var key = 'nbHkmO_V34FNW1fULoqJl3Ito52VgOmgeN_dFHoEM7vRb65sl3tpVWA4pvzI5mWDGw--';
var flags = 'JXG';
var realURL = 'http://where.yahooapis.com/geocode?'+
'q='+encodeURIComponent(q)+
'&flags='+encodeURIComponent(flags)+
'&appid='+encodeURIComponent(key);
$('#search').attr('disabled', 'disabled');
$.ajax({
dataType: 'jsonp',
url: 'slimjim.php?url='+encodeURIComponent(realURL)+'&callback=?',
success: onPlaceSearch
});
return false;
} );
function onPlaceSearch(rsp) {
$('#search').attr('disabled', '');
if (rsp && rsp.ResultSet && rsp.ResultSet.Results && rsp.ResultSet.Results.length) {
var result = rsp.ResultSet.Results[0];
if (result.boundingbox) {
bboxmap.setExtent([new mm.Location(result.boundingbox.north, result.boundingbox.west),
new mm.Location(result.boundingbox.south, result.boundingbox.east)]);
}
else {
bboxmap.setCenterZoom(new mm.Location(result.latitude, result.longitude), 12);
}
}
}
}
function prepareRecipientsListInput()
{
function addRecipient()
{
var html = ['<li>',
'name: <input type="text" name="recipients[99][name]" size="15">',
' ',
'email: <input type="email" name="recipients[99][email]" placeholder="e.g. [email protected]" size="35">',
' ',
'<a class="remove-recipient" href="#">━ Remove recipient<','/a>',
'<','/li>'];
var newLI = $(html.join(''));
$('#recipients').append(newLI);
newLI.show(onAdded);
function onAdded()
{
newLI.find('input').first().focus();
newLI.find('a.remove-recipient').live('click', removeRecipient);
newLI.find('input').each(behaveAsRequired);
}
renumberFormElements();
return false;
}
function removeRecipient()
{
var oldLI = $(this).parent('li');
function onRemoved()
{
oldLI.remove();
renumberFormElements();
}
oldLI.slideUp(onRemoved);
return false;
}
function renumberFormElements()
{
// renumber the form elements
$('#recipients li').each(function(i) {
$(this).find('input').attr('name', function(j, attr) {
return attr.replace(/\d+/, i);
});
});
}
$('#recipients a.remove-recipient').live('click', removeRecipient);
$('#add-recipient').live('click', addRecipient);
}
function measureNoteSize(text)
{
$('#dummynote').remove();
$('<p id="dummynote">'+text.replace(/\n/g,'<br>')+'</p>').appendTo(document.body);
// should ignore padding and margin and border according to http://api.jquery.com/height/
var height = $('#dummynote').height();
return height / 200; // 200px is a little bit too long, but would print OK
}
function prepareNoteTextarea()
{
// twitter style remaining character count
// (allow more chars to be typed but don't allow form submission, below)
var prevLength;
function onNoteChange() {
if (this.value.length == prevLength) return;
prevLength = this.value.length;
var measurement = measureNoteSize(this.value);
if (measurement < 1.0) {
$('#charcount').text("This note length looks OK! (" + (measurement*100).toFixed() + "% of limit)");
$('#charcount').removeClass('invalid');
}
else {
$('#charcount').text("Sorry, this note would be too long (" + (measurement*100).toFixed() + "% of limit)");
$('#charcount').addClass('invalid');
}
}
$('#fullnote').change(onNoteChange); // only fires onblur
$('#fullnote').keyup(onNoteChange); // fires with key strokes too
$('#fullnote').bind('input', onNoteChange); // html5 event, catches paste with mouse too
}