labels.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. let labelColors;
  2. Meteor.startup(() => {
  3. labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  4. });
  5. BlazeComponent.extendComponent({
  6. onCreated() {
  7. this.currentColor = new ReactiveVar(this.data().color);
  8. },
  9. labels() {
  10. return labelColors.map(color => ({ color, name: '' }));
  11. },
  12. isSelected(color) {
  13. return this.currentColor.get() === color;
  14. },
  15. events() {
  16. return [
  17. {
  18. 'click .js-palette-color'() {
  19. this.currentColor.set(this.currentData().color);
  20. },
  21. },
  22. ];
  23. },
  24. }).register('formLabel');
  25. Template.createLabelPopup.helpers({
  26. // This is the default color for a new label. We search the first color that
  27. // is not already used in the board (although it's not a problem if two
  28. // labels have the same color).
  29. defaultColor() {
  30. const labels = Boards.findOne(Session.get('currentBoard')).labels;
  31. const usedColors = _.pluck(labels, 'color');
  32. const availableColors = _.difference(labelColors, usedColors);
  33. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  34. },
  35. });
  36. BlazeComponent.extendComponent({
  37. onRendered() {
  38. const itemsSelector = 'li.js-card-label-item:not(.placeholder)';
  39. const $labels = this.$('.edit-labels-pop-over');
  40. $labels.sortable({
  41. connectWith: '.edit-labels-pop-over',
  42. tolerance: 'pointer',
  43. appendTo: '.edit-labels-pop-over',
  44. helper: 'clone',
  45. distance: 7,
  46. items: itemsSelector,
  47. placeholder: 'card-label-wrapper placeholder',
  48. start(evt, ui) {
  49. ui.helper.css('z-index', 1000);
  50. ui.placeholder.height(ui.helper.height());
  51. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  52. },
  53. stop(evt, ui) {
  54. const newLabelOrderOnlyIds = ui.item.parent().children().toArray().map(_element => Blaze.getData(_element)._id)
  55. const card = Blaze.getData(this);
  56. card.board().setNewLabelOrder(newLabelOrderOnlyIds);
  57. },
  58. });
  59. Utils.enableClickOnTouch(itemsSelector);
  60. // Disable drag-dropping if the current user is not a board member or is comment only
  61. this.autorun(() => {
  62. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  63. $labels.sortable({
  64. handle: '.label-handle',
  65. });
  66. }
  67. });
  68. },
  69. events() {
  70. return [
  71. {
  72. 'click .js-select-label'(event) {
  73. const card = Utils.getCurrentCard();
  74. const labelId = this.currentData()._id;
  75. card.toggleLabel(labelId);
  76. event.preventDefault();
  77. },
  78. 'click .js-edit-label': Popup.open('editLabel'),
  79. 'click .js-add-label': Popup.open('createLabel'),
  80. }
  81. ];
  82. }
  83. }).register('cardLabelsPopup');
  84. Template.cardLabelsPopup.events({
  85. });
  86. Template.formLabel.events({
  87. 'click .js-palette-color'(event) {
  88. const $this = $(event.currentTarget);
  89. // hide selected ll colors
  90. $('.js-palette-select').addClass('hide');
  91. // show select color
  92. $this.find('.js-palette-select').removeClass('hide');
  93. },
  94. });
  95. Template.createLabelPopup.events({
  96. // Create the new label
  97. 'submit .create-label'(event, templateInstance) {
  98. event.preventDefault();
  99. const board = Boards.findOne(Session.get('currentBoard'));
  100. const name = templateInstance
  101. .$('#labelName')
  102. .val()
  103. .trim();
  104. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  105. board.addLabel(name, color);
  106. Popup.back();
  107. },
  108. });
  109. Template.editLabelPopup.events({
  110. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  111. const board = Boards.findOne(Session.get('currentBoard'));
  112. board.removeLabel(this._id);
  113. Popup.back(2);
  114. }),
  115. 'submit .edit-label'(event, templateInstance) {
  116. event.preventDefault();
  117. const board = Boards.findOne(Session.get('currentBoard'));
  118. const name = templateInstance
  119. .$('#labelName')
  120. .val()
  121. .trim();
  122. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  123. board.editLabel(this._id, name, color);
  124. Popup.back();
  125. },
  126. });
  127. Template.cardLabelsPopup.helpers({
  128. isLabelSelected(cardId) {
  129. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  130. },
  131. });