boardsList.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. const subManager = new SubsManager();
  4. Template.boardList.helpers({
  5. hideCardCounterList() {
  6. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  7. return Utils.isMiniScreen() && Session.get('currentBoard'); */
  8. return true;
  9. },
  10. hideBoardMemberList() {
  11. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  12. return Utils.isMiniScreen() && Session.get('currentBoard'); */
  13. return true;
  14. },
  15. })
  16. Template.boardListHeaderBar.events({
  17. 'click .js-open-archived-board'() {
  18. Modal.open('archivedBoards');
  19. },
  20. });
  21. Template.boardListHeaderBar.helpers({
  22. title() {
  23. //if (FlowRouter.getRouteName() === 'template-container') {
  24. // return 'template-container';
  25. //} else {
  26. return FlowRouter.getRouteName() === 'home' ? 'my-boards' : 'public';
  27. //}
  28. },
  29. templatesBoardId() {
  30. return ReactiveCache.getCurrentUser()?.getTemplatesBoardId();
  31. },
  32. templatesBoardSlug() {
  33. return ReactiveCache.getCurrentUser()?.getTemplatesBoardSlug();
  34. },
  35. });
  36. BlazeComponent.extendComponent({
  37. onCreated() {
  38. Meteor.subscribe('setting');
  39. Meteor.subscribe('tableVisibilityModeSettings');
  40. let currUser = ReactiveCache.getCurrentUser();
  41. let userLanguage;
  42. if (currUser && currUser.profile) {
  43. userLanguage = currUser.profile.language
  44. }
  45. if (userLanguage) {
  46. TAPi18n.setLanguage(userLanguage);
  47. }
  48. },
  49. onRendered() {
  50. const itemsSelector = '.js-board:not(.placeholder)';
  51. const $boards = this.$('.js-boards');
  52. $boards.sortable({
  53. connectWith: '.js-boards',
  54. tolerance: 'pointer',
  55. appendTo: '.board-list',
  56. helper: 'clone',
  57. distance: 7,
  58. items: itemsSelector,
  59. placeholder: 'board-wrapper placeholder',
  60. start(evt, ui) {
  61. ui.helper.css('z-index', 1000);
  62. ui.placeholder.height(ui.helper.height());
  63. EscapeActions.executeUpTo('popup-close');
  64. },
  65. stop(evt, ui) {
  66. // To attribute the new index number, we need to get the DOM element
  67. // of the previous and the following card -- if any.
  68. const prevBoardDom = ui.item.prev('.js-board').get(0);
  69. const nextBoardBom = ui.item.next('.js-board').get(0);
  70. const sortIndex = Utils.calculateIndex(prevBoardDom, nextBoardBom, 1);
  71. const boardDomElement = ui.item.get(0);
  72. const board = Blaze.getData(boardDomElement);
  73. // Normally the jquery-ui sortable library moves the dragged DOM element
  74. // to its new position, which disrupts Blaze reactive updates mechanism
  75. // (especially when we move the last card of a list, or when multiple
  76. // users move some cards at the same time). To prevent these UX glitches
  77. // we ask sortable to gracefully cancel the move, and to put back the
  78. // DOM in its initial state. The card move is then handled reactively by
  79. // Blaze with the below query.
  80. $boards.sortable('cancel');
  81. board.move(sortIndex.base);
  82. },
  83. });
  84. // Disable drag-dropping if the current user is not a board member or is comment only
  85. this.autorun(() => {
  86. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  87. $boards.sortable({
  88. handle: '.board-handle',
  89. });
  90. }
  91. });
  92. },
  93. userHasTeams() {
  94. if (ReactiveCache.getCurrentUser()?.teams?.length > 0)
  95. return true;
  96. else
  97. return false;
  98. },
  99. teamsDatas() {
  100. const teams = ReactiveCache.getCurrentUser()?.teams
  101. if (teams)
  102. return teams.sort((a, b) => a.teamDisplayName.localeCompare(b.teamDisplayName));
  103. else
  104. return [];
  105. },
  106. userHasOrgs() {
  107. if (ReactiveCache.getCurrentUser()?.orgs?.length > 0)
  108. return true;
  109. else
  110. return false;
  111. },
  112. orgsDatas() {
  113. const orgs = ReactiveCache.getCurrentUser()?.orgs;
  114. if (orgs)
  115. return orgs.sort((a, b) => a.orgDisplayName.localeCompare(b.orgDisplayName));
  116. else
  117. return [];
  118. },
  119. userHasOrgsOrTeams() {
  120. const ret = this.userHasOrgs() || this.userHasTeams();
  121. return ret;
  122. },
  123. boards() {
  124. let query = {
  125. // { type: 'board' },
  126. // { type: { $in: ['board','template-container'] } },
  127. $and: [
  128. { archived: false },
  129. { type: { $in: ['board', 'template-container'] } },
  130. { $or: [] },
  131. { title: { $not: { $regex: /^\^.*\^$/ } } }
  132. ]
  133. };
  134. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  135. if (FlowRouter.getRouteName() === 'home') {
  136. query.$and[2].$or.push({ 'members.userId': Meteor.userId() });
  137. if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue) {
  138. query.$and.push({ 'permission': 'private' });
  139. }
  140. const currUser = ReactiveCache.getCurrentUser();
  141. let orgIdsUserBelongs = currUser?.orgIdsUserBelongs() || '';
  142. if (orgIdsUserBelongs) {
  143. let orgsIds = orgIdsUserBelongs.split(',');
  144. // for(let i = 0; i < orgsIds.length; i++){
  145. // query.$and[2].$or.push({'orgs.orgId': orgsIds[i]});
  146. // }
  147. //query.$and[2].$or.push({'orgs': {$elemMatch : {orgId: orgsIds[0]}}});
  148. query.$and[2].$or.push({ 'orgs.orgId': { $in: orgsIds } });
  149. }
  150. let teamIdsUserBelongs = currUser?.teamIdsUserBelongs() || '';
  151. if (teamIdsUserBelongs) {
  152. let teamsIds = teamIdsUserBelongs.split(',');
  153. // for(let i = 0; i < teamsIds.length; i++){
  154. // query.$or[2].$or.push({'teams.teamId': teamsIds[i]});
  155. // }
  156. //query.$and[2].$or.push({'teams': { $elemMatch : {teamId: teamsIds[0]}}});
  157. query.$and[2].$or.push({ 'teams.teamId': { $in: teamsIds } });
  158. }
  159. }
  160. else if (allowPrivateVisibilityOnly !== undefined && !allowPrivateVisibilityOnly.booleanValue) {
  161. query = {
  162. archived: false,
  163. //type: { $in: ['board','template-container'] },
  164. type: 'board',
  165. permission: 'public',
  166. };
  167. }
  168. const ret = ReactiveCache.getBoards(query, {
  169. sort: { sort: 1 /* boards default sorting */ },
  170. });
  171. return ret;
  172. },
  173. boardLists(boardId) {
  174. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  175. const lists = ReactiveCache.getLists({ 'boardId': boardId, 'archived': false },{sort: ['sort','asc']});
  176. const ret = lists.map(list => {
  177. let cardCount = ReactiveCache.getCards({ 'boardId': boardId, 'listId': list._id }).length;
  178. return `${list.title}: ${cardCount}`;
  179. });
  180. return ret;
  181. */
  182. return [];
  183. },
  184. boardMembers(boardId) {
  185. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  186. const lists = ReactiveCache.getBoard(boardId)
  187. const boardMembers = lists?.members.map(member => member.userId);
  188. return boardMembers;
  189. */
  190. return [];
  191. },
  192. isStarred() {
  193. const user = ReactiveCache.getCurrentUser();
  194. return user && user.hasStarred(this.currentData()._id);
  195. },
  196. isAdministrable() {
  197. const user = ReactiveCache.getCurrentUser();
  198. return user && user.isBoardAdmin(this.currentData()._id);
  199. },
  200. hasOvertimeCards() {
  201. return this.currentData().hasOvertimeCards();
  202. },
  203. hasSpentTimeCards() {
  204. return this.currentData().hasSpentTimeCards();
  205. },
  206. isInvited() {
  207. const user = ReactiveCache.getCurrentUser();
  208. return user && user.isInvitedTo(this.currentData()._id);
  209. },
  210. events() {
  211. return [
  212. {
  213. 'click .js-add-board': Popup.open('createBoard'),
  214. 'click .js-star-board'(evt) {
  215. const boardId = this.currentData()._id;
  216. ReactiveCache.getCurrentUser().toggleBoardStar(boardId);
  217. evt.preventDefault();
  218. },
  219. 'click .js-clone-board'(evt) {
  220. let title = getSlug(ReactiveCache.getBoard(this.currentData()._id).title) || 'cloned-board';
  221. Meteor.call(
  222. 'copyBoard',
  223. this.currentData()._id,
  224. {
  225. sort: ReactiveCache.getBoards({ archived: false }).length,
  226. type: 'board',
  227. title: ReactiveCache.getBoard(this.currentData()._id).title,
  228. },
  229. (err, res) => {
  230. if (err) {
  231. console.error(err);
  232. } else {
  233. Session.set('fromBoard', null);
  234. subManager.subscribe('board', res, false);
  235. FlowRouter.go('board', {
  236. id: res,
  237. slug: title,
  238. });
  239. }
  240. },
  241. );
  242. evt.preventDefault();
  243. },
  244. 'click .js-archive-board'(evt) {
  245. const boardId = this.currentData()._id;
  246. Meteor.call('archiveBoard', boardId);
  247. evt.preventDefault();
  248. },
  249. 'click .js-accept-invite'() {
  250. const boardId = this.currentData()._id;
  251. Meteor.call('acceptInvite', boardId);
  252. },
  253. 'click .js-decline-invite'() {
  254. const boardId = this.currentData()._id;
  255. Meteor.call('quitBoard', boardId, (err, ret) => {
  256. if (!err && ret) {
  257. Meteor.call('acceptInvite', boardId);
  258. FlowRouter.go('home');
  259. }
  260. });
  261. },
  262. 'click #resetBtn'(event) {
  263. let allBoards = document.getElementsByClassName("js-board");
  264. let currBoard;
  265. for (let i = 0; i < allBoards.length; i++) {
  266. currBoard = allBoards[i];
  267. currBoard.style.display = "block";
  268. }
  269. },
  270. 'click #filterBtn'(event) {
  271. event.preventDefault();
  272. let selectedTeams = document.querySelectorAll('#jsAllBoardTeams option:checked');
  273. let selectedTeamsValues = Array.from(selectedTeams).map(function (elt) { return elt.value });
  274. let index = selectedTeamsValues.indexOf("-1");
  275. if (index > -1) {
  276. selectedTeamsValues.splice(index, 1);
  277. }
  278. let selectedOrgs = document.querySelectorAll('#jsAllBoardOrgs option:checked');
  279. let selectedOrgsValues = Array.from(selectedOrgs).map(function (elt) { return elt.value });
  280. index = selectedOrgsValues.indexOf("-1");
  281. if (index > -1) {
  282. selectedOrgsValues.splice(index, 1);
  283. }
  284. if (selectedTeamsValues.length > 0 || selectedOrgsValues.length > 0) {
  285. const query = {
  286. $and: [
  287. { archived: false },
  288. { type: 'board' },
  289. { $or: [] }
  290. ]
  291. };
  292. if (selectedTeamsValues.length > 0) {
  293. query.$and[2].$or.push({ 'teams.teamId': { $in: selectedTeamsValues } });
  294. }
  295. if (selectedOrgsValues.length > 0) {
  296. query.$and[2].$or.push({ 'orgs.orgId': { $in: selectedOrgsValues } });
  297. }
  298. let filteredBoards = ReactiveCache.getBoards(query, {});
  299. let allBoards = document.getElementsByClassName("js-board");
  300. let currBoard;
  301. if (filteredBoards.length > 0) {
  302. let currBoardId;
  303. let found;
  304. for (let i = 0; i < allBoards.length; i++) {
  305. currBoard = allBoards[i];
  306. currBoardId = currBoard.classList[0];
  307. found = filteredBoards.find(function (board) {
  308. return board._id == currBoardId;
  309. });
  310. if (found !== undefined)
  311. currBoard.style.display = "block";
  312. else
  313. currBoard.style.display = "none";
  314. }
  315. }
  316. else {
  317. for (let i = 0; i < allBoards.length; i++) {
  318. currBoard = allBoards[i];
  319. currBoard.style.display = "none";
  320. }
  321. }
  322. }
  323. },
  324. },
  325. ];
  326. },
  327. }).register('boardList');