Sfoglia il codice sorgente

Import single card: map labels

Xavier Priour 9 anni fa
parent
commit
68518f5497
2 ha cambiato i file con 41 aggiunte e 12 eliminazioni
  1. 33 10
      client/components/lists/listHeader.js
  2. 8 2
      models/boards.js

+ 33 - 10
client/components/lists/listHeader.js

@@ -50,14 +50,14 @@ Template.listActionPopup.events({
 });
 });
 
 
 Template.listImportCardPopup.events({
 Template.listImportCardPopup.events({
-  submit(evt, template) {
+  submit(evt) {
     // 1. get the json data out of the form and parse it
     // 1. get the json data out of the form and parse it
     evt.preventDefault();
     evt.preventDefault();
     const jsonData = $(evt.currentTarget).find('textarea').val();
     const jsonData = $(evt.currentTarget).find('textarea').val();
     const data = JSON.parse(jsonData);
     const data = JSON.parse(jsonData);
     // 2. map all fields for the card to create
     // 2. map all fields for the card to create
     const firstCardDom = $(`#js-list-${this._id} .js-minicard:first`).get(0);
     const firstCardDom = $(`#js-list-${this._id} .js-minicard:first`).get(0);
-    sortIndex = Utils.calculateIndex(null, firstCardDom).base;
+    const sortIndex = Utils.calculateIndex(null, firstCardDom).base;
     const cardToCreate = {
     const cardToCreate = {
       title: data.name,
       title: data.name,
       description: data.desc,
       description: data.desc,
@@ -65,20 +65,43 @@ Template.listImportCardPopup.events({
       boardId: this.boardId,
       boardId: this.boardId,
       userId: Meteor.userId(),
       userId: Meteor.userId(),
       sort: sortIndex,
       sort: sortIndex,
-    }
-    // 3. insert new card into list
+    };
+    // 3. map labels
+    data.labels.forEach((current) => {
+      const color = current.color;
+      const name = current.name;
+      const existingLabel = this.board().getLabel(name, color);
+      let labelId = undefined;
+      if (existingLabel) {
+        labelId = existingLabel._id;
+      } else {
+        let labelCreated = this.board().addLabel(name, color);
+        // XXX currently mutations return no value so we have to fetch the label we just created
+        // waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
+        labelCreated = this.board().getLabel(name, color);
+        labelId = labelCreated._id;
+      }
+      if(labelId) {
+        if (!cardToCreate.labelIds) {
+          cardToCreate.labelIds = [];
+        }
+        cardToCreate.labelIds.push(labelId);
+      }
+    });
+    // 4. insert new card into list
     const _id = Cards.insert(cardToCreate);
     const _id = Cards.insert(cardToCreate);
-    // 4. parse actions and add comments/activities - if any
-    data.actions.forEach((current, i, actions)=>{
-      if(current.type == 'commentCard') {
+    // 5. parse actions and add comments
+    data.actions.forEach((current) => {
+      if(current.type === 'commentCard') {
         const commentToCreate = {
         const commentToCreate = {
           boardId: this.boardId,
           boardId: this.boardId,
           cardId: _id,
           cardId: _id,
           userId: Meteor.userId(),
           userId: Meteor.userId(),
-          text: current.data.text
-        }
+          text: current.data.text,
+        };
         CardComments.insert(commentToCreate);
         CardComments.insert(commentToCreate);
       }
       }
+      // XXX add other type of activities?
       Popup.close();
       Popup.close();
     });
     });
 
 
@@ -87,7 +110,7 @@ Template.listImportCardPopup.events({
     // card will disappear instantly.
     // card will disappear instantly.
     // See https://github.com/wekan/wekan/issues/80
     // See https://github.com/wekan/wekan/issues/80
     Filter.addException(_id);
     Filter.addException(_id);
-  }
+  },
 });
 });
 
 
 Template.listMoveCardsPopup.events({
 Template.listMoveCardsPopup.events({

+ 8 - 2
models/boards.js

@@ -92,6 +92,12 @@ Boards.helpers({
     return _.where(this.members, {isActive: true});
     return _.where(this.members, {isActive: true});
   },
   },
 
 
+  getLabel(name, color) {
+    return this.labels.find((current) => {
+      return ((current.name === name) && (current.color === color));
+    });
+  },
+
   labelIndex(labelId) {
   labelIndex(labelId) {
     return _.indexOf(_.pluck(this.labels, '_id'), labelId);
     return _.indexOf(_.pluck(this.labels, '_id'), labelId);
   },
   },
@@ -293,8 +299,8 @@ if (Meteor.isServer) {
     });
     });
   });
   });
 
 
-  // If the user remove one label from a board, we cant to remove reference of
-  // this label in any card of this board.
+  // If the user removes a label from a board, we have to remove references to
+  // this label in all cards of the board.
   Boards.after.update((userId, doc, fieldNames, modifier) => {
   Boards.after.update((userId, doc, fieldNames, modifier) => {
     if (!_.contains(fieldNames, 'labels') ||
     if (!_.contains(fieldNames, 'labels') ||
       !modifier.$pull ||
       !modifier.$pull ||