swimlanes.js 6.0 KB

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