groups.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * Copyright (c) 2014, Raghu Nayyar <beingminimal@gmail.com>
  3. * Copyright (c) 2014, Arthur Schiwon <blizzz@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or later.
  5. * See the COPYING-README file.
  6. */
  7. var $userGroupList;
  8. var GroupList;
  9. GroupList = {
  10. activeGID: '',
  11. everyoneGID: '_everyone',
  12. addGroup: function (gid, usercount) {
  13. var $li = $userGroupList.find('.isgroup:last-child').clone();
  14. $li
  15. .data('gid', gid)
  16. .find('.groupname').text(gid);
  17. GroupList.setUserCount($li, usercount);
  18. $li.appendTo($userGroupList);
  19. GroupList.sortGroups();
  20. return $li;
  21. },
  22. setUserCount: function (groupLiElement, usercount) {
  23. var $groupLiElement = $(groupLiElement);
  24. if (usercount === undefined || usercount === 0 || usercount < 0) {
  25. usercount = '';
  26. $groupLiElement.data('usercount', 0);
  27. } else {
  28. $groupLiElement.data('usercount', usercount);
  29. }
  30. $groupLiElement.find('.usercount').text(usercount);
  31. },
  32. getUserCount: function ($groupLiElement) {
  33. return parseInt($groupLiElement.data('usercount'), 10);
  34. },
  35. modEveryoneCount: function(diff) {
  36. var $li = GroupList.getGroupLI(GroupList.everyoneGID);
  37. var count = GroupList.getUserCount($li) + diff;
  38. GroupList.setUserCount($li, count);
  39. },
  40. incEveryoneCount: function() {
  41. GroupList.modEveryoneCount(1);
  42. },
  43. decEveryoneCount: function() {
  44. GroupList.modEveryoneCount(-1);
  45. },
  46. getCurrentGID: function () {
  47. return GroupList.activeGID;
  48. },
  49. sortGroups: function () {
  50. var lis = $userGroupList.find('.isgroup').get();
  51. lis.sort(function (a, b) {
  52. return UserList.alphanum(
  53. $(a).find('a span').text(),
  54. $(b).find('a span').text()
  55. );
  56. });
  57. var items = [];
  58. $.each(lis, function (index, li) {
  59. items.push(li);
  60. if (items.length === 100) {
  61. $userGroupList.append(items);
  62. items = [];
  63. }
  64. });
  65. if (items.length > 0) {
  66. $userGroupList.append(items);
  67. }
  68. },
  69. createGroup: function (groupname) {
  70. $.post(
  71. OC.generateUrl('/settings/users/groups'),
  72. {
  73. id: groupname
  74. },
  75. function (result) {
  76. if (result.groupname) {
  77. var addedGroup = result.groupname;
  78. UserList.availableGroups = $.unique($.merge(UserList.availableGroups, [addedGroup]));
  79. GroupList.addGroup(result.groupname);
  80. $('.groupsselect, .subadminsselect')
  81. .append($('<option>', { value: result.groupname })
  82. .text(result.groupname));
  83. }
  84. GroupList.toggleAddGroup();
  85. }).fail(function(result, textStatus, errorThrown) {
  86. OC.dialogs.alert(result.responseJSON.message, t('settings', 'Error creating group'));
  87. });
  88. },
  89. update: function () {
  90. if (GroupList.updating) {
  91. return;
  92. }
  93. GroupList.updating = true;
  94. $.get(
  95. OC.generateUrl('/settings/users/groups'),
  96. {
  97. pattern: filter.getPattern(),
  98. filterGroups: filter.filterGroups ? 1 : 0
  99. },
  100. function (result) {
  101. var lis = [];
  102. if (result.status === 'success') {
  103. $.each(result.data, function (i, subset) {
  104. $.each(subset, function (index, group) {
  105. if (GroupList.getGroupLI(group.name).length > 0) {
  106. GroupList.setUserCount(GroupList.getGroupLI(group.name).first(), group.usercount);
  107. }
  108. else {
  109. var $li = GroupList.addGroup(group.name, group.usercount);
  110. $li.addClass('appear transparent');
  111. lis.push($li);
  112. }
  113. });
  114. });
  115. if (result.data.length > 0) {
  116. GroupList.doSort();
  117. }
  118. else {
  119. GroupList.noMoreEntries = true;
  120. }
  121. _.defer(function () {
  122. $(lis).each(function () {
  123. this.removeClass('transparent');
  124. });
  125. });
  126. }
  127. GroupList.updating = false;
  128. }
  129. );
  130. },
  131. elementBelongsToAddGroup: function (el) {
  132. return !(el !== $('#newgroup-form').get(0) &&
  133. $('#newgroup-form').find($(el)).length === 0);
  134. },
  135. hasAddGroupNameText: function () {
  136. var name = $('#newgroupname').val();
  137. return $.trim(name) !== '';
  138. },
  139. showGroup: function (gid) {
  140. GroupList.activeGID = gid;
  141. UserList.empty();
  142. UserList.update(gid);
  143. $userGroupList.find('li').removeClass('active');
  144. if (gid !== undefined) {
  145. //TODO: treat Everyone properly
  146. GroupList.getGroupLI(gid).addClass('active');
  147. }
  148. },
  149. isAddGroupButtonVisible: function () {
  150. return $('#newgroup-init').is(":visible");
  151. },
  152. toggleAddGroup: function (event) {
  153. if (GroupList.isAddGroupButtonVisible()) {
  154. event.stopPropagation();
  155. $('#newgroup-form').show();
  156. $('#newgroup-init').hide();
  157. $('#newgroupname').focus();
  158. }
  159. else {
  160. $('#newgroup-form').hide();
  161. $('#newgroup-init').show();
  162. $('#newgroupname').val('');
  163. }
  164. },
  165. isGroupNameValid: function (groupname) {
  166. if ($.trim(groupname) === '') {
  167. OC.dialogs.alert(
  168. t('settings', 'A valid group name must be provided'),
  169. t('settings', 'Error creating group'));
  170. return false;
  171. }
  172. return true;
  173. },
  174. hide: function (gid) {
  175. GroupList.getGroupLI(gid).hide();
  176. },
  177. show: function (gid) {
  178. GroupList.getGroupLI(gid).show();
  179. },
  180. remove: function (gid) {
  181. GroupList.getGroupLI(gid).remove();
  182. },
  183. empty: function () {
  184. $userGroupList.find('.isgroup').filter(function(index, item){
  185. return $(item).data('gid') !== '';
  186. }).remove();
  187. },
  188. initDeleteHandling: function () {
  189. //set up handler
  190. GroupDeleteHandler = new DeleteHandler('/settings/users/groups', 'groupname',
  191. GroupList.hide, GroupList.remove);
  192. //configure undo
  193. OC.Notification.hide();
  194. var msg = escapeHTML(t('settings', 'deleted {groupName}', {groupName: '%oid'})) + '<span class="undo">' +
  195. escapeHTML(t('settings', 'undo')) + '</span>';
  196. GroupDeleteHandler.setNotification(OC.Notification, 'deletegroup', msg,
  197. GroupList.show);
  198. //when to mark user for delete
  199. $userGroupList.on('click', '.delete', function () {
  200. // Call function for handling delete/undo
  201. GroupDeleteHandler.mark(GroupList.getElementGID(this));
  202. });
  203. //delete a marked user when leaving the page
  204. $(window).on('beforeunload', function () {
  205. GroupDeleteHandler.deleteEntry();
  206. });
  207. },
  208. getGroupLI: function (gid) {
  209. return $userGroupList.find('li.isgroup').filter(function () {
  210. return GroupList.getElementGID(this) === gid;
  211. });
  212. },
  213. getElementGID: function (element) {
  214. return ($(element).closest('li').data('gid') || '').toString();
  215. },
  216. getEveryoneCount: function () {
  217. $.ajax({
  218. type: "GET",
  219. dataType: "json",
  220. url: OC.generateUrl('/settings/ajax/geteveryonecount')
  221. }).success(function (data) {
  222. $('#everyonegroup').data('usercount', data.count);
  223. $('#everyonecount').text(data.count);
  224. });
  225. }
  226. };
  227. $(document).ready( function () {
  228. $userGroupList = $('#usergrouplist');
  229. GroupList.initDeleteHandling();
  230. GroupList.getEveryoneCount();
  231. // Display or hide of Create Group List Element
  232. $('#newgroup-form').hide();
  233. $('#newgroup-init').on('click', function (e) {
  234. GroupList.toggleAddGroup(e);
  235. });
  236. $(document).on('click keydown keyup', function(event) {
  237. if(!GroupList.isAddGroupButtonVisible() &&
  238. !GroupList.elementBelongsToAddGroup(event.target) &&
  239. !GroupList.hasAddGroupNameText()) {
  240. GroupList.toggleAddGroup();
  241. }
  242. // Escape
  243. if(!GroupList.isAddGroupButtonVisible() && event.keyCode && event.keyCode === 27) {
  244. GroupList.toggleAddGroup();
  245. }
  246. });
  247. // Responsible for Creating Groups.
  248. $('#newgroup-form form').submit(function (event) {
  249. event.preventDefault();
  250. if(GroupList.isGroupNameValid($('#newgroupname').val())) {
  251. GroupList.createGroup($('#newgroupname').val());
  252. }
  253. });
  254. // click on group name
  255. $userGroupList.on('click', '.isgroup', function () {
  256. GroupList.showGroup(GroupList.getElementGID(this));
  257. });
  258. });