Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 3.35 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. JQ = JQ || {};
  2.  
  3. JQ.Animate = Ember.Mixin.create({
  4.   cssProperties: ['background', 'backgroundAttachment', 'backgroundColor', 'backgroundImage', 'backgroundPosition',
  5.   'backgroundRepeat', 'border', 'borderBottom', 'borderBottomColor', 'borderBottomStyle', 'borderBottomWidth',
  6.   'borderColor', 'borderLeft', 'borderLeftColor', 'borderLeftStyle', 'borderLeftWidth', 'borderRight', 'borderRightColor',
  7.   'borderRightStyle', 'borderRightWidth', 'borderStyle', 'borderTop', 'borderTopColor', 'borderTopStyle', 'borderTopWidth',
  8.   'borderWidth', 'clear', 'clip', 'color', 'cursor', 'display', 'filter', 'font', 'fontFamily', 'fontSize',
  9.   'fontVariant', 'fontWeight', 'height', 'left', 'letterSpacing', 'lineHeight', 'listStyle', 'listStyleImage',
  10.   'listStylePosition', 'listStyleType', 'margin', 'marginBottom', 'marginLeft', 'marginRight', 'marginTop', 'overflow',
  11.   'padding', 'paddingBottom', 'paddingLeft', 'paddingRight', 'paddingTop', 'pageBreakAfter', 'pageBreakBefore',
  12.   'position', 'styleFloat', 'textAlign', 'textDecoration', 'textIndent', 'textTransform', 'top', 'verticalAlign',
  13.   'visibility', 'width', 'zIndex'],
  14.  
  15.   elementIsInserted: false,
  16.    
  17.   didInsertElement: function() {
  18.     this._super();
  19.    
  20.     this.set('elementIsInserted', true);
  21.     this._gatherProperties();
  22.   },
  23.  
  24.   willDestroyElement: function() {
  25.     this._super();
  26.    
  27.     this.set('elementIsInserted', false);
  28.    
  29.     // Tear down any observers that were created
  30.     var observers = this._observers;
  31.     for (var prop in observers) {
  32.         if (observers.hasOwnProperty(prop)) {
  33.           this.removeObserver(prop, observers[prop]);
  34.         }
  35.     }
  36.  
  37.   },
  38.  
  39.   _gatherProperties: function() {
  40.     var cssProperties = this.get('cssProperties');
  41.    
  42.     // The view can specify a list of css properties that should be treated
  43.     // as Ember properties.
  44.     cssProperties.forEach(function(key) {
  45.  
  46.       // Set up an observer on the Ember property.
  47.       // When it changes, call animate()
  48.       var observer = function() {
  49.         this.animate();
  50.       };
  51.  
  52.       this.addObserver(key, observer);
  53.  
  54.       // Insert the observer in a Hash so we can remove it later.
  55.       this._observers = this._observers || {};
  56.       this._observers[key] = observer;
  57.     }, this);
  58.  
  59.   },
  60.  
  61.  
  62.   // Do the animation. Called whenever a css property changes.
  63.   // You can define a beforeAnimate() function that will be called before the actual animation takes place
  64.   // You can also define a afterAnimate() function that will be called just after the animation ends
  65.   animate: function(){
  66.       var cssProperties = this.get('cssProperties'), properties = {},
  67.         duration = this.get('duration') || 400, easing = this.get('easing') || null, self = this;
  68.        
  69.       // Ensure this view is inserted. If not, animate later.
  70.       if (!this.get('elementIsInserted')){
  71.           return Ember.run.next(this, function() {
  72.               this.animate();
  73.           });
  74.       }
  75.      
  76.       // Gather css properties values
  77.       cssProperties.forEach(function(key) {
  78.         properties[key] = self.get(key);
  79.       }, this);
  80.      
  81.       // Before animation
  82.       if (typeof this.beforeAnimate == "function"){
  83.           this.beforeAnimate();
  84.       }
  85.      
  86.       // Animate
  87.       return this.$().animate(properties, duration, easing, function(){
  88.           if (typeof self.afterAnimate == "function"){
  89.             self.afterAnimate();
  90.           }
  91.       });
  92.   }
  93. });