2015-12-02 23:38:11 +01:00
|
|
|
/**
|
|
|
|
* @provides javelin-behavior-comment-actions
|
|
|
|
* @requires javelin-behavior
|
|
|
|
* javelin-stratcom
|
|
|
|
* javelin-workflow
|
|
|
|
* javelin-dom
|
|
|
|
* phuix-form-control-view
|
|
|
|
* phuix-icon-view
|
2015-12-23 03:07:57 +01:00
|
|
|
* javelin-behavior-phabricator-gesture
|
2015-12-02 23:38:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
JX.behavior('comment-actions', function(config) {
|
|
|
|
var action_map = config.actions;
|
|
|
|
|
|
|
|
var action_node = JX.$(config.actionID);
|
|
|
|
var form_node = JX.$(config.formID);
|
|
|
|
var input_node = JX.$(config.inputID);
|
2015-12-04 15:59:58 +01:00
|
|
|
var place_node = JX.$(config.placeID);
|
2015-12-02 23:38:11 +01:00
|
|
|
|
|
|
|
var rows = {};
|
|
|
|
|
|
|
|
JX.DOM.listen(action_node, 'change', null, function() {
|
2015-12-04 23:37:27 +01:00
|
|
|
var option = find_option(action_node.value);
|
2015-12-02 23:38:11 +01:00
|
|
|
|
|
|
|
action_node.value = '+';
|
|
|
|
|
2015-12-04 23:37:27 +01:00
|
|
|
if (option) {
|
|
|
|
add_row(option);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function find_option(key) {
|
|
|
|
var options = action_node.options;
|
|
|
|
var option;
|
|
|
|
|
2015-12-02 23:38:11 +01:00
|
|
|
for (var ii = 0; ii < options.length; ii++) {
|
|
|
|
option = options[ii];
|
2015-12-04 23:37:27 +01:00
|
|
|
if (option.value == key) {
|
|
|
|
return option;
|
2015-12-02 23:38:11 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 23:37:27 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2015-12-02 23:38:11 +01:00
|
|
|
|
|
|
|
function add_row(option) {
|
|
|
|
var action = action_map[option.value];
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
option.disabled = true;
|
|
|
|
|
|
|
|
var icon = new JX.PHUIXIconView()
|
|
|
|
.setIcon('fa-times-circle');
|
|
|
|
var remove = JX.$N('a', {href: '#'}, icon.getNode());
|
|
|
|
|
|
|
|
var control = new JX.PHUIXFormControl()
|
|
|
|
.setLabel(action.label)
|
|
|
|
.setError(remove)
|
2015-12-04 16:56:03 +01:00
|
|
|
.setControl(action.type, action.spec);
|
2015-12-02 23:38:11 +01:00
|
|
|
var node = control.getNode();
|
|
|
|
|
2015-12-23 03:07:57 +01:00
|
|
|
JX.Stratcom.addSigil(node, 'touchable');
|
2015-12-02 23:38:11 +01:00
|
|
|
|
2015-12-23 03:07:57 +01:00
|
|
|
var remove_action = function() {
|
2015-12-02 23:38:11 +01:00
|
|
|
JX.DOM.remove(node);
|
|
|
|
delete rows[action.key];
|
|
|
|
option.disabled = false;
|
2015-12-23 03:07:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
JX.DOM.listen(node, 'gesture.swipe.end', null, function(e) {
|
|
|
|
var data = e.getData();
|
|
|
|
|
|
|
|
if (data.direction != 'left') {
|
|
|
|
// Didn't swipe left.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.length <= (JX.Vector.getDim(node).x / 2)) {
|
|
|
|
// Didn't swipe far enough.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_action();
|
|
|
|
});
|
|
|
|
|
|
|
|
rows[action.key] = control;
|
|
|
|
|
|
|
|
JX.DOM.listen(remove, 'click', null, function(e) {
|
|
|
|
e.kill();
|
|
|
|
remove_action();
|
2015-12-02 23:38:11 +01:00
|
|
|
});
|
|
|
|
|
2015-12-04 15:59:58 +01:00
|
|
|
place_node.parentNode.insertBefore(node, place_node);
|
2015-12-04 23:37:27 +01:00
|
|
|
|
|
|
|
return control;
|
2015-12-02 23:38:11 +01:00
|
|
|
}
|
|
|
|
|
2015-12-04 22:51:18 +01:00
|
|
|
function serialize_actions() {
|
|
|
|
var data = [];
|
|
|
|
|
|
|
|
for (var k in rows) {
|
|
|
|
data.push({
|
|
|
|
type: k,
|
2015-12-15 15:57:32 +01:00
|
|
|
value: rows[k].getValue(),
|
|
|
|
initialValue: action_map[k].initialValue || null
|
2015-12-04 22:51:18 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return JX.JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_data() {
|
|
|
|
var data = JX.DOM.convertFormToDictionary(form_node);
|
|
|
|
|
|
|
|
data.__preview__ = 1;
|
|
|
|
data[input_node.name] = serialize_actions();
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:37:27 +01:00
|
|
|
function restore_draft_actions(drafts) {
|
|
|
|
var draft;
|
|
|
|
var option;
|
|
|
|
var control;
|
|
|
|
|
|
|
|
for (var ii = 0; ii < drafts.length; ii++) {
|
|
|
|
draft = drafts[ii];
|
|
|
|
|
2015-12-15 15:57:32 +01:00
|
|
|
option = find_option(draft);
|
2015-12-04 23:37:27 +01:00
|
|
|
if (!option) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = add_row(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 22:51:18 +01:00
|
|
|
function onresponse(response) {
|
2015-12-22 21:27:51 +01:00
|
|
|
if (JX.Device.getDevice() != 'desktop') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-04 22:51:18 +01:00
|
|
|
var panel = JX.$(config.panelID);
|
|
|
|
if (!response.xactions.length) {
|
|
|
|
JX.DOM.hide(panel);
|
|
|
|
} else {
|
|
|
|
JX.DOM.setContent(
|
|
|
|
JX.$(config.timelineID),
|
2016-04-01 15:02:53 +02:00
|
|
|
JX.$H(response.xactions.join('')));
|
2015-12-04 22:51:18 +01:00
|
|
|
JX.DOM.show(panel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:59:51 +01:00
|
|
|
JX.DOM.listen(form_node, ['submit', 'didSyntheticSubmit'], null, function() {
|
2015-12-04 22:51:18 +01:00
|
|
|
input_node.value = serialize_actions();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (config.showPreview) {
|
|
|
|
var request = new JX.PhabricatorShapedRequest(
|
|
|
|
config.actionURI,
|
|
|
|
onresponse,
|
|
|
|
get_data);
|
|
|
|
|
|
|
|
var trigger = JX.bind(request, request.trigger);
|
|
|
|
|
|
|
|
JX.DOM.listen(form_node, 'keydown', null, trigger);
|
|
|
|
|
|
|
|
var always_trigger = function() {
|
|
|
|
new JX.Request(config.actionURI, onresponse)
|
|
|
|
.setData(get_data())
|
|
|
|
.send();
|
|
|
|
};
|
|
|
|
|
|
|
|
JX.DOM.listen(form_node, 'shouldRefresh', null, always_trigger);
|
|
|
|
request.start();
|
2015-12-22 21:27:51 +01:00
|
|
|
|
|
|
|
var ondevicechange = function() {
|
|
|
|
var panel = JX.$(config.panelID);
|
|
|
|
if (JX.Device.getDevice() == 'desktop') {
|
|
|
|
request.setRateLimit(500);
|
|
|
|
always_trigger();
|
|
|
|
} else {
|
|
|
|
// On mobile, don't show live previews and only save drafts every
|
|
|
|
// 10 seconds.
|
|
|
|
request.setRateLimit(10000);
|
|
|
|
JX.DOM.hide(panel);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ondevicechange();
|
|
|
|
|
|
|
|
JX.Stratcom.listen('phabricator-device-change', null, ondevicechange);
|
2015-12-04 22:51:18 +01:00
|
|
|
}
|
|
|
|
|
2015-12-04 23:37:27 +01:00
|
|
|
restore_draft_actions(config.drafts || []);
|
2015-12-22 21:27:51 +01:00
|
|
|
|
2015-12-02 23:38:11 +01:00
|
|
|
});
|