Browse Source

WIP: impl PagePath model class

Yuki Takei 6 years ago
parent
commit
c4259d34ec
2 changed files with 72 additions and 0 deletions
  1. 40 0
      src/client/js/components/PageList/PagePath2.jsx
  2. 32 0
      src/client/js/models/PagePath.js

+ 40 - 0
src/client/js/components/PageList/PagePath2.jsx

@@ -0,0 +1,40 @@
+import React from 'react';
+import PropTypes from 'prop-types';
+
+// import escapeStringRegexp from 'escape-string-regexp';
+
+import PagePathModel from '../../models/PagePath';
+
+const PagePath2 = (props) => {
+
+  const model = new PagePathModel(props.page.path, true);
+
+  // const pagePath = decodeURIComponent(page.path);
+  // const shortPath = this.getShortPath(pagePath);
+
+  // const shortPathEscaped = escapeStringRegexp(shortPath);
+  // const pathPrefix = pagePath.replace(new RegExp(`${shortPathEscaped}(/)?$`), '');
+
+  let classNames = ['page-path'];
+  classNames = classNames.concat(props.additionalClassNames);
+
+  // if (isShortPathOnly) {
+  //   return <span className={classNames.join(' ')}>{shortPath}</span>;
+  // }
+
+  // return <span className={classNames.join(' ')}>{pathPrefix}<strong>{shortPath}</strong></span>;
+
+  return <span className={classNames.join(' ')}>{model.former} /// {model.latter}</span>;
+};
+
+PagePath2.propTypes = {
+  page: PropTypes.object.isRequired,
+  isShortPathOnly: PropTypes.bool,
+  additionalClassNames: PropTypes.array,
+};
+
+PagePath2.defaultProps = {
+  additionalClassNames: [],
+};
+
+export default PagePath2;

+ 32 - 0
src/client/js/models/PagePath.js

@@ -0,0 +1,32 @@
+// https://regex101.com/r/BahpKX/2
+const PATTERN_INCLUDE_DATE = /^(.+\/[^/]+)\/(\d{4}|\d{4}\/\d{2}|\d{4}\/\d{2}\/\d{2})$/;
+// https://regex101.com/r/WVpPpY/1
+const PATTERN_DEFAULT = /^((.*)\/)?([^/]+)$/;
+
+export default class PagePath {
+
+  constructor(pagePath, evalDatePath = false) {
+    this.former = null;
+    this.latter = null;
+
+    // root
+    if (pagePath === '/') {
+      return;
+    }
+
+    // evaluate date path
+    if (evalDatePath) {
+      const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
+      if (matchDate != null) {
+        this.former = matchDate[1];
+        this.latter = matchDate[2];
+        return;
+      }
+    }
+
+    const matchDefault = pagePath.match(PATTERN_DEFAULT);
+    this.former = matchDefault[2];
+    this.latter = matchDefault[3];
+  }
+
+}