swimlanes.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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: '.board-canvas',
  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. Swimlanes.update(swimlane._id, {
  29. $set: {
  30. sort: sortIndex.base,
  31. },
  32. });
  33. boardComponent.setIsDragging(false);
  34. },
  35. });
  36. },
  37. onCreated() {
  38. this.draggingActive = new ReactiveVar(false);
  39. this._isDragging = false;
  40. this._lastDragPositionX = 0;
  41. },
  42. id() {
  43. return this._id;
  44. },
  45. // XXX Flow components allow us to avoid creating these two setter methods by
  46. // exposing a public API to modify the component state. We need to investigate
  47. // best practices here.
  48. setIsDragging(bool) {
  49. this.draggingActive.set(bool);
  50. },
  51. scrollLeft(position = 0) {
  52. const lists = this.$('.js-lists');
  53. lists && lists.animate({
  54. scrollLeft: position,
  55. });
  56. },
  57. currentCardIsInThisList(listId, swimlaneId) {
  58. const currentCard = Cards.findOne(Session.get('currentCard'));
  59. const currentBoardId = Session.get('currentBoard');
  60. const board = Boards.findOne(currentBoardId);
  61. if (board.view === 'board-view-lists')
  62. return currentCard && currentCard.listId === listId;
  63. else if (board.view === 'board-view-swimlanes')
  64. return currentCard && currentCard.listId === listId && currentCard.swimlaneId === swimlaneId;
  65. else
  66. return false;
  67. },
  68. events() {
  69. return [{
  70. // Click-and-drag action
  71. 'mousedown .board-canvas'(evt) {
  72. // Translating the board canvas using the click-and-drag action can
  73. // conflict with the build-in browser mechanism to select text. We
  74. // define a list of elements in which we disable the dragging because
  75. // the user will legitimately expect to be able to select some text with
  76. // his mouse.
  77. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  78. if ($(evt.target).closest(noDragInside.join(',')).length === 0 && this.$('.swimlane').prop('clientHeight') > evt.offsetY) {
  79. this._isDragging = true;
  80. this._lastDragPositionX = evt.clientX;
  81. }
  82. },
  83. 'mouseup'() {
  84. if (this._isDragging) {
  85. this._isDragging = false;
  86. }
  87. },
  88. 'mousemove'(evt) {
  89. if (this._isDragging) {
  90. // Update the canvas position
  91. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  92. this._lastDragPositionX = evt.clientX;
  93. // Disable browser text selection while dragging
  94. evt.stopPropagation();
  95. evt.preventDefault();
  96. // Don't close opened card or inlined form at the end of the
  97. // click-and-drag.
  98. EscapeActions.executeUpTo('popup-close');
  99. EscapeActions.preventNextClick();
  100. }
  101. },
  102. }];
  103. },
  104. }).register('swimlane');
  105. BlazeComponent.extendComponent({
  106. // Proxy
  107. open() {
  108. this.childComponents('inlinedForm')[0].open();
  109. },
  110. events() {
  111. return [{
  112. submit(evt) {
  113. evt.preventDefault();
  114. const titleInput = this.find('.list-name-input');
  115. const title = titleInput.value.trim();
  116. if (title) {
  117. Lists.insert({
  118. title,
  119. boardId: Session.get('currentBoard'),
  120. sort: $('.list').length,
  121. });
  122. titleInput.value = '';
  123. titleInput.focus();
  124. }
  125. },
  126. }];
  127. },
  128. }).register('addListForm');
  129. BlazeComponent.extendComponent({
  130. // Proxy
  131. open() {
  132. this.childComponents('inlinedForm')[0].open();
  133. },
  134. events() {
  135. return [{
  136. submit(evt) {
  137. evt.preventDefault();
  138. let titleInput = this.find('.list-name-input');
  139. if (titleInput) {
  140. const title = titleInput.value.trim();
  141. if (title) {
  142. Lists.insert({
  143. title,
  144. boardId: Session.get('currentBoard'),
  145. sort: $('.list').length,
  146. });
  147. titleInput.value = '';
  148. titleInput.focus();
  149. }
  150. } else {
  151. titleInput = this.find('.swimlane-name-input');
  152. const title = titleInput.value.trim();
  153. if (title) {
  154. Swimlanes.insert({
  155. title,
  156. boardId: Session.get('currentBoard'),
  157. sort: $('.swimlane').length,
  158. });
  159. titleInput.value = '';
  160. titleInput.focus();
  161. }
  162. }
  163. },
  164. }];
  165. },
  166. }).register('addListAndSwimlaneForm');
  167. Template.swimlane.helpers({
  168. canSeeAddList() {
  169. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  170. },
  171. });