All files / runtime/src/components ActionBar.ts

100% Statements 74/74
100% Branches 6/6
100% Functions 2/2
100% Lines 74/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x  
import { FunctionalComponent, h, warn } from '@vue/runtime-core'
import { NSVElement } from '../nodes'
import {
  ActionBar as TNSActionBar,
  ActionItem as TNSActionItem,
  NavigationButton as TNSNavigationButton,
  Page as TNSPage
} from '@nativescript/core'
import { NSVViewMeta } from '../registry'
 
export const actionBarNodeOps: NSVViewMeta['nodeOps'] = {
  insert(child, parent, atIndex) {
    const actionBar = parent.nativeView as TNSActionBar
    const childView = child.nativeView
 
    if (childView instanceof TNSNavigationButton) {
      actionBar.navigationButton = childView
    } else if (childView instanceof TNSActionItem) {
      if (atIndex) {
        const ai: TNSActionItem[] = actionBar.actionItems.getItems()
        ai.splice(atIndex, 0, childView)
        ;(<any>actionBar.actionItems).setItems(ai)
      } else {
        actionBar.actionItems.addItem(childView)
      }
    } else {
      actionBar.titleView = childView
    }
  },
  remove(child, parent) {
    const actionBar = parent.nativeView as TNSActionBar
    const childView = child.nativeView
 
    if (childView instanceof TNSNavigationButton) {
      if (actionBar.navigationButton === childView) {
        // @ts-ignore
        actionBar.navigationButton = null
      }
    } else if (childView instanceof TNSActionItem) {
      actionBar.actionItems.removeItem(childView)
    } else {
      if (actionBar.titleView === childView) {
        // @ts-ignore
        actionBar.titleView = null
      }
    }
  }
}
 
export const ActionBar: FunctionalComponent = (props, ctx) => {
  return h(
    'InternalActionBar',
    {
      ...ctx.attrs,
      onVnodeMounted(vnode) {
        const el = vnode.el as NSVElement // TODO: vue-next should export ElementRender
        const actionBar = el.nativeView as TNSActionBar
        const parent = el.parentNode as NSVElement
 
        if (parent.nativeView instanceof TNSPage) {
          parent.nativeView.actionBar = actionBar
        } else {
          if (__DEV__) {
            warn(
              `<ActionBar> must be a direct child of a <Page> element - ` +
                `got <${parent.nativeView.constructor.name}> instead.`
            )
          }
        }
      }
    },
    ctx.slots.default ? ctx.slots.default() : undefined
  )
}