Forráskód Böngészése

块模板库后端存储

chenshilong 6 éve
szülő
commit
ccbbd4eacb

+ 11 - 57
modules/main/controllers/block_lib_controller.js

@@ -6,64 +6,18 @@ let mongoose = require('mongoose');
 let blFacade = require('../facade/block_lib_facade');
 
 module.exports = {
-    getLibNames: getLibNames,
-    getLib: getLib,
-    save: save,
-    saveBlock: saveBlock
-};
-
-async function getLibNames(req, res) {
-    let result = {error: 0, message: '', data: null};
-    try {
-        let dataObj = JSON.parse(req.body.data);
-        let libNames = await blFacade.getLibNames(dataObj.userID, req.body.data.compilationID);
-        result.data = libNames;
-    } catch (err) {
-        console.log(err);
-        result.error = 1;
-        result.message = err.message;
-    }
-    res.json(result);
-};
-
-async function getLib(req, res) {
-    let result = {error: 0, message: '', data: null};
-    try {
-        let dataObj = JSON.parse(req.body.data);
-        let lib = await blFacade.getLib(dataObj.libID);
-        result.data = lib;
-    } catch (err) {
-        console.log(err);
-        result.error = 1;
-        result.message = err.message;
-    }
-    res.json(result);
-};
-
-async function save(req, res) {
-    let result = {error: 0, message: '', data: null};
-    await blFacade.save(req.body.data, function (err, data) {
-        if (err == '') {
-            result.data = data;
-        } else {
+    doController: async function (req, res) {
+        let result = {error: 0, message: '', data: null};
+        try {
+            let funcName = req.url.replace(/\//g, "");
+            let dataObj = JSON.parse(req.body.data);
+            result.data = await blFacade[funcName](dataObj);
+        } catch (err) {
+            console.log(err);
             result.error = 1;
-            result.message = err;
+            result.message = err.message;
         }
-    });
-    res.json(result);
+        res.json(result);
+    }
 };
 
-async function saveBlock(req, res) {
-    let result = {error: 0, message: '', data: null};
-    let dataObj = JSON.parse(req.body.data);
-    try{
-        await blFacade.saveBlock(dataObj);
-        result.data = 'saveBlock.OK';
-    }
-    catch(err){
-        console.log(err);
-        result.error = 1;
-        result.message = err.message;
-    };
-    res.json(result);
-};

+ 14 - 13
modules/main/facade/block_lib_facade.js

@@ -8,30 +8,31 @@ let blModel = mongoose.model('blockLibsModel');
 module.exports = {
     getLibNames: getLibNames,
     getLib: getLib,
-    save: save,
+    getLibNamesAndFirstLib: getLibNamesAndFirstLib,
     saveBlock: saveBlock
 };
 
-async function getLibNames(userID, compilationID) {
-    let libNames = await blModel.find({userID: userID, compilationID: compilationID});
+// userID、compilationID
+async function getLibNames(data) {
+    let libNames = await blModel.find({userID: data.userID, compilation: data.compilationID}, ["libID","libName","-_id"]);
     return libNames;
 };
 
-async function getLib(libID) {
-    let lib = await blModel.findOne({libID: libID});
+// libID
+async function getLib(data) {
+    let lib = await blModel.findOne({libID: data.libID});
     return lib;
 };
 
-async function save(data, callback) {
-//
+// userID、compilationID
+async function getLibNamesAndFirstLib(data) {
+    let libNames = await getLibNames(data);
+    let firstLib = await getLib(libNames[0]);
+    return {libNames: libNames, firstLib: firstLib};
 };
 
+// libID
 async function saveBlock(data) {
     await blModel.update({libID: data.libID}, {"$addToSet": {"datas": data}});
+    return 'saveBlock.OK';
 };
-
-// test Model and write DB functions.
-// function cbExec(err) {if (err) {console.log(err)} else {console.log('saved.')}};
-// async function testData(obj) {await blModel.create(obj, cbExec)};
-// let obj = {"userID":"2164","libID":25,"libName":"模板库25","datas":[],"share":{shareName: 'CSL共享库', shareTo: [2,3,4]}};
-// testData(obj);

+ 5 - 6
modules/main/routes/block_lib_route.js

@@ -6,12 +6,11 @@ let express = require('express');
 let blController = require('../controllers/block_lib_controller');
 
 module.exports = function (app) {
-    var blRouter = express.Router();
-    blRouter.post('/getLibNames', blController.getLibNames);
-    blRouter.post('/getLib', blController.getLib);
-    blRouter.post('/saveBlock', blController.saveBlock);
-    blRouter.post('/save', blController.save);
-
+    let blRouter = express.Router();
+    let funcNames = ['getLibNames', 'getLib', 'getLibNamesAndFirstLib', 'saveBlock'];
+    for (let name of funcNames) {
+        blRouter.post(`/${name}`, blController.doController);
+    }
     app.use('/blockLib', blRouter);
 }
 

+ 1 - 1
web/building_saas/main/html/main.html

@@ -614,7 +614,7 @@
                               <div class="tab-pane" id="kmbk">
                                   <div class="tools-bar-height-d container-fluid" id="kmbkToolsBar">
                                       <div class="p-1 row">
-                                          <select class="form-control form-control-sm col-6" id="exampleSelect1">
+                                          <select class="form-control form-control-sm col-6" id="select_block_lib_names">
                                               <option>我的模板库</option>
                                           </select>
                                           <div class="col-6">

+ 35 - 28
web/building_saas/main/js/views/block_lib.js

@@ -3,6 +3,8 @@
  * Created by CSL on 2018-09-19.
  */
 var blockLibObj = {
+    libs: [],
+    activeLib: null,
     mainSpread: null,
     mainSheet: null,
     mainTree: null,
@@ -66,12 +68,11 @@ var blockLibObj = {
         }
     },
     cloneType: null,
-    libID: 1,
 
-    buildSheet: function () {
+    buildSheet: async function () {
         $.bootstrapLoading.start();
         let me = this;
-        me.mainDatas = [
+       /* me.mainDatas = [
             {ID: 1, ParentID: -1, NextSiblingID: 2, nodeName: '分类1', type: 1},
             {ID: 2, ParentID: -1, NextSiblingID: 3, nodeName: '分类2', type: 1},
             {ID: 3, ParentID: -1, NextSiblingID: 4, nodeName: '分类3', type: 1},
@@ -80,9 +81,10 @@ var blockLibObj = {
             {ID: 7, ParentID: -1, NextSiblingID: 8, nodeName: '分类7', type: 1},
             {ID: 9, ParentID: -1, NextSiblingID: -1, nodeName: '分类9', type: 1}//,
             // {ID: 201, ParentID: 52, NextSiblingID: -1, nodeName: '块201', type: 2}
-        ];
+        ];*/
 
-        if (me.mainSpread) {
+       if (me.libs.length > 0) me.libs.splice(0, me.libs.length);
+       if (me.mainSpread) {
             me.mainSpread.destroy();
             me.mainSpread = null;
         };
@@ -95,6 +97,21 @@ var blockLibObj = {
             me.rationSpread = null;
         };
 
+        let namesAndLib = await ajaxPost('/blockLib/getLibNamesAndFirstLib', {userID: userID, compilationID: projectInfoObj.projectInfo.compilation});
+        me.mainDatas = namesAndLib.firstLib.datas;
+        me.libs.push(namesAndLib.firstLib);
+        me.activeLib = namesAndLib.firstLib;
+
+        function getLibNamesHtml(libsArr) {
+            let result = '';
+            for (let lib of libsArr) {
+                result += '<option value="' + lib.libID + '">' + lib.libName + '</option>';
+            };
+            return result;
+        };
+        let html = getLibNamesHtml(namesAndLib.libNames);
+        $("#select_block_lib_names").html(html);
+
         me.mainSpread = SheetDataHelper.createNewSpread($('#div_block_tree')[0]);
         // me.mainSpread = TREE_SHEET_HELPER.createNewSpread($('#div_block_tree')[0]);
         me.mainSheet = me.mainSpread.getSheet(0);
@@ -367,14 +384,14 @@ var blockLibObj = {
         }
 
         let newN = tree.insert(pID, nID);
-        newN.data.libID = blockLibObj.libID;
+        newN.data.libID = blockLibObj.activeLib.libID;
         newN.data.type = nodeType;
         newN.data.nodeName = nodeName;
         if (nodeType == 2)
             blockLibObj.assignData(newN, source);
 
-        let a = await ajaxPost('/blockLib/saveBlock', newN.data);
-        alert(a);
+        let save = await ajaxPost('/blockLib/saveBlock', newN.data);
+        alert(save);
         tree.selected = newN;
         let sheet = blockLibObj.mainSheet;
         sheet.suspendPaint();
@@ -386,25 +403,6 @@ var blockLibObj = {
         sheet.setSelection(idx, 0, 1, 1);
         sheet.resumeEvent();
         sheet.resumePaint();
-       /* .then(
-            function () {
-                tree.selected = newN;
-                let sheet = blockLibObj.mainSheet;
-                sheet.suspendPaint();
-                sheet.suspendEvent();
-                let idx = tree.items.indexOf(newN);
-                sheet.addRows(idx, 1);
-                sheet.getRange(idx, 0, 1, 1).locked(true);
-                sheet.setValue(idx, 0, newN.data.nodeName);
-                sheet.setSelection(idx, 0, 1, 1);
-                sheet.resumeEvent();
-                sheet.resumePaint();
-            }
-        ).catch(
-                function () {
-                    console.log('块文件入库存储失败!');
-                }
-        );*/
     },
     assignData: function (block, source){
         block.data.compilationID = source.compilationID;
@@ -599,10 +597,19 @@ var blockLibObj = {
         };
         vBlock_WC = JSON.parse(JSON.stringify(vBlock_WC));
         BlockController.confirmPaste(vBlock_WC, projectNode, 'sub');
+    },
+    checkShow: async function () {   // 这里需要处理异步:模板库装载完再弹出位置选择窗。
+        if (!$("#kmbk").is(":visible")){
+            if (!blockLibObj.mainSpread){
+                await blockLibObj.buildSheet();
+            };
+            $('#blockLibTab').click();
+        };
+        $("#div_createBlocks").modal({show: true});
     }
 };
 
-$(document).ready(function(){
+$(document).ready(function(){    // 这里不需要处理异步:因为不需要弹出位置选择窗。
     $('#blockLibTab').on('click', function (){
         if ($("#kmbk").is(":visible")){
             if (!blockLibObj.mainSpread){

+ 2 - 5
web/building_saas/main/js/views/project_view.js

@@ -1511,11 +1511,8 @@ var projectObj = {
                         let selected = project.mainTree.selected;
                         return selected.sourceType != ModuleNames.bills;
                     },
-                    callback:function(){
-                        if (!$("#kmbk").is(":visible")){
-                            $('#blockLibTab').click()
-                        };
-                        $("#div_createBlocks").modal({show: true});
+                    callback: function(){
+                        blockLibObj.checkShow();
                     },
                     visible: function(key, opt){
                         return G_SHOW_BLOCK_LIB;