Просмотр исходного кода

get list of all applicable templates

sou 7 лет назад
Родитель
Сommit
2678433c49
2 измененных файлов с 83 добавлено и 31 удалено
  1. 17 0
      lib/models/page.js
  2. 66 31
      lib/routes/page.js

+ 17 - 0
lib/models/page.js

@@ -527,6 +527,23 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
+  // find all templates applicable to the new page
+  pageSchema.statics.findAllTemplates = function(pathList) {
+    const Page = this;
+    const regexpList = pathList.map(path => new RegExp(`${path}/[_@]template`));
+
+    return new Promise((resolve, reject) => {
+      //find all tempaltes with ~/_template or ~/@template
+      Page.find({path: {$in: regexpList}}, (err, pages) => {
+        if (err || pages === null) {
+          return reject(err);
+        }
+
+        return resolve(pages);
+      });
+    });
+  };
+
   // find page by path
   // find page by path
   pageSchema.statics.findPageByPath = function(path) {
   pageSchema.statics.findPageByPath = function(path) {
     var Page = this;
     var Page = this;

+ 66 - 31
lib/routes/page.js

@@ -310,46 +310,81 @@ module.exports = function(crowi, app) {
     // look for templates if page not exists
     // look for templates if page not exists
     .catch(function(err) {
     .catch(function(err) {
       // use not_found pageTempalte for both a new page with either tempalte and a new page with no templates
       // use not_found pageTempalte for both a new page with either tempalte and a new page with no templates
+      // pageTeamplate = 'customlayout-selector/not_found';
+
+      // const lastSlash = originalUrl.lastIndexOf('/');
+      // const templateUrl = `${originalUrl.substr(0, lastSlash)}/@template`;
+
+      // // @tempate: applicable only to immediate decendants
+      // const getLocalTemplate = Page.findPage(templateUrl)
+      // .then(function(page) {
+      //   return Promise.resolve(page.path);
+      // })
+      // .catch(function(err) {
+      //   console.log('no @template for you');
+      //   return Promise.resolve();
+      // });
+
+      // //FIXME
+      // const rootPageName = originalUrl.replace(/^\/([^\/]*).*$/, '$1');
+
+      // // _tempate: applicable to all pages under
+      // const getGlobalTemplate = Page.findPage(`/${rootPageName}/_template`)
+      //  .then(page => {
+      //    return Promise.resolve(page.path);
+      //  })
+      //  .catch(function(err) {
+      //    console.log('no _template for you');
+      //    return Promise.resolve();
+      //  });
+
       pageTeamplate = 'customlayout-selector/not_found';
       pageTeamplate = 'customlayout-selector/not_found';
 
 
       const lastSlash = originalUrl.lastIndexOf('/');
       const lastSlash = originalUrl.lastIndexOf('/');
-      const templateUrl = `${originalUrl.substr(0, lastSlash)}/@template`;
+      const templateUrl = originalUrl.substr(0, lastSlash);
+      const pathList = pathGenerator(originalUrl, []);
 
 
-      // @tempate: applicable only to immediate decendants
-      const getLocalTemplate = Page.findPage(templateUrl)
-      .then(function(page) {
-        return Promise.resolve(page.path);
-      })
-      .catch(function(err) {
-        console.log('no @template for you');
-        return Promise.resolve();
-      });
+      return Page.findAllTemplates(pathList)
+        .then(pages => {
 
 
-      //FIXME
-      const rootPageName = originalUrl.replace(/^\/([^\/]*).*$/, '$1');
+          //get local template
+          //@tempate: applicable only to immediate decendants
+          const regexForLocalTemplate = new RegExp(`${templateUrl}/@template`);
+          const localTemplate = pages.filter(page => page.path.match(regexForLocalTemplate)).pop();
 
 
-      // _tempate: applicable to all pages under
-      const getGlobalTemplate = Page.findPage(`/${rootPageName}/_template`)
-      .then(function(page) {
-        return Promise.resolve(page.path);
-      })
-      .catch(function(err) {
-        console.log('no _template for you');
-        return Promise.resolve();
-      });
+          if (localTemplate) {
+            renderVars.template = localTemplate.revision.body; //this is not working, gotta populate revision somewhere
+            console.log(renderVars.template)
+            return Promise.resolve();
+          }
 
 
-      return Promise.all([getGlobalTemplate, getLocalTemplate])
-        .then(function(templates) {
-          const globalTemplate = templates[0];
-          const localTemplate = templates[1];
+          //get global templates
+          //_tempate: applicable to all pages under
+          const globalTemplates = pages.filter(page => page.path.match(/_template/));
 
 
-          if (globalTemplate) {
-            renderVars.template = globalTemplate.revision.body;
-          }
-          else if (localTemplate) {
-            renderVars.template = localTemplate.revision.body;
-          }
+          return Promise.resolve();
+        })
+        .catch(err => {
+          console.log('no templates found');
+          return Promise.resolve();
         });
         });
+
+
+
+
+
+      // return Promise.all([getGlobalTemplate, getLocalTemplate])
+      //   .then(function(templates) {
+      //     const globalTemplate = templates[0];
+      //     const localTemplate = templates[1];
+
+      //     if (globalTemplate) {
+      //       renderVars.template = globalTemplate.revision.body;
+      //     }
+      //     else if (localTemplate) {
+      //       renderVars.template = localTemplate.revision.body;
+      //     }
+      //   });
     })
     })
     // get list pages
     // get list pages
     .then(function() {
     .then(function() {