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

Merge pull request #451 from weseek/imprv/auto-template-api

Imprv/auto template api
Sou Mizobuchi 7 лет назад
Родитель
Сommit
6dd4c2f90f

+ 2 - 2
lib/locales/en-US/translation.json

@@ -231,11 +231,11 @@
       "create/edit": "Create/Edit Template page..",
       "select": "Select template page type"
     },
-    "local": {
+    "children": {
       "label": "Template for children",
       "desc": "Applies only to the same level pages which the template exists"
     },
-    "global": {
+    "decendants": {
       "label": "Template for descendants",
       "desc": "Applies to all decendant pages"
     }

+ 2 - 2
lib/locales/ja/translation.json

@@ -246,11 +246,11 @@
       "select": "テンプレートタイプを選択してください",
       "create/edit": "テンプレートページの作成/編集.."
     },
-    "local": {
+    "children": {
       "label": "同一階層テンプレート",
       "desc": "テンプレートページが存在する階層にのみ適応されます"
     },
-    "global": {
+    "decendants": {
       "label": "下位層テンプレート",
       "desc": "テンプレートページが存在する下位層のすべてのページに適応されます"
     }

+ 7 - 1
lib/models/page.js

@@ -2,6 +2,7 @@ module.exports = function(crowi) {
   var debug = require('debug')('growi:models:page')
     , mongoose = require('mongoose')
     , escapeStringRegexp = require('escape-string-regexp')
+    , templateChecker = require('../util/templateChecker')
     , ObjectId = mongoose.Schema.Types.ObjectId
     , GRANT_PUBLIC = 1
     , GRANT_RESTRICTED = 2
@@ -96,6 +97,10 @@ module.exports = function(crowi) {
     return isPortalPath(this.path);
   };
 
+  pageSchema.methods.isTemplate = function() {
+    return templateChecker(this.path);
+  };
+
   pageSchema.methods.isCreator = function(userData) {
     // ゲスト閲覧の場合は userData に false が入る
     if (!userData) {
@@ -570,11 +575,12 @@ module.exports = function(crowi) {
   };
 
   const generatePathsOnTree = (path, pathList) => {
+    pathList.push(path);
+
     if (path === '') {
       return pathList;
     }
 
-    pathList.push(path);
     const newPath = cutOffLastSlash(path);
 
     return generatePathsOnTree(newPath, pathList);

+ 1 - 0
lib/routes/index.js

@@ -167,6 +167,7 @@ module.exports = function(crowi, app) {
   app.post('/_api/pages.revertRemove' , loginRequired(crowi, app) , csrf, page.api.revertRemove); // (Avoid from API Token)
   app.post('/_api/pages.unlink'       , loginRequired(crowi, app) , csrf, page.api.unlink); // (Avoid from API Token)
   app.post('/_api/pages.duplicate'    , accessTokenParser, loginRequired(crowi, app), csrf, page.api.duplicate);
+  app.get('/_api/pages.templates'   , accessTokenParser , loginRequired(crowi, app, false) , page.api.templates);
   app.get('/_api/comments.get'        , accessTokenParser , loginRequired(crowi, app, false) , comment.api.get);
   app.post('/_api/comments.add'       , form.comment, accessTokenParser , loginRequired(crowi, app) , csrf, comment.api.add);
   app.post('/_api/comments.remove'    , accessTokenParser , loginRequired(crowi, app) , csrf, comment.api.remove);

+ 18 - 14
lib/routes/page.js

@@ -252,8 +252,6 @@ module.exports = function(crowi, app) {
       tree: [],
       pageRelatedGroup: null,
       template: null,
-      childrenTemplateExists: false,
-      decendantsTemplateExists: false,
     };
 
     var pageTeamplate = 'customlayout-selector/page';
@@ -285,10 +283,6 @@ module.exports = function(crowi, app) {
         .then(function() {
           return Page.checkIfTemplatesExist(originalPath);
         })
-        .then(function(templateInfo) {
-          renderVars.childrenTemplateExists = templateInfo.childrenTemplateExists;
-          renderVars.decendantsTemplateExists = templateInfo.decendantsTemplateExists;
-        })
         .then(() => {
           return PageGroupRelation.findByPage(renderVars.page);
         })
@@ -463,8 +457,6 @@ module.exports = function(crowi, app) {
       page: pageData,
       revision: pageData.revision || {},
       author: pageData.revision.author || false,
-      childrenTemplateExists: false,
-      decendantsTemplateExists: false,
     };
     var userPage = isUserPage(pageData.path);
     var userData = null;
@@ -509,12 +501,6 @@ module.exports = function(crowi, app) {
       }
     }).then(function() {
       return interceptorManager.process('beforeRenderPage', req, res, renderVars);
-    }).then(function() {
-      return Page.checkIfTemplatesExist(pageData.path)
-        .then(function(templateInfo) {
-          renderVars.childrenTemplateExists = templateInfo.childrenTemplateExists;
-          renderVars.decendantsTemplateExists = templateInfo.decendantsTemplateExists;
-        });
     }).then(function() {
       var defaultPageTeamplate = 'customlayout-selector/page';
       if (userData) {
@@ -1179,5 +1165,23 @@ module.exports = function(crowi, app) {
     });
   };
 
+  /**
+   * @api {get} /pages.templates Check if templates exist for page
+   * @apiName FindTemplates
+   * @apiGroup Page
+   *
+   * @apiParam {String} path
+   */
+  api.templates = function(req, res) {
+    const pagePath = req.query.path;
+    const templateFinder = Page.checkIfTemplatesExist(pagePath);
+
+    templateFinder.then(function(templateInfo) {
+      return res.json(ApiResponse.success(templateInfo));
+    }).catch(function(err) {
+      return res.json(ApiResponse.error(err));
+    });
+  };
+
   return actions;
 };

+ 13 - 0
lib/util/templateChecker.js

@@ -0,0 +1,13 @@
+/**
+ * templateChecker
+ */
+
+module.exports = function(path) {
+  'use strict';
+
+  if (path.match(/.*\/_{1,2}template$/)) {
+    return true;
+  }
+
+  return false;
+};

+ 35 - 17
lib/views/modal/create_page.html

@@ -51,12 +51,17 @@
               <div class="create-page-input-row d-flex align-items-center">
                 <select id="template-type" class="page-name-input form-control">
                   <option value="" disabled selected>{{ t('template.option_label.select') }}</option>
-                  <option value="children">{{ t('template.local.label') }}(_template) - {{ t('template.local.desc') }}</option>
-                  <option value="decentants">{{ t('template.global.label') }}(__template) - {{ t('template.global.desc') }}</option>
+                  <option value="children">{{ t('template.children.label') }}(_template) - {{ t('template.children.desc') }}</option>
+                  <option value="decentants">{{ t('template.decendants.label') }}(__template) - {{ t('template.decendants.desc') }}</option>
                 </select>
               </div>
               <div class="create-page-button-container">
-                  <a id="link-to-template" href="{{ page.path || path }}"><button class="fcbtn btn btn-outline btn-rounded btn-primary btn-1b"><i class="icon-fw icon-doc"></i>{{ t('Create') }}</button></a>
+                  <a id="link-to-template" href="{{ page.path || path }}">
+                    <button class="fcbtn btn btn-outline btn-rounded btn-primary btn-1b">
+                      <i class="icon-fw icon-doc"></i>
+                      <span id="template-button">{{ t('Create') }}/{{ t('Edit') }}</span>
+                    </button>
+                  </a>
               </div>
             </div>
           </fieldset>
@@ -68,22 +73,35 @@
   </div><!-- /.modal-dialog -->
 </div><!-- /.modal -->
 <script>
-  if($("#create-page")) {
-    let pagePath = $("#link-to-template").attr("href");
+  let buttonTextChildren;
+  let buttonTextDecendants;
+  let pagePath = $("#link-to-template").attr("href");
 
-    if (pagePath.endsWith("/")) {
+  if (pagePath.endsWith("/")) {
       pagePath = pagePath.slice(0, -1);
-    };
+  };
 
-    $("#template-type").on("change", () => {
-      if ($("#template-type").val() === "children") {
-        href = pagePath + "/_template#edit-form";
-        $("#link-to-template").attr("href", href);
-      }
-      else if ($("#template-type").val() === "decentants") {
-        href = pagePath + "/__template#edit-form";
-        $("#link-to-template").attr("href", href);
-      };
+  $.get(`/_api/pages.templates?path=${pagePath}`)
+    .then(templateInfo => {
+      buttonTextChildren = templateInfo.childrenTemplateExists ? '{{ t('Edit') }}' : '{{ t('Create') }}';
+      buttonTextDecendants = templateInfo.decendantsTemplateExists ? '{{ t('Edit') }}' : '{{ t('Create') }}';
     });
-  };
+
+  $("#create-template").ready(() => {
+    $('#template-button-children').text(buttonTextChildren);
+    $('#template-button-decendants').text(buttonTextDecendants);
+  });
+
+  $("#template-type").on("change", () => {
+    if ($("#template-type").val() === "children") {
+      href = pagePath + "/_template#edit-form";
+      $("#link-to-template").attr("href", href);
+      $('#template-button').text(buttonTextChildren);
+    }
+    else if ($("#template-type").val() === "decentants") {
+      href = pagePath + "/__template#edit-form";
+      $("#link-to-template").attr("href", href);
+      $('#template-button').text(buttonTextDecendants);
+    };
+  });
 </script>

+ 6 - 8
lib/views/modal/create_template.html

@@ -12,30 +12,28 @@
           <div class="row">
             <div class="col-sm-6">
               <div class="panel panel-default panel-select-template">
-                <div class="panel-heading">{{ t('template.local.label') }}</div>
+                <div class="panel-heading">{{ t('template.children.label') }}</div>
                 <div class="panel-body">
                   <p class="text-center"><code>_template</code></p>
-                  <p class="help-block text-center"><small>{{ t('template.local.desc') }}</small></p>
+                  <p class="help-block text-center"><small>{{ t('template.children.desc') }}</small></p>
                 </div>
                 <div class="panel-footer text-center">
                   <a href="{% if page.path.endsWith('/') %}{{ page.path }}{% else %}{{ page.path}}/{% endif %}_template#edit-form"
-                      class="btn btn-sm btn-primary">
-                    {% if childrenTemplateExists %}{{ t('Edit') }}{% else %}{{ t('Create') }}{% endif %}
+                      class="btn btn-sm btn-primary" id="template-button-children">
                   </a>
                 </div>
               </div>
             </div>
             <div class="col-sm-6">
               <div class="panel panel-default panel-select-template">
-                <div class="panel-heading">{{ t('template.global.label') }}</div>
+                <div class="panel-heading">{{ t('template.decendants.label') }}</div>
                 <div class="panel-body">
                   <p class="text-center"><code>__template</code></p>
-                  <p class="help-block text-center"><small>{{ t('template.global.desc') }}</small></p>
+                  <p class="help-block text-center"><small>{{ t('template.decendants.desc') }}</small></p>
                 </div>
                 <div class="panel-footer text-center">
                   <a href="{% if page.path.endsWith('/') %}{{ page.path }}{% else %}{{ page.path}}/{% endif %}__template#edit-form"
-                      class="btn btn-sm btn-primary">
-                  {% if decendantsTemplateExists %}{{ t('Edit') }}{% else %}{{ t('Create') }}{% endif %}
+                      class="btn btn-sm btn-primary" id="template-button-decendants">
                   </a>
                 </div>
               </div>

+ 4 - 0
lib/views/widget/page_list.html

@@ -19,6 +19,10 @@
       <span class="label label-info">PORTAL</span>
     {% endif  %}
 
+    {% if page.isTemplate() %}
+      <span class="label label-info">TMPL</span>
+    {% endif  %}
+
     {% if page.commentCount > 0 %}
     <span>
       <i class="icon-bubble"></i>{{ page.commentCount }}

+ 8 - 0
resource/js/components/PageList/PageListMeta.js

@@ -14,6 +14,7 @@ export default class PageListMeta extends React.Component {
   render() {
     // TODO isPortal()
     const page = this.props.page;
+    const templateChecker = require('../../../../lib/util/templateChecker');
 
     // portal check
     let PortalLabel;
@@ -21,6 +22,12 @@ export default class PageListMeta extends React.Component {
       PortalLabel = <span className="label label-info">PORTAL</span>;
     }
 
+    // template check
+    let TemplateLabel;
+    if (templateChecker(page.path)) {
+      TemplateLabel = <span className="label label-info">TMPL</span>;
+    }
+
     let CommentCount;
     if (page.commentCount > 0) {
       CommentCount = <span><i className="icon-bubble" />{page.commentCount}</span>;
@@ -35,6 +42,7 @@ export default class PageListMeta extends React.Component {
     return (
       <span className="page-list-meta">
         {PortalLabel}
+        {TemplateLabel}
         {CommentCount}
         {LikerCount}
       </span>