Remove goog.global (#5178)

* Remove goog.global

* Correct externs location

* Use Function('return this')

* Remove global externs
This commit is contained in:
Nicholas Latham
2016-04-08 09:36:48 +12:00
committed by Andreas Hocevar
parent 6efd3dab0b
commit cd6494149b
15 changed files with 54 additions and 51 deletions

View File

@@ -156,7 +156,7 @@ ol.control.FullScreen.prototype.setMap = function(map) {
goog.base(this, 'setMap', map); goog.base(this, 'setMap', map);
if (map) { if (map) {
this.listenerKeys.push( this.listenerKeys.push(
ol.events.listen(goog.global.document, goog.dom.fullscreen.EventType.CHANGE, ol.events.listen(ol.global.document, goog.dom.fullscreen.EventType.CHANGE,
this.handleFullScreenChange_, this) this.handleFullScreenChange_, this)
); );
} }

View File

@@ -207,7 +207,7 @@ ol.DeviceOrientation.prototype.handleTrackingChanged_ = function() {
if (ol.has.DEVICE_ORIENTATION) { if (ol.has.DEVICE_ORIENTATION) {
var tracking = this.getTracking(); var tracking = this.getTracking();
if (tracking && !this.listenerKey_) { if (tracking && !this.listenerKey_) {
this.listenerKey_ = ol.events.listen(goog.global, 'deviceorientation', this.listenerKey_ = ol.events.listen(ol.global, 'deviceorientation',
this.orientationChange_, this); this.orientationChange_, this);
} else if (!tracking && this.listenerKey_ !== null) { } else if (!tracking && this.listenerKey_ !== null) {
ol.events.unlistenByKey(this.listenerKey_); ol.events.unlistenByKey(this.listenerKey_);

View File

@@ -36,7 +36,7 @@ ol.dom.canUseCssTransform = (function() {
if (canUseCssTransform === undefined) { if (canUseCssTransform === undefined) {
goog.asserts.assert(document.body, goog.asserts.assert(document.body,
'document.body should not be null'); 'document.body should not be null');
goog.asserts.assert(goog.global.getComputedStyle, goog.asserts.assert(ol.global.getComputedStyle,
'getComputedStyle is required (unsupported browser?)'); 'getComputedStyle is required (unsupported browser?)');
var el = document.createElement('P'), var el = document.createElement('P'),
@@ -52,7 +52,7 @@ ol.dom.canUseCssTransform = (function() {
for (var t in transforms) { for (var t in transforms) {
if (t in el.style) { if (t in el.style) {
el.style[t] = 'translate(1px,1px)'; el.style[t] = 'translate(1px,1px)';
has2d = goog.global.getComputedStyle(el).getPropertyValue( has2d = ol.global.getComputedStyle(el).getPropertyValue(
transforms[t]); transforms[t]);
} }
} }
@@ -77,7 +77,7 @@ ol.dom.canUseCssTransform3D = (function() {
if (canUseCssTransform3D === undefined) { if (canUseCssTransform3D === undefined) {
goog.asserts.assert(document.body, goog.asserts.assert(document.body,
'document.body should not be null'); 'document.body should not be null');
goog.asserts.assert(goog.global.getComputedStyle, goog.asserts.assert(ol.global.getComputedStyle,
'getComputedStyle is required (unsupported browser?)'); 'getComputedStyle is required (unsupported browser?)');
var el = document.createElement('P'), var el = document.createElement('P'),
@@ -93,7 +93,7 @@ ol.dom.canUseCssTransform3D = (function() {
for (var t in transforms) { for (var t in transforms) {
if (t in el.style) { if (t in el.style) {
el.style[t] = 'translate3d(1px,1px,1px)'; el.style[t] = 'translate3d(1px,1px,1px)';
has3d = goog.global.getComputedStyle(el).getPropertyValue( has3d = ol.global.getComputedStyle(el).getPropertyValue(
transforms[t]); transforms[t]);
} }
} }
@@ -192,7 +192,7 @@ ol.dom.transformElement2D = function(element, transform, opt_precision) {
*/ */
ol.dom.outerWidth = function(element) { ol.dom.outerWidth = function(element) {
var width = element.offsetWidth; var width = element.offsetWidth;
var style = element.currentStyle || goog.global.getComputedStyle(element); var style = element.currentStyle || ol.global.getComputedStyle(element);
width += parseInt(style.marginLeft, 10) + parseInt(style.marginRight, 10); width += parseInt(style.marginLeft, 10) + parseInt(style.marginRight, 10);
return width; return width;
@@ -208,7 +208,7 @@ ol.dom.outerWidth = function(element) {
*/ */
ol.dom.outerHeight = function(element) { ol.dom.outerHeight = function(element) {
var height = element.offsetHeight; var height = element.offsetHeight;
var style = element.currentStyle || goog.global.getComputedStyle(element); var style = element.currentStyle || ol.global.getComputedStyle(element);
height += parseInt(style.marginTop, 10) + parseInt(style.marginBottom, 10); height += parseInt(style.marginTop, 10) + parseInt(style.marginBottom, 10);
return height; return height;

View File

@@ -135,12 +135,12 @@ ol.Geolocation.prototype.handleTrackingChanged_ = function() {
if (ol.has.GEOLOCATION) { if (ol.has.GEOLOCATION) {
var tracking = this.getTracking(); var tracking = this.getTracking();
if (tracking && this.watchId_ === undefined) { if (tracking && this.watchId_ === undefined) {
this.watchId_ = goog.global.navigator.geolocation.watchPosition( this.watchId_ = ol.global.navigator.geolocation.watchPosition(
this.positionChange_.bind(this), this.positionChange_.bind(this),
this.positionError_.bind(this), this.positionError_.bind(this),
this.getTrackingOptions()); this.getTrackingOptions());
} else if (!tracking && this.watchId_ !== undefined) { } else if (!tracking && this.watchId_ !== undefined) {
goog.global.navigator.geolocation.clearWatch(this.watchId_); ol.global.navigator.geolocation.clearWatch(this.watchId_);
this.watchId_ = undefined; this.watchId_ = undefined;
} }
} }

View File

@@ -34,7 +34,7 @@ ol.has.MAC = ua.indexOf('macintosh') !== -1;
* @type {number} * @type {number}
* @api stable * @api stable
*/ */
ol.has.DEVICE_PIXEL_RATIO = goog.global.devicePixelRatio || 1; ol.has.DEVICE_PIXEL_RATIO = ol.global.devicePixelRatio || 1;
/** /**
@@ -56,7 +56,7 @@ ol.has.CANVAS = ol.ENABLE_CANVAS && (
* @return {boolean} Canvas supported. * @return {boolean} Canvas supported.
*/ */
function() { function() {
if (!('HTMLCanvasElement' in goog.global)) { if (!('HTMLCanvasElement' in ol.global)) {
return false; return false;
} }
try { try {
@@ -81,7 +81,7 @@ ol.has.CANVAS = ol.ENABLE_CANVAS && (
* @type {boolean} * @type {boolean}
* @api stable * @api stable
*/ */
ol.has.DEVICE_ORIENTATION = 'DeviceOrientationEvent' in goog.global; ol.has.DEVICE_ORIENTATION = 'DeviceOrientationEvent' in ol.global;
/** /**
@@ -98,7 +98,7 @@ ol.has.DOM = ol.ENABLE_DOM;
* @type {boolean} * @type {boolean}
* @api stable * @api stable
*/ */
ol.has.GEOLOCATION = 'geolocation' in goog.global.navigator; ol.has.GEOLOCATION = 'geolocation' in ol.global.navigator;
/** /**
@@ -107,7 +107,7 @@ ol.has.GEOLOCATION = 'geolocation' in goog.global.navigator;
* @type {boolean} * @type {boolean}
* @api stable * @api stable
*/ */
ol.has.TOUCH = ol.ASSUME_TOUCH || 'ontouchstart' in goog.global; ol.has.TOUCH = ol.ASSUME_TOUCH || 'ontouchstart' in ol.global;
/** /**
@@ -115,7 +115,7 @@ ol.has.TOUCH = ol.ASSUME_TOUCH || 'ontouchstart' in goog.global;
* @const * @const
* @type {boolean} * @type {boolean}
*/ */
ol.has.POINTER = 'PointerEvent' in goog.global; ol.has.POINTER = 'PointerEvent' in ol.global;
/** /**
@@ -123,7 +123,7 @@ ol.has.POINTER = 'PointerEvent' in goog.global;
* @const * @const
* @type {boolean} * @type {boolean}
*/ */
ol.has.MSPOINTER = !!(goog.global.navigator.msPointerEnabled); ol.has.MSPOINTER = !!(ol.global.navigator.msPointerEnabled);
/** /**
@@ -142,7 +142,7 @@ ol.has.WEBGL;
var textureSize; var textureSize;
var /** @type {Array.<string>} */ extensions = []; var /** @type {Array.<string>} */ extensions = [];
if ('WebGLRenderingContext' in goog.global) { if ('WebGLRenderingContext' in ol.global) {
try { try {
var canvas = /** @type {HTMLCanvasElement} */ var canvas = /** @type {HTMLCanvasElement} */
(document.createElement('CANVAS')); (document.createElement('CANVAS'));

View File

@@ -91,10 +91,10 @@ ol.interaction.MouseWheelZoom.handleEvent = function(mapBrowserEvent) {
if (mapBrowserEvent.type == ol.events.EventType.WHEEL) { if (mapBrowserEvent.type == ol.events.EventType.WHEEL) {
delta = wheelEvent.deltaY; delta = wheelEvent.deltaY;
if (ol.has.FIREFOX && if (ol.has.FIREFOX &&
wheelEvent.deltaMode === goog.global.WheelEvent.DOM_DELTA_PIXEL) { wheelEvent.deltaMode === ol.global.WheelEvent.DOM_DELTA_PIXEL) {
delta /= ol.has.DEVICE_PIXEL_RATIO; delta /= ol.has.DEVICE_PIXEL_RATIO;
} }
if (wheelEvent.deltaMode === goog.global.WheelEvent.DOM_DELTA_LINE) { if (wheelEvent.deltaMode === ol.global.WheelEvent.DOM_DELTA_LINE) {
delta *= 40; delta *= 40;
} }
} else if (mapBrowserEvent.type == ol.events.EventType.MOUSEWHEEL) { } else if (mapBrowserEvent.type == ol.events.EventType.MOUSEWHEEL) {
@@ -113,8 +113,8 @@ ol.interaction.MouseWheelZoom.handleEvent = function(mapBrowserEvent) {
var duration = ol.MOUSEWHEELZOOM_TIMEOUT_DURATION; var duration = ol.MOUSEWHEELZOOM_TIMEOUT_DURATION;
var timeLeft = Math.max(duration - (Date.now() - this.startTime_), 0); var timeLeft = Math.max(duration - (Date.now() - this.startTime_), 0);
goog.global.clearTimeout(this.timeoutId_); ol.global.clearTimeout(this.timeoutId_);
this.timeoutId_ = goog.global.setTimeout( this.timeoutId_ = ol.global.setTimeout(
this.doZoom_.bind(this, map), timeLeft); this.doZoom_.bind(this, map), timeLeft);
mapBrowserEvent.preventDefault(); mapBrowserEvent.preventDefault();

View File

@@ -578,12 +578,12 @@ ol.Map.prototype.disposeInternal = function() {
ol.events.unlisten(this.viewport_, ol.events.EventType.MOUSEWHEEL, ol.events.unlisten(this.viewport_, ol.events.EventType.MOUSEWHEEL,
this.handleBrowserEvent, this); this.handleBrowserEvent, this);
if (this.handleResize_ !== undefined) { if (this.handleResize_ !== undefined) {
goog.global.removeEventListener(ol.events.EventType.RESIZE, ol.global.removeEventListener(ol.events.EventType.RESIZE,
this.handleResize_, false); this.handleResize_, false);
this.handleResize_ = undefined; this.handleResize_ = undefined;
} }
if (this.animationDelayKey_) { if (this.animationDelayKey_) {
goog.global.cancelAnimationFrame(this.animationDelayKey_); ol.global.cancelAnimationFrame(this.animationDelayKey_);
this.animationDelayKey_ = undefined; this.animationDelayKey_ = undefined;
} }
this.setTarget(null); this.setTarget(null);
@@ -1077,7 +1077,7 @@ ol.Map.prototype.handleTargetChanged_ = function() {
if (!targetElement) { if (!targetElement) {
goog.dom.removeNode(this.viewport_); goog.dom.removeNode(this.viewport_);
if (this.handleResize_ !== undefined) { if (this.handleResize_ !== undefined) {
goog.global.removeEventListener(ol.events.EventType.RESIZE, ol.global.removeEventListener(ol.events.EventType.RESIZE,
this.handleResize_, false); this.handleResize_, false);
this.handleResize_ = undefined; this.handleResize_ = undefined;
} }
@@ -1095,7 +1095,7 @@ ol.Map.prototype.handleTargetChanged_ = function() {
if (!this.handleResize_) { if (!this.handleResize_) {
this.handleResize_ = this.updateSize.bind(this); this.handleResize_ = this.updateSize.bind(this);
goog.global.addEventListener(ol.events.EventType.RESIZE, ol.global.addEventListener(ol.events.EventType.RESIZE,
this.handleResize_, false); this.handleResize_, false);
} }
} }
@@ -1199,7 +1199,7 @@ ol.Map.prototype.isRendered = function() {
*/ */
ol.Map.prototype.renderSync = function() { ol.Map.prototype.renderSync = function() {
if (this.animationDelayKey_) { if (this.animationDelayKey_) {
goog.global.cancelAnimationFrame(this.animationDelayKey_); ol.global.cancelAnimationFrame(this.animationDelayKey_);
} }
this.animationDelay_(); this.animationDelay_();
}; };
@@ -1211,7 +1211,7 @@ ol.Map.prototype.renderSync = function() {
*/ */
ol.Map.prototype.render = function() { ol.Map.prototype.render = function() {
if (this.animationDelayKey_ === undefined) { if (this.animationDelayKey_ === undefined) {
this.animationDelayKey_ = goog.global.requestAnimationFrame( this.animationDelayKey_ = ol.global.requestAnimationFrame(
this.animationDelay_); this.animationDelay_);
} }
}; };

View File

@@ -223,14 +223,14 @@ ol.MapBrowserEventHandler.prototype.emulateClick_ = function(pointerEvent) {
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
if (this.clickTimeoutId_ !== 0) { if (this.clickTimeoutId_ !== 0) {
// double-click // double-click
goog.global.clearTimeout(this.clickTimeoutId_); ol.global.clearTimeout(this.clickTimeoutId_);
this.clickTimeoutId_ = 0; this.clickTimeoutId_ = 0;
newEvent = new ol.MapBrowserPointerEvent( newEvent = new ol.MapBrowserPointerEvent(
ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, pointerEvent); ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, pointerEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
} else { } else {
// click // click
this.clickTimeoutId_ = goog.global.setTimeout(function() { this.clickTimeoutId_ = ol.global.setTimeout(function() {
this.clickTimeoutId_ = 0; this.clickTimeoutId_ = 0;
var newEvent = new ol.MapBrowserPointerEvent( var newEvent = new ol.MapBrowserPointerEvent(
ol.MapBrowserEvent.EventType.SINGLECLICK, this.map_, pointerEvent); ol.MapBrowserEvent.EventType.SINGLECLICK, this.map_, pointerEvent);

View File

@@ -13,25 +13,25 @@ goog.provide('ol.net');
* callback. Default is 'callback'. * callback. Default is 'callback'.
*/ */
ol.net.jsonp = function(url, callback, opt_errback, opt_callbackParam) { ol.net.jsonp = function(url, callback, opt_errback, opt_callbackParam) {
var script = goog.global.document.createElement('script'); var script = ol.global.document.createElement('script');
var key = 'olc_' + goog.getUid(callback); var key = 'olc_' + goog.getUid(callback);
function cleanup() { function cleanup() {
delete goog.global[key]; delete ol.global[key];
script.parentNode.removeChild(script); script.parentNode.removeChild(script);
} }
script.async = true; script.async = true;
script.src = url + (url.indexOf('?') == -1 ? '?' : '&') + script.src = url + (url.indexOf('?') == -1 ? '?' : '&') +
(opt_callbackParam || 'callback') + '=' + key; (opt_callbackParam || 'callback') + '=' + key;
var timer = goog.global.setTimeout(function() { var timer = ol.global.setTimeout(function() {
cleanup(); cleanup();
if (opt_errback) { if (opt_errback) {
opt_errback(); opt_errback();
} }
}, 10000); }, 10000);
goog.global[key] = function(data) { ol.global[key] = function(data) {
goog.global.clearTimeout(timer); ol.global.clearTimeout(timer);
cleanup(); cleanup();
callback(data); callback(data);
}; };
goog.global.document.getElementsByTagName('head')[0].appendChild(script); ol.global.document.getElementsByTagName('head')[0].appendChild(script);
}; };

View File

@@ -272,3 +272,6 @@ ol.inherits =
* @return {undefined} Nothing. * @return {undefined} Nothing.
*/ */
ol.nullFunction = function() {}; ol.nullFunction = function() {};
ol.global = Function('return this')();

View File

@@ -148,7 +148,7 @@ ol.pointer.TouchSource.prototype.removePrimaryPointer_ = function(inPointer) {
* @private * @private
*/ */
ol.pointer.TouchSource.prototype.resetClickCount_ = function() { ol.pointer.TouchSource.prototype.resetClickCount_ = function() {
this.resetId_ = goog.global.setTimeout( this.resetId_ = ol.global.setTimeout(
this.resetClickCountHandler_.bind(this), this.resetClickCountHandler_.bind(this),
ol.pointer.TouchSource.CLICK_COUNT_TIMEOUT); ol.pointer.TouchSource.CLICK_COUNT_TIMEOUT);
}; };
@@ -168,7 +168,7 @@ ol.pointer.TouchSource.prototype.resetClickCountHandler_ = function() {
*/ */
ol.pointer.TouchSource.prototype.cancelResetClickCount_ = function() { ol.pointer.TouchSource.prototype.cancelResetClickCount_ = function() {
if (this.resetId_ !== undefined) { if (this.resetId_ !== undefined) {
goog.global.clearTimeout(this.resetId_); ol.global.clearTimeout(this.resetId_);
} }
}; };
@@ -440,7 +440,7 @@ ol.pointer.TouchSource.prototype.dedupSynthMouse_ = function(inEvent) {
var lt = [t.clientX, t.clientY]; var lt = [t.clientX, t.clientY];
lts.push(lt); lts.push(lt);
goog.global.setTimeout(function() { ol.global.setTimeout(function() {
// remove touch after timeout // remove touch after timeout
ol.array.remove(lts, lt); ol.array.remove(lts, lt);
}, ol.pointer.TouchSource.DEDUP_TIMEOUT); }, ol.pointer.TouchSource.DEDUP_TIMEOUT);

View File

@@ -153,7 +153,7 @@ ol.proj.Projection = function(options) {
goog.asserts.assert(code !== undefined, goog.asserts.assert(code !== undefined,
'Option "code" is required for constructing instance'); 'Option "code" is required for constructing instance');
if (ol.ENABLE_PROJ4JS) { if (ol.ENABLE_PROJ4JS) {
var proj4js = ol.proj.proj4_ || goog.global['proj4']; var proj4js = ol.proj.proj4_ || ol.global['proj4'];
if (typeof proj4js == 'function' && projections[code] === undefined) { if (typeof proj4js == 'function' && projections[code] === undefined) {
var def = proj4js.defs(code); var def = proj4js.defs(code);
if (def !== undefined) { if (def !== undefined) {
@@ -684,7 +684,7 @@ ol.proj.get = function(projectionLike) {
var code = projectionLike; var code = projectionLike;
projection = ol.proj.projections_[code]; projection = ol.proj.projections_[code];
if (ol.ENABLE_PROJ4JS) { if (ol.ENABLE_PROJ4JS) {
var proj4js = ol.proj.proj4_ || goog.global['proj4']; var proj4js = ol.proj.proj4_ || ol.global['proj4'];
if (projection === undefined && typeof proj4js == 'function' && if (projection === undefined && typeof proj4js == 'function' &&
proj4js.defs(code) !== undefined) { proj4js.defs(code) !== undefined) {
projection = new ol.proj.Projection({code: code}); projection = new ol.proj.Projection({code: code});

View File

@@ -331,7 +331,7 @@ ol.reproj.Tile.prototype.load = function() {
}); });
if (leftToLoad === 0) { if (leftToLoad === 0) {
goog.global.setTimeout(this.reproject_.bind(this), 0); ol.global.setTimeout(this.reproject_.bind(this), 0);
} }
} }
}; };

View File

@@ -62,7 +62,7 @@ describe('ol.interaction.DragAndDrop', function() {
}); });
describe('#handleDrop_', function() { describe('#handleDrop_', function() {
var origFileReader = goog.global.FileReader; var origFileReader = ol.global.FileReader;
beforeEach(function() { beforeEach(function() {
FileReader = function() { FileReader = function() {
@@ -76,7 +76,7 @@ describe('ol.interaction.DragAndDrop', function() {
}); });
afterEach(function() { afterEach(function() {
goog.global.FileReader = origFileReader; ol.global.FileReader = origFileReader;
}); });
it('reads dropped files', function(done) { it('reads dropped files', function(done) {

View File

@@ -4,10 +4,10 @@ goog.provide('ol.test.net');
describe('ol.net', function() { describe('ol.net', function() {
describe('jsonp()', function() { describe('jsonp()', function() {
var head = goog.global.document.getElementsByTagName('head')[0]; var head = ol.global.document.getElementsByTagName('head')[0];
var origAppendChild = head.appendChild; var origAppendChild = head.appendChild;
var origCreateElement = document.createElement; var origCreateElement = document.createElement;
var origSetTimeout = goog.global.setTimeout; var origSetTimeout = ol.global.setTimeout;
var key, removeChild; var key, removeChild;
function createCallback(url, done) { function createCallback(url, done) {
@@ -27,7 +27,7 @@ describe('ol.net', function() {
if (arg == 'script') { if (arg == 'script') {
return element; return element;
} else { } else {
return origCreateElement.apply(goog.global.document, arguments); return origCreateElement.apply(ol.global.document, arguments);
} }
}; };
head.appendChild = function(el) { head.appendChild = function(el) {
@@ -36,13 +36,13 @@ describe('ol.net', function() {
removeChild: removeChild removeChild: removeChild
}; };
origSetTimeout(function() { origSetTimeout(function() {
goog.global[key](element.src); ol.global[key](element.src);
}, 0); }, 0);
} else { } else {
origAppendChild.apply(head, arguments); origAppendChild.apply(head, arguments);
} }
}; };
goog.global.setTimeout = function(fn, time) { ol.global.setTimeout = function(fn, time) {
origSetTimeout(fn, 100); origSetTimeout(fn, 100);
}; };
}); });
@@ -50,7 +50,7 @@ describe('ol.net', function() {
afterEach(function() { afterEach(function() {
document.createElement = origCreateElement; document.createElement = origCreateElement;
head.appendChild = origAppendChild; head.appendChild = origAppendChild;
goog.global.setTimeout = origSetTimeout; ol.global.setTimeout = origSetTimeout;
}); });
it('appends callback param to url, cleans up after call', function(done) { it('appends callback param to url, cleans up after call', function(done) {
@@ -70,7 +70,7 @@ describe('ol.net', function() {
expect.fail(); expect.fail();
} }
function errback() { function errback() {
expect(goog.global[key]).to.be(undefined); expect(ol.global[key]).to.be(undefined);
expect(removeChild.called).to.be(true); expect(removeChild.called).to.be(true);
done(); done();
} }