@limetech/mdc-tab-bar
Version:
The Material Components for the web tab bar component
170 lines • 8.15 kB
JavaScript
/**
* @license
* Copyright 2018 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import * as tslib_1 from "tslib";
import { MDCComponent } from '@limetech/mdc-base/component';
import { MDCTabScroller } from '@limetech/mdc-tab-scroller/component';
import { MDCTab } from '@limetech/mdc-tab/component';
import { MDCTabFoundation } from '@limetech/mdc-tab/foundation';
import { MDCTabBarFoundation } from './foundation';
var strings = MDCTabBarFoundation.strings;
var tabIdCounter = 0;
var MDCTabBar = /** @class */ (function (_super) {
tslib_1.__extends(MDCTabBar, _super);
function MDCTabBar() {
return _super !== null && _super.apply(this, arguments) || this;
}
MDCTabBar.attachTo = function (root) {
return new MDCTabBar(root);
};
Object.defineProperty(MDCTabBar.prototype, "focusOnActivate", {
set: function (focusOnActivate) {
this.tabList_.forEach(function (tab) { return tab.focusOnActivate = focusOnActivate; });
},
enumerable: true,
configurable: true
});
Object.defineProperty(MDCTabBar.prototype, "useAutomaticActivation", {
set: function (useAutomaticActivation) {
this.foundation_.setUseAutomaticActivation(useAutomaticActivation);
},
enumerable: true,
configurable: true
});
MDCTabBar.prototype.initialize = function (tabFactory, tabScrollerFactory) {
if (tabFactory === void 0) { tabFactory = function (el) { return new MDCTab(el); }; }
if (tabScrollerFactory === void 0) { tabScrollerFactory = function (el) { return new MDCTabScroller(el); }; }
this.tabList_ = this.instantiateTabs_(tabFactory);
this.tabScroller_ = this.instantiateTabScroller_(tabScrollerFactory);
};
MDCTabBar.prototype.initialSyncWithDOM = function () {
var _this = this;
this.handleTabInteraction_ = function (evt) { return _this.foundation_.handleTabInteraction(evt); };
this.handleKeyDown_ = function (evt) { return _this.foundation_.handleKeyDown(evt); };
this.listen(MDCTabFoundation.strings.INTERACTED_EVENT, this.handleTabInteraction_);
this.listen('keydown', this.handleKeyDown_);
for (var i = 0; i < this.tabList_.length; i++) {
if (this.tabList_[i].active) {
this.scrollIntoView(i);
break;
}
}
};
MDCTabBar.prototype.destroy = function () {
_super.prototype.destroy.call(this);
this.unlisten(MDCTabFoundation.strings.INTERACTED_EVENT, this.handleTabInteraction_);
this.unlisten('keydown', this.handleKeyDown_);
this.tabList_.forEach(function (tab) { return tab.destroy(); });
if (this.tabScroller_) {
this.tabScroller_.destroy();
}
};
MDCTabBar.prototype.getDefaultFoundation = function () {
var _this = this;
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
var adapter = {
scrollTo: function (scrollX) { return _this.tabScroller_.scrollTo(scrollX); },
incrementScroll: function (scrollXIncrement) { return _this.tabScroller_.incrementScroll(scrollXIncrement); },
getScrollPosition: function () { return _this.tabScroller_.getScrollPosition(); },
getScrollContentWidth: function () { return _this.tabScroller_.getScrollContentWidth(); },
getOffsetWidth: function () { return _this.root_.offsetWidth; },
isRTL: function () { return window.getComputedStyle(_this.root_).getPropertyValue('direction') === 'rtl'; },
setActiveTab: function (index) { return _this.foundation_.activateTab(index); },
activateTabAtIndex: function (index, clientRect) { return _this.tabList_[index].activate(clientRect); },
deactivateTabAtIndex: function (index) { return _this.tabList_[index].deactivate(); },
focusTabAtIndex: function (index) { return _this.tabList_[index].focus(); },
getTabIndicatorClientRectAtIndex: function (index) { return _this.tabList_[index].computeIndicatorClientRect(); },
getTabDimensionsAtIndex: function (index) { return _this.tabList_[index].computeDimensions(); },
getPreviousActiveTabIndex: function () {
for (var i = 0; i < _this.tabList_.length; i++) {
if (_this.tabList_[i].active) {
return i;
}
}
return -1;
},
getFocusedTabIndex: function () {
var tabElements = _this.getTabElements_();
var activeElement = document.activeElement;
return tabElements.indexOf(activeElement);
},
getIndexOfTabById: function (id) {
for (var i = 0; i < _this.tabList_.length; i++) {
if (_this.tabList_[i].id === id) {
return i;
}
}
return -1;
},
getTabListLength: function () { return _this.tabList_.length; },
notifyTabActivated: function (index) {
return _this.emit(strings.TAB_ACTIVATED_EVENT, { index: index }, true);
},
};
// tslint:enable:object-literal-sort-keys
return new MDCTabBarFoundation(adapter);
};
/**
* Activates the tab at the given index
* @param index The index of the tab
*/
MDCTabBar.prototype.activateTab = function (index) {
this.foundation_.activateTab(index);
};
/**
* Scrolls the tab at the given index into view
* @param index THe index of the tab
*/
MDCTabBar.prototype.scrollIntoView = function (index) {
this.foundation_.scrollIntoView(index);
};
/**
* Returns all the tab elements in a nice clean array
*/
MDCTabBar.prototype.getTabElements_ = function () {
return [].slice.call(this.root_.querySelectorAll(strings.TAB_SELECTOR));
};
/**
* Instantiates tab components on all child tab elements
*/
MDCTabBar.prototype.instantiateTabs_ = function (tabFactory) {
return this.getTabElements_().map(function (el) {
el.id = el.id || "mdc-tab-" + ++tabIdCounter;
return tabFactory(el);
});
};
/**
* Instantiates tab scroller component on the child tab scroller element
*/
MDCTabBar.prototype.instantiateTabScroller_ = function (tabScrollerFactory) {
var tabScrollerElement = this.root_.querySelector(strings.TAB_SCROLLER_SELECTOR);
if (tabScrollerElement) {
return tabScrollerFactory(tabScrollerElement);
}
return null;
};
return MDCTabBar;
}(MDCComponent));
export { MDCTabBar };
//# sourceMappingURL=component.js.map