|
| 1 | +/** |
| 2 | + * Copyright (c) 2014, Vincent Petry <pvince81@owncloud.com> |
| 3 | + * This file is licensed under the Affero General Public License version 3 or later. |
| 4 | + * See the COPYING-README file. |
| 5 | + */ |
| 6 | +OC.Settings = OC.Settings || {}; |
| 7 | +OC.Settings = _.extend(OC.Settings, { |
| 8 | + |
| 9 | + _cachedGroups: null, |
| 10 | + |
| 11 | + escapeHTML: function (text) { |
| 12 | + return text.toString() |
| 13 | + .split('&').join('&') |
| 14 | + .split('<').join('<') |
| 15 | + .split('>').join('>') |
| 16 | + .split('"').join('"') |
| 17 | + .split('\'').join('''); |
| 18 | + }, |
| 19 | + |
| 20 | + /** |
| 21 | + * Setup selection box for group selection. |
| 22 | + * |
| 23 | + * Values need to be separated by a pipe "|" character. |
| 24 | + * (mostly because a comma is more likely to be used |
| 25 | + * for groups) |
| 26 | + * |
| 27 | + * @param $elements jQuery element (hidden input) to setup select2 on |
| 28 | + * @param {Array} [extraOptions] extra options hash to pass to select2 |
| 29 | + * @param {Array} [options] extra options |
| 30 | + * @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups |
| 31 | + */ |
| 32 | + setupGroupsSelect: function($elements, extraOptions, options) { |
| 33 | + var self = this; |
| 34 | + options = options || {}; |
| 35 | + if ($elements.length > 0) { |
| 36 | + // Let's load the data and THEN init our select |
| 37 | + $.ajax({ |
| 38 | + url: OC.linkToOCS('cloud/groups', 2) + 'details', |
| 39 | + dataType: 'json', |
| 40 | + success: function(data) { |
| 41 | + var results = []; |
| 42 | + |
| 43 | + if (data.ocs.data.groups && data.ocs.data.groups.length > 0) { |
| 44 | + |
| 45 | + data.ocs.data.groups.forEach(function(group) { |
| 46 | + if (!options.excludeAdmins || group.id !== 'admin') { |
| 47 | + results.push({ id: group.id, displayname: group.displayname }); |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + // note: settings are saved through a "change" event registered |
| 52 | + // on all input fields |
| 53 | + $elements.select2(_.extend({ |
| 54 | + placeholder: t('settings', 'Groups'), |
| 55 | + allowClear: true, |
| 56 | + multiple: true, |
| 57 | + toggleSelect: true, |
| 58 | + separator: '|', |
| 59 | + data: { results: results, text: 'displayname' }, |
| 60 | + initSelection: function(element, callback) { |
| 61 | + var groups = $(element).val(); |
| 62 | + var selection; |
| 63 | + if (groups && results.length > 0) { |
| 64 | + selection = _.map(_.filter((groups || []).split('|').sort(), function(groupId) { |
| 65 | + return results.find(function(group) { |
| 66 | + return group.id === groupId |
| 67 | + }) !== undefined |
| 68 | + }), function(groupId) { |
| 69 | + return { |
| 70 | + id: groupId, |
| 71 | + displayname: results.find(function(group) { |
| 72 | + return group.id === groupId |
| 73 | + }).displayname |
| 74 | + } |
| 75 | + }) |
| 76 | + } else if (groups) { |
| 77 | + selection = _.map((groups || []).split('|').sort(), function(groupId) { |
| 78 | + return { |
| 79 | + id: groupId, |
| 80 | + displayname: groupId |
| 81 | + }; |
| 82 | + }); |
| 83 | + } |
| 84 | + callback(selection); |
| 85 | + }, |
| 86 | + formatResult: function(element) { |
| 87 | + return self.escapeHTML(element.displayname); |
| 88 | + }, |
| 89 | + formatSelection: function(element) { |
| 90 | + return self.escapeHTML(element.displayname); |
| 91 | + }, |
| 92 | + escapeMarkup: function(m) { |
| 93 | + // prevent double markup escape |
| 94 | + return m; |
| 95 | + } |
| 96 | + }, extraOptions || {})); |
| 97 | + } else { |
| 98 | + OC.Notification.show(t('settings', 'Group list is empty'), { type: 'error' }); |
| 99 | + console.log(data); |
| 100 | + } |
| 101 | + }, |
| 102 | + error: function(data) { |
| 103 | + OC.Notification.show(t('settings', 'Unable to retrieve the group list'), { type: 'error' }); |
| 104 | + console.log(data); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | +}); |
0 commit comments