groups.js 9.2 KB

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