index.js 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import { createUniInstance } from './uni';

function getWebviewStyle () {
  return {
    titleNView: {
      autoBackButton: true,
      titleText: 'titleText'
    }
  }
}
class Router {
  constructor (routes, plus) {
    this.routes = routes;
    this.plus = plus;
    this.id = 0;

    this.aniShow = plus.os.name === 'Android' ? 'slide-in-right' : 'pop-in';
    this.aniClose = 'pop-out';
    this.aniDuration = 300;
  }

  push ({
    type,
    path
  } = {}) {
    this.plus.webview.open(
      '',
      String(this.id++),
      getWebviewStyle(),
      this.aniShow,
      this.aniDuration,
      () => {
        console.log('show.callback');
      });
  }

  replace ({
    type,
    path
  } = {}) {

  }

  go (delta) {

  }
fxy060608's avatar
fxy060608 已提交
47
}
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

let appCtx;

function getApp () {
  return appCtx
}

function registerApp (appVm, routes, plus) {
  appCtx = appVm;
  appCtx.$router = new Router(routes, plus);
}

const pageVms = [];

function getCurrentPages () {
  return pageVms
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}
/**
 * @param {Object} pageVm
 *
 * page.beforeCreate 时添加 page
 * page.beforeDestroy 时移出 page
 *
 * page.viewappear  onShow
 * page.viewdisappear onHide
 *
 * navigateTo
 * redirectTo
 *
 *
 *
 *
 *
 *
 */
function registerPage (pageVm) {
  pageVms.push(pageVm);
fxy060608's avatar
fxy060608 已提交
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
const uniConfig = Object.create(null);
const uniRoutes = [];

function parseRoutes (config) {
  uniRoutes.length = 0;
  /* eslint-disable no-mixed-operators */
  const tabBarList = (config.tabBar && config.tabBar.list || []).map(item => item.pagePath);

  Object.keys(config.page).forEach(function (pagePath) {
    uniRoutes.push({
      path: '/' + pagePath,
      meta: {
        isTabBar: tabBarList.indexOf(pagePath) !== -1
      }
    });
  });
}
fxy060608's avatar
fxy060608 已提交
104

105 106 107 108 109
function registerConfig (config) {
  Object.assign(uniConfig, config);
  parseRoutes(uniConfig);
}

fxy060608's avatar
fxy060608 已提交
110 111 112 113 114 115
function createInstanceContext ({
  weex,
  WeexPlus
}) {
  const plus = new WeexPlus(weex);
  return {
116 117 118 119 120 121 122 123 124 125
    __uniConfig: uniConfig,
    __uniRoutes: uniRoutes,
    __registerConfig (config) {
      registerConfig(config);
    },
    __registerApp (appVm) {
      registerApp(appVm, uniRoutes, plus);
    },
    __registerPage (pageVm) {
      registerPage(pageVm);
fxy060608's avatar
fxy060608 已提交
126
    },
127 128 129 130 131 132 133 134
    uni: createUniInstance(
      weex,
      plus,
      __uniConfig,
      __uniRoutes,
      getApp,
      getCurrentPages
    ),
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140
    getApp,
    getCurrentPages
  }
}

export { createInstanceContext };