boardsList.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 Meteor.user() && Meteor.user().getTemplatesBoardId();
  31. },
  32. templatesBoardSlug() {
  33. return Meteor.user() && Meteor.user().getTemplatesBoardSlug();
  34. },
  35. });
  36. BlazeComponent.extendComponent({
  37. onCreated() {
  38. Meteor.subscribe('setting');
  39. Meteor.subscribe('tableVisibilityModeSettings');
  40. let currUser = Meteor.user();
  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 (Meteor.user() != null && Meteor.user().teams && Meteor.user().teams.length > 0)
  95. return true;
  96. else
  97. return false;
  98. },
  99. teamsDatas() {
  100. if (Meteor.user().teams)
  101. return Meteor.user().teams.sort((a, b) => a.teamDisplayName.localeCompare(b.teamDisplayName));
  102. else
  103. return [];
  104. },
  105. userHasOrgs() {
  106. if (Meteor.user() != null && Meteor.user().orgs && Meteor.user().orgs.length > 0)
  107. return true;
  108. else
  109. return false;
  110. },
  111. /*
  112. userHasTemplates(){
  113. if(Meteor.user() != null && Meteor.user().orgs && Meteor.user().orgs.length > 0)
  114. return true;
  115. else
  116. return false;
  117. },
  118. */
  119. orgsDatas() {
  120. if (Meteor.user().orgs)
  121. return Meteor.user().orgs.sort((a, b) => a.orgDisplayName.localeCompare(b.orgDisplayName));
  122. else
  123. return [];
  124. },
  125. userHasOrgsOrTeams() {
  126. let boolUserHasOrgs;
  127. if (Meteor.user() != null && Meteor.user().orgs && Meteor.user().orgs.length > 0)
  128. boolUserHasOrgs = true;
  129. else
  130. boolUserHasOrgs = false;
  131. let boolUserHasTeams;
  132. if (Meteor.user() != null && Meteor.user().teams && Meteor.user().teams.length > 0)
  133. boolUserHasTeams = true;
  134. else
  135. boolUserHasTeams = false;
  136. return (boolUserHasOrgs || boolUserHasTeams);
  137. },
  138. boards() {
  139. let query = {
  140. // { type: 'board' },
  141. // { type: { $in: ['board','template-container'] } },
  142. $and: [
  143. { archived: false },
  144. { type: { $in: ['board', 'template-container'] } },
  145. { $or: [] },
  146. { title: { $not: { $regex: /^\^.*\^$/ } } }
  147. ]
  148. };
  149. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  150. if (FlowRouter.getRouteName() === 'home') {
  151. query.$and[2].$or.push({ 'members.userId': Meteor.userId() });
  152. if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue) {
  153. query.$and.push({ 'permission': 'private' });
  154. }
  155. const currUser = ReactiveCache.getCurrentUser();
  156. let orgIdsUserBelongs = currUser !== undefined && currUser.teams !== 'undefined' ? currUser.orgIdsUserBelongs() : '';
  157. if (orgIdsUserBelongs && orgIdsUserBelongs != '') {
  158. let orgsIds = orgIdsUserBelongs.split(',');
  159. // for(let i = 0; i < orgsIds.length; i++){
  160. // query.$and[2].$or.push({'orgs.orgId': orgsIds[i]});
  161. // }
  162. //query.$and[2].$or.push({'orgs': {$elemMatch : {orgId: orgsIds[0]}}});
  163. query.$and[2].$or.push({ 'orgs.orgId': { $in: orgsIds } });
  164. }
  165. let teamIdsUserBelongs = currUser !== undefined && currUser.teams !== 'undefined' ? currUser.teamIdsUserBelongs() : '';
  166. if (teamIdsUserBelongs && teamIdsUserBelongs != '') {
  167. let teamsIds = teamIdsUserBelongs.split(',');
  168. // for(let i = 0; i < teamsIds.length; i++){
  169. // query.$or[2].$or.push({'teams.teamId': teamsIds[i]});
  170. // }
  171. //query.$and[2].$or.push({'teams': { $elemMatch : {teamId: teamsIds[0]}}});
  172. query.$and[2].$or.push({ 'teams.teamId': { $in: teamsIds } });
  173. }
  174. }
  175. else if (allowPrivateVisibilityOnly !== undefined && !allowPrivateVisibilityOnly.booleanValue) {
  176. query = {
  177. archived: false,
  178. //type: { $in: ['board','template-container'] },
  179. type: 'board',
  180. permission: 'public',
  181. };
  182. }
  183. return Boards.find(query, {
  184. sort: { sort: 1 /* boards default sorting */ },
  185. });
  186. },
  187. boardLists(boardId) {
  188. let boardLists = [];
  189. const lists = Lists.find({ 'boardId': boardId, 'archived': false },{sort: ['sort','asc']});
  190. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  191. lists.forEach(list => {
  192. let cardCount = Cards.find({ 'boardId': boardId, 'listId': list._id }).count()
  193. boardLists.push(`${list.title}: ${cardCount}`);
  194. });
  195. */
  196. return boardLists;
  197. },
  198. boardMembers(boardId) {
  199. let boardMembers = [];
  200. /* Bug Board icons random dance https://github.com/wekan/wekan/issues/4214
  201. const lists = ReactiveCache.getBoard(boardId)
  202. let members = lists.members
  203. members.forEach(member => {
  204. boardMembers.push(member.userId);
  205. });
  206. */
  207. return boardMembers;
  208. },
  209. isStarred() {
  210. const user = Meteor.user();
  211. return user && user.hasStarred(this.currentData()._id);
  212. },
  213. isAdministrable() {
  214. const user = Meteor.user();
  215. return user && user.isBoardAdmin(this.currentData()._id);
  216. },
  217. hasOvertimeCards() {
  218. subManager.subscribe('board', this.currentData()._id, false);
  219. return this.currentData().hasOvertimeCards();
  220. },
  221. hasSpentTimeCards() {
  222. subManager.subscribe('board', this.currentData()._id, false);
  223. return this.currentData().hasSpentTimeCards();
  224. },
  225. isInvited() {
  226. const user = Meteor.user();
  227. return user && user.isInvitedTo(this.currentData()._id);
  228. },
  229. events() {
  230. return [
  231. {
  232. 'click .js-add-board': Popup.open('createBoard'),
  233. 'click .js-star-board'(evt) {
  234. const boardId = this.currentData()._id;
  235. Meteor.user().toggleBoardStar(boardId);
  236. evt.preventDefault();
  237. },
  238. 'click .js-clone-board'(evt) {
  239. let title = getSlug(ReactiveCache.getBoard(this.currentData()._id).title) || 'cloned-board';
  240. Meteor.call(
  241. 'copyBoard',
  242. this.currentData()._id,
  243. {
  244. sort: Boards.find({ archived: false }).count(),
  245. type: 'board',
  246. title: ReactiveCache.getBoard(this.currentData()._id).title,
  247. },
  248. (err, res) => {
  249. if (err) {
  250. console.error(err);
  251. } else {
  252. Session.set('fromBoard', null);
  253. subManager.subscribe('board', res, false);
  254. FlowRouter.go('board', {
  255. id: res,
  256. slug: title,
  257. });
  258. }
  259. },
  260. );
  261. evt.preventDefault();
  262. },
  263. 'click .js-archive-board'(evt) {
  264. const boardId = this.currentData()._id;
  265. Meteor.call('archiveBoard', boardId);
  266. evt.preventDefault();
  267. },
  268. 'click .js-accept-invite'() {
  269. const boardId = this.currentData()._id;
  270. Meteor.call('acceptInvite', boardId);
  271. },
  272. 'click .js-decline-invite'() {
  273. const boardId = this.currentData()._id;
  274. Meteor.call('quitBoard', boardId, (err, ret) => {
  275. if (!err && ret) {
  276. Meteor.call('acceptInvite', boardId);
  277. FlowRouter.go('home');
  278. }
  279. });
  280. },
  281. 'click #resetBtn'(event) {
  282. let allBoards = document.getElementsByClassName("js-board");
  283. let currBoard;
  284. for (let i = 0; i < allBoards.length; i++) {
  285. currBoard = allBoards[i];
  286. currBoard.style.display = "block";
  287. }
  288. },
  289. 'click #filterBtn'(event) {
  290. event.preventDefault();
  291. let selectedTeams = document.querySelectorAll('#jsAllBoardTeams option:checked');
  292. let selectedTeamsValues = Array.from(selectedTeams).map(function (elt) { return elt.value });
  293. let index = selectedTeamsValues.indexOf("-1");
  294. if (index > -1) {
  295. selectedTeamsValues.splice(index, 1);
  296. }
  297. let selectedOrgs = document.querySelectorAll('#jsAllBoardOrgs option:checked');
  298. let selectedOrgsValues = Array.from(selectedOrgs).map(function (elt) { return elt.value });
  299. index = selectedOrgsValues.indexOf("-1");
  300. if (index > -1) {
  301. selectedOrgsValues.splice(index, 1);
  302. }
  303. if (selectedTeamsValues.length > 0 || selectedOrgsValues.length > 0) {
  304. const query = {
  305. $and: [
  306. { archived: false },
  307. { type: 'board' },
  308. { $or: [] }
  309. ]
  310. };
  311. if (selectedTeamsValues.length > 0) {
  312. query.$and[2].$or.push({ 'teams.teamId': { $in: selectedTeamsValues } });
  313. }
  314. if (selectedOrgsValues.length > 0) {
  315. query.$and[2].$or.push({ 'orgs.orgId': { $in: selectedOrgsValues } });
  316. }
  317. let filteredBoards = Boards.find(query, {}).fetch();
  318. let allBoards = document.getElementsByClassName("js-board");
  319. let currBoard;
  320. if (filteredBoards.length > 0) {
  321. let currBoardId;
  322. let found;
  323. for (let i = 0; i < allBoards.length; i++) {
  324. currBoard = allBoards[i];
  325. currBoardId = currBoard.classList[0];
  326. found = filteredBoards.find(function (board) {
  327. return board._id == currBoardId;
  328. });
  329. if (found !== undefined)
  330. currBoard.style.display = "block";
  331. else
  332. currBoard.style.display = "none";
  333. }
  334. }
  335. else {
  336. for (let i = 0; i < allBoards.length; i++) {
  337. currBoard = allBoards[i];
  338. currBoard.style.display = "none";
  339. }
  340. }
  341. }
  342. },
  343. },
  344. ];
  345. },
  346. }).register('boardList');