blob: 8159fb3039c408a384288fbd046d60ae576876cc [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001/**
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(function() {
19 'use strict';
20
21 /**
22 * Class constructor for Spinner MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
25 *
26 * @param {HTMLElement} element The element that will be upgraded.
27 * @constructor
28 */
29 var MaterialSpinner = function MaterialSpinner(element) {
30 this.element_ = element;
31
32 // Initialize instance.
33 this.init();
34 };
35 window['MaterialSpinner'] = MaterialSpinner;
36
37 /**
38 * Store constants in one place so they can be updated easily.
39 *
40 * @enum {string | number}
41 * @private
42 */
43 MaterialSpinner.prototype.Constant_ = {
44 MDL_SPINNER_LAYER_COUNT: 4
45 };
46
47 /**
48 * Store strings for class names defined by this component that are used in
49 * JavaScript. This allows us to simply change it in one place should we
50 * decide to modify at a later date.
51 *
52 * @enum {string}
53 * @private
54 */
55 MaterialSpinner.prototype.CssClasses_ = {
56 MDL_SPINNER_LAYER: 'mdl-spinner__layer',
57 MDL_SPINNER_CIRCLE_CLIPPER: 'mdl-spinner__circle-clipper',
58 MDL_SPINNER_CIRCLE: 'mdl-spinner__circle',
59 MDL_SPINNER_GAP_PATCH: 'mdl-spinner__gap-patch',
60 MDL_SPINNER_LEFT: 'mdl-spinner__left',
61 MDL_SPINNER_RIGHT: 'mdl-spinner__right'
62 };
63
64 /**
65 * Auxiliary method to create a spinner layer.
66 *
67 * @param {number} index Index of the layer to be created.
68 * @public
69 */
70 MaterialSpinner.prototype.createLayer = function(index) {
71 var layer = document.createElement('div');
72 layer.classList.add(this.CssClasses_.MDL_SPINNER_LAYER);
73 layer.classList.add(this.CssClasses_.MDL_SPINNER_LAYER + '-' + index);
74
75 var leftClipper = document.createElement('div');
76 leftClipper.classList.add(this.CssClasses_.MDL_SPINNER_CIRCLE_CLIPPER);
77 leftClipper.classList.add(this.CssClasses_.MDL_SPINNER_LEFT);
78
79 var gapPatch = document.createElement('div');
80 gapPatch.classList.add(this.CssClasses_.MDL_SPINNER_GAP_PATCH);
81
82 var rightClipper = document.createElement('div');
83 rightClipper.classList.add(this.CssClasses_.MDL_SPINNER_CIRCLE_CLIPPER);
84 rightClipper.classList.add(this.CssClasses_.MDL_SPINNER_RIGHT);
85
86 var circleOwners = [leftClipper, gapPatch, rightClipper];
87
88 for (var i = 0; i < circleOwners.length; i++) {
89 var circle = document.createElement('div');
90 circle.classList.add(this.CssClasses_.MDL_SPINNER_CIRCLE);
91 circleOwners[i].appendChild(circle);
92 }
93
94 layer.appendChild(leftClipper);
95 layer.appendChild(gapPatch);
96 layer.appendChild(rightClipper);
97
98 this.element_.appendChild(layer);
99 };
100 MaterialSpinner.prototype['createLayer'] =
101 MaterialSpinner.prototype.createLayer;
102
103 /**
104 * Stops the spinner animation.
105 * Public method for users who need to stop the spinner for any reason.
106 *
107 * @public
108 */
109 MaterialSpinner.prototype.stop = function() {
110 this.element_.classList.remove('is-active');
111 };
112 MaterialSpinner.prototype['stop'] = MaterialSpinner.prototype.stop;
113
114 /**
115 * Starts the spinner animation.
116 * Public method for users who need to manually start the spinner for any reason
117 * (instead of just adding the 'is-active' class to their markup).
118 *
119 * @public
120 */
121 MaterialSpinner.prototype.start = function() {
122 this.element_.classList.add('is-active');
123 };
124 MaterialSpinner.prototype['start'] = MaterialSpinner.prototype.start;
125
126 /**
127 * Initialize element.
128 */
129 MaterialSpinner.prototype.init = function() {
130 if (this.element_) {
131 for (var i = 1; i <= this.Constant_.MDL_SPINNER_LAYER_COUNT; i++) {
132 this.createLayer(i);
133 }
134
135 this.element_.classList.add('is-upgraded');
136 }
137 };
138
139 // The component registers itself. It can assume componentHandler is available
140 // in the global scope.
141 componentHandler.register({
142 constructor: MaterialSpinner,
143 classAsString: 'MaterialSpinner',
144 cssClass: 'mdl-js-spinner',
145 widget: true
146 });
147})();