pandora/static/js/groupsDialog.js

257 lines
7.7 KiB
JavaScript
Raw Permalink Normal View History

2014-10-01 13:53:09 +00:00
'use strict';
pandora.ui.groupsDialog = function(options) {
var that = Ox.Dialog({
buttons: [
Ox.Button({
title: Ox._('Done')
})
.bindEvent({
click: function() {
that.close();
}
})
],
closeButton: true,
content: Ox.LoadingScreen().start(),
height: 256,
removeOnClose: true,
title: Ox._('Manage Groups'),
width: 584 + Ox.UI.SCROLLBAR_SIZE
}),
2017-11-04 09:53:27 +00:00
canManageGroups = pandora.hasCapability('canManageUsers'),
2014-10-01 13:53:09 +00:00
isItem = options.type == 'item',
2020-05-23 14:17:35 +00:00
isDocument = options.type == 'document',
2014-10-01 13:53:09 +00:00
$content,
$label,
$groups = [],
$checkboxes = [],
$inputs = [],
$labels = [],
$removeButtons = [],
$addButton,
groups,
selectedGroups;
if (options.groups) {
selectedGroups = options.groups;
2014-10-01 13:53:09 +00:00
renderGroups();
} else {
2020-05-23 14:17:35 +00:00
pandora.api[
isItem ? 'get'
: isDocument ? 'getDocument'
: 'getUser'
]({
id: options.id,
keys: ['groups']
}, function(result) {
selectedGroups = result.data.groups;
renderGroups();
});
}
2014-10-01 13:53:09 +00:00
function addGroup() {
// disableElements();
pandora.api.addGroup({name: Ox._('Untitled')}, function(result) {
renderGroups(result.data.id);
});
}
function disableElements() {
$checkboxes.concat($inputs).concat($removeButtons).concat($addButton)
.forEach(function($element) {
$element.options({disabled: true});
});
}
function edit(id) {
selectedGroups = groups.filter(function(group, index) {
return $checkboxes[index].value();
}).map(function(group) {
return group.name;
});
// disableElements();
2020-05-23 14:17:35 +00:00
Ox.Request.clearCache(
isItem ? 'get'
: isDocument ? 'getDocument'
: 'getUser'
);
pandora.api[
isItem ? 'edit'
: isDocument ? 'editDocument'
: 'editUser'
]({
2014-10-01 13:53:09 +00:00
id: options.id,
groups: selectedGroups
}, function(result) {
// enableElements();
updateLabel(id);
that.triggerEvent('groups', {groups: selectedGroups});
});
}
function editGroup(id, name) {
var index = Ox.getIndexById(groups, id),
previousName = groups[index].name;
2014-10-01 13:53:09 +00:00
// disableElements();
pandora.api.editGroup({id: id, name: name}, function(result) {
name = result.data.name;
if (Ox.contains(selectedGroups, previousName)) {
selectedGroups.splice(
selectedGroups.indexOf(previousName), 1, name
);
that.triggerEvent('groups', {groups: selectedGroups});
}
2014-10-01 13:53:09 +00:00
renderGroups();
});
}
function enableElements() {
$checkboxes.concat($inputs).concat($removeButtons).concat($addButton)
.forEach(function($element) {
$element.options({disabled: false});
});
}
function getLabelTitle(group) {
return (group.items ? Ox.formatNumber(group.items) : Ox._('No'))
+ ' ' + Ox._(group.items == 1 ? 'item' : 'items') + ', '
+ (group.users ? Ox.formatNumber(group.users) : Ox._('no'))
2014-10-01 13:53:09 +00:00
+ ' ' + Ox._(group.users == 1 ? 'user' : 'users');
}
function removeGroup(id) {
// disableElements();
pandora.api.removeGroup({id: id}, function(result) {
var index = Ox.getIndexById(groups, id),
name = groups[index].name;
groups.splice(index, 1);
index = selectedGroups.indexOf(name);
if (index > -1) {
selectedGroups.splice(index, 1);
that.triggerEvent('groups', {groups: selectedGroups});
}
renderGroups();
});
}
function renderGroup(group, index) {
var $group = $groups[index] = Ox.$('<div>')
.appendTo($content);
$checkboxes[index] = Ox.Checkbox({
disabled: !canManageGroups && !Ox.contains(pandora.user.groups, group.name),
2014-10-01 13:53:09 +00:00
value: Ox.contains(selectedGroups, group.name)
})
.css({
float: 'left',
margin: '4px'
})
.bindEvent({
change: function() {
edit(group.id);
}
})
.appendTo($group);
$inputs[index] = Ox.Input({
disabled: !canManageGroups,
2014-10-01 13:53:09 +00:00
value: group.name,
width: 192
})
.css({
float: 'left',
margin: '4px'
})
.bindEvent({
change: function(data) {
editGroup(group.id, data.value);
}
})
.appendTo($group);
$labels[index] = Ox.Label({
title: getLabelTitle(group),
width: 192
})
.css({
float: 'left',
margin: '4px'
})
.appendTo($group);
2014-10-01 13:53:09 +00:00
$removeButtons[index] = Ox.Button({
disabled: !canManageGroups,
2014-10-01 13:53:09 +00:00
title: Ox._('Remove Group'),
width: 128
})
.css({
float: 'left',
margin: '4px'
})
.bindEvent({
click: function() {
removeGroup(group.id);
}
})
.appendTo($group);
}
function renderGroups(focusInput) {
Ox.Request.clearCache('getGroups');
pandora.api.getGroups(function(result) {
if (!canManageGroups) {
result.data.groups = result.data.groups.filter(function(group) {
return Ox.contains(pandora.user.groups, group.name) ||
Ox.contains(selectedGroups, group.name);
});
}
2014-10-01 13:53:09 +00:00
groups = Ox.sortBy(result.data.groups, 'name');
$content = Ox.Element().css({margin: '12px'});
$label = Ox.Label({
textAlign: 'center',
title: Ox._(
2020-05-23 14:17:35 +00:00
isItem ? pandora.site.itemName.singular
: isDocument ? 'Document'
: 'User'
2014-10-01 13:53:09 +00:00
) + ': ' + options.name,
width: 552
})
.css({
2014-10-01 15:34:24 +00:00
margin: '16px 4px 4px 4px'
2014-10-01 13:53:09 +00:00
})
.appendTo($content);
groups.forEach(renderGroup);
$addButton = Ox.Button({
disabled: !canManageGroups,
2014-10-01 13:53:09 +00:00
title: Ox._('Add Group'),
width: 128
})
.css({
float: 'left',
2014-10-01 15:34:24 +00:00
margin: '4px 4px 16px 428px'
2014-10-01 13:53:09 +00:00
})
.bindEvent({
click: addGroup
})
.appendTo($content);
that.options({content: $content});
if (focusInput) {
$inputs[Ox.getIndexById(groups, focusInput)].focusInput(true);
}
});
}
function updateLabel(id) {
Ox.Request.clearCache('getGroup');
pandora.api.getGroup({id: id}, function(result) {
$labels[Ox.getIndexById(groups, id)].options({
title: getLabelTitle(result.data)
});
});
}
return that;
};