|
| 1 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 2 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 3 | +import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js"; |
| 4 | +import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js"; |
| 5 | +import CSSColor from "@ui5/webcomponents-base/dist/types/CSSColor.js"; |
| 6 | +import ItemNavigationBehavior from "@ui5/webcomponents-base/dist/types/ItemNavigationBehavior.js"; |
| 7 | +import { |
| 8 | + isSpace, |
| 9 | + isEnter, |
| 10 | +} from "@ui5/webcomponents-base/dist/Keys.js"; |
| 11 | +import ColorPaletteTemplate from "./generated/templates/ColorPaletteTemplate.lit.js"; |
| 12 | +import ColorPaletteItem from "./ColorPaletteItem.js"; |
| 13 | +import { |
| 14 | + COLORPALETTE_CONTAINER_LABEL, |
| 15 | +} from "./generated/i18n/i18n-defaults.js"; |
| 16 | + |
| 17 | +// Styles |
| 18 | +import ColorPaletteCss from "./generated/themes/ColorPalette.css.js"; |
| 19 | + |
| 20 | +/** |
| 21 | + * @public |
| 22 | + */ |
| 23 | +const metadata = { |
| 24 | + tag: "ui5-color-palette", |
| 25 | + managedSlots: true, |
| 26 | + properties: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ { |
| 27 | + /** |
| 28 | + * |
| 29 | + * The selected color. |
| 30 | + * @type {CSSColor} |
| 31 | + * @public |
| 32 | + */ |
| 33 | + value: { |
| 34 | + type: CSSColor, |
| 35 | + }, |
| 36 | + }, |
| 37 | + slots: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ { |
| 38 | + /** |
| 39 | + * Defines the <code>ui5-color-palette-item</code> items. |
| 40 | + * @type {HTMLElement[]} |
| 41 | + * @slot |
| 42 | + * @public |
| 43 | + */ |
| 44 | + "default": { |
| 45 | + propertyName: "colors", |
| 46 | + type: HTMLElement, |
| 47 | + invalidateOnChildChange: true, |
| 48 | + individualSlots: true, |
| 49 | + }, |
| 50 | + }, |
| 51 | + events: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ { |
| 52 | + /** |
| 53 | + * Fired when the user selects a color. |
| 54 | + * |
| 55 | + * @event |
| 56 | + * @public |
| 57 | + * @param {String} color the selected color |
| 58 | + */ |
| 59 | + change: { |
| 60 | + details: { |
| 61 | + color: { |
| 62 | + type: "String", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * @class |
| 71 | + * |
| 72 | + * <h3 class="comment-api-title">Overview</h3> |
| 73 | + * The ColorPalette provides the users with a range of predefined colors. |
| 74 | + * You can set them by using the ColorPaletteItem items as slots. |
| 75 | + * |
| 76 | + * <h3>Usage</h3> |
| 77 | + * The palette is intended for users, who don't want to check and remember the different values of the colors . |
| 78 | + * |
| 79 | + * For the <code>ui5-color-palette</code> |
| 80 | + * <h3>ES6 Module Import</h3> |
| 81 | + * |
| 82 | + * <code>import @ui5/webcomponents/dist/ColorPalette.js";</code> |
| 83 | + * |
| 84 | + * @constructor |
| 85 | + * @author SAP SE |
| 86 | + * @alias sap.ui.webcomponents.main.ColorPalette |
| 87 | + * @extends UI5Element |
| 88 | + * @tagname ui5-color-palette |
| 89 | + * @since 1.0.0-rc.12 |
| 90 | + * @appenddocs ColorPaletteItem |
| 91 | + * @public |
| 92 | + */ |
| 93 | +class ColorPalette extends UI5Element { |
| 94 | + static get metadata() { |
| 95 | + return metadata; |
| 96 | + } |
| 97 | + |
| 98 | + static get render() { |
| 99 | + return litRender; |
| 100 | + } |
| 101 | + |
| 102 | + static get styles() { |
| 103 | + return ColorPaletteCss; |
| 104 | + } |
| 105 | + |
| 106 | + static get template() { |
| 107 | + return ColorPaletteTemplate; |
| 108 | + } |
| 109 | + |
| 110 | + static get dependencies() { |
| 111 | + return [ColorPaletteItem]; |
| 112 | + } |
| 113 | + |
| 114 | + static async onDefine() { |
| 115 | + await fetchI18nBundle("@ui5/webcomponents"); |
| 116 | + } |
| 117 | + |
| 118 | + constructor() { |
| 119 | + super(); |
| 120 | + this.i18nBundle = getI18nBundle("@ui5/webcomponents"); |
| 121 | + this._itemNavigation = new ItemNavigation(this, { |
| 122 | + getItemsCallback: () => this.displayedColors, |
| 123 | + rowSize: 5, |
| 124 | + behavior: ItemNavigationBehavior.Cyclic, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + onBeforeRendering() { |
| 129 | + this.displayedColors.forEach((item, index) => { |
| 130 | + item.index = index + 1; |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + selectColor(item) { |
| 135 | + item.focus(); |
| 136 | + this._itemNavigation.update(item); |
| 137 | + |
| 138 | + this.value = item.value; |
| 139 | + |
| 140 | + this.fireEvent("change", { |
| 141 | + color: this.value, |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + _onclick(event) { |
| 146 | + if (event.target.localName === "ui5-color-palette-item") { |
| 147 | + this.selectColor(event.target); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + _onkeyup(event) { |
| 152 | + if (isSpace(event)) { |
| 153 | + event.preventDefault(); |
| 154 | + this.selectColor(event.target); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + _onkeydown(event) { |
| 159 | + if (isEnter(event)) { |
| 160 | + this.selectColor(event.target); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + get displayedColors() { |
| 165 | + return this.colors.filter(item => item.value).slice(0, 15); |
| 166 | + } |
| 167 | + |
| 168 | + get colorContainerLabel() { |
| 169 | + return this.i18nBundle.getText(COLORPALETTE_CONTAINER_LABEL); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +ColorPalette.define(); |
| 174 | + |
| 175 | +export default ColorPalette; |
0 commit comments