2013-10-13 02:08:11 +02:00
|
|
|
/**
|
|
|
|
* @provides javelin-behavior-policy-control
|
|
|
|
* @requires javelin-behavior
|
|
|
|
* javelin-dom
|
|
|
|
* javelin-util
|
2014-05-05 19:56:57 +02:00
|
|
|
* phuix-dropdown-menu
|
|
|
|
* phuix-action-list-view
|
|
|
|
* phuix-action-view
|
2013-10-15 01:59:16 +02:00
|
|
|
* javelin-workflow
|
2016-02-04 18:43:40 +01:00
|
|
|
* phuix-icon-view
|
2013-10-13 02:08:11 +02:00
|
|
|
* @javelin
|
|
|
|
*/
|
|
|
|
JX.behavior('policy-control', function(config) {
|
|
|
|
var control = JX.$(config.controlID);
|
|
|
|
var input = JX.$(config.inputID);
|
2013-10-13 02:08:35 +02:00
|
|
|
var value = config.value;
|
2013-10-13 02:08:11 +02:00
|
|
|
|
2015-12-17 22:27:36 +01:00
|
|
|
if (config.disabled) {
|
|
|
|
JX.DOM.alterClass(control, 'disabled-control', true);
|
|
|
|
JX.DOM.listen(control, 'click', null, function(e) {
|
|
|
|
e.kill();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:56:57 +02:00
|
|
|
var menu = new JX.PHUIXDropdownMenu(control)
|
|
|
|
.setWidth(260)
|
|
|
|
.setAlign('left');
|
2013-10-13 02:08:11 +02:00
|
|
|
|
|
|
|
menu.listen('open', function() {
|
2014-05-05 19:56:57 +02:00
|
|
|
var list = new JX.PHUIXActionListView();
|
2013-10-13 02:08:11 +02:00
|
|
|
|
|
|
|
for (var ii = 0; ii < config.groups.length; ii++) {
|
|
|
|
var group = config.groups[ii];
|
|
|
|
|
2014-05-05 19:56:57 +02:00
|
|
|
list.addItem(
|
|
|
|
new JX.PHUIXActionView()
|
|
|
|
.setName(config.labels[group])
|
2015-04-27 19:02:03 +02:00
|
|
|
.setLabel(true));
|
2013-10-13 02:08:11 +02:00
|
|
|
|
|
|
|
for (var jj = 0; jj < config.order[group].length; jj++) {
|
|
|
|
var phid = config.order[group][jj];
|
|
|
|
|
2013-10-15 01:59:16 +02:00
|
|
|
var onselect;
|
|
|
|
if (group == 'custom') {
|
|
|
|
onselect = JX.bind(null, function(phid) {
|
2015-05-06 00:59:56 +02:00
|
|
|
var uri = get_custom_uri(phid, config.capability);
|
2013-10-15 01:59:16 +02:00
|
|
|
|
|
|
|
new JX.Workflow(uri)
|
|
|
|
.setHandler(function(response) {
|
|
|
|
if (!response.phid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
replace_policy(phid, response.phid, response.info);
|
|
|
|
select_policy(response.phid);
|
|
|
|
})
|
|
|
|
.start();
|
|
|
|
|
|
|
|
}, phid);
|
2016-02-04 18:43:40 +01:00
|
|
|
} else if (phid == config.projectKey) {
|
|
|
|
onselect = JX.bind(null, function(phid) {
|
|
|
|
var uri = get_custom_uri(phid, config.capability);
|
|
|
|
|
|
|
|
new JX.Workflow(uri)
|
|
|
|
.setHandler(function(response) {
|
|
|
|
if (!response.phid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_policy(phid, response.phid, response.info);
|
|
|
|
select_policy(response.phid);
|
|
|
|
})
|
|
|
|
.start();
|
|
|
|
}, phid);
|
2013-10-15 01:59:16 +02:00
|
|
|
} else {
|
|
|
|
onselect = JX.bind(null, select_policy, phid);
|
|
|
|
}
|
2013-10-13 02:08:11 +02:00
|
|
|
|
2014-05-05 19:56:57 +02:00
|
|
|
var option = config.options[phid];
|
|
|
|
var item = new JX.PHUIXActionView()
|
|
|
|
.setName(option.name)
|
2014-05-29 05:56:20 +02:00
|
|
|
.setIcon(option.icon + ' darkgreytext')
|
2014-05-05 19:56:57 +02:00
|
|
|
.setHandler(JX.bind(null, function(fn, e) {
|
|
|
|
e.prevent();
|
|
|
|
menu.close();
|
|
|
|
fn();
|
|
|
|
}, onselect));
|
2013-10-13 02:08:11 +02:00
|
|
|
|
2013-10-13 02:08:35 +02:00
|
|
|
if (phid == value) {
|
|
|
|
item.setSelected(true);
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:56:57 +02:00
|
|
|
list.addItem(item);
|
2013-10-13 02:08:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:56:57 +02:00
|
|
|
menu.setContent(list.getNode());
|
2013-10-13 02:08:11 +02:00
|
|
|
});
|
|
|
|
|
2013-10-15 01:59:16 +02:00
|
|
|
|
|
|
|
var select_policy = function(phid) {
|
|
|
|
JX.DOM.setContent(
|
|
|
|
JX.DOM.find(control, 'span', 'policy-label'),
|
|
|
|
render_option(phid));
|
|
|
|
|
|
|
|
input.value = phid;
|
|
|
|
value = phid;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var render_option = function(phid, with_title) {
|
|
|
|
var option = config.options[phid];
|
|
|
|
|
|
|
|
var name = option.name;
|
|
|
|
if (with_title && (option.full != option.name)) {
|
|
|
|
name = JX.$N('span', {title: option.full}, name);
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:43:40 +01:00
|
|
|
return [render_icon(option.icon), name];
|
2013-10-15 01:59:16 +02:00
|
|
|
};
|
|
|
|
|
2016-02-04 18:43:40 +01:00
|
|
|
var render_icon = function(icon) {
|
|
|
|
return new JX.PHUIXIconView()
|
|
|
|
.setIcon(icon)
|
|
|
|
.getNode();
|
|
|
|
};
|
2013-10-15 01:59:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the workflow URI to create or edit a policy with a given PHID.
|
|
|
|
*/
|
2015-05-06 00:59:56 +02:00
|
|
|
var get_custom_uri = function(phid, capability) {
|
2016-02-04 18:43:40 +01:00
|
|
|
return JX.$U(config.editURI + phid + '/')
|
|
|
|
.setQueryParam('capability', capability)
|
|
|
|
.toString();
|
2013-10-15 01:59:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace an existing policy option with a new one. Used to swap out custom
|
|
|
|
* policies after the user edits them.
|
|
|
|
*/
|
|
|
|
var replace_policy = function(old_phid, new_phid, info) {
|
2016-02-04 18:43:40 +01:00
|
|
|
return add_policy(old_phid, new_phid, info, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new policy above an existing one, optionally replacing it.
|
|
|
|
*/
|
|
|
|
var add_policy = function(old_phid, new_phid, info, replace) {
|
|
|
|
if (config.options[new_phid]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-15 01:59:16 +02:00
|
|
|
config.options[new_phid] = info;
|
2016-02-04 18:43:40 +01:00
|
|
|
|
2013-10-15 01:59:16 +02:00
|
|
|
for (var k in config.order) {
|
|
|
|
for (var ii = 0; ii < config.order[k].length; ii++) {
|
|
|
|
if (config.order[k][ii] == old_phid) {
|
2016-02-04 18:43:40 +01:00
|
|
|
config.order[k].splice(ii, (replace ? 1 : 0), new_phid);
|
2013-10-15 01:59:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-13 02:08:11 +02:00
|
|
|
});
|