app-popover.vue 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
<template>
  <van-popup
    class="app-popup outside-bottom-center"
    close-icon="close"
    v-model="isShow"
    v-if="visible"
    closeable
    :position="position"
    round
    :lock-scroll="false"
    :close-on-click-overlay="false"
    :close-on-popstate="true"
    @close="onVisibleChange"
    :style="{ height: view.height ? view.height + 'px' : '', width: view.width ? view.width + 'px' : '' }"
  >
    <div class="popup-content">
      <component
        :is="viewName"
        class="view-container2 popup-body"
        :dynamicProps="{ _context: JSON.stringify(context), _viewparams: JSON.stringify(viewparams)}"
        :staticProps="{ viewDefaultUsage: 'INCLUDEDVIEW', viewModelData: view.viewModelData }"
        @viewdataschange="dataChange($event)"
        @close="close($event)"
        :ref="viewName"
      >
      </component>
    </div>
  </van-popup>
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator';
import { Subject } from 'rxjs';
@Component({
  components: {},
})
export default class AppPopoverComponent extends Vue {
  /**
   * 视图参数
   *
   * @type {*}
   * @memberof AppPopoverComponent
   */
  @Prop() public view!: any;

  /**
   * 视图动态参数
   *
   * @type {any}
   * @memberof AppPopoverComponent
   */
  @Prop({ default: {} }) public context?: any;

  /**
   * 视图静态参数
   *
   * @type {any}
   * @memberof AppPopoverComponent
   */
  @Prop({ default: {} }) public viewparams?: any;

  /**
   * 是否显示头部
   *
   * @type {Subject<any>}
   * @memberof AppPopoverComponent
   */
  @Prop({ default: true }) visibleHeader: boolean;

  /**
   * popup位置
   *
   * @memberof AppPopoverComponent
   */
  position: string = 'center';

  /**
   * 关闭图标位置
   *
   * @type {boolean}
   * @memberof AppPopoverComponent
   */
  closeIconPosition: 'top-right' | 'outside-bottom-center' = 'outside-bottom-center';

  /**
   * 是否显示
   *
   * @type {boolean}
   * @memberof AppPopoverComponent
   */
  isShow: boolean = false;

  /**
   * 元素销毁变量
   *
   * @type {boolean}
   * @memberof AppPopoverComponent
   */
  visible: boolean = true;

  /**
   * 数据传递对象
   *
   * @type {(null | Subject<any>)}
   * @memberof AppPopoverComponent
   */
  public subject: null | Subject<any> = new Subject<any>();

  /**
   * 临时结果
   *
   * @type {*}
   * @memberof AppPopoverComponent
   */
  public tempResult: any = { ret: '' };

  /**
   * 视图名称
   *
   * @type {string}
   * @memberof AppPopoverComponent
   */
  public viewName: string = '';

  /**
   * 获取数据传递对象
   *
   * @returns {(null | Subject<any>)}
   * @memberof AppPopoverComponent
   */
  public getSubject(): null | Subject<any> {
    return this.subject;
  }

  /**
   * Vue生命周期created
   *
   * @memberof AppPopoverComponent
   */
  public created() {
    this.initDefaultParams();
  }

  /**
   * 初始化默认参数
   *
   * @memberof AppPopoverComponent
   */
  initDefaultParams() {
    this.viewName = this.view.viewname;
  }

  /**
   * Vue生命周期mounted
   *
   * @memberof AppPopoverComponent
   */
  mounted() {
    this.isShow = true;
  }

  /**
   * 视图关闭
   *
   * @memberof AppPopoverComponent
   */
  public close(result: any) {
    if (result && Array.isArray(result) && result.length > 0) {
      Object.assign(this.tempResult, { ret: 'OK' }, { datas: JSON.parse(JSON.stringify(result)) });
    }
    this.onVisibleChange();
  }

  /**
   * 视图数据变化
   *
   * @memberof AppPopoverComponent
   */
  public dataChange(result: any) {
    this.tempResult = { ret: '' };
    if (result && Array.isArray(result) && result.length > 0) {
      Object.assign(this.tempResult, { ret: 'OK' }, { datas: JSON.parse(JSON.stringify(result)) });
    }
  }

  /**
   * 模态显示隐藏切换回调
   *
   * @memberof AppPopoverComponent
   */
  public async onVisibleChange(): Promise<any> {
    const component: any = this.$refs[this.viewName];
    if (component) {
      this.handleShowState();
    }
  }

  /**
   * 处理数据,向外抛值
   *
   * @memberof AppPopoverComponent
   */
  public handleShowState() {
    if (this.subject) {
      if (this.tempResult && Object.is(this.tempResult.ret, 'OK')) {
        this.subject.next(this.tempResult);
      } else {
        this.subject.complete();
      }
    }
    this.isShow = false;
    setTimeout(() => {
      this.visible = false;
    }, 500);
  }
}
</script>
<style lang="less">
@import './app-popover.less';
</style>