Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Prevent swiping off screen #1118

Benjamin Kinge5b8b35be9e0
diff --git a/web/js/map/compare/swipe.js b/web/js/map/compare/swipe.js
index 143a84210..5f754b0e5 100644
--- a/web/js/map/compare/swipe.js
+++ b/web/js/map/compare/swipe.js
@@ -1,5 +1,6 @@
import lodashEach from 'lodash/each';
import lodashRound from 'lodash/round';
+import util from '../../util/util';
var swipeOffset = null;
var line = null;
@@ -10,6 +11,7 @@ var events;
var mapCase;
var listenerObj;
var percentSwipe = null;
+const SWIPE_PADDING = 30;
export class Swipe {
constructor(
olMap,
@@ -56,6 +58,7 @@ var addLineOverlay = function(map) {
var iconEl = document.createElement('i');
var firstLabel = document.createElement('span');
var secondLabel = document.createElement('span');
+ var windowWidth = util.browser.dimensions[0];
mapCase = document.getElementById('wv-map');
firstLabel.className = 'ab-swipe-span left-label';
secondLabel.className = 'ab-swipe-span right-label';
@@ -88,6 +91,13 @@ var addLineOverlay = function(map) {
} else {
swipeOffset = evt.clientX;
}
+ // Prevent swiper from being swiped off screen
+ swipeOffset =
+ swipeOffset > windowWidth - SWIPE_PADDING
+ ? windowWidth - SWIPE_PADDING
+ : swipeOffset < SWIPE_PADDING
+ ? SWIPE_PADDING
+ : swipeOffset;
lineCaseEl.style.transform = 'translateX( ' + swipeOffset + 'px)';
diff --git a/web/js/util/browser.js b/web/js/util/browser.js
index fee8c018f..f8ab752ea 100644
--- a/web/js/util/browser.js
+++ b/web/js/util/browser.js
@@ -1,6 +1,6 @@
import $ from 'jquery';
-export default (function () {
+export default (function() {
var self = {};
/**
@@ -73,7 +73,7 @@ export default (function () {
self.mobileWidth = 740;
self.constrainedHeight = 320;
- var init = function () {
+ var init = function() {
var tests = self.tests;
if (tests.safari()) {
@@ -90,16 +90,17 @@ export default (function () {
self.webWorkers = tests.webWorkers();
self.localStorage = tests.localStorage();
self.small = tests.small();
+ self.dimensions = self.tests.getWindowDimensions();
self.history = tests.history();
self.touchDevice = tests.touchDevice();
- var onResize = function () {
+ var onResize = function() {
self.small = tests.small();
self.constrained = tests.constrained();
+ self.dimensions = self.tests.getWindowDimensions();
};
- $(window)
- .on('resize', onResize);
- $(function () {
+ $(window).on('resize', onResize);
+ $(function() {
onResize();
});
};
@@ -108,11 +109,11 @@ export default (function () {
// set in the init function. These functions are useful for testing.
self.tests = {};
- self.tests.navigator = function () {
+ self.tests.navigator = function() {
return navigator;
};
- self.tests.window = function (property) {
+ self.tests.window = function(property) {
// Some browsers throw an error when attempting to access restricted Window objects
try {
return window[property];
@@ -121,16 +122,15 @@ export default (function () {
}
};
- self.tests.safari = function () {
+ self.tests.safari = function() {
var navigator = self.tests.navigator();
if (/ Chrome\//.test(navigator.userAgent)) {
return false;
}
- return (/ Safari\//)
- .test(navigator.userAgent);
+ return / Safari\//.test(navigator.userAgent);
};
- self.tests.safariVersion = function () {
+ self.tests.safariVersion = function() {
var navigator = self.tests.navigator();
var version = navigator.userAgent.match(/ Version\/([^ ]+)/);
if (version) {
@@ -138,27 +138,28 @@ export default (function () {
}
};
- self.tests.ie = function () {
+ self.tests.ie = function() {
var navigator = self.tests.navigator();
if (/MSIE /.test(navigator.userAgent)) {
return true;
}
// IE 11
- return navigator.appName === 'Netscape' && /Trident/.test(navigator.userAgent);
+ return (
+ navigator.appName === 'Netscape' && /Trident/.test(navigator.userAgent)
+ );
};
- self.tests.firefox = function () {
- return navigator.userAgent.toLowerCase()
- .indexOf('firefox') > -1;
+ self.tests.firefox = function() {
+ return navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
};
- self.tests.touchDevice = function () {
+ self.tests.touchDevice = function() {
var el = document.createElement('div');
el.setAttribute('ontouchstart', 'return;');
return typeof el.ontouchstart === 'function';
};
- self.tests.ieVersion = function () {
+ self.tests.ieVersion = function() {
var navigator = self.tests.navigator();
// FIXME: linter says that the backslash in \d is unnessary which means
// that this regex is probably wrong.
@@ -168,18 +169,18 @@ export default (function () {
}
};
- self.tests.cors = function () {
+ self.tests.cors = function() {
if (self.tests.safari() && self.tests.safariVersion() <= 6) {
return false;
}
return !self.tests.ie();
};
- self.tests.webWorkers = function () {
+ self.tests.webWorkers = function() {
return self.tests.window('Worker');
};
- self.tests.localStorage = function () {
+ self.tests.localStorage = function() {
if (!self.tests.window('localStorage')) {
return false;
}
@@ -195,19 +196,19 @@ export default (function () {
}
};
- self.tests.small = function () {
+ self.tests.small = function() {
return self.tests.getWindowDimensions()[0] < self.mobileWidth;
};
- self.tests.getWindowDimensions = function () {
+ self.tests.getWindowDimensions = function() {
return [$(window).width(), $(window).height()];
};
- self.tests.constrained = function () {
+ self.tests.constrained = function() {
return self.tests.getWindowDimensions()[1] < self.constrainedHeight;
};
- self.tests.history = function () {
+ self.tests.history = function() {
return window.history && window.history.replaceState;
};