Przeglądaj źródła

read _templates in order

sou 7 lat temu
rodzic
commit
aa67331b6a
2 zmienionych plików z 42 dodań i 14 usunięć
  1. 8 6
      lib/models/page.js
  2. 34 8
      lib/routes/page.js

+ 8 - 6
lib/models/page.js

@@ -534,13 +534,15 @@ module.exports = function(crowi) {
 
 
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
       //find all tempaltes with ~/_template or ~/@template
       //find all tempaltes with ~/_template or ~/@template
-      Page.find({path: {$in: regexpList}}, (err, pages) => {
-        if (err || pages === null) {
-          return reject(err);
-        }
+      Page
+        .find({path: {$in: regexpList}}, (err, pages) => {
+          if (err || pages === null) {
+            return reject(err);
+          }
 
 
-        return resolve(pages);
-      });
+          return resolve(pages);
+        })
+        .populate({path: 'revision', model: 'Revision'});
     });
     });
   };
   };
 
 

+ 34 - 8
lib/routes/page.js

@@ -345,22 +345,23 @@ module.exports = function(crowi, app) {
       const pathList = pathGenerator(originalUrl, []);
       const pathList = pathGenerator(originalUrl, []);
 
 
       return Page.findAllTemplates(pathList)
       return Page.findAllTemplates(pathList)
-        .then(pages => {
-
+        .then(templates => {
           //get local template
           //get local template
           //@tempate: applicable only to immediate decendants
           //@tempate: applicable only to immediate decendants
-          const regexForLocalTemplate = new RegExp(`${templateUrl}/@template`);
-          const localTemplate = pages.filter(page => page.path.match(regexForLocalTemplate)).pop();
-
+          const localTemplate = templateFinder(templates, templateUrl, '@');
           if (localTemplate) {
           if (localTemplate) {
-            renderVars.template = localTemplate.revision.body; //this is not working, gotta populate revision somewhere
-            console.log(renderVars.template)
+            console.log('l temp found ', localTemplate.path);
+            renderVars.template = localTemplate.revision.body;
             return Promise.resolve();
             return Promise.resolve();
           }
           }
 
 
           //get global templates
           //get global templates
           //_tempate: applicable to all pages under
           //_tempate: applicable to all pages under
-          const globalTemplates = pages.filter(page => page.path.match(/_template/));
+          const globalTemplate = globalTemplateFinder(templates, templateUrl);
+          if (globalTemplate) {
+            console.log('g temp found ', globalTemplate.path);
+            renderVars.template = globalTemplate.revision.body;
+          }
 
 
           return Promise.resolve();
           return Promise.resolve();
         })
         })
@@ -446,6 +447,31 @@ module.exports = function(crowi, app) {
     return pathGenerator(newPath, pathList);
     return pathGenerator(newPath, pathList);
   };
   };
 
 
+  const globalTemplateFinder = (globalTemplates, path) => {
+    console.log(path);
+    const globalTemplate = templateFinder(globalTemplates, path, '_');
+    if (globalTemplate) {
+      return globalTemplate;
+    }
+
+    if (path === '') {
+      console.log('default template');
+      return;
+    }
+
+    const lastSlash = path.lastIndexOf('/');
+    const newPath = path.substr(0, lastSlash);
+    return globalTemplateFinder(globalTemplates, newPath);
+  };
+
+  const templateFinder = (templates, path, type) => {
+    for (let i = 0; i < templates.length; i++) {
+      if (templates[i].path === `${path}/${type}template`) {
+        return templates[i];
+      }
+    }
+  };
+
   actions.deletedPageListShow = function(req, res) {
   actions.deletedPageListShow = function(req, res) {
     var path = '/trash' + getPathFromRequest(req);
     var path = '/trash' + getPathFromRequest(req);
     var limit = 50;
     var limit = 50;