Explorar el Código

Merge pull request #424 from weseek/feat/auto-template

Feat/auto template
Yuki Takei hace 7 años
padre
commit
808644e496
Se han modificado 3 ficheros con 89 adiciones y 3 borrados
  1. 79 0
      lib/models/page.js
  2. 8 3
      lib/routes/page.js
  3. 2 0
      lib/views/widget/not_found_content.html

+ 79 - 0
lib/models/page.js

@@ -527,6 +527,85 @@ module.exports = function(crowi) {
     });
   };
 
+  /**
+   * find all templates applicable to the new page
+   */
+  pageSchema.statics.findTemplate = function(path) {
+    const Page = this;
+    const templatePath = cutOffLastSlash(path);
+    const pathList = generatePathsOnTree(templatePath, []);
+    const regexpList = pathList.map(path => new RegExp(`^${path}/_{1,2}template$`));
+
+    return Page
+      .find({path: {$in: regexpList}})
+      .populate({path: 'revision', model: 'Revision'})
+      .then(templates => {
+        return fetchTemplate(templates, templatePath);
+      });
+  };
+
+  const cutOffLastSlash = path => {
+    const lastSlash = path.lastIndexOf('/');
+    return path.substr(0, lastSlash);
+  };
+
+  const generatePathsOnTree = (path, pathList) => {
+    if (path === '') {
+      return pathList;
+    }
+
+    pathList.push(path);
+    const newPath = cutOffLastSlash(path);
+
+    return generatePathsOnTree(newPath, pathList);
+  };
+
+  const assignTemplateByType = (templates, path, type) => {
+    for (let i = 0; i < templates.length; i++) {
+      if (templates[i].path === `${path}/${type}template`) {
+        return templates[i];
+      }
+    }
+  };
+
+  const assignGlobalTemplate = (globalTemplates, path) => {
+    const globalTemplate = assignTemplateByType(globalTemplates, path, '_');
+    if (globalTemplate) {
+      return globalTemplate;
+    }
+
+    if (path === '') {
+      return;
+    }
+
+    const newPath = cutOffLastSlash(path);
+    return assignGlobalTemplate(globalTemplates, newPath);
+  };
+
+  const fetchTemplate = (templates, templatePath) => {
+    let templateBody;
+    /**
+     * get local template
+     * __tempate: applicable only to immediate decendants
+     */
+    const localTemplate = assignTemplateByType(templates, templatePath, '__');
+
+    /**
+     * get global templates
+     * _tempate: applicable to all pages under
+     */
+    const globalTemplate = assignGlobalTemplate(templates, templatePath);
+
+    if (localTemplate) {
+      templateBody =  localTemplate.revision.body;
+    }
+    else if (globalTemplate) {
+      templateBody = globalTemplate.revision.body;
+    }
+
+    return templateBody;
+  };
+
   // find page by path
   pageSchema.statics.findPageByPath = function(path) {
     if (path == null) {

+ 8 - 3
lib/routes/page.js

@@ -251,11 +251,13 @@ module.exports = function(crowi, app) {
       pages: [],
       tree: [],
       pageRelatedGroup: null,
+      template: null,
     };
 
     var pageTeamplate = 'customlayout-selector/page';
 
     var isRedirect = false;
+    var originalPath = path;
     Page.findPage(path, req.user, req.query.revision)
     .then(function(page) {
       debug('Page found', page._id, page.path);
@@ -318,11 +320,14 @@ module.exports = function(crowi, app) {
         });
       }
     })
-    // page not exists
+    // look for templates if page not exists
     .catch(function(err) {
-      debug('Page not found', path);
-      // change template
       pageTeamplate = 'customlayout-selector/not_found';
+
+      return Page.findTemplate(originalPath)
+        .then(template => {
+          renderVars.template = template;
+        });
     })
     // get list pages
     .then(function() {

+ 2 - 0
lib/views/widget/not_found_content.html

@@ -18,6 +18,8 @@
   <div class="tab-content">
     {% if isEnabledAttachTitleHeader() %}
     <script type="text/template" id="raw-text-original"># {{ path|path2name }}</script>
+    {% elseif template %}
+    <script type="text/template" id="raw-text-original">{{ template }}</script>
     {% endif %}
     {# list view #}
     <div class="p-t-10 active tab-pane page-list-container" id="revision-body">