boardsList.js 12 KB

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