UNPKG

11.4 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23import * as tslib_1 from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, numbers, strings } from './constants';
26var MDCToolbarFoundation = /** @class */ (function (_super) {
27 tslib_1.__extends(MDCToolbarFoundation, _super);
28 function MDCToolbarFoundation(adapter) {
29 var _this = _super.call(this, tslib_1.__assign({}, MDCToolbarFoundation.defaultAdapter, adapter)) || this;
30 _this.checkRowHeightFrame_ = 0;
31 _this.scrollFrame_ = 0;
32 _this.executedLastChange_ = false;
33 _this.isFixed_ = false;
34 _this.isFixedLastRow_ = false;
35 _this.hasFlexibleFirstRow_ = false;
36 _this.useFlexDefaultBehavior_ = false;
37 _this.calculations_ = {
38 flexibleExpansionHeight: 0,
39 flexibleExpansionRatio: 0,
40 maxTranslateYDistance: 0,
41 maxTranslateYRatio: 0,
42 scrollThreshold: 0,
43 scrollThresholdRatio: 0,
44 toolbarHeight: 0,
45 toolbarRatio: 0,
46 toolbarRowHeight: 0,
47 };
48 return _this;
49 }
50 Object.defineProperty(MDCToolbarFoundation, "cssClasses", {
51 get: function () {
52 return cssClasses;
53 },
54 enumerable: true,
55 configurable: true
56 });
57 Object.defineProperty(MDCToolbarFoundation, "strings", {
58 get: function () {
59 return strings;
60 },
61 enumerable: true,
62 configurable: true
63 });
64 Object.defineProperty(MDCToolbarFoundation, "numbers", {
65 get: function () {
66 return numbers;
67 },
68 enumerable: true,
69 configurable: true
70 });
71 Object.defineProperty(MDCToolbarFoundation, "defaultAdapter", {
72 get: function () {
73 // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
74 return {
75 hasClass: function () { return false; },
76 addClass: function () { return undefined; },
77 removeClass: function () { return undefined; },
78 registerScrollHandler: function () { return undefined; },
79 deregisterScrollHandler: function () { return undefined; },
80 registerResizeHandler: function () { return undefined; },
81 deregisterResizeHandler: function () { return undefined; },
82 getViewportWidth: function () { return 0; },
83 getViewportScrollY: function () { return 0; },
84 getOffsetHeight: function () { return 0; },
85 getFirstRowElementOffsetHeight: function () { return 0; },
86 notifyChange: function () { return undefined; },
87 setStyle: function () { return undefined; },
88 setStyleForTitleElement: function () { return undefined; },
89 setStyleForFlexibleRowElement: function () { return undefined; },
90 setStyleForFixedAdjustElement: function () { return undefined; },
91 };
92 // tslint:enable:object-literal-sort-keys
93 },
94 enumerable: true,
95 configurable: true
96 });
97 MDCToolbarFoundation.prototype.init = function () {
98 var _this = this;
99 this.isFixed_ = this.adapter_.hasClass(cssClasses.FIXED);
100 this.isFixedLastRow_ = this.adapter_.hasClass(cssClasses.FIXED_LASTROW) && this.isFixed_;
101 this.hasFlexibleFirstRow_ = this.adapter_.hasClass(cssClasses.TOOLBAR_ROW_FLEXIBLE);
102 if (this.hasFlexibleFirstRow_) {
103 this.useFlexDefaultBehavior_ = this.adapter_.hasClass(cssClasses.FLEXIBLE_DEFAULT_BEHAVIOR);
104 }
105 this.resizeHandler_ = function () { return _this.checkRowHeight_(); };
106 this.scrollHandler_ = function () { return _this.updateToolbarStyles_(); };
107 this.adapter_.registerResizeHandler(this.resizeHandler_);
108 this.adapter_.registerScrollHandler(this.scrollHandler_);
109 this.initKeyRatio_();
110 this.setKeyHeights_();
111 };
112 MDCToolbarFoundation.prototype.destroy = function () {
113 this.adapter_.deregisterResizeHandler(this.resizeHandler_);
114 this.adapter_.deregisterScrollHandler(this.scrollHandler_);
115 };
116 MDCToolbarFoundation.prototype.updateAdjustElementStyles = function () {
117 if (this.isFixed_) {
118 this.adapter_.setStyleForFixedAdjustElement('margin-top', this.calculations_.toolbarHeight + "px");
119 }
120 };
121 MDCToolbarFoundation.prototype.getFlexibleExpansionRatio_ = function (scrollTop) {
122 // To prevent division by zero when there is no flexibleExpansionHeight
123 var delta = 0.0001;
124 return Math.max(0, 1 - scrollTop / (this.calculations_.flexibleExpansionHeight + delta));
125 };
126 MDCToolbarFoundation.prototype.checkRowHeight_ = function () {
127 var _this = this;
128 cancelAnimationFrame(this.checkRowHeightFrame_);
129 this.checkRowHeightFrame_ = requestAnimationFrame(function () { return _this.setKeyHeights_(); });
130 };
131 MDCToolbarFoundation.prototype.setKeyHeights_ = function () {
132 var newToolbarRowHeight = this.getRowHeight_();
133 if (newToolbarRowHeight !== this.calculations_.toolbarRowHeight) {
134 this.calculations_.toolbarRowHeight = newToolbarRowHeight;
135 this.calculations_.toolbarHeight = this.calculations_.toolbarRatio * this.calculations_.toolbarRowHeight;
136 this.calculations_.flexibleExpansionHeight =
137 this.calculations_.flexibleExpansionRatio * this.calculations_.toolbarRowHeight;
138 this.calculations_.maxTranslateYDistance =
139 this.calculations_.maxTranslateYRatio * this.calculations_.toolbarRowHeight;
140 this.calculations_.scrollThreshold =
141 this.calculations_.scrollThresholdRatio * this.calculations_.toolbarRowHeight;
142 this.updateAdjustElementStyles();
143 this.updateToolbarStyles_();
144 }
145 };
146 MDCToolbarFoundation.prototype.updateToolbarStyles_ = function () {
147 var _this = this;
148 cancelAnimationFrame(this.scrollFrame_);
149 this.scrollFrame_ = requestAnimationFrame(function () {
150 var scrollTop = _this.adapter_.getViewportScrollY();
151 var hasScrolledOutOfThreshold = _this.scrolledOutOfThreshold_(scrollTop);
152 if (hasScrolledOutOfThreshold && _this.executedLastChange_) {
153 return;
154 }
155 var flexibleExpansionRatio = _this.getFlexibleExpansionRatio_(scrollTop);
156 _this.updateToolbarFlexibleState_(flexibleExpansionRatio);
157 if (_this.isFixedLastRow_) {
158 _this.updateToolbarFixedState_(scrollTop);
159 }
160 if (_this.hasFlexibleFirstRow_) {
161 _this.updateFlexibleRowElementStyles_(flexibleExpansionRatio);
162 }
163 _this.executedLastChange_ = hasScrolledOutOfThreshold;
164 _this.adapter_.notifyChange({ flexibleExpansionRatio: flexibleExpansionRatio });
165 });
166 };
167 MDCToolbarFoundation.prototype.scrolledOutOfThreshold_ = function (scrollTop) {
168 return scrollTop > this.calculations_.scrollThreshold;
169 };
170 MDCToolbarFoundation.prototype.initKeyRatio_ = function () {
171 var toolbarRowHeight = this.getRowHeight_();
172 var firstRowMaxRatio = this.adapter_.getFirstRowElementOffsetHeight() / toolbarRowHeight;
173 this.calculations_.toolbarRatio = this.adapter_.getOffsetHeight() / toolbarRowHeight;
174 this.calculations_.flexibleExpansionRatio = firstRowMaxRatio - 1;
175 this.calculations_.maxTranslateYRatio =
176 this.isFixedLastRow_ ? this.calculations_.toolbarRatio - firstRowMaxRatio : 0;
177 this.calculations_.scrollThresholdRatio =
178 (this.isFixedLastRow_ ? this.calculations_.toolbarRatio : firstRowMaxRatio) - 1;
179 };
180 MDCToolbarFoundation.prototype.getRowHeight_ = function () {
181 var breakpoint = numbers.TOOLBAR_MOBILE_BREAKPOINT;
182 return this.adapter_.getViewportWidth() < breakpoint ?
183 numbers.TOOLBAR_ROW_MOBILE_HEIGHT : numbers.TOOLBAR_ROW_HEIGHT;
184 };
185 MDCToolbarFoundation.prototype.updateToolbarFlexibleState_ = function (flexibleExpansionRatio) {
186 this.adapter_.removeClass(cssClasses.FLEXIBLE_MAX);
187 this.adapter_.removeClass(cssClasses.FLEXIBLE_MIN);
188 if (flexibleExpansionRatio === 1) {
189 this.adapter_.addClass(cssClasses.FLEXIBLE_MAX);
190 }
191 else if (flexibleExpansionRatio === 0) {
192 this.adapter_.addClass(cssClasses.FLEXIBLE_MIN);
193 }
194 };
195 MDCToolbarFoundation.prototype.updateToolbarFixedState_ = function (scrollTop) {
196 var translateDistance = Math.max(0, Math.min(scrollTop - this.calculations_.flexibleExpansionHeight, this.calculations_.maxTranslateYDistance));
197 this.adapter_.setStyle('transform', "translateY(" + -translateDistance + "px)");
198 if (translateDistance === this.calculations_.maxTranslateYDistance) {
199 this.adapter_.addClass(cssClasses.FIXED_AT_LAST_ROW);
200 }
201 else {
202 this.adapter_.removeClass(cssClasses.FIXED_AT_LAST_ROW);
203 }
204 };
205 MDCToolbarFoundation.prototype.updateFlexibleRowElementStyles_ = function (flexibleExpansionRatio) {
206 if (this.isFixed_) {
207 var height = this.calculations_.flexibleExpansionHeight * flexibleExpansionRatio;
208 this.adapter_.setStyleForFlexibleRowElement('height', height + this.calculations_.toolbarRowHeight + "px");
209 }
210 if (this.useFlexDefaultBehavior_) {
211 this.updateElementStylesDefaultBehavior_(flexibleExpansionRatio);
212 }
213 };
214 MDCToolbarFoundation.prototype.updateElementStylesDefaultBehavior_ = function (flexibleExpansionRatio) {
215 var maxTitleSize = numbers.MAX_TITLE_SIZE;
216 var minTitleSize = numbers.MIN_TITLE_SIZE;
217 var currentTitleSize = (maxTitleSize - minTitleSize) * flexibleExpansionRatio + minTitleSize;
218 this.adapter_.setStyleForTitleElement('font-size', currentTitleSize + "rem");
219 };
220 return MDCToolbarFoundation;
221}(MDCFoundation));
222export { MDCToolbarFoundation };
223// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
224export default MDCToolbarFoundation;
225//# sourceMappingURL=foundation.js.map
\No newline at end of file