get-current-pages.uvue 5.8 KB
Newer Older
H
hdx 已提交
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
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <page-head title="getCurrentPages"></page-head>
    <view class="uni-padding-wrap">
      <button @click="_getCurrentPages">getCurrentPages</button>
      <view v-if="pages.length" style="padding: 15px 0px">
        <text>当前页面栈中 {{ pages.length }} 个页面,列表如下:</text>
        <template v-for="(page, index) in pages" :key="page.route">
          <text style="margin-top: 5px">index: {{ index }}, route: {{ page.route }}</text>
        </template>
      </view>
    </view>

    <page-head title="currentPageStyle"></page-head>
    <view class="page-style-item" v-for="(value, key) in currentPageStyle" :key="key">
      <view class="item-text">
        <text class="item-text-key">{{key}}:</text>
        <text class="item-text-value">{{value}}</text>
      </view>
      <view class="set-value" v-if="typeof value == 'boolean'">
        <switch :checked="getStyleValue(key).getBoolean('oldValue')"
          @change="switchChange(key, $event as UniSwitchChangeEvent)">
        </switch>
      </view>
      <view class="set-value" v-if="typeof value == 'number'">
        <slider :value="getStyleValue(key).getNumber('oldValue')" :show-value="true"
          @change="sliderChange(key, $event as UniSliderChangeEvent)" />
      </view>
      <view class="set-value" v-else-if="typeof value == 'string'">
        <radio-group class="radio-set-value" @change="radioChange(key, $event as RadioGroupChangeEvent)">
          <radio :value="getStyleValue(key).getString('oldValue')">{{getStyleValue(key).getString('oldValue')}}</radio>
          <text class="split-h"></text>
          <radio :value="getStyleValue(key).getString('newValue')">{{getStyleValue(key).getString('newValue')}}</radio>
        </radio-group>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
42 43
</template>

H
hdx 已提交
44 45 46
<script>
  import { PageStyleItem, PageStyleMap } from './page-style.uts';

47 48 49 50 51 52 53 54 55
  class Page {
    constructor(public route : string) {
    }
  }

  export default {
    data() {
      return {
        checked: false,
H
hdx 已提交
56 57
        pages: [] as Page[],
        PageStyleMap: PageStyleMap as Map<string, PageStyleItem>,
H
hdx 已提交
58
        currentPageStyle: {} as UTSJSONObject,
59
        currentPageStyleIsUTSJSONObject: true,
H
hdx 已提交
60 61
        // TODO
        enablePullDownRefreshStatus: true
62 63 64 65 66 67
      }
    },
    computed: {
      pageStyleText() : string {
        return JSON.stringify(this.currentPageStyle)
      }
H
hdx 已提交
68 69 70
    },
    onLoad() {
      this.getPageStyle();
71 72 73 74
    },
    onPullDownRefresh() {
      setTimeout(() => {
        uni.stopPullDownRefresh()
H
hdx 已提交
75
      }, 2000)
76
    },
H
hdx 已提交
77 78 79 80
    methods: {
      startPullDownRefresh() {
        uni.startPullDownRefresh()
      },
81 82 83 84 85 86 87 88 89 90 91 92 93
      _getCurrentPages: function () {
        this.pages.length = 0
        const pages = getCurrentPages()
        this.pages.push(new Page(pages[0].route))
        if (this.pages[0].route.includes('/tabBar/')) {
          this.checked = true
        }
        for (let i = 1; i < pages.length; i++) {
          this.pages.push(new Page(pages[i].route))
          if (pages[i].route.includes('/tabBar/')) {
            this.checked = false
          }
        }
94
      },
H
hdx 已提交
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
      /// get-set-page-style
      radioChange(key : string, e : RadioGroupChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      sliderChange(key : string, e : UniSliderChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      switchChange(key : string, e : UniSwitchChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      setStyleValue(key : string, value : any) {
        const style = {}
        style[key] = value
        this.setPageStyle(style)
        this.getPageStyle()
      },
      getStyleValue(key : string) : UTSJSONObject {
        if (PageStyleMap.has(key))
          return PageStyleMap.get(key)!.value;
        else
          return {}
      },
      getPageStyle() : UTSJSONObject {
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        this.currentPageStyle = currentPage.$getPageStyle()
        this.currentPageStyleIsUTSJSONObject = this.currentPageStyle instanceof UTSJSONObject
        return this.currentPageStyle;
      },
      setPageStyle(style : UTSJSONObject) {
        console.log('setPageStyle:', style);
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        currentPage.$setPageStyle(style);
      },
      // getPageStyle() {
      //   const pages = getCurrentPages();
      //   const currentPage = pages[pages.length - 1];
      //   this.currentPageStyle = currentPage.$getPageStyle();
      //   this.currentPageStyleIsUTSJSONObject = this.currentPageStyle instanceof UTSJSONObject
      // },
      // setPageStyle(enable : boolean) {
      //   // 目前仅支持 enablePullDownRefresh
      //   const pages = getCurrentPages();
      //   const currentPage = pages[pages.length - 1];
      //   currentPage.$setPageStyle({
      //     enablePullDownRefresh: enable
      //   });
      //   this.enablePullDownRefreshStatus = enable
      // },
145 146 147 148 149 150 151 152 153 154
      // getCurrentPage(): Page {
      //   const pages = getCurrentPages();
      //   const currentPage = pages[pages.length - 1];
      //   return currentPage;
      // }
    },
  }
</script>

<style>
H
hdx 已提交
155 156 157
  .page {
    flex: 1;
    padding: 10px;
H
hdx 已提交
158 159 160 161
  }

  .page-style {
    margin-top: 15px;
162
  }
H
hdx 已提交
163 164 165 166 167 168

  .page-style-item {
    padding: 10px;
    margin-top: 10px;
    background-color: #ffffff;
    border-radius: 5px;
H
hdx 已提交
169 170
  }

H
hdx 已提交
171 172
  .item-text {
    flex-direction: row;
H
hdx 已提交
173 174
  }

H
hdx 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  .item-text-key {
    font-weight: bold;
  }

  .item-text-value {
    margin-left: 32px;
  }

  .set-value {
    margin-top: 10px;
  }

  .radio-set-value {
    flex-direction: row;
  }

  .split-h {
    width: 15px;
193 194
  }
</style>