-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathwidget-dropdown.js
More file actions
83 lines (77 loc) · 2.69 KB
/
widget-dropdown.js
File metadata and controls
83 lines (77 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import Lang from '../../oop/lang.js';
import ReactDOM from 'react-dom';
/**
* Provides functionality for managing different dropdowns inside a widget.
*
* @class WidgetDropdown
*/
export default WrappedComponent => class extends WrappedComponent {
constructor(props) {
super(props);
this.state = {
...this.state,
dropdownTrigger: null,
itemDropdown: null
}
}
/**
* Lifecycle. Invoked when a component is receiving new props.
* This method is not called for the initial render.
*
* @instance
* @memberof WidgetDropdown
* @method componentWillReceiveProps
*/
componentWillReceiveProps(nextProps) {
if (Lang.isFunction(super.componentWillReceiveProps)) {
super.componentWillReceiveProps();
}
this.setState({
dropdownTrigger: null,
itemDropdown: null
});
}
/**
* Merges the provided object with two more properties:
* - expanded - boolean flag which indicates if an widget should be rendered exclusively.
* - toggleDropdown - function, which can be used by an widget in order to obtain exclusive state.
*
* @instance
* @memberof WidgetDropdown
* @method mergeDropdownProps
* @param {Object} obj The properties container which should be merged with the properties, related
* to dropdown state.
* @param {Object} itemKey They key of an React Widget which contains the dropdown.
* @return {Object} The merged object.
*/
mergeDropdownProps(obj, itemKey) {
return CKEDITOR.tools.merge(obj, {
expanded: this.state.itemDropdown === itemKey ? true : false,
tabIndex: this.state.dropdownTrigger === itemKey ? 0 : -1,
toggleDropdown: this.toggleDropdown.bind(this, itemKey)
});
}
/**
* Sets the active dropdown of the widget or discards the toggled item from the state.
*
* @instance
* @memberof WidgetDropdown
* @method toggleDropdown
* @param {Object} itemDropdown The widget which requests to toggle its dropdown.
* @param {Number} toggleDirection User movement direction when toggled via keyboard.
*/
toggleDropdown(itemDropdown, toggleDirection) {
this.setState({
dropdownTrigger: itemDropdown,
itemDropdown: itemDropdown !== this.state.itemDropdown ? itemDropdown : null
}, function() {
if (!this.state.itemDropdown) {
if (this.moveFocus) {
this.moveFocus(toggleDirection);
} else {
ReactDOM.findDOMNode(this).focus();
}
}
});
}
};