|
| 1 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 2 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 3 | +import ResponsivePopover from "@ui5/webcomponents/dist/ResponsivePopover.js"; |
| 4 | +import Tree from "@ui5/webcomponents/dist/Tree.js"; |
| 5 | +import SideNavigationTemplate from "./generated/templates/SideNavigationTemplate.lit.js"; |
| 6 | +import SideNavigationItemPopoverContentTemplate from "./generated/templates/SideNavigationItemPopoverContentTemplate.lit.js"; |
| 7 | + |
| 8 | +// Styles |
| 9 | +import SideNavigationCss from "./generated/themes/SideNavigation.css.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @public |
| 13 | + */ |
| 14 | +const metadata = { |
| 15 | + tag: "ui5-side-navigation", |
| 16 | + managedSlots: true, |
| 17 | + properties: /** @lends sap.ui.webcomponents.fiori.SideNavigation.prototype */ { |
| 18 | + /** |
| 19 | + * Defines whether the <code>ui5-side-navigation</code> is expanded or collapsed. |
| 20 | + * |
| 21 | + * @public |
| 22 | + * @type {boolean} |
| 23 | + * @defaultvalue false |
| 24 | + */ |
| 25 | + collapsed: { |
| 26 | + type: Boolean, |
| 27 | + }, |
| 28 | + |
| 29 | + /** |
| 30 | + * @private |
| 31 | + */ |
| 32 | + _popoverContent: { |
| 33 | + type: Object, |
| 34 | + }, |
| 35 | + }, |
| 36 | + slots: /** @lends sap.ui.webcomponents.fiori.SideNavigation.prototype */ { |
| 37 | + /** |
| 38 | + * Defines the main items of the <code>ui5-side-navigation</code>. Use the <code>ui5-side-navigation-item</code> component |
| 39 | + * for the top-level items, and the <code>ui5-side-navigation-subitem</code> component for second-level items, nested |
| 40 | + * inside the items. |
| 41 | + * |
| 42 | + * @public |
| 43 | + * @slot |
| 44 | + */ |
| 45 | + "default": { |
| 46 | + propertyName: "items", |
| 47 | + invalidateParent: true, |
| 48 | + type: HTMLElement, |
| 49 | + }, |
| 50 | + |
| 51 | + /** |
| 52 | + * Defines the fixed items at the bottom of the <code>ui5-side-navigation</code>. Use the <code>ui5-side-navigation-item</code> component |
| 53 | + * for the fixed items, and optionally the <code>ui5-side-navigation-subitem</code> component to provide second-level items inside them. |
| 54 | + * |
| 55 | + * <b>Note:</b> In order to achieve the best user experience, it is recommended that you keep the fixed items "flat" (do not pass sub-items) |
| 56 | + * |
| 57 | + * @public |
| 58 | + * @slot |
| 59 | + */ |
| 60 | + fixedItems: { |
| 61 | + type: HTMLElement, |
| 62 | + invalidateParent: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + events: /** @lends sap.ui.webcomponents.fiori.SideNavigation.prototype */ { |
| 66 | + /** |
| 67 | + * Fired when the selection has changed via user interaction |
| 68 | + * |
| 69 | + * @event sap.ui.webcomponents.fiori.SideNavigation#selection-change |
| 70 | + * @param {HTMLElement} item the clicked item. |
| 71 | + * @public |
| 72 | + */ |
| 73 | + "selection-change": { |
| 74 | + item: { |
| 75 | + type: HTMLElement, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * @class |
| 83 | + * |
| 84 | + * <h3 class="comment-api-title">Overview</h3> |
| 85 | + * |
| 86 | + * |
| 87 | + * <h3>Usage</h3> |
| 88 | + * |
| 89 | + * <code>ui5-side-navigation</code> is used as a standard menu in applications. In order to add menu items use <code>ui5-side-navigation-items</code>. |
| 90 | + * |
| 91 | + * For the <code>ui5-side-navigation</code> |
| 92 | + * <h3>ES6 Module Import</h3> |
| 93 | + * |
| 94 | + * <code>import @ui5/webcomponents/dist/SideNavigation.js";</code> |
| 95 | + * |
| 96 | + * @constructor |
| 97 | + * @author SAP SE |
| 98 | + * @alias sap.ui.webcomponents.fiori.SideNavigation |
| 99 | + * @extends UI5Element |
| 100 | + * @tagname ui5-side-navigation |
| 101 | + * @since 1.0.0-rc.8 |
| 102 | + * @appenddocs SideNavigationItem |
| 103 | + * @public |
| 104 | + */ |
| 105 | +class SideNavigation extends UI5Element { |
| 106 | + static get metadata() { |
| 107 | + return metadata; |
| 108 | + } |
| 109 | + |
| 110 | + static get render() { |
| 111 | + return litRender; |
| 112 | + } |
| 113 | + |
| 114 | + static get styles() { |
| 115 | + return SideNavigationCss; |
| 116 | + } |
| 117 | + |
| 118 | + static get template() { |
| 119 | + return SideNavigationTemplate; |
| 120 | + } |
| 121 | + |
| 122 | + static get staticAreaTemplate() { |
| 123 | + return SideNavigationItemPopoverContentTemplate; |
| 124 | + } |
| 125 | + |
| 126 | + static async onDefine() { |
| 127 | + await Promise.all([ |
| 128 | + Tree.define(), |
| 129 | + ResponsivePopover.define(), |
| 130 | + ]); |
| 131 | + } |
| 132 | + |
| 133 | + onBeforeRendering() { |
| 134 | + this._items = this.items.map(item => { |
| 135 | + return { |
| 136 | + item, |
| 137 | + selected: ((item.items.some(subItem => subItem.selected) && this.collapsed) || item.selected), |
| 138 | + }; |
| 139 | + }); |
| 140 | + |
| 141 | + this._fixedItems = this.fixedItems.map(item => { |
| 142 | + return { |
| 143 | + item, |
| 144 | + selected: ((item.items.some(subItem => subItem.selected) && this.collapsed) || item.selected), |
| 145 | + }; |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + _setSelectedItem(item) { |
| 150 | + this._walk(current => { |
| 151 | + current.selected = false; |
| 152 | + }); |
| 153 | + item.selected = true; |
| 154 | + |
| 155 | + this.fireEvent("selection-change", { item }); |
| 156 | + } |
| 157 | + |
| 158 | + _buildPopoverContent(item) { |
| 159 | + this._popoverContent = { |
| 160 | + mainItem: item, |
| 161 | + mainItemSelected: item.selected && !item.items.some(subItem => subItem.selected), |
| 162 | + subItems: item.items, |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + handleTreeItemClick(event) { |
| 167 | + const treeItem = event.detail.item; |
| 168 | + const item = treeItem.associatedItem; |
| 169 | + |
| 170 | + if (item.selected && !this.collapsed) { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if (this.collapsed && item.items.length) { |
| 175 | + this._buildPopoverContent(item); |
| 176 | + const currentTree = this._itemsTree === event.target ? this._itemsTree : this._fixedItemsTree; |
| 177 | + this.openPicker(currentTree._getListItemForTreeItem(treeItem)); |
| 178 | + } else { |
| 179 | + this._setSelectedItem(item); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + handleListItemClick(event) { |
| 184 | + const listItem = event.detail.item; |
| 185 | + const item = listItem.associatedItem; |
| 186 | + |
| 187 | + if (item.selected) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + this._setSelectedItem(item); |
| 192 | + this.closePicker(); |
| 193 | + } |
| 194 | + |
| 195 | + async getPicker() { |
| 196 | + return (await this.getStaticAreaItemDomRef()).querySelector("ui5-responsive-popover"); |
| 197 | + } |
| 198 | + |
| 199 | + async openPicker(opener) { |
| 200 | + const responsivePopover = await this.getPicker(); |
| 201 | + responsivePopover.open(opener); |
| 202 | + } |
| 203 | + |
| 204 | + async closePicker(opener) { |
| 205 | + const responsivePopover = await this.getPicker(); |
| 206 | + responsivePopover.close(); |
| 207 | + } |
| 208 | + |
| 209 | + get _itemsTree() { |
| 210 | + return this.getDomRef().querySelector("#ui5-sn-items-tree"); |
| 211 | + } |
| 212 | + |
| 213 | + get _fixedItemsTree() { |
| 214 | + return this.getDomRef().querySelector("#ui5-sn-fixed-items-tree"); |
| 215 | + } |
| 216 | + |
| 217 | + _walk(callback) { |
| 218 | + this.items.forEach(current => { |
| 219 | + callback(current); |
| 220 | + |
| 221 | + current.items.forEach(currentSubitem => { |
| 222 | + callback(currentSubitem); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + this.fixedItems.forEach(current => { |
| 227 | + callback(current); |
| 228 | + |
| 229 | + current.items.forEach(currentSubitem => { |
| 230 | + callback(currentSubitem); |
| 231 | + }); |
| 232 | + }); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +SideNavigation.define(); |
| 237 | + |
| 238 | +export default SideNavigation; |
0 commit comments