Ali Ghassemi | 07b4ef2 | 2014-10-22 17:18:38 -0700 | [diff] [blame] | 1 | <link rel="import" href="../../third-party/polymer/polymer.html"> |
| 2 | <link rel="import" href="../../third-party/core-pages/core-pages.html"> |
| 3 | <link rel="import" href="../../third-party/core-icons/core-icons.html"> |
| 4 | <link rel="import" href="../../third-party/core-icon-button/core-icon-button.html"> |
| 5 | <link rel="import" href="../../third-party/core-toolbar/core-toolbar.html"> |
| 6 | <link rel="import" href="../../third-party/core-selector/core-selector.html"> |
| 7 | <link rel="import" href="../../third-party/paper-tabs/paper-tabs.html"> |
| 8 | <link rel="import" href="tab-content/component.html"> |
| 9 | <link rel="import" href="tab-toolbar/component.html"> |
| 10 | |
| 11 | <polymer-element name="p2b-pipes"> |
| 12 | <template> |
| 13 | <link rel="stylesheet" href="../common/common.css"> |
| 14 | <link rel="stylesheet" href="component.css"> |
| 15 | <div class="container" flex> |
| 16 | <template if="{{ numTabs == 0 }}"> |
| 17 | <h2 page-title class="empty-message">No pipes to show...</h2> |
| 18 | <div class="no-pipes-bg"></div> |
| 19 | </template> |
| 20 | |
| 21 | <div id="tabsContainer" class="{{ {hidden : numTabs == 0} | tokenList}}" flex> |
| 22 | <paper-tabs id="tabs" class="{{ {hidden : numTabs <= 1} | tokenList}}" on-core-select="{{ handleTabSelectionChange }}" valueattr="key" selected="{{ selectedTabKey }}" noink></paper-tabs> |
| 23 | <core-selector id="tabPages" valueattr="key" selected="{{ selectedTabKey }}" flex></core-selector> |
| 24 | </div> |
| 25 | </div> |
| 26 | </template> |
| 27 | <script> |
| 28 | Polymer('p2b-pipes', { |
| 29 | |
| 30 | /* |
| 31 | * Map of existing pipe tabs. Key is the tab key. |
| 32 | * @type {set} |
| 33 | * @private |
| 34 | */ |
| 35 | pipeTabs: {}, |
| 36 | |
| 37 | /* |
| 38 | * Key of currently selected tab |
| 39 | * @type {string} |
| 40 | */ |
| 41 | selectedTabKey : '', |
| 42 | |
| 43 | /* |
| 44 | * Stack of previously selected tabs. |
| 45 | * @type {Array<string>} |
| 46 | * @private |
| 47 | */ |
| 48 | selectionHistoryStack: [], |
| 49 | |
| 50 | ready: function() { |
| 51 | this.numTabs = 0 |
| 52 | }, |
| 53 | |
| 54 | /* |
| 55 | * Adds a new tab |
| 56 | * @param {string} key Key of the tab to add |
| 57 | * @param {string} name Name of the tab to add |
| 58 | * @param {DOMElement} el Content of the tab |
| 59 | * @param {function} onClose Optional onClose callback. |
| 60 | */ |
| 61 | addTab: function(key, name, el, onClose) { |
| 62 | var self = this; |
| 63 | |
| 64 | // Create a tab thumb |
| 65 | var tab = document.createElement('paper-tab'); |
| 66 | tab.key = key; |
| 67 | tab.textContent = name; |
| 68 | |
| 69 | // Create a tab toolbar and assign the close handler |
| 70 | var tabToolbar = document.createElement('p2b-pipes-tab-toolbar'); |
| 71 | tabToolbar.toolbarTitle = name; |
| 72 | tabToolbar.addEventListener('close-action', function() { |
| 73 | self.removeTab(key); |
| 74 | if (onClose) { |
| 75 | onClose(); |
| 76 | } |
| 77 | }); |
| 78 | tabToolbar.addEventListener('fullscreen-action', function() { |
| 79 | var tabContent = self.pipeTabs[key].tabContent; |
| 80 | tabContent.fullscreen(); |
| 81 | }); |
| 82 | |
| 83 | // Create the content of the tab |
| 84 | var tabContent = document.createElement('p2b-pipes-tab-content'); |
| 85 | tabContent.setAttribute('key', key); |
| 86 | tabContent.appendChild(tabToolbar); |
| 87 | tabContent.appendChild(el); |
| 88 | |
| 89 | this.$.tabPages.appendChild(tabContent); |
| 90 | |
| 91 | // Add the tab to our list. |
| 92 | this.pipeTabs[key] = { |
| 93 | name: name, |
| 94 | tab: tab, |
| 95 | tabContent: tabContent, |
| 96 | tabToolbar: tabToolbar |
| 97 | }; |
| 98 | |
| 99 | this.numTabs++; |
| 100 | |
| 101 | this.selectedTabKey = key; |
| 102 | requestAnimationFrame(function() { |
| 103 | self.$.tabs.appendChild(tab); |
| 104 | }); |
| 105 | }, |
| 106 | |
| 107 | /* |
| 108 | * Adds a new toolbar action for the tab's toolbar |
| 109 | * @param {string} tabKey Key of the tab to add action to |
| 110 | * @param icon {string} icon Icon name for the action |
| 111 | * @param onClick {function} event handler for the action |
| 112 | */ |
| 113 | addToolbarAction: function(tabKey, icon, onClick) { |
| 114 | if (!this.pipeTabs[tabKey]) { |
| 115 | return; |
| 116 | } |
| 117 | var toolbar = this.pipeTabs[tabKey].tabToolbar; |
| 118 | toolbar.add(icon, onClick); |
| 119 | }, |
| 120 | |
| 121 | /* |
| 122 | * Removes a tab |
| 123 | * @param {string} key Key of the tab to remove |
| 124 | */ |
| 125 | removeTab: function(key) { |
| 126 | if (!this.pipeTabs[key]) { |
| 127 | return; |
| 128 | } |
| 129 | // Remove tab thumb and content |
| 130 | var tab = this.pipeTabs[key].tab; |
| 131 | tab.remove(); |
| 132 | var tabContent = this.pipeTabs[key].tabContent; |
| 133 | tabContent.remove(); |
| 134 | |
| 135 | // Delete tab from the map |
| 136 | delete this.pipeTabs[key]; |
| 137 | this.numTabs--; |
| 138 | |
| 139 | // Select an existing tab from previous selection history |
| 140 | var toSelect = this.selectionHistoryStack.pop(); |
| 141 | while ( toSelect && !this.pipeTabs[toSelect] ) { |
| 142 | // pop until we find one that still exists |
| 143 | toSelect = this.selectionHistoryStack.pop(); |
| 144 | } |
| 145 | if (toSelect) { |
| 146 | this.selectedTabKey = toSelect; |
| 147 | } |
| 148 | }, |
| 149 | |
| 150 | /* |
| 151 | * Replaces content of a tab |
| 152 | * @param {string} key Key of the tab to replace content for |
| 153 | * @param {string} newName new name for the tab |
| 154 | * @param {DOMElement} el New content of the tab |
| 155 | */ |
| 156 | replaceTabContent: function(key, newName, newEl) { |
| 157 | if (!this.pipeTabs[key]) { |
| 158 | return; |
| 159 | } |
| 160 | var tabContent = this.pipeTabs[key].tabContent; |
| 161 | tabContent.replaceTabContent(newEl); |
| 162 | if (newName) { |
| 163 | this.pipeTabs[key].tab.textContent = newName; |
| 164 | this.pipeTabs[key].tabToolbar.toolbarTitle = newName; |
| 165 | } |
| 166 | }, |
| 167 | |
| 168 | /* |
| 169 | * Adds the tab selection to history when selection changes |
| 170 | * @private |
| 171 | */ |
| 172 | handleTabSelectionChange: function(e) { |
| 173 | if (!e.detail.isSelected){ |
| 174 | return; |
| 175 | } |
| 176 | this.selectionHistoryStack.push(this.selectedTabKey); |
| 177 | } |
| 178 | }); |
| 179 | </script> |
| 180 | </polymer-element> |