Browse Source

impl PagePath component

Yuki Takei 9 years ago
parent
commit
053649e5c1
4 changed files with 42 additions and 79 deletions
  1. 1 1
      lib/views/page.html
  2. 4 0
      resource/js/app.js
  3. 37 43
      resource/js/components/Page/PagePath.js
  4. 0 35
      resource/js/legacy/crowi.js

+ 1 - 1
lib/views/page.html

@@ -14,7 +14,7 @@
 
 
 
 
     <div class="flex-title-line">
     <div class="flex-title-line">
-      <h1 class="title flex-item-title" id="revision-path">{{ path|insertSpaceToEachSlashes }}</h1>
+      <h1 class="title flex-item-title" id="revision-path"></h1>
       {% if page %}
       {% if page %}
       <div class="flex-item-action">
       <div class="flex-item-action">
         <a href="#" title="Bookmark" class="bookmark-link" id="bookmark-button" data-csrftoken="{{ csrf() }}" data-bookmarked="0"><i class="fa fa-star-o"></i></a>
         <a href="#" title="Bookmark" class="bookmark-link" id="bookmark-button" data-csrftoken="{{ csrf() }}" data-bookmarked="0"><i class="fa fa-star-o"></i></a>

+ 4 - 0
resource/js/app.js

@@ -9,6 +9,7 @@ import SearchPage       from './components/SearchPage';
 import PageListSearch   from './components/PageListSearch';
 import PageListSearch   from './components/PageListSearch';
 import PageHistory      from './components/PageHistory';
 import PageHistory      from './components/PageHistory';
 import SeenUserList     from './components/SeenUserList';
 import SeenUserList     from './components/SeenUserList';
+import PagePath         from './components/Page/PagePath';
 //import PageComment  from './components/PageComment';
 //import PageComment  from './components/PageComment';
 
 
 if (!window) {
 if (!window) {
@@ -17,8 +18,10 @@ if (!window) {
 
 
 const mainContent = document.querySelector('#content-main');
 const mainContent = document.querySelector('#content-main');
 let pageId = null;
 let pageId = null;
+let pagePath;
 if (mainContent !== null) {
 if (mainContent !== null) {
   pageId = mainContent.attributes['data-page-id'].value;
   pageId = mainContent.attributes['data-page-id'].value;
+  pagePath = mainContent.attributes['data-path'].value;
 }
 }
 
 
 // FIXME
 // FIXME
@@ -43,6 +46,7 @@ const componentMappings = {
   //'revision-history': <PageHistory pageId={pageId} />,
   //'revision-history': <PageHistory pageId={pageId} />,
   //'page-comment': <PageComment />,
   //'page-comment': <PageComment />,
   'seen-user-list': <SeenUserList />,
   'seen-user-list': <SeenUserList />,
+  'revision-path': <PagePath pagePath={pagePath} />,
 };
 };
 
 
 Object.keys(componentMappings).forEach((key) => {
 Object.keys(componentMappings).forEach((key) => {

+ 37 - 43
resource/js/components/Page/PagePath.js

@@ -2,63 +2,57 @@ import React from 'react';
 
 
 export default class PagePath extends React.Component {
 export default class PagePath extends React.Component {
 
 
-  // Original Crowi.linkPath
-  /*
-  Crowi.linkPath = function(revisionPath) {
-    var $revisionPath = revisionPath || '#revision-path';
-    var $title = $($revisionPath);
-    var pathData = $('#content-main').data('path');
+  constructor(props) {
+    super(props);
 
 
-    if (!pathData) {
-      return ;
-    }
-
-    var realPath = pathData.trim();
-    if (realPath.substr(-1, 1) == '/') {
-      realPath = realPath.substr(0, realPath.length - 1);
-    }
+    this.state = {
+      pages: [],
+    };
+  }
 
 
-    var path = '';
-    var pathHtml = '';
-    var splittedPath = realPath.split(/\//);
-    splittedPath.shift();
-    splittedPath.forEach(function(sub) {
-      path += '/';
-      pathHtml += ' <a href="' + path + '">/</a> ';
-      if (sub) {
-        path += sub;
-        pathHtml += '<a href="' + path + '">' + sub + '</a>';
-      }
+  componentWillMount() {
+    let splitted = this.props.pagePath.split(/\//);
+    splitted.shift();   // omit first element with shift()
+
+    let pages = [];
+    let parentPath = '/';
+    splitted.forEach((pageName) => {
+      pages.push({
+        pagePath: parentPath + pageName,
+        pageName: pageName,
+      });
+      parentPath += pageName + '/';
     });
     });
-    if (path.substr(-1, 1) != '/') {
-      path += '/';
-      pathHtml += ' <a href="' + path + '" class="last-path">/</a>';
-    }
-    $title.html(pathHtml);
-  };
-  */
 
 
-  linkPath(path) {
-    return path;
+    this.setState({ pages });
   }
   }
 
 
   render() {
   render() {
-    const page = this.props.page;
-    const shortPath = this.getShortPath(page.path);
-    const pathPrefix = page.path.replace(new RegExp(shortPath + '(/)?$'), '');
+    const pageLength = this.state.pages.length;
+    const afterElements = [];
+    this.state.pages.forEach((page, index) => {
+      afterElements.push(
+        <span key={page.pagePath} className="path-segment">
+          <a href={page.pagePath}>{page.pageName}</a>
+        </span>);
+      afterElements.push(
+        <span key={page.pagePath+'/'} className="separator">
+          <a href={page.pagePath+'/'} className={(index==pageLength-1) ? 'last-path' : ''}>/</a>
+        </span>
+      );
+    });
 
 
     return (
     return (
       <span className="page-path">
       <span className="page-path">
-        {pathPrefix}<strong>{shortPath}</strong>
+        <span className="separator">
+          <a href="/">/</a>
+        </span>
+        {afterElements}
       </span>
       </span>
     );
     );
   }
   }
 }
 }
 
 
 PagePath.propTypes = {
 PagePath.propTypes = {
-  page: React.PropTypes.object.isRequired,
-};
-
-PagePath.defaultProps = {
-  page: {},
+  pagePath: React.PropTypes.string.isRequired,
 };
 };

+ 0 - 35
resource/js/legacy/crowi.js

@@ -18,39 +18,6 @@ Crowi.createErrorView = function(msg) {
   $('#main').prepend($('<p class="alert-message error">' + msg + '</p>'));
   $('#main').prepend($('<p class="alert-message error">' + msg + '</p>'));
 };
 };
 
 
-Crowi.linkPath = function(revisionPath) {
-  var $revisionPath = revisionPath || '#revision-path';
-  var $title = $($revisionPath);
-  var pathData = $('#content-main').data('path');
-
-  if (!pathData) {
-    return ;
-  }
-
-  var realPath = pathData.trim();
-  if (realPath.substr(-1, 1) == '/') {
-    realPath = realPath.substr(0, realPath.length - 1);
-  }
-
-  var path = '';
-  var pathHtml = '';
-  var splittedPath = realPath.split(/\//);
-  splittedPath.shift();
-  splittedPath.forEach(function(sub) {
-    path += '/';
-    pathHtml += ' <a href="' + Crowi.escape(path) + '">/</a> ';
-    if (sub) {
-      path += sub;
-      pathHtml += '<a href="' + Crowi.escape(path) + '">' + Crowi.escape(sub) + '</a>';
-    }
-  });
-  if (path.substr(-1, 1) != '/') {
-    path += '/';
-    pathHtml += ' <a href="' + Crowi.escape(path) + '" class="last-path">/</a>';
-  }
-  $title.html(pathHtml);
-};
-
 Crowi.correctHeaders = function(contentId) {
 Crowi.correctHeaders = function(contentId) {
   // h1 ~ h6 の id 名を補正する
   // h1 ~ h6 の id 名を補正する
   var $content = $(contentId || '#revision-body-content');
   var $content = $(contentId || '#revision-body-content');
@@ -194,8 +161,6 @@ $(function() {
     }
     }
   };
   };
 
 
-  Crowi.linkPath();
-
   $('[data-toggle="popover"]').popover();
   $('[data-toggle="popover"]').popover();
   $('[data-toggle="tooltip"]').tooltip();
   $('[data-toggle="tooltip"]').tooltip();
   $('[data-tooltip-stay]').tooltip('show');
   $('[data-tooltip-stay]').tooltip('show');