UNPKG

5.09 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __importDefault = (this && this.__importDefault) || function (mod) {
27 return (mod && mod.__esModule) ? mod : { "default": mod };
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30exports.ToolbarSelectOption = exports.ToolbarSelect = void 0;
31var react_1 = __importDefault(require("react"));
32function hasProps(child) {
33 if (!child || typeof child !== 'object' || !('props' in child)) {
34 return false;
35 }
36 return true;
37}
38var ToolbarSelect = (function (_super) {
39 __extends(ToolbarSelect, _super);
40 function ToolbarSelect(props) {
41 var _this = _super.call(this, props) || this;
42 _this._node = null;
43 _this._listener = null;
44 _this.handleOpen = function (e) {
45 preventDefault(e);
46 _this.setState({ visible: true });
47 _this._subscribe();
48 };
49 _this.state = { visible: false };
50 return _this;
51 }
52 ToolbarSelect.prototype.componentWillUnmount = function () {
53 this._release();
54 };
55 ToolbarSelect.prototype.render = function () {
56 var _this = this;
57 var selectedChild;
58 var visible = this.state.visible;
59 var optionChildren = react_1.default.Children.map(this.props.children, function (child, i) {
60 if (!hasProps(child)) {
61 return null;
62 }
63 if (!selectedChild || child.props.selected) {
64 selectedChild = child;
65 }
66 var onChildSelect = child.props.onSelect ||
67 (_this.props.onSelect &&
68 _this.props.onSelect.bind(null, child.props.value, i));
69 return (react_1.default.createElement(ToolbarSelectOption, __assign({}, child.props, { onSelect: onChildSelect })));
70 });
71 return (react_1.default.createElement("a", { className: "toolbar-select toolbar-button", onClick: this.handleOpen.bind(this), onMouseDown: preventDefault, ref: function (node) {
72 _this._node = node;
73 }, title: this.props.title }, selectedChild === null || selectedChild === void 0 ? void 0 :
74 selectedChild.props.label,
75 react_1.default.createElement("svg", { width: "13", height: "10" },
76 react_1.default.createElement("path", { fill: "#666", d: "M 5 5 L 13 5 L 9 1 z" }),
77 react_1.default.createElement("path", { fill: "#666", d: "M 5 6 L 13 6 L 9 10 z" })),
78 react_1.default.createElement("ul", { className: 'toolbar-select-options' + (visible ? ' open' : '') }, optionChildren)));
79 };
80 ToolbarSelect.prototype._subscribe = function () {
81 if (!this._listener) {
82 this._listener = this.handleClick.bind(this);
83 document.addEventListener('click', this._listener);
84 }
85 };
86 ToolbarSelect.prototype._release = function () {
87 if (this._listener) {
88 document.removeEventListener('click', this._listener);
89 this._listener = null;
90 }
91 };
92 ToolbarSelect.prototype.handleClick = function (e) {
93 if (this._node !== e.target) {
94 preventDefault(e);
95 this.setState({ visible: false });
96 this._release();
97 }
98 };
99 return ToolbarSelect;
100}(react_1.default.Component));
101exports.ToolbarSelect = ToolbarSelect;
102function ToolbarSelectOption(_a) {
103 var onSelect = _a.onSelect, label = _a.label, selected = _a.selected;
104 return (react_1.default.createElement("li", { onMouseOver: function (e) {
105 e.currentTarget.className = 'hover';
106 }, onMouseOut: function (e) {
107 e.currentTarget.className = '';
108 }, onMouseDown: preventDefault, onMouseUp: onSelect },
109 label,
110 selected && (react_1.default.createElement("svg", { width: "13", height: "13" },
111 react_1.default.createElement("polygon", { points: "4.851,10.462 0,5.611 2.314,3.297 4.851,5.835\n 10.686,0 13,2.314 4.851,10.462" })))));
112}
113exports.ToolbarSelectOption = ToolbarSelectOption;
114function preventDefault(e) {
115 e.preventDefault();
116}
117//# sourceMappingURL=ToolbarSelect.js.map
\No newline at end of file