boardsList.js 12 KB

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