2014-09-09 20:00:53 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014, Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
OC.Settings = OC.Settings || {};
|
|
|
|
OC.Settings = _.extend(OC.Settings, {
|
2014-09-11 13:12:44 +04:00
|
|
|
|
|
|
|
_cachedGroups: null,
|
|
|
|
|
2014-09-09 20:00:53 +04:00
|
|
|
/**
|
|
|
|
* Setup selection box for group selection.
|
2014-09-10 16:30:02 +04:00
|
|
|
*
|
|
|
|
* Values need to be separated by a pipe "|" character.
|
|
|
|
* (mostly because a comma is more likely to be used
|
|
|
|
* for groups)
|
|
|
|
*
|
2014-09-09 20:00:53 +04:00
|
|
|
* @param $elements jQuery element (hidden input) to setup select2 on
|
2016-05-20 11:15:07 +03:00
|
|
|
* @param {Array} [extraOptions] extra options hash to pass to select2
|
|
|
|
* @param {Array} [options] extra options
|
|
|
|
* @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups
|
2014-09-09 20:00:53 +04:00
|
|
|
*/
|
2016-05-20 11:15:07 +03:00
|
|
|
setupGroupsSelect: function($elements, extraOptions, options) {
|
2014-09-11 13:12:44 +04:00
|
|
|
var self = this;
|
2016-05-20 11:15:07 +03:00
|
|
|
options = options || {};
|
2014-09-09 20:00:53 +04:00
|
|
|
if ($elements.length > 0) {
|
2018-03-05 18:16:43 +03:00
|
|
|
// Let's load the data and THEN init our select
|
|
|
|
$.ajax({
|
|
|
|
url: OC.generateUrl('/settings/users/groups'),
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
var results = [];
|
2014-09-09 20:00:53 +04:00
|
|
|
|
2018-03-05 18:16:43 +03:00
|
|
|
// add groups
|
|
|
|
if (!options.excludeAdmins) {
|
|
|
|
$.each(data.data.adminGroups, function(i, group) {
|
|
|
|
results.push({id:group.id, displayname:group.name});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$.each(data.data.groups, function(i, group) {
|
|
|
|
results.push({id:group.id, displayname:group.name});
|
|
|
|
});
|
|
|
|
// note: settings are saved through a "change" event registered
|
|
|
|
// on all input fields
|
|
|
|
$elements.select2(_.extend({
|
|
|
|
placeholder: t('core', 'Groups'),
|
|
|
|
allowClear: true,
|
|
|
|
multiple: true,
|
|
|
|
toggleSelect: true,
|
|
|
|
separator: '|',
|
|
|
|
data: { results: results, text: 'displayname' },
|
|
|
|
initSelection: function(element, callback) {
|
|
|
|
var groups = $(element).val();
|
|
|
|
var selection;
|
|
|
|
if (groups && results.length > 0) {
|
|
|
|
selection = _.map((groups || []).split('|').sort(), function(groupId) {
|
|
|
|
return {
|
|
|
|
id: groupId,
|
|
|
|
displayname: results.find(group =>group.id === groupId).displayname
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} else if (groups) {
|
|
|
|
selection = _.map((groups || []).split('|').sort(), function(groupId) {
|
|
|
|
return {
|
|
|
|
id: groupId,
|
|
|
|
displayname: groupId
|
|
|
|
};
|
2016-05-20 11:15:07 +03:00
|
|
|
});
|
|
|
|
}
|
2018-03-05 18:16:43 +03:00
|
|
|
callback(selection);
|
|
|
|
},
|
|
|
|
formatResult: function (element) {
|
|
|
|
return escapeHTML(element.displayname);
|
|
|
|
},
|
|
|
|
formatSelection: function (element) {
|
|
|
|
return escapeHTML(element.displayname);
|
|
|
|
},
|
|
|
|
escapeMarkup: function(m) {
|
|
|
|
// prevent double markup escape
|
|
|
|
return m;
|
2014-09-09 20:00:53 +04:00
|
|
|
}
|
2018-03-05 18:16:43 +03:00
|
|
|
}, extraOptions || {}));
|
2014-09-09 20:00:53 +04:00
|
|
|
},
|
2018-03-05 18:16:43 +03:00
|
|
|
error : function(data) {
|
|
|
|
OC.Notification.show(t('settings', 'Unable to retrieve the group list'), {type: 'error'});
|
|
|
|
console.log(data);
|
2014-09-09 20:00:53 +04:00
|
|
|
}
|
2018-03-05 18:16:43 +03:00
|
|
|
});
|
2014-09-09 20:00:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|