tab-page-exp.vue 7.3 KB
Newer Older
1
<template>
tony001's avatar
tony001 committed
2
  <div class="ibiz-page-tag">
tony001's avatar
tony001 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    <div class="tag-tabs left">
      <el-tabs
        type="card"
        @tab-click="changePage"
        v-model="editableTabsValue"
        closable 
        @tab-remove="onClose"
      >
        <el-tab-pane
          v-for="(meta, index) of $store.state.pageMetas"
          :name="index+''"
          :key="index+''"
         
        >
        <span slot="label"><span class="ivu-tag-dot-inner"></span>{{ getCaption(meta.caption, meta.info) }}</span>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div v-show="$store.state.pageMetas.length > 0" class="right">
      <el-dropdown @command="handlerClose">
        <el-button size="mini" type="primary">
          更多<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
tony001's avatar
tony001 committed
27
          <el-dropdown-item :command="item" v-for="(item,index) in actions" :key="index">{{ $t(item.text) }}</el-dropdown-item>
tony001's avatar
tony001 committed
28 29 30
        </el-dropdown-menu>
      </el-dropdown>
    </div>
tony001's avatar
tony001 committed
31
  </div>
32 33 34
</template>

<script lang="ts">
tony001's avatar
tony001 committed
35 36
import { Vue, Component, Provide, Prop, Watch } from "vue-property-decorator";
import { Environment } from "../../environments/environment";
37 38 39

@Component({})
export default class TabPageExp extends Vue {
tony001's avatar
tony001 committed
40

tony001's avatar
tony001 committed
41 42
  @Provide()
  public styleLeft: number = 0;
43

tony001's avatar
tony001 committed
44 45 46 47 48
  @Provide()
  public actions: any[] = [
    { text: "app.tabpage.closeall", value: "closeAll" },
    { text: "app.tabpage.closeother", value: "closeOther" }
  ];
49

tony001's avatar
tony001 committed
50 51 52 53 54 55 56 57
  /**
   * 关闭tab页方法
   */
  public handlerClose(item: any){
    this.doTagAction(item.value);
  }

  public editableTabsValue: any = ""; //tabs页绑定值
58

tony001's avatar
tony001 committed
59 60 61 62 63
  @Watch("$route")
  public onRouteChange(newVal: any) {
    this.moveToView(newVal);
    this.$emit("change", newVal);
  }
64

tony001's avatar
tony001 committed
65 66 67
  public created() {
    Vue.prototype.$tabPageExp = this;
  }
68

tony001's avatar
tony001 committed
69 70 71 72 73
  public getCaption(caption: any, info: any): any {
    return info && !Object.is(info, "")
      ? `${this.$t(caption)} - ${info}`
      : this.$t(caption);
  }
74

tony001's avatar
tony001 committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  /**
   * 向左移动
   *
   * @memberof TabPageExp
   */
  public leftMove() {
    const scrollBody: any = this.$refs.scrollBody;
    const scrollChild: any = this.$refs.scrollChild;
    if (
      scrollBody &&
      scrollChild &&
      scrollChild.offsetWidth > scrollBody.offsetWidth
    ) {
      if (
        scrollChild.offsetWidth - scrollBody.offsetWidth + this.styleLeft >
        100
      ) {
        this.styleLeft -= 100;
      } else {
        this.styleLeft = scrollBody.offsetWidth - scrollChild.offsetWidth;
      }
96
    }
tony001's avatar
tony001 committed
97
  }
98

tony001's avatar
tony001 committed
99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * 向右移动
   *
   * @memberof TabPageExp
   */
  public rightMove() {
    if (this.styleLeft < 0) {
      if (this.styleLeft + 100 > 0) {
        this.styleLeft = 0;
      } else {
        this.styleLeft += 100;
      }
111
    }
tony001's avatar
tony001 committed
112
  }
113

tony001's avatar
tony001 committed
114 115 116 117 118 119 120 121 122 123 124
  /**
   * 是否选中
   *
   * @param {(string | number)} index
   * @returns
   * @memberof TabPageExp
   */
  public isActive(index: string | number) {
    const page = this.$store.state.pageTagList[index];
    if (Object.is(page.fullPath, this.$route.fullPath)) {
      return true;
125
    }
tony001's avatar
tony001 committed
126 127
    return false;
  }
128

tony001's avatar
tony001 committed
129 130 131 132 133 134 135 136 137 138 139 140
  /**
   * 关闭
   *
   * @param {*} event
   * @param {*} name
   * @memberof TabPageExp
   */
  public onClose(name: any) {
    const page = this.$store.getters.getPage(name);
    if (!page) {
      this.$store.commit("deletePage", name);
      this.gotoPage();
141
    }
tony001's avatar
tony001 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    const appview = this.$store.getters["viewaction/getAppView"](page.viewtag);
    if (appview && appview.viewdatachange) {
      const title: any = this.$t("app.tabpage.sureclosetip.title");
      const content: any = this.$t("app.tabpage.sureclosetip.content");
      this.$Modal.confirm({
        title: title,
        content: content,
        onOk: () => {
          this.$store.commit("deletePage", name);
          this.gotoPage();
        },
        onCancel: () => {}
      });
    } else {
      this.$store.commit("deletePage", name);
      this.gotoPage();
158
    }
tony001's avatar
tony001 committed
159
  }
160

tony001's avatar
tony001 committed
161 162 163 164 165 166 167 168 169 170
  /**
   * 是否显示关闭
   *
   * @returns
   * @memberof TabPageExp
   */
  public isClose() {
    const pageTagList = this.$store.state.pageTagList;
    if (pageTagList.length > 1) {
      return true;
171
    }
tony001's avatar
tony001 committed
172 173
    return false;
  }
174

tony001's avatar
tony001 committed
175 176 177 178 179 180 181 182 183 184 185
  /**
   * 切换分页
   *
   * @param {*} index
   * @memberof TabPageExp
   */
  public changePage(tab: any, event: any) {
    this.editableTabsValue = tab.index;
    this.$store.commit("setCurPage", tab.index);
    this.gotoPage();
  }
186

tony001's avatar
tony001 committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  /**
   * 跳转页面
   *
   * @returns
   * @memberof TabPageExp
   */
  public gotoPage() {
    const length = this.$store.state.historyPathList.length;
    if (length > 0) {
      const path = this.$store.state.historyPathList[length - 1];
      if (Object.is(path, this.$route.fullPath)) {
        return;
      }
      const index = this.$store.state.pageTagList.findIndex((page: any) =>
        Object.is(page.fullPath, path)
      );
      if (index >= 0) {
        const page = this.$store.state.pageTagList[index];
        this.$router.push({
          path: page.path,
          params: page.params,
          query: page.query
209
        });
tony001's avatar
tony001 committed
210 211 212 213 214 215 216 217 218 219
      }
    } else {
      let path: string | null = window.sessionStorage.getItem(
        Environment.AppName
      );
      if (path) {
        this.$router.push({ path: path });
      } else {
        this.$router.push("/");
      }
220
    }
tony001's avatar
tony001 committed
221
  }
222

tony001's avatar
tony001 committed
223 224 225 226 227 228 229
  /**
   * 设置当前页标题
   *
   * @param {*} caption
   * @memberof TabPageExp
   */
  public setCurPageCaption(caption: string, title: any, info: string) {
tony001's avatar
tony001 committed
230
    if (this.$route.meta && !Object.is(this.$t(this.$route.meta.caption), caption)) {
tony001's avatar
tony001 committed
231
      return;
232
    }
tony001's avatar
tony001 committed
233 234 235 236 237 238 239 240 241
    this.$store.commit("setCurPageCaption", {
      route: this.$route,
      caption: title,
      info: info
    });
    setTimeout(() => {
      this.moveToView(this.$route);
    }, 1);
  }
242

tony001's avatar
tony001 committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  /**
   * 移动至指定页面标签
   *
   * @param {*} to
   * @memberof TabPageExp
   */
  /**
   * 移动至指定页面标签
   *
   * @param {*} to
   * @memberof TabPageExp
   */
  public moveToView(to: any) {
    let that: any = this;
    const pages: any[] = this.$store.state.pageTagList;
    let leftWidth: number = 0;
    this.$nextTick(() => {
      let _index: any = "";
      pages.forEach((page, index) => {
        if (Object.is(page.path, to.path)) {
          _index = index + "";
264
        }
tony001's avatar
tony001 committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
      });
      if (_index !== "") {
        that.editableTabsValue = _index + "";
      }
    });
  }

  /**
   * 设置左侧边距
   *
   * @param {{ offsetWidth: number; }} tag
   * @param {number} leftWidth
   * @memberof TabPageExp
   */
  public setLeft(tag: { offsetWidth: number }, leftWidth: number) {
    if (tag) {
      const scrollBody: any = this.$refs.scrollBody;
      if (leftWidth < -this.styleLeft) {
        this.styleLeft = -leftWidth;
      } else if (
        leftWidth + tag.offsetWidth >
        scrollBody.offsetWidth - this.styleLeft
      ) {
        this.styleLeft -=
          leftWidth +
          tag.offsetWidth -
          (scrollBody.offsetWidth - this.styleLeft);
      }
    }
  }

  /**
   * 执行行为操作
   *
   * @param {string} name
   * @memberof TabPageExp
   */
  public doTagAction(name: string) {
    if (Object.is(name, "closeAll")) {
      this.$store.commit("removeAllPage");
      this.gotoPage();
    } else if (Object.is(name, "closeOther")) {
      this.$store.commit("removeOtherPage");
      this.moveToView(this.$route);
    }
  }
311 312 313 314
}
</script>

<style lang="less">
tony001's avatar
tony001 committed
315
@import "./tab-page-exp.less";
316
</style>