Adding mapbox-gl branch

This commit is contained in:
Andreas Hocevar
2015-03-16 18:50:27 +01:00
parent 7985f030fa
commit 57ee7f52fd
3109 changed files with 943365 additions and 0 deletions
@@ -0,0 +1,211 @@
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Basic animation controls.
*
* @author arv@google.com (Erik Arvidsson)
*/
goog.provide('goog.fx.anim');
goog.provide('goog.fx.anim.Animated');
goog.require('goog.async.AnimationDelay');
goog.require('goog.async.Delay');
goog.require('goog.object');
/**
* An interface for programatically animated objects. I.e. rendered in
* javascript frame by frame.
*
* @interface
*/
goog.fx.anim.Animated = function() {};
/**
* Function called when a frame is requested for the animation.
*
* @param {number} now Current time in milliseconds.
*/
goog.fx.anim.Animated.prototype.onAnimationFrame;
/**
* Default wait timeout for animations (in milliseconds). Only used for timed
* animation, which uses a timer (setTimeout) to schedule animation.
*
* @type {number}
* @const
*/
goog.fx.anim.TIMEOUT = goog.async.AnimationDelay.TIMEOUT;
/**
* A map of animations which should be cycled on the global timer.
*
* @type {Object<number, goog.fx.anim.Animated>}
* @private
*/
goog.fx.anim.activeAnimations_ = {};
/**
* An optional animation window.
* @type {Window}
* @private
*/
goog.fx.anim.animationWindow_ = null;
/**
* An interval ID for the global timer or event handler uid.
* @type {goog.async.Delay|goog.async.AnimationDelay}
* @private
*/
goog.fx.anim.animationDelay_ = null;
/**
* Registers an animation to be cycled on the global timer.
* @param {goog.fx.anim.Animated} animation The animation to register.
*/
goog.fx.anim.registerAnimation = function(animation) {
var uid = goog.getUid(animation);
if (!(uid in goog.fx.anim.activeAnimations_)) {
goog.fx.anim.activeAnimations_[uid] = animation;
}
// If the timer is not already started, start it now.
goog.fx.anim.requestAnimationFrame_();
};
/**
* Removes an animation from the list of animations which are cycled on the
* global timer.
* @param {goog.fx.anim.Animated} animation The animation to unregister.
*/
goog.fx.anim.unregisterAnimation = function(animation) {
var uid = goog.getUid(animation);
delete goog.fx.anim.activeAnimations_[uid];
// If a timer is running and we no longer have any active timers we stop the
// timers.
if (goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
goog.fx.anim.cancelAnimationFrame_();
}
};
/**
* Tears down this module. Useful for testing.
*/
// TODO(nicksantos): Wow, this api is pretty broken. This should be fixed.
goog.fx.anim.tearDown = function() {
goog.fx.anim.animationWindow_ = null;
goog.dispose(goog.fx.anim.animationDelay_);
goog.fx.anim.animationDelay_ = null;
goog.fx.anim.activeAnimations_ = {};
};
/**
* Registers an animation window. This allows usage of the timing control API
* for animations. Note that this window must be visible, as non-visible
* windows can potentially stop animating. This window does not necessarily
* need to be the window inside which animation occurs, but must remain visible.
* See: https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame.
*
* @param {Window} animationWindow The window in which to animate elements.
*/
goog.fx.anim.setAnimationWindow = function(animationWindow) {
// If a timer is currently running, reset it and restart with new functions
// after a timeout. This is to avoid mismatching timer UIDs if we change the
// animation window during a running animation.
//
// In practice this cannot happen before some animation window and timer
// control functions has already been set.
var hasTimer =
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive();
goog.dispose(goog.fx.anim.animationDelay_);
goog.fx.anim.animationDelay_ = null;
goog.fx.anim.animationWindow_ = animationWindow;
// If the timer was running, start it again.
if (hasTimer) {
goog.fx.anim.requestAnimationFrame_();
}
};
/**
* Requests an animation frame based on the requestAnimationFrame and
* cancelRequestAnimationFrame function pair.
* @private
*/
goog.fx.anim.requestAnimationFrame_ = function() {
if (!goog.fx.anim.animationDelay_) {
// We cannot guarantee that the global window will be one that fires
// requestAnimationFrame events (consider off-screen chrome extension
// windows). Default to use goog.async.Delay, unless
// the client has explicitly set an animation window.
if (goog.fx.anim.animationWindow_) {
// requestAnimationFrame will call cycleAnimations_ with the current
// time in ms, as returned from goog.now().
goog.fx.anim.animationDelay_ = new goog.async.AnimationDelay(
function(now) {
goog.fx.anim.cycleAnimations_(now);
}, goog.fx.anim.animationWindow_);
} else {
goog.fx.anim.animationDelay_ = new goog.async.Delay(function() {
goog.fx.anim.cycleAnimations_(goog.now());
}, goog.fx.anim.TIMEOUT);
}
}
var delay = goog.fx.anim.animationDelay_;
if (!delay.isActive()) {
delay.start();
}
};
/**
* Cancels an animation frame created by requestAnimationFrame_().
* @private
*/
goog.fx.anim.cancelAnimationFrame_ = function() {
if (goog.fx.anim.animationDelay_) {
goog.fx.anim.animationDelay_.stop();
}
};
/**
* Cycles through all registered animations.
* @param {number} now Current time in milliseconds.
* @private
*/
goog.fx.anim.cycleAnimations_ = function(now) {
goog.object.forEach(goog.fx.anim.activeAnimations_, function(anim) {
anim.onAnimationFrame(now);
});
if (!goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
goog.fx.anim.requestAnimationFrame_();
}
};
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
Author: arv@google.com (Erik Arvidsson)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
Closure Unit Tests - goog.fx
</title>
<script src="../../base.js">
</script>
<script>
goog.require('goog.fx.animTest');
</script>
<style>
</style>
</head>
<body>
</body>
</html>
@@ -0,0 +1,218 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.fx.animTest');
goog.setTestOnly('goog.fx.animTest');
goog.require('goog.async.AnimationDelay');
goog.require('goog.async.Delay');
goog.require('goog.events');
goog.require('goog.functions');
goog.require('goog.fx.Animation');
goog.require('goog.fx.anim');
goog.require('goog.object');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.require('goog.userAgent');
var clock, replacer;
function setUpPage() {
clock = new goog.testing.MockClock(true);
}
function tearDownPage() {
clock.dispose();
}
function setUp() {
replacer = new goog.testing.PropertyReplacer();
}
function tearDown() {
replacer.reset();
goog.fx.anim.tearDown();
}
function testDelayWithMocks() {
goog.fx.anim.setAnimationWindow(null);
registerAndUnregisterAnimationWithMocks(goog.async.Delay);
}
function testAnimationDelayWithMocks() {
goog.fx.anim.setAnimationWindow(window);
registerAndUnregisterAnimationWithMocks(goog.async.AnimationDelay);
}
/**
* @param {!Function} delayType The constructor for Delay or AnimationDelay.
* The methods will be mocked out.
*/
function registerAndUnregisterAnimationWithMocks(delayType) {
var timerCount = 0;
replacer.set(delayType.prototype, 'start', function() {
timerCount++;
});
replacer.set(delayType.prototype, 'stop', function() {
timerCount--;
});
replacer.set(delayType.prototype, 'isActive', function() {
return timerCount > 0;
});
var forbiddenDelayType = delayType == goog.async.AnimationDelay ?
goog.async.Delay : goog.async.AnimationDelay;
replacer.set(forbiddenDelayType.prototype,
'start', goog.functions.error());
replacer.set(forbiddenDelayType.prototype,
'stop', goog.functions.error());
replacer.set(forbiddenDelayType.prototype,
'isActive', goog.functions.error());
var anim = new goog.fx.Animation([0], [1], 1000);
var anim2 = new goog.fx.Animation([0], [1], 1000);
goog.fx.anim.registerAnimation(anim);
assertTrue('Should contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('Should have called start once', 1, timerCount);
goog.fx.anim.registerAnimation(anim2);
assertEquals('Should not have called start again', 1, timerCount);
// Add anim again.
goog.fx.anim.registerAnimation(anim);
assertTrue('Should contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('Should not have called start again', 1, timerCount);
goog.fx.anim.unregisterAnimation(anim);
assertFalse('Should not contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('clearTimeout should not have been called', 1, timerCount);
goog.fx.anim.unregisterAnimation(anim2);
assertEquals('There should be no remaining timers', 0, timerCount);
// Make sure we don't trigger setTimeout or setInterval.
clock.tick(1000);
goog.fx.anim.cycleAnimations_(goog.now());
assertEquals('There should be no remaining timers', 0, timerCount);
anim.dispose();
anim2.dispose();
}
function testRegisterAndUnregisterAnimationWithRequestAnimationFrameGecko() {
// Only FF4 onwards support requestAnimationFrame.
if (!goog.userAgent.GECKO || !goog.userAgent.isVersionOrHigher('2.0') ||
goog.userAgent.isVersionOrHigher('17')) {
return;
}
goog.fx.anim.setAnimationWindow(window);
var anim = new goog.fx.Animation([0], [1], 1000);
var anim2 = new goog.fx.Animation([0], [1], 1000);
goog.fx.anim.registerAnimation(anim);
assertTrue('Should contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('Should have listen to MozBeforePaint once', 1,
goog.events.getListeners(window, 'MozBeforePaint', false).length);
goog.fx.anim.registerAnimation(anim2);
assertEquals('Should not add more listener for MozBeforePaint', 1,
goog.events.getListeners(window, 'MozBeforePaint', false).length);
// Add anim again.
goog.fx.anim.registerAnimation(anim);
assertTrue('Should contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('Should not add more listener for MozBeforePaint', 1,
goog.events.getListeners(window, 'MozBeforePaint', false).length);
goog.fx.anim.unregisterAnimation(anim);
assertFalse('Should not contain the animation',
goog.object.containsValue(goog.fx.anim.activeAnimations_,
anim));
assertEquals('Should not clear listener for MozBeforePaint yet', 1,
goog.events.getListeners(window, 'MozBeforePaint', false).length);
goog.fx.anim.unregisterAnimation(anim2);
assertEquals('There should be no more listener for MozBeforePaint', 0,
goog.events.getListeners(window, 'MozBeforePaint', false).length);
anim.dispose();
anim2.dispose();
goog.fx.anim.setAnimationWindow(null);
}
function testRegisterUnregisterAnimation() {
var anim = new goog.fx.Animation([0], [1], 1000);
goog.fx.anim.registerAnimation(anim);
assertTrue('There should be an active timer',
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());
assertEquals('There should be an active animations',
1, goog.object.getCount(goog.fx.anim.activeAnimations_));
goog.fx.anim.unregisterAnimation(anim);
assertTrue('There should be no active animations',
goog.object.isEmpty(goog.fx.anim.activeAnimations_));
assertFalse('There should be no active timer',
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());
anim.dispose();
}
function testCycleWithMockClock() {
goog.fx.anim.setAnimationWindow(null);
var anim = new goog.fx.Animation([0], [1], 1000);
anim.onAnimationFrame = goog.testing.recordFunction();
goog.fx.anim.registerAnimation(anim);
clock.tick(goog.fx.anim.TIMEOUT);
assertEquals(1, anim.onAnimationFrame.getCallCount());
}
function testCycleWithMockClockAndAnimationWindow() {
goog.fx.anim.setAnimationWindow(window);
var anim = new goog.fx.Animation([0], [1], 1000);
anim.onAnimationFrame = goog.testing.recordFunction();
goog.fx.anim.registerAnimation(anim);
clock.tick(goog.fx.anim.TIMEOUT);
assertEquals(1, anim.onAnimationFrame.getCallCount());
}