Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2015 Google Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Class constructor for Animation MDL component. |
| 20 | * Implements MDL component design pattern defined at: |
| 21 | * https://github.com/jasonmayes/mdl-component-design-pattern |
| 22 | * @param {HTMLElement} element The element that will be upgraded. |
| 23 | */ |
| 24 | function DemoAnimation(element) { |
| 25 | 'use strict'; |
| 26 | |
| 27 | this.element_ = element; |
| 28 | this.position_ = this.Constant_.STARTING_POSITION; |
| 29 | this.movable_ = this.element_.querySelector('.' + this.CssClasses_.MOVABLE); |
| 30 | // Initialize instance. |
| 31 | this.init(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Store strings for class names defined by this component that are used in |
| 36 | * JavaScript. This allows us to simply change it in one place should we |
| 37 | * decide to modify at a later date. |
| 38 | * @enum {string} |
| 39 | * @private |
| 40 | */ |
| 41 | DemoAnimation.prototype.CssClasses_ = { |
| 42 | MOVABLE: 'demo-animation__movable', |
| 43 | POSITION_PREFIX: 'demo-animation--position-', |
| 44 | FAST_OUT_SLOW_IN: 'mdl-animation--fast-out-slow-in', |
| 45 | LINEAR_OUT_SLOW_IN: 'mdl-animation--linear-out-slow-in', |
| 46 | FAST_OUT_LINEAR_IN: 'mdl-animation--fast-out-linear-in' |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * Store constants in one place so they can be updated easily. |
| 51 | * @enum {string | number} |
| 52 | * @private |
| 53 | */ |
| 54 | DemoAnimation.prototype.Constant_ = { |
| 55 | STARTING_POSITION: 0, |
| 56 | // Which animation to use for which state. Check demo.css for an explanation. |
| 57 | ANIMATIONS: [ |
| 58 | DemoAnimation.prototype.CssClasses_.FAST_OUT_LINEAR_IN, |
| 59 | DemoAnimation.prototype.CssClasses_.LINEAR_OUT_SLOW_IN, |
| 60 | DemoAnimation.prototype.CssClasses_.FAST_OUT_SLOW_IN, |
| 61 | DemoAnimation.prototype.CssClasses_.FAST_OUT_LINEAR_IN, |
| 62 | DemoAnimation.prototype.CssClasses_.LINEAR_OUT_SLOW_IN, |
| 63 | DemoAnimation.prototype.CssClasses_.FAST_OUT_SLOW_IN |
| 64 | ] |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Handle click of element. |
| 69 | * @param {Event} event The event that fired. |
| 70 | * @private |
| 71 | */ |
| 72 | DemoAnimation.prototype.handleClick_ = function(event) { |
| 73 | 'use strict'; |
| 74 | |
| 75 | this.movable_.classList.remove(this.CssClasses_.POSITION_PREFIX + |
| 76 | this.position_); |
| 77 | this.movable_.classList.remove(this.Constant_.ANIMATIONS[this.position_]); |
| 78 | |
| 79 | this.position_++; |
| 80 | if (this.position_ > 5) { |
| 81 | this.position_ = 0; |
| 82 | } |
| 83 | |
| 84 | this.movable_.classList.add(this.Constant_.ANIMATIONS[this.position_]); |
| 85 | this.movable_.classList.add(this.CssClasses_.POSITION_PREFIX + |
| 86 | this.position_); |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Initialize element. |
| 91 | */ |
| 92 | DemoAnimation.prototype.init = function() { |
| 93 | 'use strict'; |
| 94 | |
| 95 | if (this.element_) { |
| 96 | if (!this.movable_) { |
| 97 | console.error('Was expecting to find an element with class name ' + |
| 98 | this.CssClasses_.MOVABLE + ' inside of: ', this.element_); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | this.element_.addEventListener('click', this.handleClick_.bind(this)); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | // The component registers itself. It can assume componentHandler is available |
| 107 | // in the global scope. |
| 108 | componentHandler.register({ |
| 109 | constructor: DemoAnimation, |
| 110 | classAsString: 'DemoAnimation', |
| 111 | cssClass: 'demo-js-animation' |
| 112 | }); |