You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.2 KiB

  1. /**
  2. * Copyright (c) 2014, Raghu Nayyar <beingminimal@gmail.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. var GroupList = {
  7. addGroup: function(gid) {
  8. var li = $('li[data-gid]').last().clone();
  9. var ul = $('li[data-gid]').first().parent();
  10. li.attr('data-gid', gid);
  11. li.attr('data-usercount', 0);
  12. li.find('a span').first().text(gid);
  13. li.find('span[class=usercount]').first().text('');
  14. $(li).appendTo(ul);
  15. GroupList.sortGroups(0);
  16. },
  17. sortGroups: function(usercount) {
  18. var lis = $('li[data-gid]').filterAttr('data-usercount', usercount.toString()).get();
  19. var ul = $(lis).first().parent();
  20. lis.sort(function(a, b) {
  21. return UserList.alphanum($(a).find('a span').text(), $(b).find('a span').text());
  22. });
  23. var items = [];
  24. $.each(lis, function(index, li) {
  25. items.push(li);
  26. if(items.length === 100) {
  27. $(ul).append(items);
  28. items = [];
  29. }
  30. });
  31. if(items.length > 0) {
  32. $(ul).append(items);
  33. }
  34. },
  35. createGroup: function(groupname) {
  36. $.post(
  37. OC.filePath('settings', 'ajax', 'creategroup.php'),
  38. {
  39. groupname : groupname
  40. },
  41. function (result) {
  42. if (result.status !== 'success') {
  43. OC.dialogs.alert(result.data.message,
  44. t('settings', 'Error creating group'));
  45. } else {
  46. if (result.data.groupname) {
  47. var addedGroups = result.data.groupname;
  48. UserList.availableGroups = $.unique($.merge(UserList.availableGroups, addedGroups));
  49. GroupList.addGroup(result.data.groupname);
  50. }
  51. GroupList.toggleAddGroup();
  52. }
  53. }
  54. )
  55. },
  56. delete_group: function (gid) {
  57. if(GroupList.deleteGid !=='undefined') {
  58. GroupList.finishDelete(null);
  59. }
  60. //Set the undo flag
  61. GroupList.deleteCanceled = false;
  62. //Provide an option to undo
  63. $('#notification').data('deletegroup', true);
  64. OC.Notification.showHtml(t('settings', 'deleted') + ' ' + escapeHTML(gid) + '<span class="undo">' + t('settings', 'undo') + '</span>');
  65. },
  66. elementBelongsToAddGroup: function(el) {
  67. return !(el !== $('#newgroup-form').get(0)
  68. && $('#newgroup-form').find($(el)).length === 0);
  69. },
  70. showGroup: function (gid) {
  71. UserList.empty();
  72. UserList.update(gid);
  73. $('#app-navigation li').removeClass('active');
  74. if(gid !== undefined) {
  75. //TODO: treat Everyone properly
  76. $('#app-navigation li').filterAttr('data-gid', gid).addClass('active');
  77. }
  78. },
  79. isAddGroupButtonVisible: function() {
  80. return $('#newgroup-init').is(":visible");
  81. },
  82. toggleAddGroup: function(event) {
  83. if(GroupList.isAddGroupButtonVisible()) {
  84. event.stopPropagation();
  85. $('#newgroup-form').show();
  86. $('#newgroup-init').hide();
  87. $('#newgroupname').focus();
  88. } else {
  89. $('#newgroup-form').hide();
  90. $('#newgroup-init').show();
  91. $('#newgroupname').val('');
  92. }
  93. },
  94. isGroupNameValid: function(groupname) {
  95. if ($.trim(groupname) === '') {
  96. OC.dialogs.alert(
  97. t('settings', 'A valid groupname must be provided'),
  98. t('settings', 'Error creating group'));
  99. return false;
  100. }
  101. return true;
  102. },
  103. finishDelete: function (ready) {
  104. if (!GroupList.deleteCanceled && GroupList.deleteGid) {
  105. $.ajax({
  106. type: 'POST',
  107. url: OC.filePath('settings', 'ajax', 'removegroup.php'),
  108. async: false,
  109. data: { groupname: GroupList.deleteGid },
  110. success: function (result) {
  111. if (result.status === 'success') {
  112. // Remove undo option, & remove user from table
  113. OC.Notification.hide();
  114. $('li').filterAttr('data-gid', GroupList.deleteGid).remove();
  115. GroupList.deleteCanceled = true;
  116. if (ready) {
  117. ready();
  118. }
  119. } else {
  120. OC.dialogs.alert(result.data.message, t('settings', 'Unable to remove group'));
  121. }
  122. }
  123. });
  124. }
  125. },
  126. }
  127. $(document).ready( function () {
  128. $('ul').on('click', 'span.utils>a', function (event) {
  129. var li = $(this).parent().parent();
  130. var gid = $(li).attr('data-gid');
  131. $(li).hide();
  132. // Call function for handling delete/undo on Groups
  133. GroupList.delete_group(gid);
  134. });
  135. // Display or hide of Create Group List Element
  136. $('#newgroup-form').hide();
  137. $('#newgroup-init').on('click', function (e) {
  138. GroupList.toggleAddGroup(e);
  139. });
  140. $(document).on('click keydown keyup', function(event) {
  141. if(!GroupList.isAddGroupButtonVisible()
  142. && !GroupList.elementBelongsToAddGroup(event.target)) {
  143. GroupList.toggleAddGroup();
  144. }
  145. // Escape
  146. if(!GroupList.isAddGroupButtonVisible() && event.keyCode && event.keyCode === 27) {
  147. GroupList.toggleAddGroup();
  148. }
  149. });
  150. // Responsible for Creating Groups.
  151. $('#newgroup-form form').submit(function (event) {
  152. event.preventDefault();
  153. if(GroupList.isGroupNameValid($('#newgroupname').val())) {
  154. GroupList.createGroup($('#newgroupname').val());
  155. }
  156. });
  157. // click on group name
  158. // FIXME: also triggered when clicking on "remove"
  159. $('ul').on('click', 'li[data-gid]', function (event) {
  160. var li = $(this);
  161. var gid = $(li).attr('data-gid');
  162. // Call function for handling delete/undo on Groups
  163. GroupList.showGroup(gid);
  164. });
  165. // Implements Groupname editing.
  166. $('#app-navigation').on('click', 'img.rename', function (event) {
  167. event.stopPropagation();
  168. var img = $(this);
  169. var gid = img.parent().parent().attr('data-gid');
  170. var groupname = escapeHTML(img.parent().parent().attr('data-gid'));
  171. var input = $('<input type="text" value="' + groupname + '">');
  172. img.css('display', 'none');
  173. img.parent().children('span').replaceWith(input);
  174. input.focus();
  175. input.keypress(function (event) {
  176. if (event.keyCode === 13) {
  177. if ($(this).val().length > 0) {
  178. $.post(
  179. OC.filePath('settings', 'ajax', 'changegroupname.php'),
  180. { groupname: gid,
  181. groupname: $(this).val()
  182. }
  183. );
  184. input.blur();
  185. } else {
  186. input.blur();
  187. }
  188. }
  189. });
  190. input.blur(function () {
  191. var input = $(this), groupname = input.val();
  192. input.closest('li').attr('data-gid', groupname);
  193. input.replaceWith('<span>' + escapeHTML(groupname) + '</span>');
  194. img.css('display', '');
  195. });
  196. });
  197. // Implements Quota Settings Toggle.
  198. $('#app-navigation').find('.settings-button').on('click', function (e) {
  199. e.stopPropagation();
  200. $('#app-settings').removeClass('open');
  201. $('#app-settings').toggleClass('open');
  202. $(document).click(function() {
  203. $('#app-settings').removeClass('open');
  204. });
  205. });
  206. });