boardsList.js 12 KB

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