pageHead.vue 9.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5
<template>
  <uni-page-head>
    <div
      :style="{transitionDuration:duration,transitionTimingFunction:timingFunc,backgroundColor:bgColor,color:textColor}"
      :class="{'uni-page-head-transparent':type==='transparent'}"
Q
qiang 已提交
6 7
      class="uni-page-head"
    >
8
      <div class="uni-page-head-hd">
Q
qiang 已提交
9 10 11 12
        <div
          v-show="backButton"
          @click="_back">
          <i
13
            :style="{color:color,fontSize:'27px'}"
Q
qiang 已提交
14 15
            class="uni-btn-icon">&#xe601;</i>
        </div>
16 17 18 19 20 21 22 23 24
        <template v-for="(btn,index) in btns">
          <div
            v-if="btn.float === 'left'"
            :key="index"
            :style="{marginRight:index>0?'5px':'0px'}">
            <i
              :style="_formatBtnStyle(btn)"
              class="uni-btn-icon"
              @click="_onBtnClick(index)"
Q
qiang 已提交
25 26
              v-html="_formatBtnFontText(btn)"
            />
27 28
          </div>
        </template>
fxy060608's avatar
fxy060608 已提交
29
      </div>
Q
qiang 已提交
30 31 32
      <div
        v-if="!searchInput"
        class="uni-page-head-bd">
fxy060608's avatar
fxy060608 已提交
33 34
        <div
          :style="{fontSize:titleSize,opacity:type==='transparent'?0:1}"
Q
qiang 已提交
35 36
          class="uni-page-head__title"
        >
fxy060608's avatar
fxy060608 已提交
37 38
          <i
            v-if="loading"
Q
qiang 已提交
39
            class="uni-loading"/>
fxy060608's avatar
fxy060608 已提交
40 41 42
          {{ titleText }}
        </div>
      </div>
Q
qiang 已提交
43 44 45 46 47 48 49
      <div
        v-if="searchInput"
        :style="{'background-color':searchInput.backgroundColor}"
        class="uni-page-head-search"
      >
        <div
          :style="{color:searchInput.placeholderColor}"
50
          :class="[`uni-page-head-search-placeholder-${focus || text ? 'left' : searchInput.align}`]"
Q
qiang 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
          class="uni-page-head-search-placeholder"
        >{{ text || composing ? '' : searchInput.placeholder }}</div>
        <v-uni-input
          ref="input"
          v-model="text"
          :focus="searchInput.autoFocus"
          :disabled="searchInput.disabled"
          :style="{'border-radius':searchInput.borderRadius,color:searchInput.color}"
          :placeholder-style="`color:${searchInput.placeholderColor}`"
          class="uni-page-head-search-input"
          confirm-type="search"
          @focus="__focus"
          @blur="__blur"
          @input="__input"
        />
      </div>
fxy060608's avatar
fxy060608 已提交
67 68 69
      <div class="uni-page-head-ft">
        <template v-for="(btn,index) in btns">
          <div
70
            v-if="btn.float !== 'left'"
Q
qiang 已提交
71
            :key="index">
fxy060608's avatar
fxy060608 已提交
72 73 74 75
            <i
              :style="_formatBtnStyle(btn)"
              class="uni-btn-icon"
              @click="_onBtnClick(index)"
Q
qiang 已提交
76 77
              v-html="_formatBtnFontText(btn)"
            />
fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83
          </div>
        </template>
      </div>
    </div>
    <div
      v-if="type!=='transparent'"
Q
qiang 已提交
84
      class="uni-placeholder"/>
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
  </uni-page-head>
</template>
<style>
uni-page-head {
  display: block;
  box-sizing: border-box;
}

uni-page-head .uni-page-head {
  position: fixed;
  left: 0;
  width: 100%;
  height: 44px;
Q
qiang 已提交
98
  padding: 7px 3px;
fxy060608's avatar
fxy060608 已提交
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  display: flex;
  overflow: hidden;
  justify-content: space-between;
  box-sizing: border-box;
  z-index: 998;
  color: #fff;
  background-color: #000;
  transition-property: all;
}

uni-page-head .uni-page-head.uni-page-head-transparent .uni-page-head-hd > div,
uni-page-head .uni-page-head.uni-page-head-transparent .uni-page-head-ft > div {
  display: flex;
  align-items: center;
  width: 32px;
  height: 32px;
  background: #999;
  border-radius: 50%;
}

uni-page-head .uni-page-head.uni-page-head-transparent .uni-page-head-ft > div {
  justify-content: center;
}

uni-page-head .uni-page-head ~ .uni-placeholder {
  width: 100%;
  height: 44px;
}

uni-page-head .uni-page-head * {
  box-sizing: border-box;
}

uni-page-head .uni-page-head-hd {
  display: flex;
  align-items: center;
  font-size: 16px;
}

uni-page-head .uni-page-head-bd {
  position: absolute;
  left: 70px;
  right: 70px;
  min-width: 0;
  user-select: auto;
}

Q
qiang 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
.uni-btn-icon {
  max-width: 48px;
  word-break: keep-all;
  margin: 0 2px;
  overflow: hidden;
  word-break: keep-all;
}

.uni-page-head-search {
  position: relative;
  display: flex;
  flex: 1;
  margin: 0 2px;
  line-height: 30px;
  font-size: 16px;
}

.uni-page-head-search-input {
  width: 100%;
  height: 100%;
  padding-left: 34px;
}

.uni-page-head-search-placeholder {
  position: absolute;
  max-width: 100%;
  height: 100%;
  padding-left: 34px;
  overflow: hidden;
  word-break: keep-all;
}

.uni-page-head-search-placeholder-right {
  right: 0;
}

.uni-page-head-search-placeholder-center {
  left: 50%;
  transform: translateX(-50%);
}

.uni-page-head-search-placeholder::before {
  position: absolute;
  top: 0;
  left: 2px;
  width: 30px;
  content: "\ea0e";
  display: block;
  font-size: 20px;
  font-family: "uni";
  text-align: center;
}

fxy060608's avatar
fxy060608 已提交
199 200 201 202 203 204 205 206 207 208
uni-page-head .uni-page-head-ft {
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
  font-size: 13px;
}

uni-page-head .uni-page-head__title {
  font-weight: bold;
  font-size: 16px;
Q
qiang 已提交
209
  line-height: 30px;
fxy060608's avatar
fxy060608 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

uni-page-head .uni-page-head__title .uni-loading {
  width: 16px;
  height: 16px;
  margin-top: -3px;
}
</style>
<script>
import appendCss from 'uni-platform/helpers/append-css'
import getRealPath from 'uni-platform/helpers/get-real-path'

import transparent from './transparent'

const FONTS = {
  forward: '&#xe600;',
  back: '&#xe601;',
  share: '&#xe602;',
  favorite: '&#xe604;',
  home: '&#xe605;',
  menu: '&#xe606;',
  close: '&#xe650;'
}
export default {
  name: 'PageHead',
  mixins: [transparent],
  props: {
    backButton: {
      type: Boolean,
      default: true
    },
    backgroundColor: {
      type: String,
      default: '#000'
    },
    textColor: {
      type: String,
      default: '#fff'
    },
    titleText: {
      type: String,
      default: ''
    },
    duration: {
      type: String,
      default: '0'
    },
    timingFunc: {
      type: String,
      default: ''
    },
    loading: {
      type: Boolean,
      default: false
    },
    titleSize: {
      type: String,
      default: '16px'
    },
    type: {
      default: 'default',
      validator (value) {
        return ['default', 'transparent'].indexOf(value) !== -1
      }
    },
    coverage: {
      type: String,
      default: '132px'
    },
    buttons: {
      type: Array,
      default () {
        return []
      }
Q
qiang 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300
    },
    searchInput: {
      type: [Object, Boolean],
      default () {
        return false
      }
    }
  },
  data () {
    return {
      focus: false,
      text: '',
      composing: false
fxy060608's avatar
fxy060608 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    }
  },
  computed: {
    btns () {
      const btns = []
      if (this.buttons.length) {
        this.buttons.forEach(button => {
          let btn = Object.assign({}, button)
          if (btn.fontSrc && !btn.fontFamily) {
            btn.fontSrc = getRealPath(btn.fontSrc)
            const fontFamily = btn.fontSrc.substr(btn.fontSrc.lastIndexOf('/') + 1).replace(/\./g, '-')
            btn.fontFamily = fontFamily
          }
          btn.color = this.type === 'transparent' ? '#fff' : (btn.color || this.textColor)
          btn.fontSize = btn.fontSize || (this.type === 'default' ? '27px' : '22px')
          btn.fontWeight = btn.fontWeight || 'normal'
          btns.push(btn)
        })
      }
      return btns
    }
  },
Q
qiang 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  mounted () {
    if (this.searchInput) {
      const input = this.$refs.input
      input.$watch('composing', val => {
        this.composing = val
      })
      if (this.searchInput.disabled) {
        input.$on('click', () => {
          UniServiceJSBridge.emit('onNavigationBarSearchInputClicked', '')
        })
      } else {
        input.$refs.input.addEventListener('keyup', event => {
          if (event.key.toUpperCase() === 'ENTER') {
            UniServiceJSBridge.emit('onNavigationBarSearchInputConfirmed', {
              text: this.text
            })
          }
        })
      }
    }
  },
fxy060608's avatar
fxy060608 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  methods: {
    _back () {
      if (getCurrentPages().length === 1) {
        uni.reLaunch({
          url: '/'
        })
      } else {
        uni.navigateBack({
          from: 'backButton'
        })
      }
    },
    _onBtnClick (index) {
      UniServiceJSBridge.emit('onNavigationBarButtonTap', Object.assign({}, this.btns[index], {
        index
      }))
    },
    _formatBtnFontText (btn) {
      if (btn.fontSrc && btn.fontFamily) {
        const cssText =
          `@font-face{
              font-family: "${btn.fontFamily}";
              src: url("${btn.fontSrc}") format("truetype")
            }`
        appendCss(cssText, 'uni-btn-font-' + btn.fontFamily)
        return btn.text.replace('\\u', '&#x')
      } else if (FONTS[btn.type]) {
        return FONTS[btn.type]
      }
      return btn.text || ''
    },
    _formatBtnStyle (btn) {
      const style = {
        color: btn.color,
        fontSize: btn.fontSize,
        fontWeight: btn.fontWeight
      }
      if (btn.fontFamily) {
        style.fontFamily = btn.fontFamily
      }
      return style
Q
qiang 已提交
385 386 387 388 389 390 391 392 393 394 395
    },
    __focus () {
      this.focus = true
    },
    __blur () {
      this.focus = false
    },
    __input () {
      UniServiceJSBridge.emit('onNavigationBarSearchInputChanged', {
        text: this.text
      })
fxy060608's avatar
fxy060608 已提交
396 397 398 399
    }
  }
}
</script>