Yuki Takei 9 лет назад
Родитель
Сommit
fb71088b9b

+ 25 - 6
packages/growi-plugin-lsx/src/lib/routes/lsx.js

@@ -1,18 +1,37 @@
+const debug = require('debug')('crowi-plugin:lsx:routes:lsx');
+const OptionParser = require('../util/option-parser');
+
+class Lsx {
+
+  static addDepthCondition(query, optionDepth) {
+    return query.and({
+      // TODO implement
+      // path: new RegExp('...')
+    })
+  }
+
+}
+
 module.exports = (crowi, app) => {
-  var debug = require('debug')('crowi-plugin:lsx:routes:lsx')
-    , Page = crowi.model('Page')
+  var Page = crowi.model('Page')
     , ApiResponse = crowi.require('../util/apiResponse')
     , actions = {};
 
   actions.listPages = (req, res) => {
     let user = req.user;
     let pagePath = req.query.pagePath;
-    let queryOptions = req.query.queryOptions;
+    let options = JSON.parse(req.query.options);
 
     // find pages
-    Page.generateQueryToListByStartWith(pagePath, user, queryOptions)
-      .populate('revision', '-body')  // exclude body
-      .exec()
+    let query = Page.generateQueryToListByStartWith(pagePath, user, {})
+      .populate('revision', '-body');  // exclude body
+
+    // depth
+    if (options.depth !== undefined) {
+      query = Lsx.addDepthCondition(query, options.depth);
+    }
+
+    query.exec()
       .then((pages) => {
         res.json(ApiResponse.success({pages}));
       })

+ 57 - 0
packages/growi-plugin-lsx/src/lib/util/option-parser.js

@@ -0,0 +1,57 @@
+class OptionParser {
+
+  /**
+   * parse range expression
+   *
+   * ex:
+   *  1:2 -> { start: 1, end: 2 }
+   *  1:  -> { start: 1, end: -1 }
+   *  2+3 -> { start: 1, end: 5 }
+   *
+   * @static
+   * @param {any} str
+   * @returns
+   *
+   * @memberOf OptionParser
+   */
+  static parseRange(str) {
+    if (str == undefined) {
+      return undefined;
+    }
+
+    // see: https://regex101.com/r/w4KCwC/3
+    const match = str.match(/^(-?[0-9]+)(([:+]{1})(-?[0-9]+)?)?$/);
+    if (!match) {
+      return undefined;
+    }
+
+    // determine start
+    let start;
+    let end;
+
+    // has operator
+    if (match[3] != undefined) {
+      start = +match[1];
+      const operator = match[3];
+
+      // determine end
+      if (operator === ':') {
+        end = +match[4] || -1;  // set last(-1) if undefined
+      }
+      else if (operator === '+') {
+        end = +match[4] || 0;   // plus zero if undefined
+        end += start;
+      }
+    }
+    // don't have operator
+    else {
+      start = 1;
+      end = +match[1];
+    }
+
+    return { start, end };
+  }
+
+}
+
+module.exports = OptionParser;

+ 1 - 1
packages/growi-plugin-lsx/src/resource/js/components/Lsx.jsx

@@ -40,7 +40,7 @@ export class Lsx extends React.Component {
     // ex: '/Java/' not to match to '/JavaScript'
     let pagePath = this.addSlashOfEnd(lsxContext.pagePath);
 
-    this.props.crowi.apiGet('/plugins/lsx', {pagePath, queryOptions: lsxContext.options})
+    this.props.crowi.apiGet('/plugins/lsx', {pagePath, options: lsxContext.options})
       .catch(error => {
         const errorMessage = error.response.data.error.message;
         this.setState({ isError: true, errorMessage: errorMessage });