boardsList.js 12 KB

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