uni-im-contextmenu.vue 4.6 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
  <view class="root" v-if="isShow" @contextmenu.prevent.native="isWidescreen?closeMe():''">
DCloud_JSON's avatar
DCloud_JSON 已提交
3 4 5 6 7 8 9 10 11 12 13
    <view class="uni-im-contextmenu-mark" @touchstart.prevent="closeMe" @click="closeMe"></view>
    <!-- 注意微信小程序下style不支持传obj -->
    <view class="contextmenu" ref="contextmenu" :style="{'left':style.left,'top':style.top,'opacity':style.opacity}">
      <text class="item" v-for="(item,index) in menuList" :key="index" @click="doAction(item.action)">{{item.title}}</text>
    </view>
  </view>
</template>

<script>
  // let funList = {}
  let onCloseFnList = [];
14
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
DCloud_JSON's avatar
DCloud_JSON 已提交
15
  export default {
16 17 18
    computed: {
      ...uniIm.mapState(['isWidescreen']),
    },
DCloud_JSON's avatar
DCloud_JSON 已提交
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 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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
    data() {
      return {
        "isShow":false,
        "style":{
          "top":"",
          "left":"",
          "opacity":1
        },
        "menuList":[
          {
            "title":"title",
            "action":"defaultAction"
          },
          {
            "title":"title2",
            "action":"defaultAction2"
          },
          {
            "title":"测试其中一个特别长",
            "action":"defaultAction2"
          }
        ]
      }
    },
    methods: {
      onClose(fn){
        onCloseFnList.push(fn)
      },
      doAction(actionName){
        actionName()
        this.closeMe()
      },
      show(position,menuList){
        let {
          top = '',
          left = '',
          estimateWidth = 150, // 预估尺寸应该不小于实际渲染尺寸
          estimateHeight = 150,
        } = position

        // 根据给定的位置和尺寸,结合窗口大小,计算出 top/left 定位参数,
        // 确保不超出窗口边界。
        let calcPosition = (top, left, width, height) => {
          let { windowWidth, windowHeight } = uni.getWindowInfo()
          // console.log({windowWidth,windowHeight,top, left, width, height})
          let style = {}
          if (top) {
            if (top > windowHeight - height) {
              style.top = `${windowHeight - height}px`
            } else {
              style.top = `${top}px`
            }
          }
          if (left) {
            if (left > windowWidth - width) {
              style.left = `${windowWidth - width}px`
            } else {
              style.left = `${left}px`
            }
          }
          return style
        }
        
        // 根据预估的组件大小初步设定组件的位置
        this.style = calcPosition(top, left, estimateWidth, estimateHeight)
        this.isShow = true
        this.menuList = menuList
        this.style.opacity = 0

        setTimeout(() => {
          // 根据实际渲染出的组件大小修正定位
          // #ifdef APP-NVUE
          const dom = weex.requireModule("dom");
          const result = dom.getComponentRect(this.$refs.contextmenu, option => {
            const {top, left, width, height} = option.size
            this.style = calcPosition(top, left, width, height)
          });
          // #endif
          
          // #ifndef APP-NVUE
          const query = uni.createSelectorQuery().in(this)
          query.select('.contextmenu').boundingClientRect(option => {
            const { width, height } = option
            this.style = calcPosition(top, left, width, height)
            this.style.opacity = 1
          }).exec()
          // #endif
        }, 0)
      },
      closeMe(){
        this.isShow = false
        onCloseFnList.forEach(fn => {
          if(typeof fn === 'function'){
            fn()
          }
        })
        onCloseFnList = []
        // console.log('closeMe');
      }
    }
  }
</script>

<style scoped lang="scss">
.root{
  
}
.contextmenu{
  position: fixed;
  background-color: #fff;
  z-index: 99999;
  border-radius: 10px;
  box-shadow: 0 0 10px #888;
  background-color:#FFF;
  // opacity: 0.98;
  border: 1px solid #999;
  padding: 3px;
  flex-direction: column;
}

.item{
  padding: 5px 10px;
  font-size: 18px;
  border-radius: 5px;
  margin: 5px;
  opacity: 0.9;
  text-align: left;

  /* #ifdef H5 */
  cursor: pointer;
  /* #endif */
  
  /* #ifdef APP-NVUE */
  width: 120px;
  height: 40px;
  /* #endif */
}

.item:hover{
  background-color: #1099ff;
  color: #fff;
}

.uni-im-contextmenu-mark{
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		flex: 1;
		height: 9000px;
		
		/* #ifndef APP-NVUE */
		width: 100vw;
		height: 100vh;
		z-index: 9999;
		/* #endif */
		
		/* #ifdef H5 */
		// 解决:通过*选择器设置了最大宽高 引起的大小撑不开的问题
		max-height:100vh;
		max-width:100vw;
		/* #endif */
		background-color: rgba(0,0,0,0.5);
	}
</style>