swimlanes.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. const { calculateIndex } = Utils;
  2. BlazeComponent.extendComponent({
  3. onRendered() {
  4. const boardComponent = this.parentComponent();
  5. const $swimlanesDom = boardComponent.$('.js-swimlanes');
  6. $swimlanesDom.sortable({
  7. tolerance: 'pointer',
  8. appendTo: 'body',
  9. helper: 'clone',
  10. handle: '.js-swimlane-header',
  11. items: '.js-swimlane:not(.placeholder)',
  12. placeholder: 'swimlane placeholder',
  13. distance: 7,
  14. start(evt, ui) {
  15. ui.placeholder.height(ui.helper.height());
  16. EscapeActions.executeUpTo('popup-close');
  17. boardComponent.setIsDragging(true);
  18. },
  19. stop(evt, ui) {
  20. // To attribute the new index number, we need to get the DOM element
  21. // of the previous and the following card -- if any.
  22. const prevSwimlaneDom = ui.item.prev('.js-swimlane').get(0);
  23. const nextSwimlaneDom = ui.item.next('.js-swimlane').get(0);
  24. const sortIndex = calculateIndex(prevSwimlaneDom, nextSwimlaneDom, 1);
  25. $swimlanesDom.sortable('cancel');
  26. const swimlaneDomElement = ui.item.get(0);
  27. const swimlane = Blaze.getData(swimlaneDomElement);
  28. console.log(swimlane._id);
  29. Swimlanes.update(swimlane._id, {
  30. $set: {
  31. sort: sortIndex.base,
  32. },
  33. });
  34. },
  35. });
  36. },
  37. onCreated() {
  38. this.draggingActive = new ReactiveVar(false);
  39. this._isDragging = false;
  40. this._lastDragPositionX = 0;
  41. },
  42. openNewListForm() {
  43. this.childComponents('addListForm')[0].open();
  44. },
  45. id() {
  46. return this._id;
  47. },
  48. // XXX Flow components allow us to avoid creating these two setter methods by
  49. // exposing a public API to modify the component state. We need to investigate
  50. // best practices here.
  51. setIsDragging(bool) {
  52. this.draggingActive.set(bool);
  53. },
  54. scrollLeft(position = 0) {
  55. const lists = this.$('.js-lists');
  56. lists && lists.animate({
  57. scrollLeft: position,
  58. });
  59. },
  60. currentCardIsInThisList(listId, swimlaneId) {
  61. const currentCard = Cards.findOne(Session.get('currentCard'));
  62. const currentBoardId = Session.get('currentBoard');
  63. const board = Boards.findOne(currentBoardId);
  64. if (board.view === 'board-view-lists')
  65. return currentCard && currentCard.listId === listId;
  66. else if (board.view === 'board-view-swimlanes')
  67. return currentCard && currentCard.listId === listId && currentCard.swimlaneId === swimlaneId;
  68. else
  69. return false;
  70. },
  71. events() {
  72. return [{
  73. // Click-and-drag action
  74. 'mousedown .board-canvas'(evt) {
  75. // Translating the board canvas using the click-and-drag action can
  76. // conflict with the build-in browser mechanism to select text. We
  77. // define a list of elements in which we disable the dragging because
  78. // the user will legitimately expect to be able to select some text with
  79. // his mouse.
  80. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  81. if ($(evt.target).closest(noDragInside.join(',')).length === 0 && this.$('.swimlane').prop('clientHeight') > evt.offsetY) {
  82. this._isDragging = true;
  83. this._lastDragPositionX = evt.clientX;
  84. }
  85. },
  86. 'mouseup'() {
  87. if (this._isDragging) {
  88. this._isDragging = false;
  89. }
  90. },
  91. 'mousemove'(evt) {
  92. if (this._isDragging) {
  93. // Update the canvas position
  94. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  95. this._lastDragPositionX = evt.clientX;
  96. // Disable browser text selection while dragging
  97. evt.stopPropagation();
  98. evt.preventDefault();
  99. // Don't close opened card or inlined form at the end of the
  100. // click-and-drag.
  101. EscapeActions.executeUpTo('popup-close');
  102. EscapeActions.preventNextClick();
  103. }
  104. },
  105. }];
  106. },
  107. }).register('swimlane');
  108. Template.swimlane.onRendered(function() {
  109. const self = BlazeComponent.getComponentForElement(this.firstNode);
  110. self.listsDom = this.find('.js-lists');
  111. if (!Session.get('currentCard')) {
  112. self.scrollLeft();
  113. }
  114. // We want to animate the card details window closing. We rely on CSS
  115. // transition for the actual animation.
  116. self.listsDom._uihooks = {
  117. removeElement(node) {
  118. const removeNode = _.once(() => {
  119. node.parentNode.removeChild(node);
  120. });
  121. if ($(node).hasClass('js-card-details')) {
  122. $(node).css({
  123. flexBasis: 0,
  124. padding: 0,
  125. });
  126. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  127. } else {
  128. removeNode();
  129. }
  130. },
  131. };
  132. $(self.listsDom).sortable({
  133. tolerance: 'pointer',
  134. helper: 'clone',
  135. handle: '.js-list-header',
  136. items: '.js-list:not(.js-list-composer)',
  137. placeholder: 'list placeholder',
  138. distance: 7,
  139. start(evt, ui) {
  140. ui.placeholder.height(ui.helper.height());
  141. Popup.close();
  142. },
  143. stop() {
  144. $(self.listsDom).find('.js-list:not(.js-list-composer)').each(
  145. (i, list) => {
  146. const data = Blaze.getData(list);
  147. Lists.update(data._id, {
  148. $set: {
  149. sort: i,
  150. },
  151. });
  152. }
  153. );
  154. },
  155. });
  156. function userIsMember() {
  157. return Meteor.user() && Meteor.user().isBoardMember();
  158. }
  159. // Disable drag-dropping while in multi-selection mode, or if the current user
  160. // is not a board member
  161. self.autorun(() => {
  162. const $listDom = $(self.listsDom);
  163. if ($listDom.data('sortable')) {
  164. $(self.listsDom).sortable('option', 'disabled',
  165. MultiSelection.isActive() || !userIsMember());
  166. }
  167. });
  168. // If there is no data in the board (ie, no lists) we autofocus the list
  169. // creation form by clicking on the corresponding element.
  170. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  171. if (userIsMember() && currentBoard.lists().count() === 0) {
  172. self.openNewListForm();
  173. }
  174. });
  175. BlazeComponent.extendComponent({
  176. // Proxy
  177. open() {
  178. this.childComponents('inlinedForm')[0].open();
  179. },
  180. events() {
  181. return [{
  182. submit(evt) {
  183. evt.preventDefault();
  184. const titleInput = this.find('.list-name-input');
  185. const title = titleInput.value.trim();
  186. if (title) {
  187. Lists.insert({
  188. title,
  189. boardId: Session.get('currentBoard'),
  190. sort: $('.list').length,
  191. });
  192. titleInput.value = '';
  193. titleInput.focus();
  194. }
  195. },
  196. }];
  197. },
  198. }).register('addListForm');
  199. BlazeComponent.extendComponent({
  200. // Proxy
  201. open() {
  202. this.childComponents('inlinedForm')[0].open();
  203. },
  204. events() {
  205. return [{
  206. submit(evt) {
  207. evt.preventDefault();
  208. let titleInput = this.find('.list-name-input');
  209. if (titleInput) {
  210. const title = titleInput.value.trim();
  211. if (title) {
  212. Lists.insert({
  213. title,
  214. boardId: Session.get('currentBoard'),
  215. sort: $('.list').length,
  216. });
  217. titleInput.value = '';
  218. titleInput.focus();
  219. }
  220. } else {
  221. titleInput = this.find('.swimlane-name-input');
  222. const title = titleInput.value.trim();
  223. if (title) {
  224. Swimlanes.insert({
  225. title,
  226. boardId: Session.get('currentBoard'),
  227. sort: $('.swimlane').length,
  228. });
  229. titleInput.value = '';
  230. titleInput.focus();
  231. }
  232. }
  233. },
  234. }];
  235. },
  236. }).register('addListAndSwimlaneForm');
  237. Template.swimlane.helpers({
  238. canSeeAddList() {
  239. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  240. },
  241. });