Browse Source

添加题目

曹冬冬 5 years ago
parent
commit
f38a711d5c

+ 3 - 1
src/model/Element.js

@@ -3,7 +3,7 @@
  * Modify by liuhaixiao on 2018/07/11
  */
 export default class Element {
-  constructor (ele = {}) {
+  constructor(ele = {}) {
     this.type = ele.type || 'pic'
     this.imgSrc = ele.imgSrc || ''
     this.left = ele.left || 0
@@ -58,5 +58,7 @@ export default class Element {
     this.isFixed = ele.isFixed || false
     this.backgroundSelectedImg = ele.backgroundSelectedImg || ''
     this.backgroundUnselectedImg = ele.backgroundUnselectedImg || ''
+    this.questionId = ele.questionId || 0
+    this.optionId = ele.optionId || 0
   }
 }

+ 51 - 0
src/views/h5editor/overview.vue

@@ -197,12 +197,14 @@ export default {
       if (checked) {
         questions.push(item.questionId);
         console.log("AAAAAAAAA", questions);
+        this.addQuestion(item, true);
       } else {
         questions.splice(
           questions.findIndex(itemTemp => itemTemp === item.questionId),
           1
         );
         console.log("DDDDDDDDDD", questions);
+        this.deleteQuestion(item);
       }
 
       var selecteQuestionList = [];
@@ -217,6 +219,55 @@ export default {
       console.log("RRRRRRRRRR", selecteQuestionList);
       this.$store.dispatch("addQuestion", selecteQuestionList);
     },
+    addQuestion(item, isFirst) {
+      // 添加题目
+      let param = {};
+      param.top = 170;
+      param.left = 100;
+      param.questionId = item.questionId;
+      param["type"] = "text";
+      param["text"] = item.name;
+      param["width"] = 300;
+      param["lineHeight"] = 1.5;
+      param["backgroundColor"] = "";
+      param["verticalAlign"] = "top";
+      param["display"] = "block";
+      param["textIndent"] = "0.0";
+      param["letterSpacing"] = "0.0";
+      param["allTransparent"] = "";
+      param["nodeId"] = "Id" + Math.random();
+      this.$store.dispatch("addElement", param);
+      // 添加选项
+      var itemTop = 250;
+      for (var i = 0; i < item.optionList.length; i++) {
+        console.log("选项", item.optionList[i].content);
+        let obj = {};
+        obj.questionId = item.questionId;
+        obj.optionId = item.optionList[i].optionId;
+        obj.type = "button";
+        obj.top = itemTop + i * 40 + (i + 1) * 50;
+        obj.left = 100;
+        obj["text"] = item.optionList[i].content;
+        obj["lineHeight"] = 1.5;
+        obj.width = 300;
+        obj.height = 40;
+        obj.backgroundUnselectedImg =
+          "https://dm.static.elab-plus.com/diaoyanbao/option_default.png";
+        obj.backgroundSelectedImg =
+          "https://dm.static.elab-plus.com/diaoyanbao/option_select.png";
+        obj.loop = false;
+        this.$store.dispatch("addElement", obj);
+      }
+    },
+    deleteQuestion(item) {
+      this.$store.dispatch("deleteElementQID", item.questionId);
+      // var elements = this.pages[this.currentPage].elements;
+      // for (var i = 0; i < elements.length; i++) {
+      //   if (elements[i].questionId == item.questionId) {
+      //     this.$store.dispatch("deleteElement", elements[i]);
+      //   }
+      // }
+    },
     isShow: function(index) {
       var result = false;
       if (index > 0) {

+ 6 - 0
src/vuex/editor/actions.js

@@ -197,6 +197,12 @@ export const deleteElement = ({ commit }, element) => {
   commit(types.DELETE_ELEMENT, element)
 }
 
+// 删除元素
+export const deleteElementQID = ({ commit }, qid) => {
+  commit(types.DELETE_ELEMENT_QID, qid)
+}
+
+
 export const deleteSelectedElement = ({ commit, state }) => {
   commit(types.DELETE_ELEMENT, state.editorElement)
 }

+ 1 - 0
src/vuex/editor/mutation-type.js

@@ -9,6 +9,7 @@ export const SET_CUR_EDITOR_PAGE = 'SET_CUR_EDITOR_PAGE' // 设置当前编辑
 export const GET_USER_THEME_LIST = 'GET_USER_THEME_LIST' // 获取用户h5列表
 export const SET_CUR_EDITOR_THEME = 'SET_CUR_EDITOR_THEME' // 设置当前编辑h5页面
 export const DELETE_ELEMENT = 'DELETE_ELEMENT' // 删除元素
+export const DELETE_ELEMENT_QID = 'DELETE_ELEMENT_QID' // 删除多个元素
 export const CREATE_THEME = 'CREATE_THEME' // 创建主题
 export const UPDATE_THEME_DES = 'UPDATE_THEME_DES' // 更新主题描述
 export const UPDATE_THEME_SUCCESS = 'UPDATE_THEME_SUCCESS' // 更新数据成功

+ 11 - 0
src/vuex/editor/mutations.js

@@ -92,6 +92,17 @@ const mutations = {
       }
     })
   },
+  [types.DELETE_ELEMENT_QID](state, questionId) {
+    var elements = state.editorPage.elements;
+    var newElements = [];
+    elements.findIndex((value, index, arr) => {
+      if (value.questionId !== questionId) {
+        newElements.push(value);
+      }
+    })
+    state.editorPage.elements = newElements;
+    state.editorElement = null
+  },
   [types.CREATE_THEME](state, data) {
     state.themeList.push(data)
   },