get-current-pages.uvue 9.4 KB
Newer Older
1 2 3
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
4
  <!-- #endif -->
5 6 7 8 9 10 11
    <view>
      <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">
12
            <text style="margin-top: 5px">index: {{ index }}, route: {{ page.route }}</text>
13 14
          </template>
        </view>
15
        <button class="uni-common-mt" @click="check$page">check $page</button>
16 17 18 19 20 21
        <button class="uni-common-mt" @click="checkGetParentPage">
          check getParentPage
        </button>
        <button class="uni-common-mt" @click="checkGetDialogPages">
          check getDialogPages
        </button>
22
        <button id="check-get-element-by-id-btn" class="uni-common-mt" @click="checkGetElementById">
23 24 25 26 27
          check getElementById
        </button>
        <button class="uni-common-mt" @click="checkGetAndroidView">
          check getAndroidView
        </button>
28 29 30 31 32 33 34
        <button class="uni-common-mt" @click="checkGetIOSView">
          check getIOSView
        </button>
        <button class="uni-common-mt" @click="checkGetHTMLElement">
          check getHTMLElement
        </button>
      </view>
35
      <!-- #ifndef MP -->
36 37
      <page-head title="currentPageStyle"></page-head>
      <template v-for="(item, index) in PageStyleArray">
38
        <view class="page-style-item" v-if="currentPageStyle[item.key] != null" :key="index">
39
          <view class="item-text">
40 41 42 43
            <text class="item-text-key">{{ item.key }}:</text>
            <text class="item-text-value">{{
              currentPageStyle[item.key]
            }}</text>
44 45
          </view>
          <view class="set-value" v-if="item.type == 'boolean'">
46 47
            <switch :checked="currentPageStyle.getBoolean(item.key)"
              @change="switchChange(item.key, $event as UniSwitchChangeEvent)">
48 49 50
            </switch>
          </view>
          <view class="set-value" v-else-if="item.type == 'number'">
51 52
            <slider :value="currentPageStyle.getNumber(item.key)" :show-value="true"
              @change="sliderChange(item.key, $event as UniSliderChangeEvent)" />
53 54
          </view>
          <view class="set-value" v-else-if="item.type == 'string'">
55 56
            <radio-group class="radio-set-value" @change="radioChange(item.key, $event as RadioGroupChangeEvent)">
              <radio class="radio-value" v-for="(item2, index2) in item.value" :key="index2" :value="item2">{{ item2 }}
57 58 59
              </radio>
            </radio-group>
          </view>
60
        </view>
61
      </template>
62 63
      <button style="margin: 10px" @click="goSetDisablePullDownRefresh">
        go set disable pullDownRefresh
64
      </button>
65
      <!-- #endif -->
66
    </view>
67
  <!-- #ifdef APP -->
68
  </scroll-view>
H
hdx 已提交
69
  <!-- #endif -->
70 71
</template>

72 73
<script>
  import { PageStyleItem, PageStyleArray } from './page-style.uts';
H
hdx 已提交
74

75 76 77 78 79 80 81 82 83
  class Page {
    constructor(public route : string) {
    }
  }

  export default {
    data() {
      return {
        checked: false,
84 85
        pages: [] as Page[],
        PageStyleArray: PageStyleArray as PageStyleItem[],
86
        currentPageStyle: {} as UTSJSONObject,
87
        testing: false
88 89 90 91 92 93
      }
    },
    computed: {
      pageStyleText() : string {
        return JSON.stringify(this.currentPageStyle)
      }
94
    },
95
    onLoad(options : OnLoadOptions) {
DCloud-WZF's avatar
DCloud-WZF 已提交
96
      // #ifndef APP-ANDROID
97 98 99
      if (options instanceof UTSJSONObject) {
        this.checked = true
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
100
      // #endif
101 102 103
      // #ifdef APP-ANDROID
      this.checked = true
      // #endif
104
      this.getPageStyle();
105 106 107 108
    },
    onPullDownRefresh() {
      setTimeout(() => {
        uni.stopPullDownRefresh()
H
hdx 已提交
109
      }, 2000)
110
    },
111 112 113
    methods: {
      startPullDownRefresh() {
        uni.startPullDownRefresh()
H
hdx 已提交
114
      },
115 116 117 118
      _getCurrentPages: function () {
        this.pages.length = 0
        const pages = getCurrentPages()
        this.pages.push(new Page(pages[0].route))
119
        if (this.checked && (this.pages[0].route.includes('/tabBar/') || this.pages[0].route == '/')) {
120 121 122 123 124 125 126 127
          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
          }
        }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      },
      /// 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()
      },
      getPageStyle() : UTSJSONObject {
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
148
        this.currentPageStyle = currentPage.getPageStyle()
149 150 151 152 153 154
        return this.currentPageStyle;
      },
      setPageStyle(style : UTSJSONObject) {
        console.log('setPageStyle:', style);
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
155
        currentPage.setPageStyle(style);
H
hdx 已提交
156
      },
157 158 159 160
      goSetDisablePullDownRefresh() {
        uni.navigateTo({
          url: '/pages/API/get-current-pages/set-page-style-disable-pull-down-refresh'
        });
161 162 163 164 165 166
      },
      getCurrentPage() : UniPage {
        const pages = getCurrentPages()
        return pages[pages.length - 1]
      },
      check$page() : boolean {
167 168
        const page = this.getCurrentPage()
        let res = this.$page === page
169
        if (this.testing && res) {
170
          res = page.options['test'] == '123'
171 172 173 174 175 176 177 178
          if (res) {
            // #ifdef WEB
            res = page.route == '/pages/API/get-current-pages/get-current-pages'
            // #endif
            // #ifndef WEB
            res = page.route == 'pages/API/get-current-pages/get-current-pages'
            // #endif
          }
179 180
        }
        console.log('check $page', res)
181
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
182 183 184 185 186 187 188
        return res
      },
      checkGetParentPage() : boolean {
        const page = this.getCurrentPage()
        const parentPage = page.getParentPage()
        const res = parentPage == null
        console.log('check getParentPage', res)
189
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
190 191 192 193 194 195
        return res
      },
      checkGetDialogPages() : boolean {
        const page = this.getCurrentPage()
        const dialogPages = page.getDialogPages()
        const res = Array.isArray(dialogPages) && dialogPages.length == 0
196
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
197 198 199 200 201
        console.log('check getDialogPages', res)
        return res
      },
      checkGetElementById() : boolean {
        const page = this.getCurrentPage()
202 203 204 205 206 207 208
        const element = page.getElementById('check-get-element-by-id-btn')
        let res = element != null
        // #ifndef APP-ANDROID
        if (res) {
          const elPage = element!.getPage()
          console.log('elPage', elPage)
          res = elPage === page
209 210
        }
        // #endif
211
        console.log('check getElementById', res)
212
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
213 214 215 216 217 218 219
        return res
      },
      checkGetAndroidView() : boolean {
        const page = this.getCurrentPage()
        const androidView = page.getAndroidView()
        const res = androidView != null
        console.log('check getAndroidView', res)
220
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
221 222
        return res
      },
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      checkGetIOSView() : boolean {
        const page = this.getCurrentPage()
        const IOSView = page.getIOSView()
        const res = IOSView != null
        console.log('check getIOSView', res)
        uni.showToast(res ? { title: 'check success' } : { title: '仅 IOS uts 插件环境支持', icon: 'error' })
        return res
      },
      checkGetHTMLElement() : boolean {
        const page = this.getCurrentPage()
        const HTMLView = page.getHTMLElement()
        const res = HTMLView != null
        console.log('check getHTMLElement', res)
        uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
        return res
      },
239 240 241 242 243
    },
  }
</script>

<style>
244 245 246 247
  .page {
    flex: 1;
    padding: 10px;
  }
248

249 250 251
  .page-style {
    margin-top: 15px;
  }
252

253 254 255 256 257 258
  .page-style-item {
    padding: 10px;
    margin-top: 10px;
    background-color: #ffffff;
    border-radius: 5px;
  }
259

260 261 262
  .item-text {
    flex-direction: row;
  }
263

264 265 266
  .item-text-key {
    font-weight: bold;
  }
267

268 269 270
  .item-text-value {
    margin-left: 5px;
  }
271

272 273 274
  .set-value {
    margin-top: 10px;
  }
275

276 277 278
  .radio-set-value {
    flex-direction: row;
  }
279

280 281 282
  .radio-value {
    margin-left: 10px;
  }
283
</style>