boardsList.js 12 KB

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