Quellcode durchsuchen

removed extra promises and renamed fucntions

sou vor 7 Jahren
Ursprung
Commit
b52a6950d7
4 geänderte Dateien mit 19 neuen und 34 gelöschten Zeilen
  1. 0 6
      lib/models/config.js
  2. 3 12
      lib/models/page.js
  3. 15 15
      lib/routes/page.js
  4. 1 1
      lib/views/widget/not_found_content.html

+ 0 - 6
lib/models/config.js

@@ -92,7 +92,6 @@ module.exports = function(crowi) {
       'customize:isEnabledTimeline' : true,
       'customize:isSavedStatesOfTabChanges' : true,
       'customize:isEnabledAttachTitleHeader' : false,
-      'customize:templateExists' : true,
     };
     /* eslint-enable */
   }
@@ -405,11 +404,6 @@ module.exports = function(crowi) {
     return getValueForCrowiNS(config, key);
   };
 
-  configSchema.statics.templateExists = function(config) {
-    const key = 'customize:templateExists';
-    return getValueForCrowiNS(config, key);
-  };
-
   configSchema.statics.fileUploadEnabled = function(config) {
     const Config = this;
 

+ 3 - 12
lib/models/page.js

@@ -532,18 +532,9 @@ module.exports = function(crowi) {
     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);
-        })
-        .populate({path: 'revision', model: 'Revision'});
-    });
+    return Page
+      .find({path: {$in: regexpList}})
+      .populate({path: 'revision', model: 'Revision'});
   };
 
   // find page by path

+ 15 - 15
lib/routes/page.js

@@ -311,28 +311,25 @@ module.exports = function(crowi, app) {
     .catch(function(err) {
       pageTeamplate = 'customlayout-selector/not_found';
 
-      const lastSlash = originalUrl.lastIndexOf('/');
-      const templateUrl = originalUrl.substr(0, lastSlash);
-      const pathList = pathGenerator(originalUrl, []);
+      const templateUrl = cutOffLastSlash(originalUrl);
+      const pathList = findAllAscendantPaths(originalUrl, []);
 
       return Page.findAllTemplates(pathList)
         .then(templates => {
           //get local template
           //@tempate: applicable only to immediate decendants
           const localTemplate = templateFinder(templates, templateUrl, '@');
-          if (localTemplate) {
-            renderVars.template = localTemplate.revision.body;
-            return Promise.resolve();
-          }
 
           //get global templates
           //_tempate: applicable to all pages under
           const globalTemplate = globalTemplateFinder(templates, templateUrl);
-          if (globalTemplate) {
+
+          if (localTemplate) {
+            renderVars.template = localTemplate.revision.body;
+          }
+          else if (globalTemplate) {
             renderVars.template = globalTemplate.revision.body;
           }
-
-          return Promise.resolve();
         });
     })
     // get list pages
@@ -383,16 +380,20 @@ module.exports = function(crowi, app) {
     });
   };
 
-  const pathGenerator = (path, pathList) => {
+  const cutOffLastSlash = path => {
     const lastSlash = path.lastIndexOf('/');
-    const newPath = path.substr(0, lastSlash);
+    return path.substr(0, lastSlash);
+  };
+
+  const findAllAscendantPaths = (path, pathList) => {
+    const newPath = cutOffLastSlash(path);
 
     if (newPath === '') {
       return pathList;
     }
 
     pathList.push(newPath);
-    return pathGenerator(newPath, pathList);
+    return findAllAscendantPaths(newPath, pathList);
   };
 
   const globalTemplateFinder = (globalTemplates, path) => {
@@ -405,8 +406,7 @@ module.exports = function(crowi, app) {
       return;
     }
 
-    const lastSlash = path.lastIndexOf('/');
-    const newPath = path.substr(0, lastSlash);
+    const newPath = cutOffLastSlash(path);
     return globalTemplateFinder(globalTemplates, newPath);
   };
 

+ 1 - 1
lib/views/widget/not_found_content.html

@@ -18,7 +18,7 @@
   <div class="tab-content">
     {% if isEnabledAttachTitleHeader() %}
     <script type="text/template" id="raw-text-original"># {{ path|path2name }}</script>
-    {% elseif templateExists() %}
+    {% elseif template %}
     <script type="text/template" id="raw-text-original">{{ template }}</script>
     {% endif %}
     {# list view #}