aboutsummaryrefslogtreecommitdiffstats
path: root/web/js2.js
blob: c386469845b25c51bce23e1236ba8412cbeedafb (plain)
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
/*
 * js.js
 * Neighbourhood Fix-It JavaScript
 * 
 * TODO
 * Get pins to disappear when they're not over the map!
 * Try and put back dragging? Not sure.
 * Selection of pin doesn't really need a server request, but I don't really care
 * 
 */


window.onload = onLoad;

// I love the global
var tile_x = 0;
var tile_y = 0;
var tilewidth = 254;
var tileheight = 254;

var in_drag;
function onLoad() {
    // var Log = new YAHOO.widget.LogReader();
    var compass = document.getElementById('compass');
    if (compass) {
        var points = compass.getElementsByTagName('a');
        points[1].onclick = function() { pan(0, tileheight); return false; };
        points[3].onclick = function() { pan(tilewidth, 0); return false; };
        points[4].onclick = function() { pan(-tilewidth, 0); return false; };
        points[6].onclick = function() { pan(0, -tileheight); return false; };
        points[0].onclick = function() { pan(tilewidth, tileheight); return false; };
        points[2].onclick = function() { pan(-tilewidth, tileheight); return false; };
        points[5].onclick = function() { pan(tilewidth, -tileheight); return false; };
        points[7].onclick = function() { pan(-tilewidth, -tileheight); return false; };
    }

    var form = document.getElementById('mapForm');
    if (form) {
        form.onsubmit = form_submit;

	var drag = document.getElementById('drag');
	var inputs = drag.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		inputs[i].onclick = drag_check;
	}
	
        var url = '/tilma/tileserver/10k-full-london/' + x + '-' + (x+5) + ',' + y + '-' + (y+5) + '/JSON';
        var req = mySociety.asyncRequest(url, urls_loaded);

        var map = document.getElementById('map');
        map.onmousedown = drag_start;
        document.onmouseout = drag_end_out;

    }
}

/*
    var targ = '';
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
*/

function form_submit() {
    this.x.value = x + 2;
    this.y.value = y + 2;
    return true;
}

function image_rotate(img, x, y) {
    if (x) {
        img.style.left = (img.offsetLeft + x*tilewidth) + 'px';
	//img.xx += x;
    }
    if (y) {
        img.style.top = (img.offsetTop + y*tileheight) + 'px';
	//img.yy += y;
    }
}

var myAnim;
function pan(x, y) {
    if (!myAnim || !myAnim.isAnimated()) {
        update_tiles(x, y, true);
        myAnim = new YAHOO.util.Motion('drag', { points:{by:[x,y]} }, 1, YAHOO.util.Easing.easeBoth);
	myAnim.animate();
    }
}

var drag_x = 0;
var drag_y = 0;
function update_tiles(dx, dy, noMove) {
    drag_x += dx;
    drag_y += dy;

    if (!noMove) {
        var drag = document.getElementById('drag');
        drag.style.left = drag_x + 'px';
        drag.style.top = drag_y + 'px';
    }

    var horizontal = 0;
    var vertical = 0;
    for (var i=0; i<6; i++) {
        for (var j=0; j<6; j++) {
	    var id = 't'+i+'.'+j;
	    var img = document.getElementById(id);
            if (drag_x + img.offsetLeft > 762) {
                img.src = '/i/grey.gif';
	        image_rotate(img, -6, 0);
		horizontal--;
	    } else if (drag_x + img.offsetLeft < -508) {
                img.src = '/i/grey.gif';
	        image_rotate(img, 6, 0);
		horizontal++;
	    }
	    if (drag_y + img.offsetTop > 762) {
                img.src = '/i/grey.gif';
	        image_rotate(img, 0, -6);
		vertical--;
	    } else if (drag_y + img.offsetTop < -508) {
                img.src = '/i/grey.gif';
	        image_rotate(img, 0, 6);
		vertical++;
	    }
	}
    }
    var horizontal = floor(horizontal/6);
    var vertical = floor(vertical/6);
    if (!horizontal && !vertical) return;
    x += horizontal;
    tile_x = mod((tile_x + horizontal), 6);
    y -= vertical;
    tile_y = mod((tile_y + vertical), 6);

    var url = '/tilma/tileserver/10k-full-london/' + x + '-' + (x+5) + ',' + y + '-' + (y+5) + '/JSON';
    var req = mySociety.asyncRequest(url, urls_loaded);
}

// Load 6x6 grid of tiles around current 2x2
function urls_loaded(o) {
    if (o.readyState != 4) return;
    var tiles = eval(o.responseText);
    var drag = document.getElementById('drag');
    for (var i=0; i<6; i++) {
        var ii = (i + tile_y) % 6;
        for (var j=0; j<6; j++) {
            var jj = (j + tile_x) % 6;
	    var id = 't'+ii+'.'+jj;
	    var xx = x+j;
	    var yy = y+5-i;
	    var img = document.getElementById(id);
	    if (img) {
		if (!img.galleryimg) { img.galleryimg = false; }
                img.src = 'http://tilma.mysociety.org/tileserver/10k-full-london/' + tiles[i][j];
	        img.name = 'tile_' + xx + '.' + yy;
	        //if (!img.xx) img.xx = xx;
	        //if (!img.yy) img.yy = yy;
	        continue;
	    }
	    img = document.createElement('input');
	    img.type = 'image';
            img.src = 'http://tilma.mysociety.org/tileserver/10k-full-london/' + tiles[i][j];
	    img.name = 'tile_' + xx + '.' + yy;
	    img.id = id;
	    img.onclick = drag_check;
	    img.style.position = 'absolute';
	    img.style.width = tilewidth + 'px';
	    img.style.height = tileheight + 'px';
	    img.style.top = ((ii-2)*tileheight) + 'px';
	    img.style.left = ((jj-2)*tilewidth) + 'px';
	    img.galleryimg = false;
	    //img.xx = xx;
	    //img.yy = yy;
	    img.alt = 'Loading...';
            drag.appendChild(img);
        }
    }
}

// Floor always closer to 0
function floor(n) {
    if (n>=0) return Math.floor(n);
    return Math.ceil(n);
}

// Mod always to positive result
function mod(m, n) {
    if (m>=0) return m % n;
    return (m % n) + n;
}

/* Dragging */

var last_mouse_pos = {};
var mouse_pos = {};
function drag_move(e) {
    if (!e) var e = window.event;
    //if (e.stopPropagation) e.stopPropagation();
    var point = get_posn(e);
    in_drag = true;
    last_mouse_pos = mouse_pos;
    mouse_pos = point;
    YAHOO.log('Updating with '+(mouse_pos.x-last_mouse_pos.x)+','+(mouse_pos.y-last_mouse_pos.y));
    update_tiles(mouse_pos.x-last_mouse_pos.x, mouse_pos.y-last_mouse_pos.y);
    return false;
}

function drag_check() {
    if (in_drag) {
        in_drag=false;
	return false;
    }
    return true;
}

function drag_start(e) {
    YAHOO.log('drag start');
    if (!e) var e = window.event;
    //if (e.stopPropagation) e.stopPropagation();
    var point = get_posn(e);
    mouse_pos = point;
    setCursor('move');
    document.onmousemove = drag_move;
    document.onmouseup = drag_end;
    return false;
}


function drag_end(e) {
    if (!e) var e = window.event;
    if (e.stopPropagation) e.stopPropagation();
    document.onmousemove = null;
    document.onmouseup = null;
    setCursor('crosshair');
    //if (in_drag) return false; // XXX I don't understand!
}

function drag_end_out(e) {
    if (!e) var e = window.event;
    //if (e.stopPropagation) e.stopPropagation();
    var relTarg;
    if (e.relatedTarget) { relTarg = e.relatedTarget; }
    else if (e.toElement) { relTarg = e.toElement; }
    if (!relTarg) {
        // mouse out to unknown = left the window?
        document.onmousemove = null;
        document.onmouseup = null;
        setCursor('crosshair');
    }
    return false;
}

function get_posn(e) {
    var posx, posy;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    } else if (e.clientX || e.clientY) {
        posx = e.clientX;
        if (document.documentElement && document.documentElement.scrollLeft) {
            posx += document.documentElement.scrollLeft;
        } else {
            posx += document.body.scrollLeft;
        }
        posy = e.clientY;
        if (document.documentElement && document.documentElement.scrollTop) {
            posy += document.documentElement.scrollTop;
        } else {
            posy += document.body.scrollTop;
        }
    }
    return { x:posx, y:posy };
}

function setCursor(s) {
    var drag = document.getElementById('drag');
    var inputs = drag.getElementsByTagName('input');
    for (var i=0; i<inputs.length; i++) {
        inputs[i].style.cursor = s;
    }
}