diff --git a/static/js/groupsDialog.js b/static/js/groupsDialog.js new file mode 100644 index 000000000..14c2e7da0 --- /dev/null +++ b/static/js/groupsDialog.js @@ -0,0 +1,217 @@ +'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 + }), + + isItem = options.type == 'item', + + $content, + $label, + $groups = [], + $checkboxes = [], + $inputs = [], + $labels = [], + $removeButtons = [], + $addButton, + + groups, + selectedGroups; + + 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) { + // disableElements(); + pandora.api.editGroup({id: id, name: name}, function(result) { + 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.$('