labels.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Template.cardLabelsPopup.events({
  37. 'click .js-select-label'(event) {
  38. const card = Cards.findOne(Session.get('currentCard'));
  39. const labelId = this._id;
  40. card.toggleLabel(labelId);
  41. event.preventDefault();
  42. },
  43. 'click .js-edit-label': Popup.open('editLabel'),
  44. 'click .js-add-label': Popup.open('createLabel'),
  45. });
  46. Template.formLabel.events({
  47. 'click .js-palette-color'(event) {
  48. const $this = $(event.currentTarget);
  49. // hide selected ll colors
  50. $('.js-palette-select').addClass('hide');
  51. // show select color
  52. $this.find('.js-palette-select').removeClass('hide');
  53. },
  54. });
  55. Template.createLabelPopup.events({
  56. // Create the new label
  57. 'submit .create-label'(event, templateInstance) {
  58. event.preventDefault();
  59. const board = Boards.findOne(Session.get('currentBoard'));
  60. const name = templateInstance
  61. .$('#labelName')
  62. .val()
  63. .trim();
  64. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  65. board.addLabel(name, color);
  66. Popup.back();
  67. },
  68. });
  69. Template.editLabelPopup.events({
  70. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  71. const board = Boards.findOne(Session.get('currentBoard'));
  72. board.removeLabel(this._id);
  73. Popup.back(2);
  74. }),
  75. 'submit .edit-label'(event, templateInstance) {
  76. event.preventDefault();
  77. const board = Boards.findOne(Session.get('currentBoard'));
  78. const name = templateInstance
  79. .$('#labelName')
  80. .val()
  81. .trim();
  82. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  83. board.editLabel(this._id, name, color);
  84. Popup.back();
  85. },
  86. });
  87. Template.cardLabelsPopup.helpers({
  88. isLabelSelected(cardId) {
  89. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  90. },
  91. });