routes.js 6.43 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1
import { transformURL } from './utils';
2 3

import { matchPath, Route, Switch} from 'react-router';
邓晓峰's avatar
邓晓峰 committed
4
import React, { Suspense, Fragment } from 'react';
邓晓峰's avatar
邓晓峰 committed
5 6 7
export const isURL = function(url) {
  // eslint-disable-next-line no-useless-escape
  return /^(http|https)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url);
8 9 10
};

const DEFAULT_APPLICATION = 'civweb4';
邓晓峰's avatar
邓晓峰 committed
11
/* eslint-disable */
邓晓峰's avatar
邓晓峰 committed
12
export const guid = function(prefix) {
邓晓峰's avatar
邓晓峰 committed
13 14 15 16
  let date = new Date().getTime();
  if (window.performance && typeof window.performance.now === 'function') {
    date += performance.now();
  }
邓晓峰's avatar
邓晓峰 committed
17
  const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(
邓晓峰's avatar
邓晓峰 committed
18 19
    c,
  ) {
邓晓峰's avatar
邓晓峰 committed
20 21
    const r = (date + Math.random() * 16) % 16 | 0;
    date = Math.floor(date / 16);
邓晓峰's avatar
邓晓峰 committed
22
    return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
邓晓峰's avatar
邓晓峰 committed
23
  });
邓晓峰's avatar
邓晓峰 committed
24
  return prefix + uuid.replace(/-/g, '.') + new Date().getTime();
邓晓峰's avatar
邓晓峰 committed
25
};
邓晓峰's avatar
邓晓峰 committed
26

27 28
function isAbsoluteURL(url) {
  return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
邓晓峰's avatar
邓晓峰 committed
29
};
30

邓晓峰's avatar
邓晓峰 committed
31
const level = 0;
邓晓峰's avatar
邓晓峰 committed
32 33
const getURL = url => {
  const i = url.indexOf('#');
邓晓峰's avatar
邓晓峰 committed
34 35
  const href = i >= 0 ? url.slice(0, i) : url;
  return href;
邓晓峰's avatar
邓晓峰 committed
36
};
邓晓峰's avatar
邓晓峰 committed
37

邓晓峰's avatar
邓晓峰 committed
38
const generRotes = (widgets, parent, level = 0) => {
邓晓峰's avatar
邓晓峰 committed
39
  const ret = [];
40
  if(!widgets || widgets.length === 0){
41 42 43
    return
  }
  (widgets).forEach((item, index) => {
44 45

    if (item.hasOwnProperty('widgets') && item.widgets !== null && Array.isArray(item.widgets) && item.widgets.length > 0) {
邓晓峰's avatar
邓晓峰 committed
46 47 48
      const _level_ = level + 1;
      const path = `/civweb/${guid('web_console')}`;
      const subKey = guid('panda');
49
      const alias = DEFAULT_APPLICATION;
50 51 52 53 54
      const convertURL = transformURL(item.icon.replace(/\s*/g, ''));
      const icon = (item && item.icon && item.icon !== null &&
        isAbsoluteURL(convertURL) ? convertURL: /(\\|\/)/.test(convertURL) ?
          window.globalConfig.transformDevAssetsBaseURL(transformURL(item.icon.replace(/\s*/g, ''))): item.icon
      ) || '';
邓晓峰's avatar
邓晓峰 committed
55 56
      ret.push({
        name: item.label,
邓晓峰's avatar
邓晓峰 committed
57
        level: _level_,
邓晓峰's avatar
邓晓峰 committed
58
        path,
邓晓峰's avatar
邓晓峰 committed
59
        component: 'BasicLayout',
邓晓峰's avatar
邓晓峰 committed
60
        parent,
61
        alias: alias,
62
        hideInMenu: item.hideInMenu || false,
邓晓峰's avatar
邓晓峰 committed
63 64 65 66
        routes: generRotes(
          item.widgets,
          Object.assign({}, item, { path, key: subKey }),
          _level_,
邓晓峰's avatar
邓晓峰 committed
67
          item.label
邓晓峰's avatar
邓晓峰 committed
68 69 70
        ),
        extData: {
          ...item,
71
          icon: icon,
邓晓峰's avatar
邓晓峰 committed
72
        },
邓晓峰's avatar
邓晓峰 committed
73 74
      });
    } else {
75
      const baseURL = item.product || DEFAULT_APPLICATION;
76 77
      if(item.hasOwnProperty('url') &&  item.url === '') return;
      let url = item.hasOwnProperty('url') && item.url !== undefined && item.url !== '' ? `/${baseURL}/${item.url.replace(/\s*/g, '')}`
邓晓峰's avatar
邓晓峰 committed
78
        : !item.hasOwnProperty('url')
邓晓峰's avatar
邓晓峰 committed
79 80 81 82
          ? guid('web_console')
          : '';
      if (url.includes('url=')) {
        url = url.split('url=')[1];
邓晓峰's avatar
邓晓峰 committed
83 84
        url = getURL(url);
      }
邓晓峰's avatar
邓晓峰 committed
85

邓晓峰's avatar
邓晓峰 committed
86
      if(isURL(item.url)) {
邓晓峰's avatar
邓晓峰 committed
87 88
        url = item.url
      }
89
      url = transformURL(url);
90
      const convertURL = item.icon && transformURL(item.icon.replace(/\s*/g, ''));
91 92 93 94
      const icon = (item && item.icon && item.icon !== null &&
        isAbsoluteURL(convertURL) ? convertURL: /(\\|\/)/.test(convertURL) ?
          window.globalConfig.transformDevAssetsBaseURL(transformURL(item.icon.replace(/\s*/g, ''))): item.icon
      ) || '';
邓晓峰's avatar
邓晓峰 committed
95

邓晓峰's avatar
邓晓峰 committed
96
      const l = level ? level + 1 : -1;
邓晓峰's avatar
邓晓峰 committed
97
      const common = {
邓晓峰's avatar
邓晓峰 committed
98
        name: item.label,
邓晓峰's avatar
邓晓峰 committed
99
        level: l,
邓晓峰's avatar
邓晓峰 committed
100
        component: 'BasicLayout',
邓晓峰's avatar
邓晓峰 committed
101
        path: baseURL !== '' ? transformURL(url) : '',
邓晓峰's avatar
邓晓峰 committed
102
        href: url,
邓晓峰's avatar
邓晓峰 committed
103
        target: isURL(url) ? '_blank' : '',
邓晓峰's avatar
邓晓峰 committed
104
        key: guid('panda'),
邓晓峰's avatar
邓晓峰 committed
105 106
        hideInMenu: item.label === '系统菜单组' ? true: item.hideInMenu,
        //l > 3 ? true: item.hideInMenu || false,
107
        alias: item.product || DEFAULT_APPLICATION,
邓晓峰's avatar
邓晓峰 committed
108 109
        extData: {
          ...item,
110
          icon,
邓晓峰's avatar
邓晓峰 committed
111 112
        },
        parent,
邓晓峰's avatar
邓晓峰 committed
113 114
      };
      ret.push(common);
邓晓峰's avatar
邓晓峰 committed
115 116
    }
  });
邓晓峰's avatar
邓晓峰 committed
117
  return ret;
邓晓峰's avatar
邓晓峰 committed
118
};
邓晓峰's avatar
邓晓峰 committed
119
export function simpleNormalizeChildren(children) {
邓晓峰's avatar
邓晓峰 committed
120 121
  for (let i = 0; i < children.length; i++) {
    if (Array.isArray(children[i])) {
邓晓峰's avatar
邓晓峰 committed
122
      return Array.prototype.concat.apply([], children);
邓晓峰's avatar
邓晓峰 committed
123 124
    }
  }
邓晓峰's avatar
邓晓峰 committed
125
  return children;
邓晓峰's avatar
邓晓峰 committed
126
}
邓晓峰's avatar
邓晓峰 committed
127

邓晓峰's avatar
邓晓峰 committed
128
export const generFlatRoutes = (widgets, parent, subSystem, modulePkg) => {
邓晓峰's avatar
邓晓峰 committed
129 130
  const treeKeys = [];
  const flatMenu = [];
131 132 133
  if(!widgets || Array.isArray(widgets) && widgets.length === 0) {
    return [];
  }
邓晓峰's avatar
邓晓峰 committed
134
  (widgets || []).forEach(item => {
邓晓峰's avatar
邓晓峰 committed
135
    if (item.hasOwnProperty('routes')) {
邓晓峰's avatar
邓晓峰 committed
136
      const route = generFlatRoutes(item.routes, item, item.name, modulePkg);
邓晓峰's avatar
邓晓峰 committed
137
      flatMenu.push(simpleNormalizeChildren(route));
邓晓峰's avatar
邓晓峰 committed
138 139 140
    } else {
      flatMenu.push({
        ...item,
邓晓峰's avatar
邓晓峰 committed
141 142
        origin: item,
        moduleName: modulePkg[item.alias],
邓晓峰's avatar
邓晓峰 committed
143
        treeKeys,
邓晓峰's avatar
邓晓峰 committed
144 145
        subSystem,
        parent,
邓晓峰's avatar
邓晓峰 committed
146
      });
邓晓峰's avatar
邓晓峰 committed
147
    }
邓晓峰's avatar
邓晓峰 committed
148 149 150
  });
  return simpleNormalizeChildren(flatMenu);
};
dengxiaofeng's avatar
dengxiaofeng committed
151

152 153
export function RouteWithSubRoutes(route) {
  return (
邓晓峰's avatar
邓晓峰 committed
154 155
    <Route path={route.path} render={props => (<route.component {...props} route={route} routes={route.routes} params={route.params}/>)}></Route>
  )
156
}
157 158 159 160
function matchRoutes(routes, pathname, branch) {
    if (branch === void 0) {
      branch = [];
    }
邓晓峰's avatar
邓晓峰 committed
161

162 163 164
    routes.some(function (route) {
      var match = route.path ? matchPath(pathname, route) : branch.length ? branch[branch.length - 1].match // use parent match
      : Router.computeRootMatch(pathname); // use default "root" match
邓晓峰's avatar
邓晓峰 committed
165

166 167 168 169 170
      if (match) {
        branch.push({
          route: route,
          match: match
        });
邓晓峰's avatar
邓晓峰 committed
171

172 173
        if (route.routes) {
          matchRoutes(route.routes, pathname, branch);
邓晓峰's avatar
邓晓峰 committed
174
        }
175
      }
邓晓峰's avatar
邓晓峰 committed
176

177 178 179 180
      return match;
    });
    return branch;
  }
邓晓峰's avatar
邓晓峰 committed
181

182 183 184 185
function renderRoutes(routes, extraProps, switchProps) {
    if (extraProps === void 0) {
      extraProps = {};
    }
邓晓峰's avatar
邓晓峰 committed
186

187 188
    if (switchProps === void 0) {
      switchProps = {};
邓晓峰's avatar
邓晓峰 committed
189
    }
邓晓峰's avatar
邓晓峰 committed
190

191 192 193 194 195 196 197 198 199 200
    return routes ? React.createElement(Switch, switchProps, routes.map(function (route, i) {
      return React.createElement(Route, {
        key: route.key || i,
        path: route.path,
        exact: route.exact,
        strict: route.strict,
        render: function render(props) {
          return route.render ? route.render(Object.assign({}, props, {}, extraProps, {
            routes: route.routes,
            params: route.params,
邓晓峰's avatar
邓晓峰 committed
201 202
            route: route,
            rootPrefix: 'panda-console-base'
203 204 205 206
          })) : React.createElement(route.component, Object.assign({}, props, extraProps, {
            route: route,
            routes: route.routes,
            params: route.params,
邓晓峰's avatar
邓晓峰 committed
207
            rootPrefix: 'panda-console-base'
208 209 210 211 212 213 214 215 216 217 218
          }));
        }
      });
    })) : null;
}

export {
  renderRoutes,
  matchPath
}

邓晓峰's avatar
邓晓峰 committed
219
export default generRotes;