boardsList.js 12 KB

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