Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Initial implementation of link updating in URL bar

Mike McGann262c6b4f2d12
diff --git a/src/js/link/wv.link.model.js b/src/js/link/wv.link.model.js
index a20ea6084..96096ef17 100644
--- a/src/js/link/wv.link.model.js
+++ b/src/js/link/wv.link.model.js
@@ -9,19 +9,9 @@
* All Rights Reserved.
*/
-/**
- * @module wv.link
- */
var wv = wv || {};
wv.link = wv.link || {};
-/**
- * Permalink support.
- *
- * Note: Most of this will change in version 0.7.0.
- *
- * @class wv.link.model
- */
wv.link.model = wv.link.model || function(config) {
var self = {};
@@ -35,6 +25,8 @@ wv.link.model = wv.link.model || function(config) {
var mock = "";
var components = [];
+ self.events = wv.util.events();
+
var init = function() {
if ( config && config.parameters && config.parameters.shorten ) {
mock = "-" + config.parameters.shorten;
@@ -43,16 +35,12 @@ wv.link.model = wv.link.model || function(config) {
self.register = function(component) {
components.push(component);
+ if ( component.events ) {
+ component.events.any(triggerUpdate);
+ }
return self;
};
- /**
- * Returns a query string that is the concatenated value of all components.
- *
- * @method toPermalink
- * @return {string} The query string to use as a permalink with special
- * characters escaped.
- */
self.toQueryString = function() {
var state = {};
_.each(components, function(component) {
@@ -89,12 +77,6 @@ wv.link.model = wv.link.model || function(config) {
return strings.join("&");
};
- /**
- * Returns a permalink that is the concatenated value of all components.
- *
- * @method get
- * @return {string} The permalink with special characters escaped.
- */
self.get = function() {
var queryString = self.toQueryString();
var url = window.location.href;
@@ -103,15 +85,6 @@ wv.link.model = wv.link.model || function(config) {
return prefix + "?" + queryString;
};
- /**
- * Shortens the permalink using bit.ly.
- *
- * @method shorten
- * @param {String} [link] The link to shorten. If not specified, the
- * standard permalink for Worldview is used.
- * @return {JQuery.promise} a jQuery promise that will be notified
- * when the request is complete.
- */
self.shorten = function(link) {
if ( !link ) {
link = self.get();
@@ -151,6 +124,10 @@ wv.link.model = wv.link.model || function(config) {
return encoded;
};
+ var triggerUpdate = _.throttle(function() {
+ self.events.trigger("update")
+ }, 250, {trailing: true});
+
init();
return self;
diff --git a/src/js/link/wv.link.ui.js b/src/js/link/wv.link.ui.js
index 5f222cdd2..57da15f3f 100644
--- a/src/js/link/wv.link.ui.js
+++ b/src/js/link/wv.link.ui.js
@@ -9,17 +9,9 @@
* All Rights Reserved.
*/
-/**
- * @module wv.link
- */
var wv = wv || {};
wv.link = wv.link || {};
-/**
- * Undocumented.
- *
- * @class wv.link.ui
- */
wv.link.ui = wv.link.ui || function(models, config) {
var self = {};
@@ -55,11 +47,15 @@ wv.link.ui = wv.link.ui || function(models, config) {
wv.ui.closeDialog();
}
});
+
+ models.link.events.on("update", replaceHistoryState);
+ };
+
+ var replaceHistoryState = function() {
+ window.history.replaceState("", "@OFFICIAL_NAME@",
+ "?" + models.link.toQueryString());
};
- /**
- * @method show
- */
self.show = function() {
longLink = models.link.get();
link = longLink;
diff --git a/src/js/map/wv.map.model.js b/src/js/map/wv.map.model.js
index 1a0c8acf3..b5a50a52f 100644
--- a/src/js/map/wv.map.model.js
+++ b/src/js/map/wv.map.model.js
@@ -19,6 +19,12 @@ wv.map.model = wv.map.model || function(models, config) {
var self = {};
self.extent = null;
+ self.events = wv.util.events();
+
+ self.update = function(extent) {
+ self.extent = extent;
+ self.events.trigger("update", extent);
+ };
self.load = function(state) {
if ( state.v ) {
diff --git a/src/js/map/wv.map.ui.js b/src/js/map/wv.map.ui.js
index 7ade1a8e9..51537955f 100644
--- a/src/js/map/wv.map.ui.js
+++ b/src/js/map/wv.map.ui.js
@@ -313,7 +313,7 @@ wv.map.ui = wv.map.ui || function(models, config) {
};
var updateExtent = function() {
- models.map.extent = self.selected.getExtent().toArray();
+ models.map.update(self.selected.getExtent().toArray());
};
var createLayer = function(d, options) {
diff --git a/src/js/util/wv.util.events.js b/src/js/util/wv.util.events.js
index 00e255923..49f7362aa 100644
--- a/src/js/util/wv.util.events.js
+++ b/src/js/util/wv.util.events.js
@@ -9,50 +9,17 @@
* All Rights Reserved.
*/
-/**
- * @module wv.util
- */
var wv = wv || {};
wv.util = wv.util || {};
-/**
- * General event pump.
- *
- * * Register listeners with on
- * * Unregister listeners with off
- * * Fire events with trigger
- *
- * Example:
- *
- * var events = wv.util.events();
- * events.on("answerReceived", function(answer) {
- * console.log("The answer is", answer);
- * });
- *
- * events.trigger("answerReceived", 42);
- *
- * @class wv.util.events
- */
wv.util.events = wv.util.events || function() {
var self = {};
// Object of event types. Each event type is an array of listeners.
var events = {};
+ var allListeners = [];
- /**
- * Registers a listener for an event.
- *
- * @method on
- *
- * @param {string} event Type of event to register for.
- *
- * @param {function} callback Function called when the event of the given
- * type is fired. Arguments to the fire method are passed to the callback
- * function.
- *
- * @return {wv.util.events} this object useful for chaining.
- */
self.on = function(event, callback) {
if ( !callback ) {
throw new Error("No listener specified");
@@ -66,19 +33,6 @@ wv.util.events = wv.util.events || function() {
return self;
};
- /**
- * Unregisters a listener for an event.
- *
- * @method off
- *
- * @param {string} event Type of event to unregister for.
- *
- * @param {function} callback Function that was previously registered for
- * this type of event. If this function has not been registered, this
- * method does nothing.
- *
- * @return {wv.util.events} this object useful for chaining.
- */
self.off = function(event, callback) {
var listeners = events[event];
if ( listeners ) {
@@ -87,28 +41,25 @@ wv.util.events = wv.util.events || function() {
return self;
};
- /**
- * Notifies all listeners of an event.
- *
- * @method trigger
- *
- * @param {string} event Type of event to fire. If no listeners are
- * registered for this event, this method does nothing.
- *
- * @param {any} [arguments]* Additional arguments to pass back to the
- * function of each listener.
- *
- * @return {wv.util.events} this object useful for chaining.
- */
+ self.any = function(callback) {
+ if ( !callback ) {
+ throw new Error("No listener specified");
+ }
+ allListeners.push(callback);
+ };
+
self.trigger = function(event) {
var listeners = events[event];
- if ( !listeners ) {
+ if ( !listeners && !allListeners ) {
return;
}
var eventArguments = Array.prototype.slice.call(arguments, 1);
_.each(events[event], function(listener) {
listener.apply(self, eventArguments);
});
+ _.each(allListeners, function(listener) {
+ listener.apply(self, eventArguments);
+ });
return self;
};
diff --git a/src/js/wv.main.js b/src/js/wv.main.js
index a77df15a2..613912d90 100644
--- a/src/js/wv.main.js
+++ b/src/js/wv.main.js
@@ -55,7 +55,7 @@ $(function() {
wvx.config = config;
wv.debug.config(config);
-
+
// Load any additional scripts as needed
if ( config.scripts ) {
_.each(config.scripts, function(script) {
@@ -101,7 +101,7 @@ $(function() {
initialDate.setUTCDate(initialDate.getUTCDate() - 1);
}
}
-
+
// Models
var models = {
wv: {
@@ -172,7 +172,7 @@ $(function() {
ui.link = wv.link.ui(models, config);
ui.tour = wv.tour(models, ui, config);
ui.info = wv.ui.info(ui, config);
-
+
// Export for debugging
wvx.ui = ui;
@@ -216,7 +216,7 @@ $(function() {
$("input").blur();
}
});
-
+
// Console notifications
if ( wv.brand.release() ) {
console.info(wv.brand.NAME + " - Version " + wv.brand.VERSION +
@@ -228,7 +228,7 @@ $(function() {
errorReport();
//wv.debug.error(parameters);
-
+
models.wv.events.trigger("startup");
elapsed("done");
};