settings.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. _cachedGroups: null,
  9. /**
  10. * Setup selection box for group selection.
  11. *
  12. * Values need to be separated by a pipe "|" character.
  13. * (mostly because a comma is more likely to be used
  14. * for groups)
  15. *
  16. * @param $elements jQuery element (hidden input) to setup select2 on
  17. * @param {Array} [extraOptions] extra options hash to pass to select2
  18. * @param {Array} [options] extra options
  19. * @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups
  20. */
  21. setupGroupsSelect: function($elements, extraOptions, options) {
  22. var self = this;
  23. options = options || {};
  24. if ($elements.length > 0) {
  25. // note: settings are saved through a "change" event registered
  26. // on all input fields
  27. $elements.select2(_.extend({
  28. placeholder: t('core', 'Groups'),
  29. allowClear: true,
  30. multiple: true,
  31. separator: '|',
  32. query: _.debounce(function(query) {
  33. var queryData = {};
  34. if (self._cachedGroups && query.term === '') {
  35. query.callback({results: self._cachedGroups});
  36. return;
  37. }
  38. if (query.term !== '') {
  39. queryData = {
  40. pattern: query.term,
  41. filterGroups: 1
  42. };
  43. }
  44. $.ajax({
  45. url: OC.generateUrl('/settings/users/groups'),
  46. data: queryData,
  47. dataType: 'json',
  48. success: function(data) {
  49. var results = [];
  50. // add groups
  51. if (!options.excludeAdmins) {
  52. $.each(data.data.adminGroups, function(i, group) {
  53. results.push({id:group.id, displayname:group.name});
  54. });
  55. }
  56. $.each(data.data.groups, function(i, group) {
  57. results.push({id:group.id, displayname:group.name});
  58. });
  59. if (query.term === '') {
  60. // cache full list
  61. self._cachedGroups = results;
  62. }
  63. query.callback({results: results});
  64. }
  65. });
  66. }, 100, true),
  67. id: function(element) {
  68. return element.id;
  69. },
  70. initSelection: function(element, callback) {
  71. var selection =
  72. _.map(($(element).val() || []).split('|').sort(),
  73. function(groupName) {
  74. return {
  75. id: groupName,
  76. displayname: groupName
  77. };
  78. });
  79. callback(selection);
  80. },
  81. formatResult: function (element) {
  82. return escapeHTML(element.displayname);
  83. },
  84. formatSelection: function (element) {
  85. return escapeHTML(element.displayname);
  86. },
  87. escapeMarkup: function(m) {
  88. // prevent double markup escape
  89. return m;
  90. }
  91. }, extraOptions || {}));
  92. }
  93. }
  94. });