|
| 1 | +/** |
| 2 | + @module ember-ui-components |
| 3 | +*/ |
| 4 | +import Ember from 'ember'; |
| 5 | +import layout from '../templates/components/uic-tabs'; |
| 6 | + |
| 7 | +const { computed: { alias }, get } = Ember; |
| 8 | + |
| 9 | +/** |
| 10 | + @class TabsComponent |
| 11 | + @namespace Components |
| 12 | +*/ |
| 13 | +export default Ember.Component.extend({ |
| 14 | + layout, |
| 15 | + |
| 16 | + /** |
| 17 | + @property classNames |
| 18 | + @type {Array} |
| 19 | + @private |
| 20 | + @default `['uic-tabs', 'tabs']` |
| 21 | + */ |
| 22 | + classNames: ['uic-tabs', 'tabs'], |
| 23 | + |
| 24 | + /** |
| 25 | + ## Tabs |
| 26 | +
|
| 27 | + Provide an array of tab objects, one for each tab. The tab objects can be |
| 28 | + `Ember.Object`s, `DS.Model`s, or POJOs. |
| 29 | +
|
| 30 | + Each tab object should have a `tabName` property. |
| 31 | +
|
| 32 | + ``` |
| 33 | + tabs: [ |
| 34 | + { tabName: 'First Tab', tabTemplate: 'my-first-tab' }, |
| 35 | + { tabName: 'Second Tab', tabTemplate: 'my-second-tab' } |
| 36 | + ] |
| 37 | + ``` |
| 38 | +
|
| 39 | + ``` |
| 40 | + {{uic-tabs tabs=tabs}} |
| 41 | + ``` |
| 42 | +
|
| 43 | + @property tabs |
| 44 | + @type {Array} |
| 45 | + */ |
| 46 | + tabs: [], |
| 47 | + |
| 48 | + /** |
| 49 | + ## Default Tab Name |
| 50 | +
|
| 51 | + If the tab objects don't provide a `tabName` property then `defaultTabName` |
| 52 | + will be used for the tab names. |
| 53 | +
|
| 54 | + @property defaultTabName |
| 55 | + @type {String} |
| 56 | + @default `Untitled` |
| 57 | + */ |
| 58 | + defaultTabName: 'Untitled', |
| 59 | + |
| 60 | + /** |
| 61 | + @property activeTab |
| 62 | + @type {Object} |
| 63 | + @private |
| 64 | + */ |
| 65 | + activeTab: null, |
| 66 | + |
| 67 | + /** |
| 68 | + ## Render All Tabs |
| 69 | + If this property is set to `true` then all tabs will be rendered at the same |
| 70 | + time. The inactive tabs will be hidden using CSS. |
| 71 | +
|
| 72 | + If `false` then only the active tab will be rendered. |
| 73 | +
|
| 74 | +
|
| 75 | + @property renderAllTabs |
| 76 | + @type {Boolean} |
| 77 | + @default `false` |
| 78 | + */ |
| 79 | + renderAllTabs: false, |
| 80 | + |
| 81 | + /** |
| 82 | + @property tab |
| 83 | + @type {Object} |
| 84 | + */ |
| 85 | + tab: alias('activeTab'), |
| 86 | + |
| 87 | + tabsChanged: Ember.observer('tabs.[]', function() { |
| 88 | + if (!this.get('activeTab')) { |
| 89 | + this.setDefaultTab(); |
| 90 | + } |
| 91 | + }), |
| 92 | + |
| 93 | + /** |
| 94 | + @method init |
| 95 | + @private |
| 96 | + */ |
| 97 | + init() { |
| 98 | + this._super(...arguments); |
| 99 | + if (!this.get('activeTab')) { |
| 100 | + this.setDefaultTab(); |
| 101 | + } |
| 102 | + }, |
| 103 | + |
| 104 | + /** |
| 105 | + @method setDefaultTab |
| 106 | + @private |
| 107 | + */ |
| 108 | + setDefaultTab() { |
| 109 | + let tabs = get(this, 'tabs'); |
| 110 | + if (tabs && get(tabs, 'length')) { |
| 111 | + let tab = get(tabs, 'firstObject') || tabs[0]; |
| 112 | + if (tab) { |
| 113 | + this.selectTab(tab); |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + /** |
| 119 | + @method selectTab |
| 120 | + @param {Object} tab |
| 121 | + @private |
| 122 | + */ |
| 123 | + selectTab(tab) { |
| 124 | + this.set('activeTab', tab); |
| 125 | + } |
| 126 | +}); |
0 commit comments