Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.widget.YahooMap");
12
dojo.require("dojo.event.*");
13
dojo.require("dojo.math");
14
dojo.require("dojo.widget.*");
15
dojo.require("dojo.widget.HtmlWidget");
16
(function () {
17
	var yappid = djConfig["yAppId"] || djConfig["yahooAppId"] || "dojotoolkit";
18
	if (!dojo.hostenv.post_load_) {
19
		if (yappid == "dojotoolkit") {
20
			dojo.debug("please provide a unique Yahoo App ID in djConfig.yahooAppId when using the map widget");
21
		}
22
		var tag = "<scr" + "ipt src='http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=" + yappid + "'></scri" + "pt>";
23
		if (!dj_global["YMap"]) {
24
			document.write(tag);
25
		}
26
	} else {
27
		dojo.debug("cannot initialize map system after the page has been loaded! Please either manually include the script block provided by Yahoo in your page or require() the YahooMap widget before onload has fired");
28
	}
29
})();
30
dojo.widget.defineWidget("dojo.widget.YahooMap", dojo.widget.HtmlWidget, function () {
31
	this.map = null;
32
	this.datasrc = "";
33
	this.data = [];
34
	this.width = 0;
35
	this.height = 0;
36
	this.controls = ["zoomlong", "maptype", "pan"];
37
}, {isContainer:false, templatePath:null, templateCssPath:null, findCenter:function (aPts) {
38
	var start = new YGeoPoint(37, -90);
39
	if (aPts.length == 0) {
40
		return start;
41
	}
42
	var minLat, maxLat, minLon, maxLon, cLat, cLon;
43
	minLat = maxLat = aPts[0].Lat;
44
	minLon = maxLon = aPts[0].Lon;
45
	for (var i = 0; i < aPts.length; i++) {
46
		minLat = Math.min(minLat, aPts[i].Lat);
47
		maxLat = Math.max(maxLat, aPts[i].Lat);
48
		minLon = Math.min(minLon, aPts[i].Lon);
49
		maxLon = Math.max(maxLon, aPts[i].Lon);
50
	}
51
	cLat = dojo.math.round((minLat + maxLat) / 2, 6);
52
	cLon = dojo.math.round((minLon + maxLon) / 2, 6);
53
	return new YGeoPoint(cLat, cLon);
54
}, setControls:function () {
55
	var methodmap = {maptype:"addTypeControl", pan:"addPanControl", zoomlong:"addZoomLong", zoomshort:"addZoomShort"};
56
	var c = this.controls;
57
	for (var i = 0; i < c.length; i++) {
58
		var controlMethod = methodmap[c[i].toLowerCase()];
59
		if (this.map[controlMethod]) {
60
			this.map[controlMethod]();
61
		}
62
	}
63
}, parse:function (table) {
64
	this.data = [];
65
	var h = table.getElementsByTagName("thead")[0];
66
	if (!h) {
67
		return;
68
	}
69
	var a = [];
70
	var cols = h.getElementsByTagName("td");
71
	if (cols.length == 0) {
72
		cols = h.getElementsByTagName("th");
73
	}
74
	for (var i = 0; i < cols.length; i++) {
75
		var c = cols[i].innerHTML.toLowerCase();
76
		if (c == "long") {
77
			c = "lng";
78
		}
79
		a.push(c);
80
	}
81
	var b = table.getElementsByTagName("tbody")[0];
82
	if (!b) {
83
		return;
84
	}
85
	for (var i = 0; i < b.childNodes.length; i++) {
86
		if (!(b.childNodes[i].nodeName && b.childNodes[i].nodeName.toLowerCase() == "tr")) {
87
			continue;
88
		}
89
		var cells = b.childNodes[i].getElementsByTagName("td");
90
		var o = {};
91
		for (var j = 0; j < a.length; j++) {
92
			var col = a[j];
93
			if (col == "lat" || col == "lng") {
94
				o[col] = parseFloat(cells[j].innerHTML);
95
			} else {
96
				o[col] = cells[j].innerHTML;
97
			}
98
		}
99
		this.data.push(o);
100
	}
101
}, render:function () {
102
	var pts = [];
103
	var d = this.data;
104
	for (var i = 0; i < d.length; i++) {
105
		var pt = new YGeoPoint(d[i].lat, d[i].lng);
106
		pts.push(pt);
107
		var icon = d[i].icon || null;
108
		if (icon) {
109
			icon = new YImage(icon);
110
		}
111
		var m = new YMarker(pt, icon);
112
		if (d[i].description) {
113
			m.addAutoExpand("<div>" + d[i].description + "</div>");
114
		}
115
		this.map.addOverlay(m);
116
	}
117
	var c = this.findCenter(pts);
118
	var z = this.map.getZoomLevel(pts);
119
	this.map.drawZoomAndCenter(c, z);
120
}, initialize:function (args, frag) {
121
	if (!YMap || !YGeoPoint) {
122
		dojo.raise("dojo.widget.YahooMap: The Yahoo Map script must be included in order to use this widget.");
123
	}
124
	if (this.datasrc) {
125
		this.parse(dojo.byId(this.datasrc));
126
	} else {
127
		if (this.domNode.getElementsByTagName("table")[0]) {
128
			this.parse(this.domNode.getElementsByTagName("table")[0]);
129
		}
130
	}
131
}, postCreate:function () {
132
	while (this.domNode.childNodes.length > 0) {
133
		this.domNode.removeChild(this.domNode.childNodes[0]);
134
	}
135
	if (this.width > 0 && this.height > 0) {
136
		this.map = new YMap(this.domNode, YAHOO_MAP_REG, new YSize(this.width, this.height));
137
	} else {
138
		this.map = new YMap(this.domNode);
139
	}
140
	this.setControls();
141
	this.render();
142
}});
143