Newer
Older
/* $Id$ */
/**
* Drupal to Google Maps API bridge.
*/
// GMap overseer singleton
Drupal.gmap = new function() {
var _handlers = {};
var _maps = {};
/**
* Retrieve a map object for use by a non-widget.
* Use this if you need to be able to fire events against a certain map
* which you have the mapid for.
* Be a good GMap citizen! Remember to send change()s after modifying variables!
*/
this.getMap = function(mapid) {
return _maps[mapid];
};
this.unloadMap = function(mapid) {
delete _maps[mapid];
};
this.addHandler = function(handler,callback) {
if (!_handlers[handler]) {
_handlers[handler] = [];
}
_handlers[handler].push(callback);
};
this.globalChange = function(name,userdata) {
for (var mapid in Drupal.settings.gmap) {
_maps[mapid].change(name,-1,userdata);
}
};
this.setup = function() {
var initcallback = function(mapid) {
return (function() {
_maps[mapid].change("bootstrap_options",-1);
_maps[mapid].change("boot",-1);
_maps[mapid].change("init",-1);
// Send some changed events to fire up the rest of the initial settings..
_maps[mapid].change("maptypechange",-1);
_maps[mapid].change("controltypechange",-1);
_maps[mapid].change("alignchange",-1);
// Set ready to put the event system into action.
_maps[mapid].ready = true;
_maps[mapid].change("ready",-1);
});
};
if (Drupal.settings && Drupal.settings.gmap) {
var mapid = obj.id.split('-');
var instanceid = mapid.pop();
mapid.shift();
Brandon Bergren
committed
mapid = mapid.join('-');
var control = instanceid.replace(/\d+$/, '');
// Lazy init the map object.
if (!_maps[mapid]) {
_maps[mapid] = new Drupal.gmap.map(Drupal.settings.gmap[mapid]);
// Prepare the initialization callback.
var callback = initcallback(mapid);
setTimeout(callback, 0);
}
if (_handlers[control]) {
for (var i=0; i<_handlers[control].length; i++) {
_handlers[control][i].call(_maps[mapid], obj);
}
}
else {
// Element with wrong class?
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
}
}
};
}();
Drupal.gmap.factory = {};
Drupal.gmap.map = function(v) {
this.vars = v;
this.map = undefined;
this.ready = false;
var _bindings = {};
/**
* Register interest in a change.
*/
this.bind = function(name,callback) {
if (!_bindings[name]) {
_bindings[name] = [];
}
return _bindings[name].push(callback) - 1;
};
/**
* Change notification.
* Interested parties can act on changes.
*/
this.change = function(name,id,userdata) {
var c;
if (_bindings[name]) {
for (c = 0; c < _bindings[name].length; c++) {
if (c != id) {
_bindings[name][c](userdata);
}
}
}
if (name != 'all') {
this.change('all',-1,name,userdata);
}
};
/**
* Deferred change notification.
* This will cause a change notification to be tacked on to the *end* of the event queue.
*/
this.deferChange = function(name,id,userdata) {
var obj = this;
// This will move the function call to the end of the event loop.
setTimeout(function(){
obj.change(name,id,userdata);
}, 0);
};
};
////////////////////////////////////////
// Map widget //
////////////////////////////////////////
Drupal.gmap.addHandler('gmap',function(elem) {
var obj = this;
var _ib = {};
// Respond to incoming zooms
_ib.zoom = obj.bind("zoom",function(){
obj.map.setZoom(obj.vars.zoom);
});
// Respond to incoming moves
_ib.move = obj.bind("move", function(){
obj.map.panTo(new GLatLng(obj.vars.latitude,obj.vars.longitude));
});
// Respond to incoming map type changes
_ib.mtc = obj.bind("maptypechange", function() {
var i;
for (i = 0; i < obj.opts.mapTypeNames.length; i++) {
if (obj.opts.mapTypeNames[i] == obj.vars.maptype) {
obj.map.setMapType(obj.opts.mapTypes[i]);
break;
}
}
});
// Respond to incoming width changes.
_ib.width = obj.bind("widthchange",function(w){
obj.map.getContainer().style.width = w;
obj.map.checkResize();
});
// Send out outgoing width changes.
// N/A
// Respond to incoming height changes.
_ib.height = obj.bind("heightchange",function(h){
obj.map.getContainer().style.height = h;
obj.map.checkResize();
});
// Send out outgoing height changes.
// N/A
// Respond to incoming control type changes.
_ib.ctc = obj.bind("controltypechange",function() {
if(obj.currentcontrol) {
obj.map.removeControl(obj.currentcontrol);
}
if (obj.vars.controltype=='Micro') {obj.map.addControl(obj.currentcontrol = new GSmallZoomControl());}
if (obj.vars.controltype=='Small') {obj.map.addControl(obj.currentcontrol = new GSmallMapControl());}
if (obj.vars.controltype=='Large') {obj.map.addControl(obj.currentcontrol = new GLargeMapControl());}
});
// Send out outgoing control type changes.
// N/A
obj.bind("bootstrap_options", function() {
// Bootup options.
var opts = {}; // Object literal GMapOptions
obj.opts = opts;
// Null out the enabled types.
opts.mapTypes = [];
opts.mapTypeNames = [];
// Load google map types.
if (obj.vars.baselayers['Map']) {
opts.mapTypes.push(G_NORMAL_MAP);
opts.mapTypeNames.push('Map');
}
if (obj.vars.baselayers['Satellite']) {
opts.mapTypes.push(G_SATELLITE_MAP);
opts.mapTypeNames.push('Satellite');
}
if (obj.vars.baselayers['Hybrid']) {
opts.mapTypes.push(G_HYBRID_MAP);
opts.mapTypeNames.push('Hybrid');
}
if (obj.vars.baselayers['Physical']) {
opts.mapTypes.push(G_PHYSICAL_MAP);
opts.mapTypeNames.push('Physical');
}
});
obj.bind("boot", function() {
obj.map = new GMap2(elem, obj.opts);
});
obj.bind("init",function() {
var map = obj.map;
// Map type control
if (obj.vars.mtc == 'standard') {
map.addControl(new GMapTypeControl());
}
else if (obj.vars.mtc == 'hier') {
map.addControl(new GHierarchicalMapTypeControl());
}
else if (obj.vars.mtc == 'menu') {
map.addControl(new GMenuMapTypeControl());
}
if (obj.vars.behavior.overview) {
map.addControl(new GOverviewMapControl());
}
if (obj.vars.behavior.scale) {
map.addControl(new GScaleControl());
}
if (obj.vars.behavior.nodrag) {
map.disableDragging();
}
else if (!obj.vars.behavior.nokeyboard) {
obj._kbdhandler = new GKeyboardHandler(map);
}
if (obj.vars.extent) {
var c = obj.vars.extent;
var extent = new GLatLngBounds(new GLatLng(c[0][0], c[0][1]), new GLatLng(c[1][0], c[1][1]));
obj.vars.latitude = extent.getCenter().lat();
obj.vars.longitude = extent.getCenter().lng();
obj.vars.zoom = map.getBoundsZoomLevel(extent);
}
if (obj.vars.behavior.collapsehack) {
// Modify collapsable fieldsets to make maps check dom state when the resize handle
// is clicked. This may not necessarily be the correct thing to do in all themes,
// hence it being a behavior.
setTimeout(function(){
var r = function() {
map.checkResize();
map.setCenter(new GLatLng(obj.vars.latitude,obj.vars.longitude), obj.vars.zoom);
};
$(elem).parents('fieldset.collapsible').children('legend').children('a').click(r);
// Would be nice, but doesn't work.
//$(elem).parents('fieldset.collapsible').children('.fieldset-wrapper').scroll(r);
},0);
}
map.setCenter(new GLatLng(obj.vars.latitude,obj.vars.longitude), obj.vars.zoom);
Brandon Bergren
committed
if (!obj.vars.nocontzoom) {
map.enableDoubleClickZoom();
map.enableContinuousZoom();
}
if (!obj.vars.nomousezoom) {
map.enableScrollWheelZoom();
}
// Send out outgoing zooms
Brandon Bergren
committed
GEvent.addListener(map, "zoomend", function(oldzoom,newzoom) {
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
obj.vars.zoom = newzoom;
obj.change("zoom", _ib.zoom);
});
// Send out outgoing moves
GEvent.addListener(map,"moveend",function() {
var coord = map.getCenter();
obj.vars.latitude = coord.lat();
obj.vars.longitude = coord.lng();
obj.change("move", _ib.move);
});
// Send out outgoing map type changes.
GEvent.addListener(map,"maptypechanged",function() {
// If the map isn't ready yet, ignore it.
if (obj.ready) {
var type = map.getCurrentMapType();
var i;
for (i = 0; i < obj.opts.mapTypes.length; i++) {
if (obj.opts.mapTypes[i] == type) {
obj.vars.maptype = obj.opts.mapTypeNames[i];
}
}
obj.change("maptypechange", _ib.mtc);
}
});
});
});
////////////////////////////////////////
// Zoom widget //
////////////////////////////////////////
Drupal.gmap.addHandler('zoom', function(elem) {
var obj = this;
// Respond to incoming zooms
var binding = obj.bind("zoom", function(){
elem.value = obj.vars.zoom;
});
// Send out outgoing zooms
$(elem).change(function() {
obj.vars.zoom = parseInt(elem.value, 10);
obj.change("zoom", binding);
});
});
////////////////////////////////////////
// Latitude widget //
////////////////////////////////////////
Drupal.gmap.addHandler('latitude', function(elem) {
var obj = this;
// Respond to incoming movements.
var binding = obj.bind("move", function(){
elem.value = '' + obj.vars.latitude;
});
// Send out outgoing movements.
$(elem).change(function() {
obj.vars.latitude = Number(this.value);
obj.change("move", binding);
});
});
////////////////////////////////////////
// Longitude widget //
////////////////////////////////////////
Drupal.gmap.addHandler('longitude', function(elem) {
var obj = this;
// Respond to incoming movements.
var binding = obj.bind("move", function(){
elem.value = '' + obj.vars.longitude;
});
// Send out outgoing movements.
$(elem).change(function() {
obj.vars.longitude = Number(this.value);
obj.change("move", binding);
});
});
////////////////////////////////////////
// Latlon widget //
////////////////////////////////////////
Drupal.gmap.addHandler('latlon', function(elem) {
var obj = this;
// Respond to incoming movements.
var binding = obj.bind("move", function(){
elem.value = '' + obj.vars.latitude + ',' + obj.vars.longitude;
});
// Send out outgoing movements.
$(elem).change(function() {
var t = this.value.split(',');
obj.vars.latitude = Number(t[0]);
obj.vars.longitude = Number(t[1]);
obj.change("move", binding);
});
});
////////////////////////////////////////
// Maptype widget //
////////////////////////////////////////
Drupal.gmap.addHandler('maptype', function(elem) {
var obj = this;
// Respond to incoming movements.
var binding = obj.bind("maptypechange", function(){
elem.value = obj.vars.maptype;
});
// Send out outgoing movements.
$(elem).change(function() {
obj.vars.maptype = elem.value;
obj.change("maptypechange", binding);
});
});
(function() { // BEGIN CLOSURE
var re = /([0-9.]+)\s*(em|ex|px|in|cm|mm|pt|pc|%)/;
var normalize = function(str) {
var ar;
if ((ar = re.exec(str.toLowerCase()))) {
return ar[1] + ar[2];
}
return null;
};
////////////////////////////////////////
// Width widget //
////////////////////////////////////////
Drupal.gmap.addHandler('width', function(elem) {
var obj = this;
// Respond to incoming width changes.
var binding = obj.bind("widthchange", function(w){
elem.value = normalize(w);
});
// Send out outgoing width changes.
$(elem).change(function() {
var n;
if ((n = normalize(elem.value))) {
elem.value = n;
obj.change('widthchange', binding, n);
}
});
obj.bind('init',function(){
$(elem).change();
});
});
////////////////////////////////////////
// Height widget //
////////////////////////////////////////
Drupal.gmap.addHandler('height', function(elem) {
var obj = this;
// Respond to incoming height changes.
var binding = obj.bind("heightchange",function(h){
elem.value = normalize(h);
});
// Send out outgoing height changes.
$(elem).change(function() {
var n;
if ((n = normalize(elem.value))) {
elem.value = n;
obj.change('heightchange', binding, n);
}
});
obj.bind('init',function(){
$(elem).change();
});
});
})(); // END CLOSURE
////////////////////////////////////////
// Control type widget //
////////////////////////////////////////
Drupal.gmap.addHandler('controltype', function(elem) {
var obj = this;
// Respond to incoming height changes.
var binding = obj.bind("controltypechange", function(){
elem.value = obj.vars.controltype;
});
// Send out outgoing height changes.
$(elem).change(function() {
obj.vars.controltype = elem.value;
obj.change("controltypechange", binding);
});
});
Drupal.behaviors.GMap = function (context) {
$('.gmap-control:not(.gmap-processed)', context).addClass('gmap-processed').each(Drupal.gmap.setup);
};