groups.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. $sortGroupBy;
  9. var GroupList;
  10. GroupList = {
  11. activeGID: '',
  12. everyoneGID: '_everyone',
  13. filter: '',
  14. filterGroups: false,
  15. addGroup: function (gid, usercount) {
  16. var $li = $userGroupList.find('.isgroup:last-child').clone();
  17. $li
  18. .data('gid', gid)
  19. .find('.groupname').text(gid);
  20. GroupList.setUserCount($li, usercount);
  21. $li.appendTo($userGroupList);
  22. GroupList.sortGroups();
  23. return $li;
  24. },
  25. setUserCount: function (groupLiElement, usercount) {
  26. if ($sortGroupBy !== 1) {
  27. // If we don't sort by group count we don't display them either
  28. return;
  29. }
  30. var $groupLiElement = $(groupLiElement);
  31. if (usercount === undefined || usercount === 0 || usercount < 0) {
  32. usercount = '';
  33. $groupLiElement.data('usercount', 0);
  34. } else {
  35. $groupLiElement.data('usercount', usercount);
  36. }
  37. $groupLiElement.find('.usercount').text(usercount);
  38. },
  39. getUserCount: function ($groupLiElement) {
  40. return parseInt($groupLiElement.data('usercount'), 10);
  41. },
  42. modGroupCount: function(gid, diff) {
  43. var $li = GroupList.getGroupLI(gid);
  44. var count = GroupList.getUserCount($li) + diff;
  45. GroupList.setUserCount($li, count);
  46. },
  47. incEveryoneCount: function() {
  48. GroupList.modGroupCount(GroupList.everyoneGID, 1);
  49. },
  50. decEveryoneCount: function() {
  51. GroupList.modGroupCount(GroupList.everyoneGID, -1);
  52. },
  53. incGroupCount: function(gid) {
  54. GroupList.modGroupCount(gid, 1);
  55. },
  56. decGroupCount: function(gid) {
  57. GroupList.modGroupCount(gid, -1);
  58. },
  59. getCurrentGID: function () {
  60. return GroupList.activeGID;
  61. },
  62. sortGroups: function () {
  63. var lis = $userGroupList.find('.isgroup').get();
  64. lis.sort(function (a, b) {
  65. // "Everyone" always at the top
  66. if ($(a).data('gid') === '_everyone') {
  67. return -1;
  68. } else if ($(b).data('gid') === '_everyone') {
  69. return 1;
  70. }
  71. // "admin" always as second
  72. if ($(a).data('gid') === 'admin') {
  73. return -1;
  74. } else if ($(b).data('gid') === 'admin') {
  75. return 1;
  76. }
  77. if ($sortGroupBy === 1) {
  78. // Sort by user count first
  79. var $usersGroupA = $(a).data('usercount'),
  80. $usersGroupB = $(b).data('usercount');
  81. if ($usersGroupA > 0 && $usersGroupA > $usersGroupB) {
  82. return -1;
  83. }
  84. if ($usersGroupB > 0 && $usersGroupB > $usersGroupA) {
  85. return 1;
  86. }
  87. }
  88. // Fallback or sort by group name
  89. return UserList.alphanum(
  90. $(a).find('a span').text(),
  91. $(b).find('a span').text()
  92. );
  93. });
  94. var items = [];
  95. $.each(lis, function (index, li) {
  96. items.push(li);
  97. if (items.length === 100) {
  98. $userGroupList.append(items);
  99. items = [];
  100. }
  101. });
  102. if (items.length > 0) {
  103. $userGroupList.append(items);
  104. }
  105. },
  106. createGroup: function (groupname) {
  107. $.post(
  108. OC.generateUrl('/settings/users/groups'),
  109. {
  110. id: groupname
  111. },
  112. function (result) {
  113. if (result.groupname) {
  114. var addedGroup = result.groupname;
  115. UserList.availableGroups = $.unique($.merge(UserList.availableGroups, [addedGroup]));
  116. GroupList.addGroup(result.groupname);
  117. $('.groupsselect, .subadminsselect')
  118. .append($('<option>', { value: result.groupname })
  119. .text(result.groupname));
  120. }
  121. GroupList.toggleAddGroup();
  122. }).fail(function(result) {
  123. OC.Notification.showTemporary(t('settings', 'Error creating group: {message}', {message: result.responseJSON.message}));
  124. });
  125. },
  126. update: function () {
  127. if (GroupList.updating) {
  128. return;
  129. }
  130. GroupList.updating = true;
  131. $.get(
  132. OC.generateUrl('/settings/users/groups'),
  133. {
  134. pattern: this.filter,
  135. filterGroups: this.filterGroups ? 1 : 0,
  136. sortGroups: $sortGroupBy
  137. },
  138. function (result) {
  139. var lis = [];
  140. if (result.status === 'success') {
  141. $.each(result.data, function (i, subset) {
  142. $.each(subset, function (index, group) {
  143. if (GroupList.getGroupLI(group.name).length > 0) {
  144. GroupList.setUserCount(GroupList.getGroupLI(group.name).first(), group.usercount);
  145. }
  146. else {
  147. var $li = GroupList.addGroup(group.name, group.usercount);
  148. $li.addClass('appear transparent');
  149. lis.push($li);
  150. }
  151. });
  152. });
  153. if (result.data.length > 0) {
  154. GroupList.doSort();
  155. }
  156. else {
  157. GroupList.noMoreEntries = true;
  158. }
  159. _.defer(function () {
  160. $(lis).each(function () {
  161. this.removeClass('transparent');
  162. });
  163. });
  164. }
  165. GroupList.updating = false;
  166. }
  167. );
  168. },
  169. elementBelongsToAddGroup: function (el) {
  170. return !(el !== $('#newgroup-form').get(0) &&
  171. $('#newgroup-form').find($(el)).length === 0);
  172. },
  173. hasAddGroupNameText: function () {
  174. var name = $('#newgroupname').val();
  175. return $.trim(name) !== '';
  176. },
  177. showGroup: function (gid) {
  178. GroupList.activeGID = gid;
  179. UserList.empty();
  180. UserList.update(gid);
  181. $userGroupList.find('li').removeClass('active');
  182. if (gid !== undefined) {
  183. //TODO: treat Everyone properly
  184. GroupList.getGroupLI(gid).addClass('active');
  185. }
  186. },
  187. isAddGroupButtonVisible: function () {
  188. return $('#newgroup-init').is(":visible");
  189. },
  190. toggleAddGroup: function (event) {
  191. if (GroupList.isAddGroupButtonVisible()) {
  192. event.stopPropagation();
  193. $('#newgroup-form').show();
  194. $('#newgroup-init').hide();
  195. $('#newgroupname').focus();
  196. GroupList.handleAddGroupInput('');
  197. }
  198. else {
  199. $('#newgroup-form').hide();
  200. $('#newgroup-init').show();
  201. $('#newgroupname').val('');
  202. }
  203. },
  204. handleAddGroupInput: function (input) {
  205. if(input.length) {
  206. $('#newgroup-form input[type="submit"]').attr('disabled', null);
  207. } else {
  208. $('#newgroup-form input[type="submit"]').attr('disabled', 'disabled');
  209. }
  210. },
  211. isGroupNameValid: function (groupname) {
  212. if ($.trim(groupname) === '') {
  213. OC.Notification.showTemporary(t('settings', 'Error creating group: {message}', {
  214. message: t('settings', 'A valid group name must be provided')
  215. }));
  216. return false;
  217. }
  218. return true;
  219. },
  220. hide: function (gid) {
  221. GroupList.getGroupLI(gid).hide();
  222. },
  223. show: function (gid) {
  224. GroupList.getGroupLI(gid).show();
  225. },
  226. remove: function (gid) {
  227. GroupList.getGroupLI(gid).remove();
  228. },
  229. empty: function () {
  230. $userGroupList.find('.isgroup').filter(function(index, item){
  231. return $(item).data('gid') !== '';
  232. }).remove();
  233. },
  234. initDeleteHandling: function () {
  235. //set up handler
  236. GroupDeleteHandler = new DeleteHandler('/settings/users/groups', 'groupname',
  237. GroupList.hide, GroupList.remove);
  238. //configure undo
  239. OC.Notification.hide();
  240. var msg = escapeHTML(t('settings', 'deleted {groupName}', {groupName: '%oid'})) + '<span class="undo">' +
  241. escapeHTML(t('settings', 'undo')) + '</span>';
  242. GroupDeleteHandler.setNotification(OC.Notification, 'deletegroup', msg,
  243. GroupList.show);
  244. //when to mark user for delete
  245. $userGroupList.on('click', '.delete', function () {
  246. // Call function for handling delete/undo
  247. GroupDeleteHandler.mark(GroupList.getElementGID(this));
  248. });
  249. //delete a marked user when leaving the page
  250. $(window).on('beforeunload', function () {
  251. GroupDeleteHandler.deleteEntry();
  252. });
  253. },
  254. getGroupLI: function (gid) {
  255. return $userGroupList.find('li.isgroup').filter(function () {
  256. return GroupList.getElementGID(this) === gid;
  257. });
  258. },
  259. getElementGID: function (element) {
  260. return ($(element).closest('li').data('gid') || '').toString();
  261. },
  262. getEveryoneCount: function () {
  263. $.ajax({
  264. type: "GET",
  265. dataType: "json",
  266. url: OC.generateUrl('/settings/users/stats')
  267. }).success(function (data) {
  268. $('#everyonegroup').data('usercount', data.totalUsers);
  269. $('#everyonecount').text(data.totalUsers);
  270. });
  271. }
  272. };
  273. $(document).ready( function () {
  274. $userGroupList = $('#usergrouplist');
  275. GroupList.initDeleteHandling();
  276. $sortGroupBy = $userGroupList.data('sort-groups');
  277. if ($sortGroupBy === 1) {
  278. // Disabled due to performance issues, when we don't need it for sorting
  279. GroupList.getEveryoneCount();
  280. }
  281. // Display or hide of Create Group List Element
  282. $('#newgroup-form').hide();
  283. $('#newgroup-init').on('click', function (e) {
  284. GroupList.toggleAddGroup(e);
  285. });
  286. $(document).on('click keydown keyup', function(event) {
  287. if(!GroupList.isAddGroupButtonVisible() &&
  288. !GroupList.elementBelongsToAddGroup(event.target) &&
  289. !GroupList.hasAddGroupNameText()) {
  290. GroupList.toggleAddGroup();
  291. }
  292. // Escape
  293. if(!GroupList.isAddGroupButtonVisible() && event.keyCode && event.keyCode === 27) {
  294. GroupList.toggleAddGroup();
  295. }
  296. });
  297. // Responsible for Creating Groups.
  298. $('#newgroup-form form').submit(function (event) {
  299. event.preventDefault();
  300. if(GroupList.isGroupNameValid($('#newgroupname').val())) {
  301. GroupList.createGroup($('#newgroupname').val());
  302. }
  303. });
  304. // click on group name
  305. $userGroupList.on('click', '.isgroup', function () {
  306. GroupList.showGroup(GroupList.getElementGID(this));
  307. });
  308. $('#newgroupname').on('input', function(){
  309. GroupList.handleAddGroupInput(this.value);
  310. });
  311. });