|
|
@@ -1,207 +1,79 @@
|
|
1
|
|
-/* @flow */
|
|
2
|
|
-
|
|
3
|
|
-import InlineDialog from '@atlaskit/inline-dialog';
|
|
4
|
1
|
import Tooltip from '@atlaskit/tooltip';
|
|
5
|
2
|
import PropTypes from 'prop-types';
|
|
6
|
|
-import React, { Component } from 'react';
|
|
7
|
|
-
|
|
8
|
|
-import { translate } from '../../base/i18n';
|
|
|
3
|
+import React from 'react';
|
|
9
|
4
|
|
|
10
|
|
-import { TOOLTIP_TO_POPUP_POSITION } from '../constants';
|
|
11
|
|
-import StatelessToolbarButton from './StatelessToolbarButton';
|
|
12
|
|
-
|
|
13
|
|
-declare var APP: Object;
|
|
|
5
|
+import AbstractToolbarButton from './AbstractToolbarButton';
|
|
14
|
6
|
|
|
15
|
7
|
/**
|
|
16
|
|
- * Represents a button in Toolbar on React.
|
|
|
8
|
+ * Represents a button in the toolbar.
|
|
17
|
9
|
*
|
|
18
|
|
- * @class ToolbarButton
|
|
19
|
10
|
* @extends AbstractToolbarButton
|
|
20
|
11
|
*/
|
|
21
|
|
-class ToolbarButton extends Component<*> {
|
|
22
|
|
- button: Object;
|
|
23
|
|
-
|
|
24
|
|
- _onClick: Function;
|
|
|
12
|
+class ToolbarButton extends AbstractToolbarButton {
|
|
|
13
|
+ /**
|
|
|
14
|
+ * Default values for {@code ToolbarButton} component's properties.
|
|
|
15
|
+ *
|
|
|
16
|
+ * @static
|
|
|
17
|
+ */
|
|
|
18
|
+ static defaultProps = {
|
|
|
19
|
+ tooltipPosition: 'top'
|
|
|
20
|
+ };
|
|
25
|
21
|
|
|
26
|
22
|
/**
|
|
27
|
|
- * Toolbar button component's property types.
|
|
|
23
|
+ * {@code ToolbarButton} component's property types.
|
|
28
|
24
|
*
|
|
29
|
25
|
* @static
|
|
30
|
26
|
*/
|
|
31
|
27
|
static propTypes = {
|
|
32
|
|
- ...StatelessToolbarButton.propTypes,
|
|
33
|
|
-
|
|
34
|
|
- /**
|
|
35
|
|
- * Object describing button.
|
|
36
|
|
- */
|
|
37
|
|
- button: PropTypes.object.isRequired,
|
|
38
|
|
-
|
|
39
|
|
- /**
|
|
40
|
|
- * Handler for component mount.
|
|
41
|
|
- */
|
|
42
|
|
- onMount: PropTypes.func,
|
|
|
28
|
+ ...AbstractToolbarButton.propTypes,
|
|
43
|
29
|
|
|
44
|
30
|
/**
|
|
45
|
|
- * Handler for component unmount.
|
|
|
31
|
+ * The text to display in the tooltip.
|
|
46
|
32
|
*/
|
|
47
|
|
- onUnmount: PropTypes.func,
|
|
|
33
|
+ tooltip: PropTypes.string,
|
|
48
|
34
|
|
|
49
|
35
|
/**
|
|
50
|
|
- * Translation helper function.
|
|
|
36
|
+ * From which direction the tooltip should appear, relative to the
|
|
|
37
|
+ * button.
|
|
51
|
38
|
*/
|
|
52
|
|
- t: PropTypes.func,
|
|
53
|
|
-
|
|
54
|
|
- /**
|
|
55
|
|
- * Indicates the position of the tooltip.
|
|
56
|
|
- */
|
|
57
|
|
- tooltipPosition: PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
|
|
58
|
|
- };
|
|
59
|
|
-
|
|
60
|
|
- /**
|
|
61
|
|
- * Initializes new ToolbarButton instance.
|
|
62
|
|
- *
|
|
63
|
|
- * @param {Object} props - The read-only properties with which the new
|
|
64
|
|
- * instance is to be initialized.
|
|
65
|
|
- */
|
|
66
|
|
- constructor(props: Object) {
|
|
67
|
|
- super(props);
|
|
68
|
|
-
|
|
69
|
|
- // Bind methods to save the context
|
|
70
|
|
- this._onClick = this._onClick.bind(this);
|
|
71
|
|
- }
|
|
72
|
|
-
|
|
73
|
|
- /**
|
|
74
|
|
- * Sets shortcut/tooltip
|
|
75
|
|
- * after mounting of the component.
|
|
76
|
|
- *
|
|
77
|
|
- * @inheritdoc
|
|
78
|
|
- * @returns {void}
|
|
79
|
|
- */
|
|
80
|
|
- componentDidMount(): void {
|
|
81
|
|
- this._setShortcut();
|
|
82
|
|
-
|
|
83
|
|
- if (this.props.onMount) {
|
|
84
|
|
- this.props.onMount();
|
|
85
|
|
- }
|
|
|
39
|
+ tooltipPosition: PropTypes.string
|
|
86
|
40
|
}
|
|
87
|
41
|
|
|
88
|
42
|
/**
|
|
89
|
|
- * Invokes on unmount handler if it was passed to the props.
|
|
|
43
|
+ * Renders the button of this {@code ToolbarButton}.
|
|
90
|
44
|
*
|
|
91
|
|
- * @inheritdoc
|
|
92
|
|
- * @returns {void}
|
|
|
45
|
+ * @param {Object} children - The children, if any, to be rendered inside
|
|
|
46
|
+ * the button. Presumably, contains the icon of this {@code ToolbarButton}.
|
|
|
47
|
+ * @protected
|
|
|
48
|
+ * @returns {ReactElement} The button of this {@code ToolbarButton}.
|
|
93
|
49
|
*/
|
|
94
|
|
- componentWillUnmount(): void {
|
|
95
|
|
- if (this.props.onUnmount) {
|
|
96
|
|
- this.props.onUnmount();
|
|
97
|
|
- }
|
|
|
50
|
+ _renderButton(children) {
|
|
|
51
|
+ return (
|
|
|
52
|
+ <div
|
|
|
53
|
+ aria-label = { this.props.accessibilityLabel }
|
|
|
54
|
+ className = 'toolbox-button'
|
|
|
55
|
+ onClick = { this.props.onClick }>
|
|
|
56
|
+ <Tooltip
|
|
|
57
|
+ description = { this.props.tooltip }
|
|
|
58
|
+ position = { this.props.tooltipPosition }>
|
|
|
59
|
+ { children }
|
|
|
60
|
+ </Tooltip>
|
|
|
61
|
+ </div>
|
|
|
62
|
+ );
|
|
98
|
63
|
}
|
|
99
|
64
|
|
|
100
|
65
|
/**
|
|
101
|
|
- * Implements React's {@link Component#render()}.
|
|
|
66
|
+ * Renders the icon of this {@code ToolbarButton}.
|
|
102
|
67
|
*
|
|
103
|
68
|
* @inheritdoc
|
|
104
|
|
- * @returns {ReactElement}
|
|
105
|
69
|
*/
|
|
106
|
|
- render(): React$Element<*> {
|
|
107
|
|
- const { button, t, tooltipPosition } = this.props;
|
|
108
|
|
- const props = {
|
|
109
|
|
- ...this.props,
|
|
110
|
|
- onClick: this._onClick
|
|
111
|
|
- };
|
|
112
|
|
-
|
|
113
|
|
- const buttonComponent = ( // eslint-disable-line no-extra-parens
|
|
114
|
|
- <Tooltip
|
|
115
|
|
- description = { button.tooltipText || t(button.tooltipKey) }
|
|
116
|
|
- position = { tooltipPosition }>
|
|
117
|
|
- <StatelessToolbarButton { ...props }>
|
|
118
|
|
- { this.props.children }
|
|
119
|
|
- </StatelessToolbarButton>
|
|
120
|
|
- </Tooltip>
|
|
121
|
|
- );
|
|
122
|
|
- let children = buttonComponent;
|
|
123
|
|
-
|
|
124
|
|
- const popupConfig = this._getPopupDisplayConfiguration();
|
|
125
|
|
-
|
|
126
|
|
- if (popupConfig) {
|
|
127
|
|
- const { dataAttr, dataInterpolate, position } = popupConfig;
|
|
128
|
|
-
|
|
129
|
|
- children = ( // eslint-disable-line no-extra-parens
|
|
130
|
|
- <InlineDialog
|
|
131
|
|
- content = {
|
|
132
|
|
- <div className = 'button-popover-message'>
|
|
133
|
|
- { t(dataAttr, dataInterpolate) }
|
|
134
|
|
- </div>
|
|
135
|
|
- }
|
|
136
|
|
- isOpen = { Boolean(popupConfig) }
|
|
137
|
|
- position = { position }>
|
|
138
|
|
- { buttonComponent }
|
|
139
|
|
- </InlineDialog>
|
|
140
|
|
- );
|
|
141
|
|
- }
|
|
142
|
|
-
|
|
|
70
|
+ _renderIcon() {
|
|
143
|
71
|
return (
|
|
144
|
|
- <div className = { `toolbar-button-wrapper ${button.id}-wrapper` }>
|
|
145
|
|
- { children }
|
|
|
72
|
+ <div className = 'toolbox-icon'>
|
|
|
73
|
+ <i className = { this.props.iconName } />
|
|
146
|
74
|
</div>
|
|
147
|
75
|
);
|
|
148
|
76
|
}
|
|
149
|
|
-
|
|
150
|
|
- /**
|
|
151
|
|
- * Wrapper on on click handler props for current button.
|
|
152
|
|
- *
|
|
153
|
|
- * @param {Event} event - Click event object.
|
|
154
|
|
- * @returns {void}
|
|
155
|
|
- * @private
|
|
156
|
|
- */
|
|
157
|
|
- _onClick(event) {
|
|
158
|
|
- this.props.onClick(event);
|
|
159
|
|
- }
|
|
160
|
|
-
|
|
161
|
|
- /**
|
|
162
|
|
- * Parses the props and state to find any popup that should be displayed
|
|
163
|
|
- * and returns an object describing how the popup should display.
|
|
164
|
|
- *
|
|
165
|
|
- * @private
|
|
166
|
|
- * @returns {Object|null}
|
|
167
|
|
- */
|
|
168
|
|
- _getPopupDisplayConfiguration() {
|
|
169
|
|
- const { button, tooltipPosition } = this.props;
|
|
170
|
|
- const { popups, popupDisplay } = button;
|
|
171
|
|
-
|
|
172
|
|
- if (!popups || !popupDisplay) {
|
|
173
|
|
- return null;
|
|
174
|
|
- }
|
|
175
|
|
-
|
|
176
|
|
- const { popupID } = popupDisplay;
|
|
177
|
|
- const currentPopup = popups.find(popup => popup.id === popupID);
|
|
178
|
|
-
|
|
179
|
|
- return Object.assign(
|
|
180
|
|
- {},
|
|
181
|
|
- currentPopup || {},
|
|
182
|
|
- {
|
|
183
|
|
- position: TOOLTIP_TO_POPUP_POSITION[tooltipPosition]
|
|
184
|
|
- });
|
|
185
|
|
- }
|
|
186
|
|
-
|
|
187
|
|
- /**
|
|
188
|
|
- * Sets shortcut and tooltip for current toolbar button.
|
|
189
|
|
- *
|
|
190
|
|
- * @private
|
|
191
|
|
- * @returns {void}
|
|
192
|
|
- */
|
|
193
|
|
- _setShortcut(): void {
|
|
194
|
|
- const { button } = this.props;
|
|
195
|
|
-
|
|
196
|
|
- if (button.shortcut && APP && APP.keyboardshortcut) {
|
|
197
|
|
- APP.keyboardshortcut.registerShortcut(
|
|
198
|
|
- button.shortcut,
|
|
199
|
|
- button.shortcutAttr,
|
|
200
|
|
- button.shortcutFunc,
|
|
201
|
|
- button.shortcutDescription
|
|
202
|
|
- );
|
|
203
|
|
- }
|
|
204
|
|
- }
|
|
205
|
77
|
}
|
|
206
|
78
|
|
|
207
|
|
-export default translate(ToolbarButton);
|
|
|
79
|
+export default ToolbarButton;
|