UNPKG

5.03 kBJavaScriptView Raw
1import { Component, HostBinding, Input } from '@angular/core';
2import { TabsetConfig } from './tabset.config';
3// todo: add active event to tab
4// todo: fix? mixing static and dynamic tabs position tabs in order of creation
5export var TabsetComponent = (function () {
6 function TabsetComponent(config) {
7 this.clazz = true;
8 this.tabs = [];
9 this.classMap = {};
10 Object.assign(this, config);
11 }
12 Object.defineProperty(TabsetComponent.prototype, "vertical", {
13 /** if true tabs will be placed vertically */
14 get: function () {
15 return this._vertical;
16 },
17 set: function (value) {
18 this._vertical = value;
19 this.setClassMap();
20 },
21 enumerable: true,
22 configurable: true
23 });
24 Object.defineProperty(TabsetComponent.prototype, "justified", {
25 /** if true tabs fill the container and have a consistent width */
26 get: function () {
27 return this._justified;
28 },
29 set: function (value) {
30 this._justified = value;
31 this.setClassMap();
32 },
33 enumerable: true,
34 configurable: true
35 });
36 Object.defineProperty(TabsetComponent.prototype, "type", {
37 /** navigation context class: 'tabs' or 'pills' */
38 get: function () {
39 return this._type;
40 },
41 set: function (value) {
42 this._type = value;
43 this.setClassMap();
44 },
45 enumerable: true,
46 configurable: true
47 });
48 TabsetComponent.prototype.ngOnDestroy = function () {
49 this.isDestroyed = true;
50 };
51 TabsetComponent.prototype.addTab = function (tab) {
52 this.tabs.push(tab);
53 tab.active = this.tabs.length === 1 && tab.active !== false;
54 };
55 TabsetComponent.prototype.removeTab = function (tab) {
56 var index = this.tabs.indexOf(tab);
57 if (index === -1 || this.isDestroyed) {
58 return;
59 }
60 // Select a new tab if the tab to be removed is selected and not destroyed
61 if (tab.active && this.hasAvailableTabs(index)) {
62 var newActiveIndex = this.getClosestTabIndex(index);
63 this.tabs[newActiveIndex].active = true;
64 }
65 tab.removed.emit(tab);
66 this.tabs.splice(index, 1);
67 };
68 TabsetComponent.prototype.getClosestTabIndex = function (index) {
69 var tabsLength = this.tabs.length;
70 if (!tabsLength) {
71 return -1;
72 }
73 for (var step = 1; step <= tabsLength; step += 1) {
74 var prevIndex = index - step;
75 var nextIndex = index + step;
76 if (this.tabs[prevIndex] && !this.tabs[prevIndex].disabled) {
77 return prevIndex;
78 }
79 if (this.tabs[nextIndex] && !this.tabs[nextIndex].disabled) {
80 return nextIndex;
81 }
82 }
83 return -1;
84 };
85 TabsetComponent.prototype.hasAvailableTabs = function (index) {
86 var tabsLength = this.tabs.length;
87 if (!tabsLength) {
88 return false;
89 }
90 for (var i = 0; i < tabsLength; i += 1) {
91 if (!this.tabs[i].disabled && i !== index) {
92 return true;
93 }
94 }
95 return false;
96 };
97 TabsetComponent.prototype.setClassMap = function () {
98 this.classMap = (_a = {
99 'nav-stacked': this.vertical,
100 'nav-justified': this.justified
101 },
102 _a["nav-" + this.type] = true,
103 _a
104 );
105 var _a;
106 };
107 TabsetComponent.decorators = [
108 { type: Component, args: [{
109 selector: 'tabset',
110 template: "\n <ul class=\"nav\" [ngClass]=\"classMap\" (click)=\"$event.preventDefault()\">\n <li *ngFor=\"let tabz of tabs\" [ngClass]=\"['nav-item', tabz.customClass || '']\"\n [class.active]=\"tabz.active\" [class.disabled]=\"tabz.disabled\">\n <a href=\"javascript:void(0);\" class=\"nav-link\"\n [class.active]=\"tabz.active\" [class.disabled]=\"tabz.disabled\"\n (click)=\"tabz.active = true\">\n <span [ngTransclude]=\"tabz.headingRef\">{{tabz.heading}}</span>\n <span *ngIf=\"tabz.removable\">\n <span (click)=\"$event.preventDefault(); removeTab(tabz);\" class=\"glyphicon glyphicon-remove-circle\"></span>\n </span>\n </a>\n </li>\n </ul>\n <div class=\"tab-content\">\n <ng-content></ng-content>\n </div>\n "
111 },] },
112 ];
113 /** @nocollapse */
114 TabsetComponent.ctorParameters = function () { return [
115 { type: TabsetConfig, },
116 ]; };
117 TabsetComponent.propDecorators = {
118 'vertical': [{ type: Input },],
119 'justified': [{ type: Input },],
120 'type': [{ type: Input },],
121 'clazz': [{ type: HostBinding, args: ['class.tab-container',] },],
122 };
123 return TabsetComponent;
124}());
125//# sourceMappingURL=tabset.component.js.map
\No newline at end of file