index.vue 5.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<template>
  <uni-page :data-page="$route.meta.pagePath">
Q
qiang 已提交
3 4
    <page-head
      v-if="navigationBar.type!=='none'"
fxy060608's avatar
fxy060608 已提交
5
      v-bind="navigationBar" />
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13
    <page-refresh
      v-if="enablePullDownRefresh"
      ref="refresh"
      :color="refreshOptions.color"
      :offset="refreshOptions.offset" />
    <page-body
      v-if="enablePullDownRefresh"
      @touchstart.native="_touchstart"
fxy060608's avatar
fxy060608 已提交
14
      @touchmove.native="_touchmove"
fxy060608's avatar
fxy060608 已提交
15 16
      @touchend.native="_touchend"
      @touchcancel.native="_touchend">
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22 23 24
      <slot name="page" />
    </page-body>
    <page-body v-else>
      <slot name="page" />
    </page-body>
  </uni-page>
</template>
<style>
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29
  uni-page {
    display: block;
    width: 100%;
    height: 100%;
  }
fxy060608's avatar
fxy060608 已提交
30 31 32 33
</style>
<script>
import {
  upx2px
fxy060608's avatar
fxy060608 已提交
34
} from 'uni-helpers/index'
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

import {
  NAVBAR_HEIGHT
} from 'uni-helpers/constants'

import {
  mergeTitleNView
} from 'uni-helpers/patch'

import PageHead from './pageHead'
import PageBody from './pageBody'
import PageRefresh from './pageRefresh'

import pullToRefresh from './pull-to-refresh'

Q
qiang 已提交
50 51
import safeAreaInsets from 'safe-area-insets'

fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
export default {
  name: 'Page',
  mpType: 'page',
  components: {
    PageHead,
    PageBody,
    PageRefresh
  },
  mixins: [pullToRefresh],
  props: {
    isQuit: {
      type: Boolean,
      default: false
    },
    isEntry: {
      type: Boolean,
      default: false
    },
    isTabBar: {
      type: Boolean,
      default: false
    },
    tabBarIndex: {
      type: Number,
      default: -1
    },
    navigationBarBackgroundColor: {
      type: String,
      default: '#000'
    },
    navigationBarTextStyle: {
      default: 'white',
      validator (value) {
        return ['white', 'black'].indexOf(value) !== -1
      }
    },
    navigationBarTitleText: {
      type: String,
      default: ''
    },
    navigationStyle: {
      default: 'default',
      validator (value) {
        return ['default', 'custom'].indexOf(value) !== -1
      }
    },
    backgroundColor: {
      type: String,
      default: '#ffffff'
    },
    backgroundTextStyle: {
      default: 'dark',
      validator (value) {
        return ['dark', 'light'].indexOf(value) !== -1
      }
    },
    backgroundColorTop: {
      type: String,
      default: '#fff'
    },
    backgroundColorBottom: {
      type: String,
      default: '#fff'
    },
    enablePullDownRefresh: {
      type: Boolean,
      default: false
    },
    onReachBottomDistance: {
      type: Number,
      default: 50
    },
    disableScroll: {
      type: Boolean,
      default: false
    },
    titleNView: {
Q
qiang 已提交
129 130
      type: [Boolean, Object, String],
      default: ''
fxy060608's avatar
fxy060608 已提交
131 132 133 134 135 136
    },
    pullToRefresh: {
      type: Object,
      default () {
        return {}
      }
Q
qiang 已提交
137 138 139 140 141 142 143 144 145 146 147 148
    },
    titleImage: {
      type: String,
      default: ''
    },
    transparentTitle: {
      type: String,
      default: ''
    },
    titlePenetrate: {
      type: String,
      default: 'NO'
fxy060608's avatar
fxy060608 已提交
149 150
    }
  },
Q
qiang 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  data () {
    const titleNViewTypeList = {
      'none': 'default',
      'auto': 'transparent',
      'always': 'float'
    }
    // 将 navigationStyle 和 transparentTitle 都合并到 titleNView
    let titleNView = this.titleNView
    titleNView = Object.assign({}, {
      type: this.navigationStyle === 'custom' ? 'none' : 'default'
    }, this.transparentTitle in titleNViewTypeList ? {
      type: titleNViewTypeList[this.transparentTitle],
      backgroundColor: 'rgba(0,0,0,0)'
    } : null, typeof titleNView === 'object' ? titleNView : (typeof titleNView === 'boolean' ? {
      type: titleNView ? 'default' : 'none'
    } : null))

    const yesNoParseList = {
      'YES': true,
      'NO': false
    }
172

fxy060608's avatar
fxy060608 已提交
173 174
    const navigationBar = mergeTitleNView({
      loading: false,
175
      backButton: !this.isQuit && !this.$route.meta.isQuit, // redirectTo,reLaunch时可能动态修改 meta.isQuit
fxy060608's avatar
fxy060608 已提交
176 177
      backgroundColor: this.navigationBarBackgroundColor,
      textColor: this.navigationBarTextStyle === 'black' ? '#000' : '#fff',
Q
qiang 已提交
178
      titleText: this.navigationBarTitleText,
W
wangyaqi 已提交
179
      titleImage: this.titleImage,
fxy060608's avatar
fxy060608 已提交
180
      duration: '0',
Q
qiang 已提交
181
      timingFunc: '',
雪洛's avatar
雪洛 已提交
182
      titlePenetrate: yesNoParseList[this.titlePenetrate]
Q
qiang 已提交
183
    }, titleNView)
fxy060608's avatar
fxy060608 已提交
184 185 186 187 188 189 190 191 192 193 194 195

    const refreshOptions = Object.assign({
      support: true,
      color: '#2BD009',
      style: 'circle',
      height: 70,
      range: 150,
      offset: 0
    }, this.pullToRefresh)

    let offset = upx2px(refreshOptions.offset)

Q
qiang 已提交
196
    if (titleNView.type !== 'none' && titleNView.type !== 'transparent') {
Q
qiang 已提交
197
      offset += NAVBAR_HEIGHT + safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
198 199 200 201 202 203 204 205 206 207
    }

    refreshOptions.offset = offset
    refreshOptions.height = upx2px(refreshOptions.height)
    refreshOptions.range = upx2px(refreshOptions.range)

    return {
      navigationBar,
      refreshOptions
    }
208 209 210
  },
  created () {
    if (__PLATFORM__ === 'h5') {
fxy060608's avatar
fxy060608 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
      const navigationBar = this.navigationBar
      document.title = navigationBar.titleText
      if (typeof qh !== 'undefined') {
        qh.setNavigationBarTitle({
          title: document.title
        })
        qh.setNavigationBarColor({
          backgroundColor: navigationBar.backgroundColor
        })
        qh.setNavigationBarTextStyle({
          textStyle: navigationBar.textColor === '#000' ? 'black' : 'white'
        })
      }
224
    }
fxy060608's avatar
fxy060608 已提交
225 226
  }
}
fxy060608's avatar
fxy060608 已提交
227
</script>