Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/js/phuix/PHUIXActionView.js
12241 views
1
/**
2
* @provides phuix-action-view
3
* @requires javelin-install
4
* javelin-dom
5
* javelin-util
6
* @javelin
7
*/
8
9
JX.install('PHUIXActionView', {
10
11
members: {
12
_node: null,
13
_name: null,
14
_icon: 'none',
15
_iconColor: null,
16
_disabled: false,
17
_label: false,
18
_handler: null,
19
_selected: false,
20
_divider: false,
21
_keyCommand: null,
22
_unresponsive: null,
23
24
_iconNode: null,
25
_nameNode: null,
26
27
setDisabled: function(disabled) {
28
this._disabled = disabled;
29
JX.DOM.alterClass(
30
this.getNode(),
31
'phabricator-action-view-disabled',
32
disabled);
33
34
this._buildIconNode(true);
35
36
return this;
37
},
38
39
setUnresponsive: function(unresponsive) {
40
this._unresponsive = unresponsive;
41
this._buildNameNode(true);
42
return this;
43
},
44
45
getDisabled: function() {
46
return this._disabled;
47
},
48
49
setLabel: function(label) {
50
this._label = label;
51
JX.DOM.alterClass(
52
this.getNode(),
53
'phabricator-action-view-label',
54
label);
55
return this;
56
},
57
58
setDivider: function(divider) {
59
this._divider = divider;
60
JX.DOM.alterClass(
61
this.getNode(),
62
'phabricator-action-view-type-divider',
63
divider);
64
return this;
65
},
66
67
setSelected: function(selected) {
68
this._selected = selected;
69
JX.DOM.alterClass(
70
this.getNode(),
71
'phabricator-action-view-selected',
72
selected);
73
74
return this;
75
},
76
77
setName: function(name) {
78
this._name = name;
79
this._buildNameNode(true);
80
return this;
81
},
82
83
setHandler: function(handler) {
84
this._handler = handler;
85
this._buildNameNode(true);
86
this._rebuildClasses();
87
return this;
88
},
89
90
setIcon: function(icon) {
91
this._icon = icon;
92
this._buildIconNode(true);
93
return this;
94
},
95
96
setIconColor: function(color) {
97
this._iconColor = color;
98
this._buildIconNode(true);
99
return this;
100
},
101
102
setHref: function(href) {
103
this._href = href;
104
this._buildNameNode(true);
105
this._rebuildClasses();
106
return this;
107
},
108
109
setKeyCommand: function(command) {
110
this._keyCommand = command;
111
112
var key_node = this._buildKeyCommandNode();
113
JX.DOM.setContent(key_node, this._keyCommand);
114
115
var node = this.getNode();
116
JX.DOM.alterClass(node, 'has-key-command', !!this._keyCommand);
117
118
return this;
119
},
120
121
getNode: function() {
122
if (!this._node) {
123
var classes = ['phabricator-action-view'];
124
125
if (this._icon) {
126
classes.push('action-has-icon');
127
}
128
129
var content = [
130
this._buildIconNode(),
131
this._buildNameNode(),
132
this._buildKeyCommandNode(),
133
];
134
135
var attr = {
136
className: classes.join(' ')
137
};
138
this._node = JX.$N('li', attr, content);
139
140
JX.Stratcom.addSigil(this._node, 'phuix-action-view');
141
142
this._rebuildClasses();
143
}
144
145
return this._node;
146
},
147
148
_rebuildClasses: function() {
149
var node = this.getNode();
150
151
var is_href = !!(this._href || this._handler);
152
JX.DOM.alterClass(node, 'phabricator-action-view-href', is_href);
153
},
154
155
_buildIconNode: function(dirty) {
156
if (!this._iconNode || dirty) {
157
var attr = {
158
className: [
159
'phui-icon-view',
160
'phabricator-action-view-icon',
161
'phui-font-fa'
162
].join(' ')
163
};
164
var node = JX.$N('span', attr);
165
166
var icon_class = this._icon;
167
if (this._disabled) {
168
icon_class = icon_class + ' grey';
169
}
170
171
if (this._iconColor) {
172
icon_class = icon_class + ' ' + this._iconColor;
173
}
174
175
JX.DOM.alterClass(node, icon_class, true);
176
177
if (this._iconNode && this._iconNode.parentNode) {
178
JX.DOM.replace(this._iconNode, node);
179
}
180
this._iconNode = node;
181
}
182
183
return this._iconNode;
184
},
185
186
_buildKeyCommandNode: function() {
187
if (!this._keyCommandNode) {
188
var attrs = {
189
className: 'keyboard-shortcut-key'
190
};
191
192
this._keyCommandNode = JX.$N('div', attrs);
193
}
194
return this._keyCommandNode;
195
},
196
197
_buildNameNode: function(dirty) {
198
if (!this._nameNode || dirty) {
199
var attr = {
200
className: 'phabricator-action-view-item'
201
};
202
203
var href = this._href;
204
if (!href && this._handler && !this._unresponsive) {
205
href = '#';
206
}
207
if (href) {
208
attr.href = href;
209
}
210
211
var tag = href ? 'a' : 'span';
212
213
var node = JX.$N(tag, attr, this._name);
214
JX.DOM.listen(node, 'click', null, JX.bind(this, this._onclick));
215
216
if (this._nameNode && this._nameNode.parentNode) {
217
JX.DOM.replace(this._nameNode, node);
218
}
219
this._nameNode = node;
220
}
221
222
return this._nameNode;
223
},
224
225
_onclick: function(e) {
226
if (this._unresponsive) {
227
e.prevent();
228
return;
229
}
230
231
if (this._handler) {
232
this._handler(e);
233
}
234
}
235
236
}
237
238
});
239
240