busido /media/js/backbone.googlemaps.js

Language Javascript Lines 398
MD5 Hash 2014a158063a9538185f11ffdb979bda
Repository https://bitbucket.org/semion/busido View Raw File View Project SPDX
| Open JSFiddle
  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
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
/*!
 * Backbone.GoogleMaps
 * A Backbone JS layer for the GoogleMaps API
 * Copyright (c)2012 Edan Schwartz
 * Distributed under MIT license
 * https://github.com/eschwartz/backbone.googlemaps
 */
Backbone.GoogleMaps = (function(Backbone, _, $){
  var GoogleMaps = {};

  /**
   * GoogleMaps.Location
   * --------------------
   * Representing a lat/lng location on a map
   */
  GoogleMaps.Location = Backbone.Model.extend({
    constructor: function() {
      _.bindAll(this, 'select', 'deselect', 'toggleSelect', 'getLatlng');

      this.defaults = _.extend({}, {
        lat				: 0,
        lng				: 0,
        selected	: false,
        title			: ""
      }, this.defaults);

      Backbone.Model.prototype.constructor.apply(this, arguments);
    },

    select: function() {
      if(!this.get("selected")) {
        this.set("selected", true);
        this.trigger('selected', this);
        Backbone.Events.trigger("location:selected", this);
      }
    },

    deselect: function() {
      if(this.get("selected")) {
        this.set("selected", false);
        this.trigger("deselected", this);
        Backbone.Events.trigger('location:deselected', this);
      }
    },

    toggleSelect: function() {
      if(this.get("selected")) {
        this.deselect();
      }
      else {
        this.select();
      }
    },

    getLatlng: function() {
      return new google.maps.LatLng(this.get("lat"), this.get("lng"));
    }
  });

  /**
   * GoogleMaps.LocationCollection
   * ------------------------------
   * A collection of map locations
   */
  GoogleMaps.LocationCollection = Backbone.Collection.extend({
    model: GoogleMaps.Location,

    constructor: function() {
      Backbone.Collection.prototype.constructor.apply(this, arguments);

      // Deselect other models on model select
      // ie. Only a single model can be selected in a collection
      Backbone.Events.on("location:selected", function(selectedModel) {
        this.each(function(model) {
          if(selectedModel.cid !== model.cid) { model.deselect(); }
        });
      }, this);
    }
  });


  /**
   * GoogleMaps.MapView
   * ------------------
   * Base maps overlay view from which all other overlay views extend
   */
  GoogleMaps.MapView = Backbone.View.extend({
    // Hash of Google Map events
    // Events will be attached to this.gOverlay (google map or overlay)
    // eg `zoom_changed': 'handleZoomChange'
    mapEvents: {},

    overlayOptions: {},

    constructor: function() {
      _.bindAll(this);

      Backbone.View.prototype.constructor.apply(this, arguments);

      // Ensure map and API loaded
      if(!google || !google.maps) throw new Error("Google maps API is not loaded.");
      if(!this.options.map && !this.map) throw new Error("A map must be specified.");
      this.gOverlay = this.map = this.options.map || this.map;

      // Set this.overlay options
      this.overlayOptions || (this.overlayOptions = this.options.overlayOptions);
    },

    // Attach listeners to the this.gOverlay
    // From the `mapEvents` hash
    bindMapEvents: function(mapEvents) {
      mapEvents || (mapEvents = this.mapEvents);

      for(event in mapEvents) {
        var handler = mapEvents[event];
        google.maps.event.addListener(this.gOverlay, event, this[handler]);
      }
    },

    render: function() {
      if(this.beforeRender) { this.beforeRender(); }
      this.bindMapEvents();
      if(this.onRender) { this.onRender(); }

      return this;
    },

    // Clean up view
    // Remove overlay from map and remove event listeners
    close: function() {
      if(this.beforeClose) { this.beforeClose(); }
      google.maps.event.clearInstanceListeners(this.gOverlay);
      if(this.gOverlay.setMap) { this.gOverlay.setMap(null); }
      this.gOverlay = null;
      if(this.onClose) { this.onClose(); }
    }
  });

  /**
   * GoogleMaps.InfoWindow
   * ---------------------
   * View controller for a google.maps.InfoWindow overlay instance
   */
  GoogleMaps.InfoWindow = GoogleMaps.MapView.extend({
    constructor: function() {
      GoogleMaps.MapView.prototype.constructor.apply(this, arguments);

      _.bindAll(this, 'render', 'close');

      // Require a related marker instance
      if(!this.options.marker && !this.marker) throw new Error("A marker must be specified for InfoWindow view.");
      this.marker = this.options.marker || this.marker;

      // Set InfoWindow template
      this.template = this.template || this.options.template;

    },

    // Render
    render: function() {
      if(this.beforeRender) { this.beforeRender(); }
      GoogleMaps.MapView.prototype.render.apply(this, arguments);

      // Render element
      var tmpl = (this.template)? $(this.template).html(): '<h2><%=title %></h2>';
      this.$el.html(_.template(tmpl, this.model.toJSON()));

      // Create InfoWindow
      this.gOverlay = new google.maps.InfoWindow(_.extend({
        content: this.$el[0]
      }, this.overlayOptions));

      // Display InfoWindow on map
      this.gOverlay.open(this.map, this.marker);

      if(this.onRender) { this.onRender(); }

      return this;
    },

    // Close and delete window, and clean up view
    close: function() {
      if(this.beforeClose) { this.beforeClose(); }

      GoogleMaps.MapView.prototype.close.apply(this, arguments);

      if(this.onClose) { this.onClose(); }

      return this;
    }
  });


  /**
   * GoogleMaps.MarkerView
   * ---------------------
   * View controller for a marker overlay
   */
  GoogleMaps.MarkerView = GoogleMaps.MapView.extend({
    // Set associated InfoWindow view
    infoWindow: GoogleMaps.InfoWindow,

    constructor: function() {
      GoogleMaps.MapView.prototype.constructor.apply(this, arguments);

      _.bindAll(this, 'render', 'close', 'openDetail', 'closeDetail', 'toggleSelect');

      // Ensure model
      if(!this.model) throw new Error("A model must be specified for a MarkerView");

      // Instantiate marker, with user defined properties
      this.gOverlay = new google.maps.Marker(_.extend({
        position: this.model.getLatlng(),
        map: this.map,
        title: this.model.title,
        animation: google.maps.Animation.DROP,
        visible: false										// hide, until render
      }, this.overlayOptions));

      // Add default mapEvents
      _.extend(this.mapEvents, {
        'click'	: 'toggleSelect'							// Select model on marker click
      });

      // Show detail view on model select
      this.model.on("selected", this.openDetail, this);
      this.model.on("deselected", this.closeDetail, this);
      this.model.on("change:lat change:lng", this.refreshOverlay, this);

      // Sync location model lat/lng with marker position
      this.bindMapEvents({
        'position_changed': 'updateModelPosition'
      });
    },

    // update overlay position if lat or lng change
    refreshOverlay: function() {
      // Only update overlay if we're not already in sync
      // Otherwise we end up in an endless loop of
      // update model <--eventhandler--> update overlay
      if(!this.model.getLatlng().equals(this.gOverlay.getPosition())) {
        this.gOverlay.setOptions({
          position: this.model.getLatlng()
        });
      }
    },

    updateModelPosition: function() {
      var newPosition = this.gOverlay.getPosition();

      // Only update model if we're not already in sync
      // Otherwise we end up in an endless loop of
      // update model <--eventhandler--> update overlay
      if(!this.model.getLatlng().equals(newPosition)) {
        this.model.set({
          lat: newPosition.lat(),
          lng: newPosition.lng()
        });
      }
    },

    toggleSelect: function() {
      this.model.toggleSelect();
    },

    // Show the google maps marker overlay
    render: function() {
      if(this.beforeRender) { this.beforeRender(); }

      GoogleMaps.MapView.prototype.render.apply(this, arguments);
      this.gOverlay.setVisible(true);

      if(this.onRender) { this.onRender(); }

      return this;
    },

    close: function() {
      if(this.beforeClose) { this.beforeClose(); }
      this.closeDetail();
      GoogleMaps.MapView.prototype.close.apply(this, arguments);
      this.model.off();

      if(this.onClose) { this.onClose() }

      return this;
    },

    openDetail: function() {
      this.detailView = new this.infoWindow({
        model: this.model,
        map: this.map,
        marker: this.gOverlay
      });
      this.detailView.render();
    },

    closeDetail: function() {
      if(this.detailView) {
        this.detailView.close();
        this.detailView = null;
      }
    }
  });


  /**
   * GoogleMaps.MarkerCollectionView
   * -------------------------------
   * Collection of MarkerViews
   */
  GoogleMaps.MarkerCollectionView = Backbone.View.extend({
    markerView: GoogleMaps.MarkerView,

    markerViewChildren: {},

    constructor: function() {
      Backbone.View.prototype.constructor.apply(this, arguments);

      _.bindAll(this, 'render', 'closeChildren', 'closeChild', 'addChild', 'refresh', 'close');

      // Ensure map property
      if(!this.options.map && !this.map) throw new Error("A map must be specified on MarkerCollectionView instantiation");
      this.map || (this.map = this.options.map);

      // Bind to collection
      this.collection.on("reset", this.refresh, this);
      this.collection.on("add", this.addChild, this);
      this.collection.on("remove", this.closeChild, this);

      google.maps.event.addListener(this.map, 'idle', this.handleIdleMap);
    },

    // Render MarkerViews for all models in collection
    render: function(collection) {
      var collection = collection || this.collection;

      if(this.beforeRender) { this.beforeRender(); }

      // Create marker views for each model
      collection.each(this.addChild);

      if(this.onRender) { this.onRender(); }

      return this;
    },

    // Close all child MarkerViews
    closeChildren: function() {
      for(cid in this.markerViewChildren) {
        this.closeChild(this.markerViewChildren[cid]);
      }
    },

    closeChild: function(child) {
      // Param can be child's model, or child view itself
      var childView = (child instanceof Backbone.Model)? this.markerViewChildren[child.cid]: child;

      childView.close();
      delete this.markerViewChildren[childView.model.cid];
    },

    // Add a MarkerView and render
    addChild: function(childModel) {
      var markerView = new this.markerView({
        model: childModel,
        map: this.map
      });

      this.markerViewChildren[childModel.cid] = markerView;

      markerView.render();
    },

    refresh: function() {
      this.closeChildren();
      this.render();
    },

    // Close all child MarkerViews
    close: function() {
      this.closeChildren();
      this.collection.off();
    },

    getMapBounds: function(){
      return this.map.getCenter().toUrlValue(10);
    },

    handleIdleMap: function(){
      console.log("idle fired");
    }
  })


  return GoogleMaps;
})(Backbone, _, window.jQuery || window.Zepto || window.ender);
Back to Top