aboutsummaryrefslogtreecommitdiffstats
path: root/web/js2.js
blob: ca80c9b7bc500c5bea0e57bf6fc8045cb40af5da (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
/*
 * js.js
 * Neighbourhood Fix-It JavaScript
 * 
 * TODO
 * Updating tiles from server as they rotate around, can't seem to think of this straight...
 * Get pins to disappear when they're not over the map!
 * Try and put back dragging, I suppose
 * Selection of pin doesn't really need a server request, but I don't really care
 * Add callback to tileserver JSON, so don't have to use proxy
 * 
 */


window.onload = onLoad;

var x, y;
var tilewidth = 254;
var tileheight = 254;

function onLoad() {
    //var Log = new YAHOO.widget.LogReader();

    var compass = document.getElementById('compass');
    if (compass) {
        var points = document.getElementById('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');
    x = parseInt(form.x.value, 10) - 2; /* Bottom left corner X,Y */
    y = parseInt(form.y.value, 10) - 2;

// Load 6x6 grid of tiles around current 2x2
var urls_loaded = {
    success: function(o) {
        var tiles = eval(o.responseText);
	var drag = document.getElementById('drag');
	for (var i=0; i<6; i++) {
	    for (var j=0; j<6; j++) {
	        var id = i+'.'+j;
		var xx = x+j;
		var yy = y+5-i;
		var img = document.getElementById(id);
		if (img) {
		    if (!img.xx) img.xx = xx;
		    if (!img.yy) img.yy = yy;
		    if (!img.galleryimg) { img.galleryimg = false; }
		    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.style.position = 'absolute';
		img.style.width = tilewidth + 'px';
		img.style.height = tileheight + 'px';
		img.style.top = ((i-2)*tileheight) + 'px';
		img.style.left = ((j-2)*tilewidth) + 'px';
		img.galleryimg = false;
		img.xx = xx;
		img.yy = yy;
		img.alt = 'Loading...';
		drag.appendChild(img);
	    }
	}
    }

}
    var url = '/proxy.cgi?x=' + x + ';xm=' + (x+5) + ';y=' + y + ';ym=' + (y+5);
    var req = YAHOO.util.Connect.asyncRequest('GET', url, urls_loaded, null);
}

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);
	var pins = YAHOO.util.Dom.getElementsByClassName('pin', 'img', 'content');
        myAnim = new YAHOO.util.Motion('drag', { points:{by:[x,y]} }, 1, YAHOO.util.Easing.easeBoth);
	myAnim.animate();
	for (var p in pins) {
            var a = new YAHOO.util.Anim(pins[p], { right:{by:-x}, top:{by:y} }, 1, YAHOO.util.Easing.easeBoth);
            a.animate();
	}
    }
}

var drag_x = 0;
var drag_y = 0;
function update_tiles(dx, dy) {
    // XXX Ugh, so that the server gets sent the right map co-ords. Needs more thinking about!
/*    var form = document.getElementById('mapForm');
    form.x.value += floor(x/tilewidth);
    form.y.value += floor(y/tileheight);
*/

    drag_x += dx;
    drag_y += dy;

    var newcols = {};
    for (var i=0; i<6; i++) {
        for (var j=0; j<6; j++) {
	    var id = i+'.'+j;
	    var xx = x+j;
	    var yy = y+5-i;
	    var img = document.getElementById(id);
            if (drag_x + img.offsetLeft > 762) {
	        image_rotate(img, -6, 0);
	    } else if (drag_x + img.offsetLeft < -508) {
	        image_rotate(img, 6, 0);
	    } else if (drag_y + img.offsetTop > 762) {
	        image_rotate(img, 0, -6);
	    } else if (drag_y + img.offsetTop < -508) {
	        image_rotate(img, 0, 6);
	    }
	}
    }

// XXX: Now has to fetch correct tiles from tileserver and overwrite
// correct images... Perhaps this is not right way to do it?

/*
    for (j in newcols) {
	var new_column = {
	    success: function(o) {
	        var tiles = eval(o.responseText);
		for (var i=0; i<6; i++) {
		    var tile = tiles[i][0];
		    alert(tile);
		}
	    }
	};
        var url = '/proxy.cgi?x=' + xx + ';xm=' + xx + ';y=' + y + ';ym=' + (y+5);
        var req = YAHOO.util.Connect.asyncRequest('GET', url, new_column, null);
    }
*/

}

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