settings.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // Let's load the data and THEN init our select
  26. $.ajax({
  27. url: OC.linkToOCS('cloud/groups', 2) + 'details',
  28. dataType: 'json',
  29. success: function(data) {
  30. var results = [];
  31. if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {
  32. data.ocs.data.groups.forEach(function(group) {
  33. if (!options.excludeAdmins || group.id !== 'admin') {
  34. results.push({ id: group.id, displayname: group.displayname });
  35. }
  36. })
  37. // note: settings are saved through a "change" event registered
  38. // on all input fields
  39. $elements.select2(_.extend({
  40. placeholder: t('core', 'Groups'),
  41. allowClear: true,
  42. multiple: true,
  43. toggleSelect: true,
  44. separator: '|',
  45. data: { results: results, text: 'displayname' },
  46. initSelection: function(element, callback) {
  47. var groups = $(element).val();
  48. var selection;
  49. if (groups && results.length > 0) {
  50. selection = _.map((groups || []).split('|').sort(), function(groupId) {
  51. return {
  52. id: groupId,
  53. displayname: results.find(function (group) {
  54. return group.id === groupId;
  55. }).displayname
  56. };
  57. });
  58. } else if (groups) {
  59. selection = _.map((groups || []).split('|').sort(), function(groupId) {
  60. return {
  61. id: groupId,
  62. displayname: groupId
  63. };
  64. });
  65. }
  66. callback(selection);
  67. },
  68. formatResult: function(element) {
  69. return escapeHTML(element.displayname);
  70. },
  71. formatSelection: function(element) {
  72. return escapeHTML(element.displayname);
  73. },
  74. escapeMarkup: function(m) {
  75. // prevent double markup escape
  76. return m;
  77. }
  78. }, extraOptions || {}));
  79. } else {
  80. OC.Notification.show(t('settings', 'Group list is empty'), { type: 'error' });
  81. console.log(data);
  82. }
  83. },
  84. error: function(data) {
  85. OC.Notification.show(t('settings', 'Unable to retrieve the group list'), { type: 'error' });
  86. console.log(data);
  87. }
  88. });
  89. }
  90. }
  91. });