'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 }), canManageGroups = pandora.hasCapability('canManageUsers'), isItem = options.type == 'item', $content, $label, $groups = [], $checkboxes = [], $inputs = [], $labels = [], $removeButtons = [], $addButton, groups, selectedGroups; if (options.groups) { selectedGroups = options.groups; renderGroups(); } else { pandora.api[isItem ? 'get' : 'getUser']({ id: options.id, keys: ['groups'] }, function(result) { selectedGroups = result.data.groups; renderGroups(); }); } 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(); Ox.Request.clearCache(isItem ? 'get' : 'getUser'); pandora.api[isItem ? 'edit' : 'editUser']({ 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; // 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}); } 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')) + ' ' + 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.$('