settings.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. escapeHTML: function (text) {
  10. return text.toString()
  11. .split('&').join('&amp;')
  12. .split('<').join('&lt;')
  13. .split('>').join('&gt;')
  14. .split('"').join('&quot;')
  15. .split('\'').join('&#039;');
  16. },
  17. /**
  18. * Setup selection box for group selection.
  19. *
  20. * Values need to be separated by a pipe "|" character.
  21. * (mostly because a comma is more likely to be used
  22. * for groups)
  23. *
  24. * @param $elements jQuery element (hidden input) to setup select2 on
  25. * @param {Array} [extraOptions] extra options hash to pass to select2
  26. * @param {Array} [options] extra options
  27. * @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups
  28. */
  29. setupGroupsSelect: function($elements, extraOptions, options) {
  30. var self = this;
  31. options = options || {};
  32. if ($elements.length > 0) {
  33. // Let's load the data and THEN init our select
  34. $.ajax({
  35. url: OC.linkToOCS('cloud/groups', 2) + 'details',
  36. dataType: 'json',
  37. success: function(data) {
  38. var results = [];
  39. if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {
  40. data.ocs.data.groups.forEach(function(group) {
  41. if (!options.excludeAdmins || group.id !== 'admin') {
  42. results.push({ id: group.id, displayname: group.displayname });
  43. }
  44. })
  45. // note: settings are saved through a "change" event registered
  46. // on all input fields
  47. $elements.select2(_.extend({
  48. placeholder: t('settings', 'Groups'),
  49. allowClear: true,
  50. multiple: true,
  51. toggleSelect: true,
  52. separator: '|',
  53. data: { results: results, text: 'displayname' },
  54. initSelection: function(element, callback) {
  55. var groups = $(element).val();
  56. var selection;
  57. if (groups && results.length > 0) {
  58. selection = _.map(_.filter((groups || []).split('|').sort(), function(groupId) {
  59. return results.find(function(group) {
  60. return group.id === groupId
  61. }) !== undefined
  62. }), function(groupId) {
  63. return {
  64. id: groupId,
  65. displayname: results.find(function(group) {
  66. return group.id === groupId
  67. }).displayname
  68. }
  69. })
  70. } else if (groups) {
  71. selection = _.map((groups || []).split('|').sort(), function(groupId) {
  72. return {
  73. id: groupId,
  74. displayname: groupId
  75. };
  76. });
  77. }
  78. callback(selection);
  79. },
  80. formatResult: function(element) {
  81. return self.escapeHTML(element.displayname);
  82. },
  83. formatSelection: function(element) {
  84. return self.escapeHTML(element.displayname);
  85. },
  86. escapeMarkup: function(m) {
  87. // prevent double markup escape
  88. return m;
  89. }
  90. }, extraOptions || {}));
  91. } else {
  92. OC.Notification.show(t('settings', 'Group list is empty'), { type: 'error' });
  93. console.log(data);
  94. }
  95. },
  96. error: function(data) {
  97. OC.Notification.show(t('settings', 'Unable to retrieve the group list'), { type: 'error' });
  98. console.log(data);
  99. }
  100. });
  101. }
  102. }
  103. });