get-current-pages.uvue 8.1 KB
Newer Older
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14
    <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">
            <text style="margin-top: 5px">index: {{ index }}, route: {{ page.route }}</text>
          </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
      </view>

30 31
      <page-head title="currentPageStyle"></page-head>
      <template v-for="(item, index) in PageStyleArray">
32
        <view class="page-style-item" v-if="currentPageStyle[item.key] != null" :key="index">
33
          <view class="item-text">
34 35 36 37
            <text class="item-text-key">{{ item.key }}:</text>
            <text class="item-text-value">{{
              currentPageStyle[item.key]
            }}</text>
38 39 40 41 42 43 44 45 46 47 48 49
          </view>
          <view class="set-value" v-if="item.type == 'boolean'">
            <switch :checked="currentPageStyle.getBoolean(item.key)"
              @change="switchChange(item.key, $event as UniSwitchChangeEvent)">
            </switch>
          </view>
          <view class="set-value" v-else-if="item.type == 'number'">
            <slider :value="currentPageStyle.getNumber(item.key)" :show-value="true"
              @change="sliderChange(item.key, $event as UniSliderChangeEvent)" />
          </view>
          <view class="set-value" v-else-if="item.type == 'string'">
            <radio-group class="radio-set-value" @change="radioChange(item.key, $event as RadioGroupChangeEvent)">
50
              <radio class="radio-value" v-for="(item2, index2) in item.value" :key="index2" :value="item2">{{ item2 }}
51 52 53
              </radio>
            </radio-group>
          </view>
54
        </view>
55
      </template>
56 57 58
      <button style="margin: 10px" @click="goSetDisablePullDownRefresh">
        go set disable pullDownRefresh
      </button>
59
    </view>
60 61
  <!-- #ifdef APP -->
  </scroll-view>
H
hdx 已提交
62
  <!-- #endif -->
63 64
</template>

65 66
<script>
  import { PageStyleItem, PageStyleArray } from './page-style.uts';
H
hdx 已提交
67

68 69 70 71 72 73 74 75 76
  class Page {
    constructor(public route : string) {
    }
  }

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

<style>
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  .page {
    flex: 1;
    padding: 10px;
  }

  .page-style {
    margin-top: 15px;
  }

  .page-style-item {
    padding: 10px;
    margin-top: 10px;
    background-color: #ffffff;
    border-radius: 5px;
  }

  .item-text {
    flex-direction: row;
  }

  .item-text-key {
    font-weight: bold;
  }

  .item-text-value {
246
    margin-left: 5px;
247 248 249 250
  }

  .set-value {
    margin-top: 10px;
251
  }
252 253 254 255 256

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

257 258
  .radio-value {
    margin-left: 10px;
259
  }
260
</style>