123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import { defineMixin } from '../vue'
- import { deepMerge, $parent, sleep } from '../function/index'
- import test from '../function/test'
- import route from '../util/route'
- const dom = uni.requireNativePlugin('dom')
- export const mixin = defineMixin({
-
- props: {
-
- customStyle: {
- type: [Object, String],
- default: () => ({})
- },
- customClass: {
- type: String,
- default: ''
- },
-
- url: {
- type: String,
- default: ''
- },
-
- linkType: {
- type: String,
- default: 'navigateTo'
- }
- },
- data() {
- return {}
- },
- onLoad() {
-
- this.$u.getRect = this.$uGetRect
- },
- created() {
-
- this.$u.getRect = this.$uGetRect
- },
- computed: {
-
-
-
- $u() {
-
-
- return deepMerge(uni.$u, {
- props: undefined,
- http: undefined,
- mixin: undefined
- })
-
-
- return uni.$u
-
- },
-
- bem() {
- return function (name, fixed, change) {
-
- const prefix = `u-${name}--`
- const classes = {}
- if (fixed) {
- fixed.map((item) => {
-
- classes[prefix + this[item]] = true
- })
- }
- if (change) {
- change.map((item) => {
-
- this[item] ? (classes[prefix + item] = this[item]) : (delete classes[prefix + item])
- })
- }
- return Object.keys(classes)
-
-
- .join(' ')
-
- }
- }
- },
- methods: {
-
- openPage(urlKey = 'url') {
- const url = this[urlKey]
- if (url) {
-
-
- route({ type: this.linkType, url })
-
-
-
-
- }
- },
- navTo(url = '', linkType = 'navigateTo') {
- route({ type: this.linkType, url })
- },
-
-
-
- $uGetRect(selector, all) {
- return new Promise((resolve) => {
-
- uni.createSelectorQuery()
- .in(this)[all ? 'selectAll' : 'select'](selector)
- .boundingClientRect((rect) => {
- if (all && Array.isArray(rect) && rect.length) {
- resolve(rect)
- }
- if (!all && rect) {
- resolve(rect)
- }
- })
- .exec()
-
-
-
- sleep(30).then(() => {
- let selectorNvue = selector.substring(1)
- let selectorRef = this.$refs[selectorNvue]
- if (!selectorRef) {
-
- resolve({
- with: 0,
- height: 0,
- left: 0,
- right: 0,
- top: 0,
- bottom: 0
- })
- }
- dom.getComponentRect(selectorRef, res => {
-
- resolve(res.size)
- })
- })
-
- })
- },
- getParentData(parentName = '') {
-
- if (!this.parent) this.parent = {}
-
-
-
-
- this.parent = $parent.call(this, parentName)
- if (this.parent.children) {
-
- this.parent.children.indexOf(this) === -1 && this.parent.children.push(this)
- }
- if (this.parent && this.parentData) {
-
- Object.keys(this.parentData).map((key) => {
- this.parentData[key] = this.parent[key]
- })
- }
- },
-
- preventEvent(e) {
- e && typeof (e.stopPropagation) === 'function' && e.stopPropagation()
- },
-
- noop(e) {
- this.preventEvent(e)
- }
- },
- onReachBottom() {
- uni.$emit('uOnReachBottom')
- },
- beforeUnmount() {
-
-
- if (this.parent && test.array(this.parent.children)) {
-
- const childrenList = this.parent.children
- childrenList.map((child, index) => {
-
- if (child === this) {
- childrenList.splice(index, 1)
- }
- })
- }
- }
- })
- export default mixin
|