boardsList.js 12 KB

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