Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/js/application/trigger/TriggerRule.js
12242 views
1
/**
2
* @provides trigger-rule
3
* @javelin
4
*/
5
6
JX.install('TriggerRule', {
7
8
construct: function() {
9
},
10
11
properties: {
12
rowID: null,
13
type: null,
14
value: null,
15
editor: null,
16
isValidRule: true,
17
invalidView: null
18
},
19
20
statics: {
21
newFromDictionary: function(map) {
22
return new JX.TriggerRule()
23
.setType(map.type)
24
.setValue(map.value)
25
.setIsValidRule(map.isValidRule)
26
.setInvalidView(map.invalidView);
27
},
28
},
29
30
members: {
31
_typeCell: null,
32
_valueCell: null,
33
_readValueCallback: null,
34
35
newRowContent: function() {
36
if (!this.getIsValidRule()) {
37
var invalid_cell = JX.$N(
38
'td',
39
{
40
colSpan: 2,
41
className: 'invalid-cell'
42
},
43
JX.$H(this.getInvalidView()));
44
45
return [invalid_cell];
46
}
47
48
var type_cell = this._getTypeCell();
49
var value_cell = this._getValueCell();
50
51
52
this._rebuildValueControl();
53
54
return [type_cell, value_cell];
55
},
56
57
getValueForSubmit: function() {
58
this._readValueFromControl();
59
60
return {
61
type: this.getType(),
62
value: this.getValue()
63
};
64
},
65
66
_getTypeCell: function() {
67
if (!this._typeCell) {
68
var editor = this.getEditor();
69
var types = editor.getTypes();
70
71
var options = [];
72
for (var ii = 0; ii < types.length; ii++) {
73
var type = types[ii];
74
75
if (!type.getIsSelectable()) {
76
continue;
77
}
78
79
options.push(
80
JX.$N('option', {value: type.getType()}, type.getName()));
81
}
82
83
var control = JX.$N('select', {}, options);
84
85
control.value = this.getType();
86
87
var on_change = JX.bind(this, this._onTypeChange, control);
88
JX.DOM.listen(control, 'change', null, on_change);
89
90
var attributes = {
91
className: 'type-cell'
92
};
93
94
this._typeCell = JX.$N('td', attributes, control);
95
}
96
97
return this._typeCell;
98
},
99
100
_onTypeChange: function(control) {
101
var new_type = control.value;
102
103
this.setType(new_type);
104
105
// Before we build a new control, change the rule value to be appropriate
106
// for the new rule type.
107
108
// TODO: Currently, we set the rule value to the default value for the
109
// type. This works most of the time, but if the user selects type "A",
110
// makes a change to the value, selects type "B", then changes back to
111
// type "A", it would be better to retain their edit. This is currently
112
// difficult because of tokenizers: if you save their value, you get a
113
// list of PHIDs which do not restore cleanly into tokens later.
114
115
var editor = this.getEditor();
116
var type = editor.getType(new_type);
117
this.setValue(type.getDefaultValue());
118
119
this._rebuildValueControl();
120
},
121
122
_getValueCell: function() {
123
if (!this._valueCell) {
124
var attributes = {
125
className: 'value-cell'
126
};
127
128
this._valueCell = JX.$N('td', attributes);
129
}
130
131
return this._valueCell;
132
},
133
134
_rebuildValueControl: function() {
135
var value_cell = this._getValueCell();
136
137
var editor = this.getEditor();
138
var type = editor.getType(this.getType());
139
var control = type.getControl();
140
141
var input = control.newInput(this);
142
this._readValueCallback = input.get;
143
144
JX.DOM.setContent(value_cell, input.node);
145
},
146
147
_readValueFromControl: function() {
148
if (this._readValueCallback) {
149
this.setValue(this._readValueCallback());
150
}
151
}
152
153
}
154
155
});
156
157