swimlanes.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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() {
  26. const currentCard = Cards.findOne(Session.get('currentCard'));
  27. const listId = this.currentData()._id;
  28. return currentCard && currentCard.listId === listId; //TODO: AND IN THIS SWIMLANE
  29. },
  30. events() {
  31. return [{
  32. // Click-and-drag action
  33. 'mousedown .board-canvas'(evt) {
  34. // Translating the board canvas using the click-and-drag action can
  35. // conflict with the build-in browser mechanism to select text. We
  36. // define a list of elements in which we disable the dragging because
  37. // the user will legitimately expect to be able to select some text with
  38. // his mouse.
  39. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  40. if ($(evt.target).closest(noDragInside.join(',')).length === 0 && this.$('.swimlane').prop('clientHeight') > evt.offsetY) {
  41. this._isDragging = true;
  42. this._lastDragPositionX = evt.clientX;
  43. }
  44. },
  45. 'mouseup'() {
  46. if (this._isDragging) {
  47. this._isDragging = false;
  48. }
  49. },
  50. 'mousemove'(evt) {
  51. if (this._isDragging) {
  52. // Update the canvas position
  53. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  54. this._lastDragPositionX = evt.clientX;
  55. // Disable browser text selection while dragging
  56. evt.stopPropagation();
  57. evt.preventDefault();
  58. // Don't close opened card or inlined form at the end of the
  59. // click-and-drag.
  60. EscapeActions.executeUpTo('popup-close');
  61. EscapeActions.preventNextClick();
  62. }
  63. },
  64. }];
  65. },
  66. }).register('swimlane');
  67. Template.swimlane.onRendered(function() {
  68. const self = BlazeComponent.getComponentForElement(this.firstNode);
  69. self.listsDom = this.find('.js-lists');
  70. if (!Session.get('currentCard')) {
  71. self.scrollLeft();
  72. }
  73. // We want to animate the card details window closing. We rely on CSS
  74. // transition for the actual animation.
  75. self.listsDom._uihooks = {
  76. removeElement(node) {
  77. const removeNode = _.once(() => {
  78. node.parentNode.removeChild(node);
  79. });
  80. if ($(node).hasClass('js-card-details')) {
  81. $(node).css({
  82. flexBasis: 0,
  83. padding: 0,
  84. });
  85. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  86. } else {
  87. removeNode();
  88. }
  89. },
  90. };
  91. $(self.listsDom).sortable({
  92. tolerance: 'pointer',
  93. helper: 'clone',
  94. handle: '.js-list-header',
  95. items: '.js-list:not(.js-list-composer)',
  96. placeholder: 'list placeholder',
  97. distance: 7,
  98. start(evt, ui) {
  99. ui.placeholder.height(ui.helper.height());
  100. Popup.close();
  101. },
  102. stop() {
  103. $(self.listsDom).find('.js-list:not(.js-list-composer)').each(
  104. (i, list) => {
  105. const data = Blaze.getData(list);
  106. Lists.update(data._id, {
  107. $set: {
  108. sort: i,
  109. },
  110. });
  111. }
  112. );
  113. },
  114. });
  115. function userIsMember() {
  116. return Meteor.user() && Meteor.user().isBoardMember();
  117. }
  118. // Disable drag-dropping while in multi-selection mode, or if the current user
  119. // is not a board member
  120. self.autorun(() => {
  121. const $listDom = $(self.listsDom);
  122. if ($listDom.data('sortable')) {
  123. $(self.listsDom).sortable('option', 'disabled',
  124. MultiSelection.isActive() || !userIsMember());
  125. }
  126. });
  127. // If there is no data in the board (ie, no lists) we autofocus the list
  128. // creation form by clicking on the corresponding element.
  129. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  130. if (userIsMember() && currentBoard.lists().count() === 0) {
  131. self.openNewListForm();
  132. }
  133. });
  134. BlazeComponent.extendComponent({
  135. // Proxy
  136. open() {
  137. this.childComponents('inlinedForm')[0].open();
  138. },
  139. events() {
  140. return [{
  141. submit(evt) {
  142. evt.preventDefault();
  143. const titleInput = this.find('.list-name-input');
  144. const title = titleInput.value.trim();
  145. if (title) {
  146. Lists.insert({
  147. title,
  148. boardId: Session.get('currentBoard'),
  149. sort: $('.list').length,
  150. });
  151. titleInput.value = '';
  152. titleInput.focus();
  153. }
  154. },
  155. }];
  156. },
  157. }).register('addListForm');
  158. Template.swimlane.helpers({
  159. canSeeAddList() {
  160. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  161. },
  162. });