|
| 1 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 2 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 3 | +import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/events/PseudoEvents.js"; |
| 4 | +import ProductSwitchItemTemplate from "./generated/templates/ProductSwitchItemTemplate.lit.js"; |
| 5 | + |
| 6 | +// Styles |
| 7 | +import ProductSwitchItemCss from "./generated/themes/ProductSwitchItem.css.js"; |
| 8 | + |
| 9 | + |
| 10 | +const metadata = { |
| 11 | + tag: "ui5-product-switch-item", |
| 12 | + properties: { |
| 13 | + /** |
| 14 | + * Defines the title of the <code>ui5-product-switch-item</code>. |
| 15 | + * @type {string} |
| 16 | + * @defaultvalue "" |
| 17 | + * @public |
| 18 | + */ |
| 19 | + heading: { |
| 20 | + type: String, |
| 21 | + }, |
| 22 | + /** |
| 23 | + * Defines the subtitle of the <code>ui5-product-switch-item</code>. |
| 24 | + * @type {string} |
| 25 | + * @defaultvalue "" |
| 26 | + * @public |
| 27 | + */ |
| 28 | + subtitle: { |
| 29 | + type: String, |
| 30 | + }, |
| 31 | + /** |
| 32 | + * Defines the icon to be displayed as a graphical element within the <code>ui5-product-switch-item</code>. |
| 33 | + * <br><br> |
| 34 | + * Example: |
| 35 | + * <br> |
| 36 | + * <pre>ui5-product-switch-item icon="palette"</pre> |
| 37 | + * |
| 38 | + * See all the available icons in the <ui5-link target="_blank" href="https://openui5.hana.ondemand.com/test-resources/sap/m/demokit/iconExplorer/webapp/index.html" class="api-table-content-cell-link">Icon Explorer</ui5-link>. |
| 39 | + * |
| 40 | + * @type {string} |
| 41 | + * @defaultvalue "" |
| 42 | + * @public |
| 43 | + */ |
| 44 | + icon: { |
| 45 | + type: String, |
| 46 | + }, |
| 47 | + /** |
| 48 | + * Specifies a target where the <code>targetSrc</code> content must be open. |
| 49 | + * Options are the standard values for window.open() supported by browsers: _self, _top, _blank, _parent, _search. Alternatively, a frame name can be entered. |
| 50 | + * @type {string} |
| 51 | + * @defaultvalue "_self" |
| 52 | + * @public |
| 53 | + */ |
| 54 | + target: { |
| 55 | + type: String, |
| 56 | + defaultValue: "_self", |
| 57 | + }, |
| 58 | + /** |
| 59 | + * Defines the <code>ui5-product-switch-item</code> target URI. Supports standard hyperlink behavior. |
| 60 | + * @type {string} |
| 61 | + * @defaultvalue "" |
| 62 | + * @public |
| 63 | + */ |
| 64 | + targetSrc: { |
| 65 | + type: String, |
| 66 | + }, |
| 67 | + /** |
| 68 | + * Used to switch the active state (pressed or not) of the <code>ui5-product-switch-item</code>. |
| 69 | + * @private |
| 70 | + */ |
| 71 | + active: { |
| 72 | + type: Boolean, |
| 73 | + }, |
| 74 | + /** |
| 75 | + * Indicates whether the element is focused. |
| 76 | + * @private |
| 77 | + */ |
| 78 | + focused: { |
| 79 | + type: Boolean, |
| 80 | + }, |
| 81 | + _tabIndex: { |
| 82 | + type: String, |
| 83 | + defaultValue: "-1", |
| 84 | + noAttribute: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + slots: { |
| 88 | + // |
| 89 | + }, |
| 90 | + events: { |
| 91 | + /** |
| 92 | + * Fired when the <code>ui5-product-switch-item</code> is activated either with a |
| 93 | + * click/tap or by using the Enter or Space key. |
| 94 | + * |
| 95 | + * @event |
| 96 | + * @public |
| 97 | + */ |
| 98 | + click: {}, |
| 99 | + _focused: {}, |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * @class |
| 105 | + * <h3 class="comment-api-title">Overview</h3> |
| 106 | + * The <code>ui5-product-switch-item</code> represents the items displayed in the <code>ui5-product-switch</code> web component |
| 107 | + * <b>Note:</b> <code>ui5-product-switch-item</code> is not supported when used outside of <code>ui5-product-switch</code>. |
| 108 | + * <br><br> |
| 109 | + * <h3>ES6 Module Import</h3> |
| 110 | + * <code>import "@ui5/webcomponents-fiori/dist/ProductSwitchItem.js";</code> |
| 111 | + * |
| 112 | + * @constructor |
| 113 | + * @author SAP SE |
| 114 | + * @alias sap.ui.webcomponents.fiori.ProductSwitchItem |
| 115 | + * @extends sap.ui.webcomponents.base.UI5Element |
| 116 | + * @tagname ui5-product-switch-item |
| 117 | + * @public |
| 118 | + * @since 1.0.0-rc.5 |
| 119 | + */ |
| 120 | +class ProductSwitchItem extends UI5Element { |
| 121 | + constructor() { |
| 122 | + super(); |
| 123 | + |
| 124 | + this._deactivate = () => { |
| 125 | + if (this.active) { |
| 126 | + this.active = false; |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + static get metadata() { |
| 132 | + return metadata; |
| 133 | + } |
| 134 | + |
| 135 | + static get render() { |
| 136 | + return litRender; |
| 137 | + } |
| 138 | + |
| 139 | + static get styles() { |
| 140 | + return ProductSwitchItemCss; |
| 141 | + } |
| 142 | + |
| 143 | + static get template() { |
| 144 | + return ProductSwitchItemTemplate; |
| 145 | + } |
| 146 | + |
| 147 | + onEnterDOM() { |
| 148 | + document.addEventListener("mouseup", this._deactivate); |
| 149 | + } |
| 150 | + |
| 151 | + onExitDOM() { |
| 152 | + document.removeEventListener("mouseup", this._deactivate); |
| 153 | + } |
| 154 | + |
| 155 | + _onmousedown() { |
| 156 | + this.active = true; |
| 157 | + } |
| 158 | + |
| 159 | + _onkeydown(event) { |
| 160 | + if (isSpace(event) || isEnter(event)) { |
| 161 | + this.active = true; |
| 162 | + } |
| 163 | + |
| 164 | + if (isSpace(event)) { |
| 165 | + event.preventDefault(); |
| 166 | + } |
| 167 | + |
| 168 | + if (isEnter(event)) { |
| 169 | + this._fireItemClick(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + _onkeyup(event) { |
| 174 | + if (isSpace(event) || isEnter(event)) { |
| 175 | + this.active = false; |
| 176 | + } |
| 177 | + |
| 178 | + if (isSpace(event)) { |
| 179 | + this._fireItemClick(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + _onfocusout() { |
| 184 | + this.active = false; |
| 185 | + this.focused = false; |
| 186 | + } |
| 187 | + |
| 188 | + _onfocusin(event) { |
| 189 | + this.focused = true; |
| 190 | + |
| 191 | + this.fireEvent("_focused", event); |
| 192 | + } |
| 193 | + |
| 194 | + _fireItemClick() { |
| 195 | + this.fireEvent("click", { item: this }); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +ProductSwitchItem.define(); |
| 200 | + |
| 201 | +export default ProductSwitchItem; |
0 commit comments