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

uni-page-head .uni-page-head {
  position: fixed;
119
  left: 0;
雪洛's avatar
雪洛 已提交
120
  top: 0;
fxy060608's avatar
fxy060608 已提交
121 122
  width: 100%;
  height: 44px;
Q
qiang 已提交
123 124
  height: calc(44px + constant(safe-area-inset-top));
  height: calc(44px + env(safe-area-inset-top));
Q
qiang 已提交
125
  padding: 7px 3px;
Q
qiang 已提交
126 127
  padding-top: calc(7px + constant(safe-area-inset-top));
  padding-top: calc(7px + env(safe-area-inset-top));
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132 133 134 135
  display: flex;
  overflow: hidden;
  justify-content: space-between;
  box-sizing: border-box;
  z-index: 998;
  color: #fff;
  background-color: #000;
  transition-property: all;
Q
qiang 已提交
136 137 138 139 140 141 142 143 144 145
}

uni-page-head .uni-page-head-titlePenetrate,
uni-page-head .uni-page-head-titlePenetrate .uni-page-head-bd,
uni-page-head .uni-page-head-titlePenetrate .uni-page-head-bd * {
  pointer-events: none;
}

uni-page-head .uni-page-head-titlePenetrate * {
  pointer-events: auto;
fxy060608's avatar
fxy060608 已提交
146 147 148 149 150 151 152 153 154
}

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;
Q
qiang 已提交
155 156 157 158 159 160
  height: calc(44px + constant(safe-area-inset-top));
  height: calc(44px + env(safe-area-inset-top));
}

uni-page-head .uni-placeholder-titlePenetrate {
  pointer-events: none;
fxy060608's avatar
fxy060608 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

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;
}

181
.uni-page-head-btn {
182
  position: relative;
183
  width: auto;
Q
qiang 已提交
184 185
  margin: 0 2px;
  word-break: keep-all;
186
  white-space: pre;
187
  cursor: pointer;
188 189
}

190 191 192 193 194 195 196
.uni-page-head-transparent .uni-page-head-btn {
  display: flex;
  align-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
197 198 199 200
}

uni-page-head .uni-btn-icon {
  overflow: hidden;
201
  min-width: 1em;
202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

.uni-page-head-btn-red-dot::after {
  content: attr(badge-text);
  position: absolute;
  right: 0;
  top: 0;
  background-color: red;
  color: white;
  width: 18px;
  height: 18px;
  line-height: 18px;
  border-radius: 18px;
  overflow: hidden;
216
  transform: scale(0.5) translate(40%, -40%);
217 218 219 220 221 222 223 224 225 226
  transform-origin: 100% 0;
}

.uni-page-head-btn-red-dot[badge-text]::after {
  font-size: 12px;
  width: auto;
  min-width: 18px;
  max-width: 42px;
  text-align: center;
  padding: 0 3px;
227
  transform: scale(0.7) translate(40%, -40%);
Q
qiang 已提交
228 229
}

230
.uni-page-head-btn-select > .uni-btn-icon::after {
231 232 233 234 235 236 237
  display: inline-block;
  font-family: "unibtn";
  content: "\e601";
  margin-left: 2px;
  transform: rotate(-90deg) scale(0.8);
}

Q
qiang 已提交
238 239 240 241 242 243
.uni-page-head-search {
  position: relative;
  display: flex;
  flex: 1;
  margin: 0 2px;
  line-height: 30px;
244
  font-size: 15px;
Q
qiang 已提交
245 246 247 248 249 250
}

.uni-page-head-search-input {
  width: 100%;
  height: 100%;
  padding-left: 34px;
251
  text-align: left;
Q
qiang 已提交
252 253 254 255 256 257 258 259 260
}

.uni-page-head-search-placeholder {
  position: absolute;
  max-width: 100%;
  height: 100%;
  padding-left: 34px;
  overflow: hidden;
  word-break: keep-all;
261
  white-space: pre;
Q
qiang 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

.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 已提交
285 286 287 288 289 290 291 292 293 294
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 已提交
295
  line-height: 30px;
fxy060608's avatar
fxy060608 已提交
296 297 298 299 300 301 302 303 304 305
  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;
Q
qiang 已提交
306 307 308 309 310 311
}

uni-page-head .uni-page-head__title .uni-page-head__title_image {
  width: auto;
  height: 26px;
  vertical-align: middle;
fxy060608's avatar
fxy060608 已提交
312
}
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

uni-page-head .uni-page-head-shadow {
  overflow: visible;
}

uni-page-head .uni-page-head-shadow::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 5px;
  background-size: 100% 100%;
}

uni-page-head .uni-page-head-shadow-grey::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-grey.png");
}

uni-page-head .uni-page-head-shadow-blue::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-blue.png");
}

uni-page-head .uni-page-head-shadow-green::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-green.png");
}

uni-page-head .uni-page-head-shadow-orange::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-orange.png");
}

uni-page-head .uni-page-head-shadow-red::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-red.png");
}

uni-page-head .uni-page-head-shadow-yellow::after {
  background-image: url("https://cdn.dcloud.net.cn/img/shadow-yellow.png");
}
fxy060608's avatar
fxy060608 已提交
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
</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,
Q
qiang 已提交
377 378 379
      default () {
        return this.type === 'transparent' ? '#000' : '#F8F8F8'
      }
fxy060608's avatar
fxy060608 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    },
    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) {
408
        return ['default', 'transparent', 'float'].indexOf(value) !== -1
fxy060608's avatar
fxy060608 已提交
409 410 411 412 413 414 415 416 417 418 419
      }
    },
    coverage: {
      type: String,
      default: '132px'
    },
    buttons: {
      type: Array,
      default () {
        return []
      }
Q
qiang 已提交
420 421 422 423 424 425
    },
    searchInput: {
      type: [Object, Boolean],
      default () {
        return false
      }
Q
qiang 已提交
426 427 428 429 430 431 432 433
    },
    titleImage: {
      type: String,
      default: ''
    },
    titlePenetrate: {
      type: Boolean,
      default: false
434 435 436 437 438 439
    },
    shadow: {
      type: Object,
      default () {
        return {}
      }
Q
qiang 已提交
440 441 442 443 444 445 446
    }
  },
  data () {
    return {
      focus: false,
      text: '',
      composing: false
fxy060608's avatar
fxy060608 已提交
447 448 449 450 451
    }
  },
  computed: {
    btns () {
      const btns = []
452
      const fonts = {}
fxy060608's avatar
fxy060608 已提交
453 454
      if (this.buttons.length) {
        this.buttons.forEach(button => {
fxy060608's avatar
fxy060608 已提交
455
          const btn = Object.assign({}, button)
fxy060608's avatar
fxy060608 已提交
456
          if (btn.fontSrc && !btn.fontFamily) {
fxy060608's avatar
fxy060608 已提交
457
            const fontSrc = btn.fontSrc = getRealPath(btn.fontSrc)
458 459 460 461 462 463 464 465 466
            let fontFamily
            if (fontSrc in fonts) {
              fontFamily = fonts[fontSrc]
            } else {
              fontFamily = `font${Date.now()}`
              fonts[fontSrc] = fontFamily
              const cssText = `@font-face{font-family: "${fontFamily}";src: url("${fontSrc}") format("truetype")}`
              appendCss(cssText, 'uni-btn-font-' + fontFamily)
            }
fxy060608's avatar
fxy060608 已提交
467 468 469
            btn.fontFamily = fontFamily
          }
          btn.color = this.type === 'transparent' ? '#fff' : (btn.color || this.textColor)
470
          let fontSize = btn.fontSize || (this.type === 'transparent' || /\\u/.test(btn.text) ? '22px' : '27px')
471 472 473 474
          if (/\d$/.test(fontSize)) {
            fontSize += 'px'
          }
          btn.fontSize = fontSize
fxy060608's avatar
fxy060608 已提交
475 476 477 478 479
          btn.fontWeight = btn.fontWeight || 'normal'
          btns.push(btn)
        })
      }
      return btns
480 481 482 483 484 485 486 487 488 489 490 491
    },
    headClass () {
      const shadowColorType = this.shadow.colorType
      const data = {
        'uni-page-head-transparent': this.type === 'transparent',
        'uni-page-head-titlePenetrate': this.titlePenetrate,
        'uni-page-head-shadow': shadowColorType
      }
      if (shadowColorType) {
        data[`uni-page-head-shadow-${shadowColorType}`] = shadowColorType
      }
      return data
fxy060608's avatar
fxy060608 已提交
492 493
    }
  },
494
  mounted () {
Q
qiang 已提交
495 496 497 498 499 500
    if (this.searchInput) {
      const input = this.$refs.input
      input.$watch('composing', val => {
        this.composing = val
      })
      if (this.searchInput.disabled) {
501
        input.$el.addEventListener('click', () => {
Q
qiang 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514
          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 已提交
515 516 517 518 519 520 521 522
  methods: {
    _back () {
      if (getCurrentPages().length === 1) {
        uni.reLaunch({
          url: '/'
        })
      } else {
        uni.navigateBack({
雪洛's avatar
雪洛 已提交
523
          from: 'backbutton'
fxy060608's avatar
fxy060608 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
        })
      }
    },
    _onBtnClick (index) {
      UniServiceJSBridge.emit('onNavigationBarButtonTap', Object.assign({}, this.btns[index], {
        index
      }))
    },
    _formatBtnFontText (btn) {
      if (btn.fontSrc && 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 已提交
550
    },
551
    _focus () {
Q
qiang 已提交
552 553
      this.focus = true
    },
554
    _blur () {
Q
qiang 已提交
555 556
      this.focus = false
    },
557
    _input (text) {
Q
qiang 已提交
558
      UniServiceJSBridge.emit('onNavigationBarSearchInputChanged', {
559
        text
Q
qiang 已提交
560
      })
fxy060608's avatar
fxy060608 已提交
561 562 563 564
    }
  }
}
</script>