settings.js 2.5 KB

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