swimlanes.js 6.3 KB

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