labels.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var labelColors;
  2. Meteor.startup(function() {
  3. labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  4. });
  5. BlazeComponent.extendComponent({
  6. template: function() {
  7. return 'formLabel';
  8. },
  9. onCreated: function() {
  10. this.currentColor = new ReactiveVar(this.data().color);
  11. },
  12. labels: function() {
  13. return _.map(labelColors, function(color) {
  14. return { color: color, name: '' };
  15. });
  16. },
  17. isSelected: function(color) {
  18. return this.currentColor.get() === color;
  19. },
  20. events: function() {
  21. return [{
  22. 'click .js-palette-color': function() {
  23. this.currentColor.set(this.currentData().color);
  24. }
  25. }];
  26. }
  27. }).register('formLabel');
  28. Template.createLabelPopup.helpers({
  29. // This is the default color for a new label. We search the first color that
  30. // is not already used in the board (although it's not a problem if two
  31. // labels have the same color).
  32. defaultColor: function() {
  33. var labels = this.labels || this.card.board().labels;
  34. var usedColors = _.pluck(labels, 'color');
  35. var availableColors = _.difference(labelColors, usedColors);
  36. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  37. }
  38. });
  39. Template.cardLabelsPopup.events({
  40. 'click .js-select-label': function(evt) {
  41. var cardId = Template.parentData(2).data._id;
  42. var labelId = this._id;
  43. var operation;
  44. if (Cards.find({ _id: cardId, labelIds: labelId}).count() === 0)
  45. operation = '$addToSet';
  46. else
  47. operation = '$pull';
  48. var query = {};
  49. query[operation] = {
  50. labelIds: labelId
  51. };
  52. Cards.update(cardId, query);
  53. evt.preventDefault();
  54. },
  55. 'click .js-edit-label': Popup.open('editLabel'),
  56. 'click .js-add-label': Popup.open('createLabel')
  57. });
  58. Template.formLabel.events({
  59. 'click .js-palette-color': function(evt) {
  60. var $this = $(evt.currentTarget);
  61. // hide selected ll colors
  62. $('.js-palette-select').addClass('hide');
  63. // show select color
  64. $this.find('.js-palette-select').removeClass('hide');
  65. }
  66. });
  67. Template.createLabelPopup.events({
  68. // Create the new label
  69. 'submit .create-label': function(evt, tpl) {
  70. var name = tpl.$('#labelName').val().trim();
  71. var boardId = Session.get('currentBoard');
  72. var color = Blaze.getData(tpl.find('.fa-check')).color;
  73. Boards.update(boardId, {
  74. $push: {
  75. labels: {
  76. _id: Random.id(6),
  77. name: name,
  78. color: color
  79. }
  80. }
  81. });
  82. Popup.back();
  83. evt.preventDefault();
  84. }
  85. });
  86. Template.editLabelPopup.events({
  87. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  88. var boardId = Session.get('currentBoard');
  89. Boards.update(boardId, {
  90. $pull: {
  91. labels: {
  92. _id: this._id
  93. }
  94. }
  95. });
  96. Popup.back(2);
  97. }),
  98. 'submit .edit-label': function(evt, tpl) {
  99. evt.preventDefault();
  100. var name = tpl.$('#labelName').val().trim();
  101. var boardId = Session.get('currentBoard');
  102. var getLabel = Utils.getLabelIndex(boardId, this._id);
  103. var color = Blaze.getData(tpl.find('.fa-check')).color;
  104. var $set = {};
  105. $set[getLabel.key('name')] = name;
  106. $set[getLabel.key('color')] = color;
  107. Boards.update(boardId, { $set: $set });
  108. Popup.back();
  109. }
  110. });
  111. Template.cardLabelsPopup.helpers({
  112. isLabelSelected: function(cardId) {
  113. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  114. }
  115. });