2013-12-01 04:15:31 +00:00
/ *
Copyright 2013 The Camlistore Authors
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
2014-01-08 03:41:58 +00:00
http : //www.apache.org/licenses/LICENSE-2.0
2013-12-01 04:15:31 +00:00
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 .
* /
2014-01-08 05:28:01 +00:00
goog . provide ( 'cam.Spinner' ) ;
2013-12-01 04:15:31 +00:00
goog . require ( 'goog.dom' ) ;
goog . require ( 'goog.events.EventHandler' ) ;
goog . require ( 'goog.style' ) ;
goog . require ( 'goog.math.Coordinate' ) ;
goog . require ( 'goog.math.Size' ) ;
goog . require ( 'goog.ui.Control' ) ;
2014-01-08 06:07:26 +00:00
goog . require ( 'cam.AnimationLoop' ) ;
goog . require ( 'cam.style' ) ;
2014-01-08 03:41:58 +00:00
// An indeterminite progress meter using the safe icon.
// @param {goog.dom.DomHelper} domHelper
2014-01-08 05:28:01 +00:00
cam . Spinner = function ( domHelper ) {
2014-01-08 03:41:58 +00:00
goog . base ( this , null , this . dom _ ) ;
this . dom _ = domHelper ;
this . eh _ = new goog . events . EventHandler ( this ) ;
2014-01-08 05:28:01 +00:00
this . animationLoop _ = new cam . AnimationLoop ( this . dom _ . getWindow ( ) ) ;
2014-01-08 03:41:58 +00:00
this . currentRotation _ = 0 ;
2013-12-01 04:15:31 +00:00
} ;
2014-01-08 05:28:01 +00:00
goog . inherits ( cam . Spinner , goog . ui . Control ) ;
2013-12-01 04:15:31 +00:00
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . backgroundImage = "safe-no-wheel.svg" ;
2013-12-01 04:15:31 +00:00
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . foregroundImage = "safe-wheel.svg" ;
2013-12-01 04:15:31 +00:00
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . degreesPerSecond = 500 ;
2013-12-01 04:15:31 +00:00
2014-01-08 03:41:58 +00:00
// The origin the safe wheel rotates around, expressed as a fraction of the image's width and height.
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . wheelRotationOrigin _ = new goog . math . Coordinate ( 0.37 , 0.505 ) ;
2014-01-08 03:41:58 +00:00
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . createDom = function ( ) {
2014-01-08 03:41:58 +00:00
this . background _ = this . dom _ . createDom ( 'div' , 'cam-spinner' , this . dom _ . createDom ( 'div' ) ) ;
this . foreground _ = this . background _ . firstChild ;
2014-01-08 05:28:01 +00:00
cam . style . setURLStyle ( this . background _ , 'background-image' , this . backgroundImage ) ;
cam . style . setURLStyle ( this . foreground _ , 'background-image' , this . foregroundImage ) ;
2014-01-08 03:41:58 +00:00
// TODO(aa): This will need to be configurable. Not sure how makes sense yet.
var size = new goog . math . Size ( 75 , 75 ) ;
goog . style . setSize ( this . background _ , size ) ;
// We should be able to set the origin as a percentage directly, but the browsers end up rounding differently, and we get less off-center spinning on the whole if we set this using pixels.
var origin = new goog . math . Coordinate ( size . width , size . height ) ;
2014-01-08 05:28:01 +00:00
cam . style . setTransformOrigin (
2014-01-08 03:41:58 +00:00
this . foreground _ ,
origin . scale ( this . wheelRotationOrigin _ . x , this . wheelRotationOrigin _ . y ) ) ;
2014-01-08 05:28:01 +00:00
this . eh _ . listen ( this . animationLoop _ , cam . AnimationLoop . FRAME _EVENT _TYPE , this . updateRotation _ ) ;
2014-01-08 03:41:58 +00:00
this . decorateInternal ( this . background _ ) ;
2013-12-01 04:15:31 +00:00
} ;
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . isRunning = function ( ) {
2014-01-08 03:41:58 +00:00
return this . animationLoop _ . isRunning ( ) ;
2013-12-01 04:15:31 +00:00
} ;
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . start = function ( ) {
2014-01-08 03:41:58 +00:00
this . animationLoop _ . start ( ) ;
2013-12-01 04:15:31 +00:00
} ;
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . stop = function ( ) {
2014-01-08 03:41:58 +00:00
this . animationLoop _ . stop ( ) ;
2013-12-01 04:15:31 +00:00
} ;
2014-01-08 05:28:01 +00:00
cam . Spinner . prototype . updateRotation _ = function ( e ) {
2014-01-08 03:41:58 +00:00
rotation = e . delay / 1000 * this . degreesPerSecond ;
this . currentRotation _ += rotation ;
this . currentRotation _ %= 360 ;
2014-01-08 05:28:01 +00:00
cam . style . setRotation ( this . foreground _ , this . currentRotation _ ) ;
2013-12-01 04:15:31 +00:00
} ;