| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- // @ts-ignore
- import type { Callback } from '../index.uts'
- import {
- getElementById,
- getElementByNodeIdOrElementId,
- getComponentVmBySelector,
- getValidNodes,
- getComponentVmByNodeId,
- componentGetData,
- componentSetData,
- getElementByIdOrNodeId,
- // @ts-ignore
- } from './util.uts'
- // @ts-ignore
- import { getChildrenText, toCamelCase } from './util.uts'
- export type GetElementParams = {
- pageId: string
- nodeId?: number | null
- elementId?: string | null
- selector: string
- }
- export const getElement = (
- params: GetElementParams,
- callback: Callback
- ): void => {
- // TODO: support get component by class or id selector
- const element = getElementByNodeIdOrElementId(
- params.pageId,
- params.nodeId,
- params.elementId,
- callback
- )
- if (element != null) {
- let selector = params.selector
- if (selector.startsWith('uni-')) {
- selector = selector.replace('uni-', '')
- const component = getComponentVmBySelector(
- params.pageId,
- selector,
- callback
- )
- const result = {
- nodeId: component != null ? component.$.uid : null,
- tagName: component != null ? selector : null,
- elementId: component != null ? `${Date.now()}` : null,
- }
- callback(result, null)
- return
- }
- // @ts-ignore
- const list: UTSJSONObject[] = []
- getValidNodes(element, selector, list)
- if (list.length > 0) {
- callback(list[0], null)
- } else {
- callback(null, { errMsg: `Element[${params.selector}] not exists` })
- }
- }
- }
- export const getElements = (
- params: GetElementParams,
- callback: Callback
- ): void => {
- const element = getElementByNodeIdOrElementId(
- params.pageId,
- params.nodeId,
- params.elementId,
- callback
- )
- if (element != null) {
- let selector = params.selector
- if (selector.startsWith('uni-')) {
- selector = selector.replace('uni-', '')
- }
- // @ts-ignore
- const list: UTSJSONObject[] = []
- getValidNodes(element, selector, list, true)
- callback({ elements: list }, null)
- }
- }
- export type GetDOMPropertiesParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- names: string[]
- }
- export const getDOMProperties = (
- params: GetDOMPropertiesParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const properties = params.names.map((name: string): any | null => {
- if (name == 'innerText') {
- // @ts-expect-error
- if (isTextElement(dom)) {
- return dom.getAttribute('value')
- } else {
- return getChildrenText(dom)
- }
- }
- if (name == 'value') {
- return dom.getAttribute('value')
- }
- if (name == 'offsetWidth') {
- return dom.offsetWidth
- }
- if (name == 'offsetHeight') {
- return dom.offsetHeight
- }
- return `Element.getDOMProperties not support ${name}`
- })
- callback({ properties }, null)
- }
- }
- export type GetPropertiesParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- names: string[]
- }
- export const getProperties = (
- params: GetPropertiesParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- // @ts-ignore
- let component: ComponentPublicInstance | null = null
- if (params.nodeId != null) {
- component = getComponentVmByNodeId(params.pageId, params.nodeId!, callback)
- }
- const properties = params.names.map((name: string): any | null => {
- if (component != null && component.$props[toCamelCase(name)] != null) {
- return component.$props[toCamelCase(name)]
- }
- if (dom != null) {
- return dom.getAttribute(name)
- }
- return null
- })
- callback({ properties }, null)
- }
- export type GetAttributesParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- names: string[]
- }
- export const getAttributes = (
- params: GetAttributesParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const attributes = params.names.map((name: string): any | null => {
- if (name == 'class') {
- return dom.classList.join(' ')
- }
- return dom.getAttribute(name)
- })
- callback({ attributes }, null)
- }
- }
- export type CallFunctionParams = {
- pageId: string
- elementId: string
- functionName: string
- args: any[]
- }
- type Coordinate = {
- x: number
- y: number
- }
- export const callFunction = (
- params: CallFunctionParams,
- callback: Callback
- ): void => {
- const element = getElementById(params.pageId, params.elementId, callback)
- if (element != null) {
- const functionName = params.functionName
- switch (functionName) {
- case 'input.input':
- element.dispatchEvent(
- 'input',
- // @ts-ignore
- new InputEvent(
- 'input',
- // @ts-ignore
- InputEventDetail(params.args[0] as string, 0, 0)
- )
- )
- callback({ result: `${functionName} success` }, null)
- break
- case 'scroll-view.scrollTo':
- if (element.tagName == 'SCROLL-VIEW') {
- // @ts-ignore
- const arg = JSON.parse<Coordinate>(JSON.stringify(params.args[0]))!
- element.scrollLeft = arg.x
- element.scrollTop = arg.y
- callback({ result: `${functionName} success` }, null)
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not scroll-view`,
- })
- }
- break
- case 'swiper.swipeTo':
- if (element.tagName == 'SWIPER') {
- callback(null, { errMsg: `${functionName} not support` })
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not swiper`,
- })
- return
- }
- break
- case 'scroll-view.scrollWidth':
- if (element.tagName == 'SCROLL-VIEW') {
- callback({ result: element.scrollWidth }, null)
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not scroll-view`,
- })
- return
- }
- break
- case 'scroll-view.scrollHeight':
- if (element.tagName == 'SCROLL-VIEW') {
- callback({ result: element.scrollHeight }, null)
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not scroll-view`,
- })
- return
- }
- break
- case 'scroll-view.scrollTop':
- if (element.tagName == 'SCROLL-VIEW') {
- callback({ result: element.scrollTop }, null)
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not scroll-view`,
- })
- return
- }
- break
- case 'scroll-view.scrollLeft':
- if (element.tagName == 'SCROLL-VIEW') {
- callback({ result: element.scrollLeft }, null)
- } else {
- callback(null, {
- errMsg: `${functionName} fail, element is not scroll-view`,
- })
- return
- }
- break
- default:
- callback(null, { errMsg: `${functionName} not support` })
- break
- }
- } else {
- callback(null, { errMsg: `Element not exists` })
- }
- }
- export type TapParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- }
- export const tap = (params: TapParams, callback: Callback): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const num = 0
- // @ts-ignore
- const _float = num.toFloat()
- dom.dispatchEvent(
- 'click',
- new MouseEvent(
- 'click',
- _float,
- // @ts-ignore
- _float,
- _float,
- _float,
- _float,
- _float,
- _float,
- _float
- )
- )
- callback({ result: `Element.tap success` }, null)
- }
- }
- export type CallMethodParams = {
- pageId: string
- nodeId: number
- method: string
- args: any[]
- }
- export const callMethod = (
- params: CallMethodParams,
- callback: Callback
- ): void => {
- const component = getComponentVmByNodeId(
- params.pageId,
- params.nodeId,
- callback
- )
- if (component != null) {
- const result =
- params.args.length > 0
- ? component.$callMethod(params.method, params.args[0])
- : component.$callMethod(params.method)
- // @ts-ignore
- if (result instanceof Promise<unknown>) {
- (result as Promise<any>).then((res: any) => {
- callback({ result: res }, null)
- }).catch((err) => {
- const errMsg = err instanceof Error ? err.message : err
- callback({ result: errMsg }, null)
- })
- } else {
- callback({ result }, null)
- }
- }
- }
- export type GetDataParams = {
- pageId: string
- nodeId: number
- path?: string | null
- }
- export const getData = (params: GetDataParams, callback: Callback): void => {
- const component = getComponentVmByNodeId(
- params.pageId,
- params.nodeId,
- callback
- )
- if (component != null) {
- const data = componentGetData(
- component,
- )
- callback({ data }, null)
- }
- }
- export type SetDataParams = {
- pageId: string
- nodeId: number
- data: Map<string, any | null>
- }
- export const setData = (params: SetDataParams, callback: Callback): void => {
- const component = getComponentVmByNodeId(
- params.pageId,
- params.nodeId,
- callback
- )
- if (component != null) {
- componentSetData(component, params.data)
- callback({ result: { errMsg: 'Page.setData: ok.' } }, null)
- }
- }
- export type GetOffsetParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- }
- export const getOffset = (
- params: GetOffsetParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- callback({ left: dom.offsetLeft, top: dom.offsetTop }, null)
- }
- }
- export type LongpressParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- }
- export const longpress = (
- params: LongpressParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const x: number = 0
- const y: number = 0
- dom.dispatchEvent(
- 'longpress',
- // @ts-ignore
- new TouchEvent(null, 'longpress', getTouches([]), getTouches([]))
- )
- callback({ result: `Element.longpress success` }, null)
- }
- }
- export type HandleTouchEventParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- touches: any[]
- changedTouches: any[]
- }
- export const handleTouchEvent = (
- params: HandleTouchEventParams,
- eventName: string,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const touches = getTouches(params.touches)
- const changedTouches = getTouches(params.changedTouches)
- dom.dispatchEvent(
- eventName,
- // @ts-ignore
- new TouchEvent(null, eventName, touches, changedTouches)
- )
- callback({ result: `Element.${eventName} success` }, null)
- }
- }
- type TypeTouch = {
- identifier: number
- pageX: number
- pageY: number,
- screenX?: number | null,
- screenY?: number | null,
- clientX?: number | null,
- clientY?: number | null,
- }
- function getTouches(touches: any[]): Touch[] {
- return touches.map((touch): Touch => {
- // @ts-ignore
- const touchObj = JSON.parse<TypeTouch>(JSON.stringify(touch))!
- // @ts-ignore
- const result = Touch()
- result.identifier = touchObj.identifier.toFloat()
- result.pageX = touchObj.pageX.toFloat()
- result.pageY = touchObj.pageY.toFloat()
- if (touchObj.screenX !== null) {
- result.screenX = touchObj.screenX!.toFloat()
- }
- if (touchObj.screenY !== null) {
- result.screenY = touchObj.screenY!.toFloat()
- }
- if (touchObj.clientX !== null) {
- result.clientX = touchObj.clientX!.toFloat()
- }
- if (touchObj.clientY !== null) {
- result.clientY = touchObj.clientY!.toFloat()
- }
- return result
- })
- }
- export type GetStylesParams = {
- pageId: string
- elementId?: string | null
- nodeId?: number | null
- names: string[]
- }
- export const getStyles = (
- params: GetStylesParams,
- callback: Callback
- ): void => {
- const dom = getElementByIdOrNodeId(
- params.pageId,
- params.elementId,
- params.nodeId,
- callback
- )
- if (dom != null) {
- const styles = params.names.map((name: string): any | null => {
- return dom.style.getPropertyValue(name)
- })
- callback({ styles }, null)
- }
- }
|