boardsList.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 !== undefined && currUser.teams !== 'undefined' ? currUser.orgIdsUserBelongs() : '';
  142. if (orgIdsUserBelongs && 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 !== undefined && currUser.teams !== 'undefined' ? currUser.teamIdsUserBelongs() : '';
  151. if (teamIdsUserBelongs && 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. return Boards.find(query, {
  169. sort: { sort: 1 /* boards default sorting */ },
  170. });
  171. },
  172. boardLists(boardId) {
  173. let boardLists = [];
  174. const lists = Lists.find({ 'boardId': boardId, 'archived': false },{sort: ['sort','asc']});
  175. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  176. lists.forEach(list => {
  177. let cardCount = Cards.find({ 'boardId': boardId, 'listId': list._id }).count()
  178. boardLists.push(`${list.title}: ${cardCount}`);
  179. });
  180. */
  181. return boardLists;
  182. },
  183. boardMembers(boardId) {
  184. let boardMembers = [];
  185. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  186. const lists = ReactiveCache.getBoard(boardId)
  187. let members = lists.members
  188. members.forEach(member => {
  189. boardMembers.push(member.userId);
  190. });
  191. */
  192. return boardMembers;
  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. subManager.subscribe('board', this.currentData()._id, false);
  204. return this.currentData().hasOvertimeCards();
  205. },
  206. hasSpentTimeCards() {
  207. subManager.subscribe('board', this.currentData()._id, false);
  208. return this.currentData().hasSpentTimeCards();
  209. },
  210. isInvited() {
  211. const user = ReactiveCache.getCurrentUser();
  212. return user && user.isInvitedTo(this.currentData()._id);
  213. },
  214. events() {
  215. return [
  216. {
  217. 'click .js-add-board': Popup.open('createBoard'),
  218. 'click .js-star-board'(evt) {
  219. const boardId = this.currentData()._id;
  220. ReactiveCache.getCurrentUser().toggleBoardStar(boardId);
  221. evt.preventDefault();
  222. },
  223. 'click .js-clone-board'(evt) {
  224. let title = getSlug(ReactiveCache.getBoard(this.currentData()._id).title) || 'cloned-board';
  225. Meteor.call(
  226. 'copyBoard',
  227. this.currentData()._id,
  228. {
  229. sort: Boards.find({ archived: false }).count(),
  230. type: 'board',
  231. title: ReactiveCache.getBoard(this.currentData()._id).title,
  232. },
  233. (err, res) => {
  234. if (err) {
  235. console.error(err);
  236. } else {
  237. Session.set('fromBoard', null);
  238. subManager.subscribe('board', res, false);
  239. FlowRouter.go('board', {
  240. id: res,
  241. slug: title,
  242. });
  243. }
  244. },
  245. );
  246. evt.preventDefault();
  247. },
  248. 'click .js-archive-board'(evt) {
  249. const boardId = this.currentData()._id;
  250. Meteor.call('archiveBoard', boardId);
  251. evt.preventDefault();
  252. },
  253. 'click .js-accept-invite'() {
  254. const boardId = this.currentData()._id;
  255. Meteor.call('acceptInvite', boardId);
  256. },
  257. 'click .js-decline-invite'() {
  258. const boardId = this.currentData()._id;
  259. Meteor.call('quitBoard', boardId, (err, ret) => {
  260. if (!err && ret) {
  261. Meteor.call('acceptInvite', boardId);
  262. FlowRouter.go('home');
  263. }
  264. });
  265. },
  266. 'click #resetBtn'(event) {
  267. let allBoards = document.getElementsByClassName("js-board");
  268. let currBoard;
  269. for (let i = 0; i < allBoards.length; i++) {
  270. currBoard = allBoards[i];
  271. currBoard.style.display = "block";
  272. }
  273. },
  274. 'click #filterBtn'(event) {
  275. event.preventDefault();
  276. let selectedTeams = document.querySelectorAll('#jsAllBoardTeams option:checked');
  277. let selectedTeamsValues = Array.from(selectedTeams).map(function (elt) { return elt.value });
  278. let index = selectedTeamsValues.indexOf("-1");
  279. if (index > -1) {
  280. selectedTeamsValues.splice(index, 1);
  281. }
  282. let selectedOrgs = document.querySelectorAll('#jsAllBoardOrgs option:checked');
  283. let selectedOrgsValues = Array.from(selectedOrgs).map(function (elt) { return elt.value });
  284. index = selectedOrgsValues.indexOf("-1");
  285. if (index > -1) {
  286. selectedOrgsValues.splice(index, 1);
  287. }
  288. if (selectedTeamsValues.length > 0 || selectedOrgsValues.length > 0) {
  289. const query = {
  290. $and: [
  291. { archived: false },
  292. { type: 'board' },
  293. { $or: [] }
  294. ]
  295. };
  296. if (selectedTeamsValues.length > 0) {
  297. query.$and[2].$or.push({ 'teams.teamId': { $in: selectedTeamsValues } });
  298. }
  299. if (selectedOrgsValues.length > 0) {
  300. query.$and[2].$or.push({ 'orgs.orgId': { $in: selectedOrgsValues } });
  301. }
  302. let filteredBoards = Boards.find(query, {}).fetch();
  303. let allBoards = document.getElementsByClassName("js-board");
  304. let currBoard;
  305. if (filteredBoards.length > 0) {
  306. let currBoardId;
  307. let found;
  308. for (let i = 0; i < allBoards.length; i++) {
  309. currBoard = allBoards[i];
  310. currBoardId = currBoard.classList[0];
  311. found = filteredBoards.find(function (board) {
  312. return board._id == currBoardId;
  313. });
  314. if (found !== undefined)
  315. currBoard.style.display = "block";
  316. else
  317. currBoard.style.display = "none";
  318. }
  319. }
  320. else {
  321. for (let i = 0; i < allBoards.length; i++) {
  322. currBoard = allBoards[i];
  323. currBoard.style.display = "none";
  324. }
  325. }
  326. }
  327. },
  328. },
  329. ];
  330. },
  331. }).register('boardList');