labels.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'click .js-palette-color'() {
  18. this.currentColor.set(this.currentData().color);
  19. },
  20. }];
  21. },
  22. }).register('formLabel');
  23. Template.createLabelPopup.helpers({
  24. // This is the default color for a new label. We search the first color that
  25. // is not already used in the board (although it's not a problem if two
  26. // labels have the same color).
  27. defaultColor() {
  28. const labels = Boards.findOne(Session.get('currentBoard')).labels;
  29. const usedColors = _.pluck(labels, 'color');
  30. const availableColors = _.difference(labelColors, usedColors);
  31. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  32. },
  33. });
  34. Template.cardLabelsPopup.events({
  35. 'click .js-select-label'(evt) {
  36. const card = Cards.findOne(Session.get('currentCard'));
  37. const labelId = this._id;
  38. card.toggleLabel(labelId);
  39. evt.preventDefault();
  40. },
  41. 'click .js-edit-label': Popup.open('editLabel'),
  42. 'click .js-add-label': Popup.open('createLabel'),
  43. });
  44. Template.formLabel.events({
  45. 'click .js-palette-color'(evt) {
  46. const $this = $(evt.currentTarget);
  47. // hide selected ll colors
  48. $('.js-palette-select').addClass('hide');
  49. // show select color
  50. $this.find('.js-palette-select').removeClass('hide');
  51. },
  52. });
  53. Template.createLabelPopup.events({
  54. // Create the new label
  55. 'submit .create-label'(evt, tpl) {
  56. evt.preventDefault();
  57. const board = Boards.findOne(Session.get('currentBoard'));
  58. const name = tpl.$('#labelName').val().trim();
  59. const color = Blaze.getData(tpl.find('.fa-check')).color;
  60. board.addLabel(name, color);
  61. Popup.back();
  62. },
  63. });
  64. Template.editLabelPopup.events({
  65. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  66. const board = Boards.findOne(Session.get('currentBoard'));
  67. board.removeLabel(this._id);
  68. Popup.back(2);
  69. }),
  70. 'submit .edit-label'(evt, tpl) {
  71. evt.preventDefault();
  72. const board = Boards.findOne(Session.get('currentBoard'));
  73. const name = tpl.$('#labelName').val().trim();
  74. const color = Blaze.getData(tpl.find('.fa-check')).color;
  75. board.editLabel(this._id, name, color);
  76. Popup.back();
  77. },
  78. });
  79. Template.cardLabelsPopup.helpers({
  80. isLabelSelected(cardId) {
  81. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  82. },
  83. });