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
|
|
|
|
* @param [extraOptions] extra options hash to pass to select2
|
|
|
|
*/
|
|
|
|
setupGroupsSelect: function($elements, extraOptions) {
|
2014-09-11 13:12:44 +04:00
|
|
|
var self = this;
|
2014-09-09 20:00:53 +04:00
|
|
|
if ($elements.length > 0) {
|
|
|
|
// note: settings are saved through a "change" event registered
|
|
|
|
// on all input fields
|
|
|
|
$elements.select2(_.extend({
|
|
|
|
placeholder: t('core', 'Groups'),
|
|
|
|
allowClear: true,
|
|
|
|
multiple: true,
|
2014-09-10 16:30:02 +04:00
|
|
|
separator: '|',
|
2014-09-11 13:12:44 +04:00
|
|
|
query: _.debounce(function(query) {
|
|
|
|
var queryData = {};
|
|
|
|
if (self._cachedGroups && query.term === '') {
|
|
|
|
query.callback({results: self._cachedGroups});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (query.term !== '') {
|
|
|
|
queryData = {
|
|
|
|
pattern: query.term,
|
|
|
|
filterGroups: 1
|
2014-09-09 20:00:53 +04:00
|
|
|
};
|
2014-09-11 13:12:44 +04:00
|
|
|
}
|
|
|
|
$.ajax({
|
2014-12-04 16:15:55 +03:00
|
|
|
url: OC.generateUrl('/settings/users/groups'),
|
2014-09-11 13:12:44 +04:00
|
|
|
data: queryData,
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
if (data.status === "success") {
|
|
|
|
var results = [];
|
2014-09-09 20:00:53 +04:00
|
|
|
|
2014-09-11 13:12:44 +04:00
|
|
|
// add groups
|
|
|
|
$.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});
|
|
|
|
});
|
2014-09-09 20:00:53 +04:00
|
|
|
|
2014-09-11 13:12:44 +04:00
|
|
|
if (query.term === '') {
|
|
|
|
// cache full list
|
|
|
|
self._cachedGroups = results;
|
|
|
|
}
|
|
|
|
query.callback({results: results});
|
|
|
|
} else {
|
|
|
|
//FIXME add error handling
|
|
|
|
}
|
2014-09-09 20:00:53 +04:00
|
|
|
}
|
2014-09-11 13:12:44 +04:00
|
|
|
});
|
|
|
|
}, 100, true),
|
2014-09-09 20:00:53 +04:00
|
|
|
id: function(element) {
|
|
|
|
return element.id;
|
|
|
|
},
|
|
|
|
initSelection: function(element, callback) {
|
|
|
|
var selection =
|
2014-09-10 16:30:02 +04:00
|
|
|
_.map(($(element).val() || []).split('|').sort(),
|
2014-09-09 20:00:53 +04:00
|
|
|
function(groupName) {
|
|
|
|
return {
|
|
|
|
id: groupName,
|
|
|
|
displayname: groupName
|
|
|
|
};
|
|
|
|
});
|
|
|
|
callback(selection);
|
|
|
|
},
|
|
|
|
formatResult: function (element) {
|
2014-09-10 16:30:02 +04:00
|
|
|
return escapeHTML(element.displayname);
|
2014-09-09 20:00:53 +04:00
|
|
|
},
|
|
|
|
formatSelection: function (element) {
|
2014-09-10 16:30:02 +04:00
|
|
|
return escapeHTML(element.displayname);
|
|
|
|
},
|
|
|
|
escapeMarkup: function(m) {
|
|
|
|
// prevent double markup escape
|
|
|
|
return m;
|
2014-09-09 20:00:53 +04:00
|
|
|
}
|
|
|
|
}, extraOptions || {}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|