swimlanes.js 5.5 KB

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