1 | import { a as __extends } from './tslib.es6-f952ba6f.js';
|
2 | import { K as KeyCodes, U as Utils } from './Utils-38a0872e.js';
|
3 | import { select } from 'd3';
|
4 | import { C as Component } from './Component-5173b5ea.js';
|
5 |
|
6 | var ColorPicker = (function (_super) {
|
7 | __extends(ColorPicker, _super);
|
8 | function ColorPicker(renderTarget, componentId) {
|
9 | if (componentId === void 0) { componentId = Utils.guid(); }
|
10 | var _this = _super.call(this, renderTarget) || this;
|
11 | _this.getSelectedColorValue = function () {
|
12 | return _this.selectedColor;
|
13 | };
|
14 | _this.showColorGrid = function () {
|
15 | _this.colorPickerElem.select(".tsi-colorGrid").style("display", "flex");
|
16 | _this.isColorGridVisible = true;
|
17 | _this.colorPickerElem.selectAll(".tsi-colorItem").nodes()[0].focus();
|
18 | _this.colorPickerElem.select(".tsi-colorPickerButton").attr("aria-expanded", true);
|
19 | };
|
20 | _this.hideColorGrid = function (withFocusBackToPickerButton) {
|
21 | if (withFocusBackToPickerButton === void 0) { withFocusBackToPickerButton = false; }
|
22 | _this.colorPickerElem.select(".tsi-colorGrid").style("display", "none");
|
23 | _this.isColorGridVisible = false;
|
24 | _this.colorPickerElem.select(".tsi-colorPickerButton").attr("aria-expanded", false);
|
25 | if (withFocusBackToPickerButton) {
|
26 | _this.colorPickerElem.select(".tsi-colorPickerButton").node().focus();
|
27 | }
|
28 | };
|
29 | _this.setSelectedColor = function (cStr, gridItem) {
|
30 | _this.colorPickerElem.select(".tsi-selectedColor").classed("tsi-noColor", false);
|
31 | _this.colorPickerElem.select(".tsi-selectedColor").style("background-color", cStr);
|
32 | _this.colorPickerElem.select(".tsi-selectedColorValue").text(cStr);
|
33 | _this.colorPickerElem.select(".tsi-colorItem.tsi-selected").classed("tsi-selected", false);
|
34 | _this.colorPickerElem.select(".tsi-colorItem.tsi-selected").attr("aria-selected", false);
|
35 | _this.colorPickerElem.select(".tsi-colorPickerButton").attr("aria-label", (_this.selectedColor ? _this.selectedColor : _this.getString('No color')) + ' ' + _this.getString('Select color'));
|
36 | gridItem.classed("tsi-selected", true);
|
37 | gridItem.attr("aria-selected", true);
|
38 | _this.selectedColor = cStr;
|
39 | };
|
40 | _this.getColorPickerElem = function () {
|
41 | return _this.colorPickerElem;
|
42 | };
|
43 | _this.componentId = componentId;
|
44 | return _this;
|
45 | }
|
46 | ColorPicker.prototype.render = function (options) {
|
47 | var _this = this;
|
48 | if (options === void 0) { options = {}; }
|
49 | this.chartOptions.setOptions(options);
|
50 | this.selectedColor = this.chartOptions.defaultColor;
|
51 | if (this.chartOptions.colors.indexOf(this.selectedColor) === -1) {
|
52 | this.chartOptions.colors.push(this.selectedColor);
|
53 | }
|
54 | this.colorPickerElem = select(this.renderTarget).classed("tsi-colorPicker", true);
|
55 | this.colorPickerElem.text('');
|
56 | _super.prototype.themify.call(this, this.colorPickerElem, this.chartOptions.theme);
|
57 |
|
58 | var colorPickerButton = this.colorPickerElem.append('button').classed("tsi-colorPickerButton", true)
|
59 | .attr("title", this.getString('Select color'))
|
60 | .attr("aria-label", (this.selectedColor ? this.selectedColor : this.getString('No color')) + ' ' + this.getString('Select color'))
|
61 | .attr("aria-controls", "tsi-colorGrid_" + this.componentId)
|
62 | .on('click', function (event) {
|
63 | if (_this.isColorGridVisible) {
|
64 | _this.hideColorGrid(true);
|
65 | }
|
66 | else {
|
67 | _this.chartOptions.onClick(event);
|
68 | _this.showColorGrid();
|
69 | }
|
70 | });
|
71 | if (this.selectedColor) {
|
72 | colorPickerButton.append('div').classed("tsi-selectedColor", true).style("background-color", this.selectedColor);
|
73 | }
|
74 | else {
|
75 | colorPickerButton.append('div').classed("tsi-selectedColor", true).classed("tsi-noColor", true);
|
76 | }
|
77 | colorPickerButton.append('span').classed("tsi-selectedColorValue", true).classed("hidden", this.chartOptions.isColorValueHidden)
|
78 | .attr("id", "tsi-selectedColorValue_" + this.componentId)
|
79 | .text(this.selectedColor ? this.selectedColor : this.getString('No color'));
|
80 |
|
81 | var colorGridElem = this.colorPickerElem.append('div').classed("tsi-colorGrid", true).attr("id", "tsi-colorGrid_" + this.componentId).attr("role", "grid");
|
82 | var colorGridRowElem = colorGridElem.append('div').classed("tsi-colorGridRow", true).attr("role", "row");
|
83 | this.chartOptions.colors.forEach(function (c, idx) {
|
84 | var gridItem = colorGridRowElem.append('div').classed("tsi-colorItem", true).classed("tsi-selected", c === _this.selectedColor)
|
85 | .attr("tabindex", 0)
|
86 | .attr("role", "gridcell")
|
87 | .attr("aria-label", c)
|
88 | .attr("aria-selected", c === _this.selectedColor)
|
89 | .style("background-color", c)
|
90 | .on('click', function (event) {
|
91 | event.preventDefault();
|
92 | event.stopPropagation();
|
93 | _this.chartOptions.onSelect(c);
|
94 | _this.hideColorGrid(true);
|
95 | _this.setSelectedColor(c, gridItem);
|
96 | })
|
97 | .on('keydown', function (event) {
|
98 | if (event.keyCode === KeyCodes.Tab && !event.shiftKey && idx === _this.chartOptions.colors.length - 1) {
|
99 | event.preventDefault();
|
100 | _this.colorPickerElem.selectAll(".tsi-colorItem").nodes()[0].focus();
|
101 | }
|
102 | else if (event.keyCode === KeyCodes.Enter) {
|
103 | event.preventDefault();
|
104 | event.stopPropagation();
|
105 | _this.chartOptions.onSelect(c);
|
106 | _this.hideColorGrid(true);
|
107 | _this.setSelectedColor(c, gridItem);
|
108 | }
|
109 | else if (event.keyCode === KeyCodes.Esc) {
|
110 | event.preventDefault();
|
111 | event.stopPropagation();
|
112 | _this.hideColorGrid(true);
|
113 | }
|
114 | });
|
115 | });
|
116 | };
|
117 | return ColorPicker;
|
118 | }(Component));
|
119 |
|
120 | export { ColorPicker as C };
|