Browse Source

"new" modal modified, checkIfTemplateExists method

sou 7 years ago
parent
commit
c906a2ef74

+ 20 - 0
lib/models/page.js

@@ -527,6 +527,26 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
+  // check if a given page has a local and global tempalte
+  pageSchema.statics.checkIfTemplatesExist = function(path) {
+    const Page = this;
+    const pathList = generatePathsOnTree(path, []);
+    const regexpList = pathList.map(path => new RegExp(`${path}/[_@]template`));
+    let templateInfo = {
+      localTemplateExists: false,
+      globalTemplateExists: false,
+    };
+
+    return Page
+      .find({path: {$in: regexpList}})
+      .then(templates => {
+        templateInfo.localTemplateExists = (assignTemplateByType(templates, path, '@') ? true : false);
+        templateInfo.globalTemplateExists = (assignGlobalTemplate(templates, path) ? true : false);
+
+        return templateInfo;
+      });
+  };
+
   // find all templates applicable to the new page
   // find all templates applicable to the new page
   pageSchema.statics.findTemplate = function(path) {
   pageSchema.statics.findTemplate = function(path) {
     const Page = this;
     const Page = this;

+ 9 - 0
lib/routes/page.js

@@ -241,6 +241,8 @@ module.exports = function(crowi, app) {
       userRelatedGroups: [],
       userRelatedGroups: [],
       pageRelatedGroup: null,
       pageRelatedGroup: null,
       template: null,
       template: null,
+      localTemplateExists: false,
+      globalTemplateExists: false,
     };
     };
 
 
     var pageTeamplate = 'customlayout-selector/page';
     var pageTeamplate = 'customlayout-selector/page';
@@ -269,6 +271,13 @@ module.exports = function(crowi, app) {
         .then(function(tree) {
         .then(function(tree) {
           renderVars.tree = tree;
           renderVars.tree = tree;
           return Promise.resolve();
           return Promise.resolve();
+        })
+        .then(function() {
+          return Page.checkIfTemplatesExist(originalPath)
+            .then(function(templateInfo) {
+              renderVars.localTemplateExists = templateInfo.localTemplateExists;
+              renderVars.globalTemplateExists = templateInfo.globalTemplateExists;
+            });
         }).then(function() {
         }).then(function() {
           var userPage = isUserPage(page.path);
           var userPage = isUserPage(page.path);
           var userData = null;
           var userData = null;

+ 32 - 0
lib/views/modal/create_page.html

@@ -44,8 +44,40 @@
           </fieldset>
           </fieldset>
         </form>
         </form>
 
 
+        <form class="row form-horizontal m-t-15">
+          <fieldset class="col-xs-12">
+            <legend>Create template under <code>{{ parentPath(path) }}</code></legend>
+            <div class="d-flex create-page-input-container">
+              <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>Select tempate type</option>
+                  <option value="local">Local Template(/@template) - Applies only to immediate decendant pages</option>
+                  <option value="global">Global Template(/_template) - Applies to all decendant pages</option>
+                </select>
+              </div>
+              <div class="create-page-button-container">
+                  <a id="link-to-template" href="{{ page.path }}"><button class="fcbtn btn btn-outline btn-rounded btn-primary btn-1b"><i class="icon-fw icon-doc"></i>{{ t('Create') }}</button></a>
+              </div>
+            </div>
+          </fieldset>
+        </form>
+
       </div><!-- /.modal-body -->
       </div><!-- /.modal-body -->
 
 
     </div><!-- /.modal-content -->
     </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
   </div><!-- /.modal-dialog -->
 </div><!-- /.modal -->
 </div><!-- /.modal -->
+<script>
+  const pagePath = $("#link-to-template").attr("href");
+
+  $('#template-type').on('change', () => {
+    if ($('#template-type').val() === 'local') {
+      href = pagePath + "/@template#edit-form";
+      $("#link-to-template").attr("href", href);
+    }
+    else if ($('#template-type').val() === 'global') {
+      href = pagePath + "/_template#edit-form";
+      $("#link-to-template").attr("href", href);
+    }
+  })
+  </script>

+ 13 - 11
lib/views/modal/create_template.html

@@ -8,27 +8,29 @@
       </div>
       </div>
       <div class="modal-body">
       <div class="modal-body">
           <div class="form-group">
           <div class="form-group">
-            <label for="">Create a new template for <code>{{ page.path }}</code></label><br>
+            <label for="">Create a new template for <code>{{ page.path }}</code></label><br><br>
             <div class="row">
             <div class="row">
               <div class="col-sm-6">
               <div class="col-sm-6">
-                <p class="help-block">Local Template</p>
-                <p class="help-block">Applies only to immediate decendant pages</p>
+                <p class="help-block text-center"><strong>Local Template</strong></p>
+                <p class="help-block text-center"><small>Applies only to immediate decendant pages</small></p>
+                <br>
                 <div class="d-flex justify-content-center">
                 <div class="d-flex justify-content-center">
-                  {% if template %}
-                  <a href="{{ page.path }}/@template#edit-form"><button type="submit" class="btn btn-xs btn-primary">Edit</button></a>
+                  {% if localTemplateExists %}
+                  <a href="{{ page.path }}/@template#edit-form"><button class="btn btn-xs btn-primary">Edit</button></a>
                   {% else %}
                   {% else %}
-                  <a href="{{ page.path }}/@template#edit-form"><button type="submit" class="btn btn-xs btn-primary">Create</button></a>
+                  <a href="{{ page.path }}/@template#edit-form"><button class="btn btn-xs btn-primary">Create</button></a>
                   {% endif %}
                   {% endif %}
                 </div>
                 </div>
               </div>
               </div>
               <div class="col-sm-6">
               <div class="col-sm-6">
-                  <p class="help-block">Global Template</p>
-                  <p class="help-block">Applies to all decendant pages</p>
+                  <p class="help-block text-center"><strong>Global Template</strong></p>
+                  <p class="help-block text-center"><small>Applies to all decendant pages</small></p>
+                  <br>
                   <div class="d-flex justify-content-center">
                   <div class="d-flex justify-content-center">
-                    {% if template %}
-                    <a href="{{ page.path }}/_template#edit-form"><button type="submit" class="btn btn-xs btn-primary">Edit</button></a>
+                    {% if globalTemplateExists %}
+                    <a href="{{ page.path }}/_template#edit-form"><button class="btn btn-xs btn-primary">Edit</button></a>
                     {% else %}
                     {% else %}
-                    <a href="{{ page.path }}/_template#edit-form"><button type="submit" class="btn btn-xs btn-primary">Create</button></a>
+                    <a href="{{ page.path }}/_template#edit-form"><button class="btn btn-xs btn-primary">Create</button></a>
                     {% endif %}
                     {% endif %}
                   </div>
                   </div>
               </div>
               </div>

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

@@ -30,7 +30,7 @@
       <ul class="dropdown-menu">
       <ul class="dropdown-menu">
         <li><a href="#" data-target="#renamePage" data-toggle="modal"><i class="icon-fw icon-action-redo"></i> {{ t('Move') }}</a></li>
         <li><a href="#" data-target="#renamePage" data-toggle="modal"><i class="icon-fw icon-action-redo"></i> {{ t('Move') }}</a></li>
         <li class="divider"></li>
         <li class="divider"></li>
-        <li class=""><a href="#" data-target="#create-template" data-toggle="modal"><i class="icon-fw icon-docs"></i> Create Template</a></li>
+        <li class=""><a href="#" data-target="#create-template" data-toggle="modal"><i class="icon-fw icon-docs"></i> Create/Edit Template</a></li>
         <li><a href="#" data-target="#duplicatePage" data-toggle="modal"><i class="icon-fw icon-docs"></i> {{ t('Duplicate') }}</a></li>
         <li><a href="#" data-target="#duplicatePage" data-toggle="modal"><i class="icon-fw icon-docs"></i> {{ t('Duplicate') }}</a></li>
         {% if isDeletablePage() %}
         {% if isDeletablePage() %}
         <li class="divider"></li>
         <li class="divider"></li>