Action2Page.ets 1.99 KB
import { WDRouterPage } from './WDRouterPage'
import { Action } from 'wdBean'
import ArrayList from '@ohos.util.ArrayList';

interface HandleObject {
  handle: (action: Action) => (WDRouterPage | undefined)
  priority: number
}

export class Action2Page {

  private static handles: Record<string, ArrayList<HandleObject>> = {};

  static register(actionType: string, handle: (action: Action) => (WDRouterPage | undefined),  priority: number = 0) {
    let handles = Action2Page.handles[actionType] ?? new ArrayList();
    let obj: HandleObject = {
      handle: handle,
      priority: priority
    };
    handles.add(obj);
    handles.sort((f, s) => {
      return f.priority - s.priority;
    })
    Action2Page.handles[actionType] = handles;
  }

  static get(action?: Action): WDRouterPage | undefined {
    if (!action || !action.type) {
      return undefined;
    }
    let handles = Action2Page.handles[action.type];
    if (!handles) {
      return undefined;
    }
    let page: WDRouterPage | undefined
    for (let i = 0; i < handles.length; i++) {
      let tmp = (handles[i] as HandleObject).handle(action);
      if (tmp) {
        page = tmp;
        break
      }
    }
    return page
  }
}

export function registerRouter() {
  // Action2Page.register("USER_LOGIN", (action: Action) => {
  //   return WDRouterPage.webLoginPage
  // })

  // Action2Page.register("JUMP_DETAIL_PAGE", (action) => {
  //   if (action.params?.pageID == "296ff8a4b07d457cb15b6f9e5f433cc0") {
  //     return WDRouterPage.tvLivePage
  //   }
  //   if (action.params?.detailPageType == 7 || action.params?.detailPageType == 8) {
  //     return WDRouterPage.shortVideoDetail
  //   }
  //   return WDRouterPage.playDetail
  // })

  Action2Page.register("JUMP_H5_BY_WEB_VIEW", (action) => {
    return WDRouterPage.defaultWebPage
  })

  // Action2Page.register("JUMP_INNER_NEW_PAGE", (action) => {
  //   if (action.params?.pageID == "WORLDCUP_DETAIL") {
  //     return WDRouterPage.livependantpage
  //   }
  //   return undefined
  // })
}