waterflow.uvue 15.1 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4 5 6 7
<script>
  type ScrollEventTest = {
    type : string;
    target : UniElement | null;
    currentTarget : UniElement | null;
    direction ?: string
  }
shutao-dc's avatar
shutao-dc 已提交
8
  type flowItemData = {
shutao-dc's avatar
shutao-dc 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    height : number,
    text : string
  }
  import { ItemType } from '@/components/enum-data/enum-data-types'
  export default {
    data() {
      return {
        refresher_triggered_boolean: false,
        refresher_enabled_boolean: false,
        scroll_with_animation_boolean: false,
        show_scrollbar_boolean: false,
        bounces_boolean: true,
        upper_threshold_input: 50,
        lower_threshold_input: 50,
        scroll_top_input: 0,
        scroll_left_input: 0,
        refresher_background_input: "#FFF",
shutao-dc's avatar
shutao-dc 已提交
26
        scrollData: [] as Array<flowItemData>,
shutao-dc's avatar
shutao-dc 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        size_enum: [{ "value": 0, "name": "item---0" }, { "value": 3, "name": "item---3" }] as ItemType[],
        scrollIntoView: "",
        refresherrefresh: false,
        refresher_default_style_input: "black",
        text: ['继续下拉执行刷新', '释放立即刷新', '刷新中', ""],
        state: 3,
        reset: true,
        // 自动化测试
        isScrollTest: '',
        isScrolltolowerTest: '',
        isScrolltoupperTest: '',
        scrollDetailTest: null as UniScrollEventDetail | null,
        scrollEndDetailTest: null as UniScrollEventDetail | null,
        cross_axis_count: 2,
        main_axis_gap: 2,
        cross_axis_gap: 2,
shutao-dc's avatar
shutao-dc 已提交
43
        waterflowPadding: [10, 5, 10, 5] as Array<number>,
shutao-dc's avatar
shutao-dc 已提交
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
        loadMore: true,
        isLoadMore: true
      }
    },
    onLoad() {
      //静态瀑布流数据
      this.scrollData = [
        { height: 300, text: "item---0" },
        { height: 150, text: "item---1" },
        { height: 120, text: "item---2" },
        { height: 100, text: "item---3" },
        { height: 100, text: "item---4" },
        { height: 150, text: "item---5" },
        { height: 140, text: "item---6" },
        { height: 190, text: "item---7" },
        { height: 160, text: "item---8" },
        { height: 120, text: "item---9" },
        { height: 109, text: "item---10" },
        { height: 102, text: "item---11" },
        { height: 123, text: "item---12" },
        { height: 156, text: "item---13" },
        { height: 177, text: "item---14" },
        { height: 105, text: "item---15" },
        { height: 110, text: "item---16" },
        { height: 90, text: "item---17" },
        { height: 130, text: "item---18" },
        { height: 140, text: "item---19" },
shutao-dc's avatar
shutao-dc 已提交
71
      ] as Array<flowItemData>
shutao-dc's avatar
shutao-dc 已提交
72 73
    },
    methods: {
shutao-dc's avatar
shutao-dc 已提交
74 75 76 77 78 79 80 81
      waterflow_click() { console.log("组件被点击时触发") },
      waterflow_touchstart() { console.log("手指触摸动作开始") },
      waterflow_touchmove() { console.log("手指触摸后移动") },
      waterflow_touchcancel() { console.log("手指触摸动作被打断,如来电提醒,弹窗") },
      waterflow_touchend() { console.log("手指触摸动作结束") },
      waterflow_tap() { console.log("手指触摸后马上离开") },
      waterflow_longpress() { console.log("如果一个组件被绑定了 longpress 事件,那么当用户长按这个组件时,该事件将会被触发。") },
      waterflow_refresherpulling(e : RefresherEvent) {
shutao-dc's avatar
shutao-dc 已提交
82 83 84 85 86 87 88 89 90
        console.log("下拉刷新控件被下拉")
        if (this.reset) {
          if (e.detail.dy > 45) {
            this.state = 1
          } else {
            this.state = 0
          }
        }
      },
shutao-dc's avatar
shutao-dc 已提交
91
      waterflow_refresherrefresh() {
shutao-dc's avatar
shutao-dc 已提交
92 93 94 95 96 97 98 99 100
        console.log("下拉刷新被触发 ")
        this.refresherrefresh = true
        this.refresher_triggered_boolean = true
        this.state = 2
        this.reset = false;
        setTimeout(() => {
          this.refresher_triggered_boolean = false
        }, 1500)
      },
shutao-dc's avatar
shutao-dc 已提交
101
      waterflow_refresherrestore() {
shutao-dc's avatar
shutao-dc 已提交
102 103 104 105 106
        this.refresherrefresh = false
        this.state = 3
        this.reset = true
        console.log("下拉刷新被复位")
      },
shutao-dc's avatar
shutao-dc 已提交
107 108
      waterflow_refresherabort() { console.log("下拉刷新被中止") },
      waterflow_scrolltoupper(e : UniScrollToUpperEvent) {
shutao-dc's avatar
shutao-dc 已提交
109 110 111 112 113 114 115 116
        console.log("滚动到顶部/左边,会触发 scrolltoupper 事件  direction=" + e.detail.direction)
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          direction: e.detail.direction,
        } as ScrollEventTest, 'scrolltoupper')
      },
shutao-dc's avatar
shutao-dc 已提交
117
      waterflow_scrolltolower(e : UniScrollToLowerEvent) {
shutao-dc's avatar
shutao-dc 已提交
118 119 120 121 122 123 124 125
        console.log("滚动到底部/右边,会触发 scrolltolower 事件  direction=" + e.detail.direction)
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          direction: e.detail.direction,
        } as ScrollEventTest, 'scrolltolower')
      },
shutao-dc's avatar
shutao-dc 已提交
126
      waterflow_scroll(e : UniScrollEvent) {
shutao-dc's avatar
shutao-dc 已提交
127 128 129 130 131 132 133 134
        console.log("滚动时触发,event.detail = ", e.detail)
        this.scrollDetailTest = e.detail
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget
        } as ScrollEventTest, 'scroll')
      },
shutao-dc's avatar
shutao-dc 已提交
135
      waterflow_scrollend(e : UniScrollEvent) {
shutao-dc's avatar
shutao-dc 已提交
136 137 138 139 140 141 142 143
        console.log("滚动结束时触发", e.detail)
        this.scrollEndDetailTest = e.detail
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget
        } as ScrollEventTest, 'scrollend')
      },
shutao-dc's avatar
shutao-dc 已提交
144 145 146 147 148 149 150
      flow_item_click() { console.log("flow-item组件被点击时触发") },
      flow_item_touchstart() { console.log("手指触摸flow-item组件动作开始") },
      flow_item_touchmove() { console.log("手指触摸flow-item组件后移动") },
      flow_item_touchcancel() { console.log("手指触摸flow-item组件动作被打断,如来电提醒,弹窗") },
      flow_item_touchend() { console.log("手指触摸flow-item组件动作结束") },
      flow_item_tap() { console.log("手指触摸flow-item组件后马上离开") },
      flow_item_longpress() { console.log("flow-item组件被绑定了 longpress 事件,那么当用户长按这个组件时,该事件将会被触发。") },
shutao-dc's avatar
shutao-dc 已提交
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
      change_refresher_triggered_boolean(checked : boolean) { this.refresher_triggered_boolean = checked },
      change_refresher_enabled_boolean(checked : boolean) { this.refresher_enabled_boolean = checked },
      change_scroll_with_animation_boolean(checked : boolean) { this.scroll_with_animation_boolean = checked },
      change_show_scrollbar_boolean(checked : boolean) { this.show_scrollbar_boolean = checked },
      change_bounces_boolean(checked : boolean) { this.bounces_boolean = checked },
      confirm_upper_threshold_input(value : number) { this.upper_threshold_input = value },
      confirm_lower_threshold_input(value : number) { this.lower_threshold_input = value },
      confirm_scroll_top_input(value : number) { this.scroll_top_input = value },
      confirm_scroll_left_input(value : number) { this.scroll_left_input = value },
      confirm_refresher_background_input(value : string) { this.refresher_background_input = value },
      item_change_size_enum(index : number) { this.scrollIntoView = "item---" + index },
      //自动化测试专用
      setScrollIntoView(id : string) { this.scrollIntoView = id },
      // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
      checkEventTest(e : ScrollEventTest, eventName : String) {
        const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement;
        const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
        switch (eventName) {
          case 'scroll':
            this.isScrollTest = result
            break;
          case 'scrolltolower':
            this.isScrolltolowerTest = result + `-${e.direction}`
            break;
          case 'scrolltoupper':
            this.isScrolltoupperTest = result + `-${e.direction}`
            break;
          default:
            break;
        }
      },
      //自动化测试例专用
      check_scroll_height() : Boolean {
shutao-dc's avatar
shutao-dc 已提交
184
        var listElement = this.$refs["waterflow"] as UniElement
shutao-dc's avatar
shutao-dc 已提交
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
        console.log("check_scroll_height--" + listElement.scrollHeight)
        if (listElement.scrollHeight > 1400) {
          return true
        }
        return false
      },
      change_refresher_style_boolean(checked : boolean) {
        if (checked) {
          this.refresher_default_style_input = "none"
        } else {
          this.refresher_default_style_input = "black"
        }
      },
      change_load_more_boolean(checked : boolean) {
        this.isLoadMore = checked
      },
      handleChangeCrossAxisCount(value : number) {
        if (value < 2) {
          uni.showToast({ title: "cross-axis-count 最小值为 2 请重新设置" })
          return
        }
        this.cross_axis_count = value
      },
      handleChangeCrossAxisGap(e : UniSliderChangeEvent) {
        this.cross_axis_gap = e.detail.value
      },
      handleChangeMainAxisGap(e : UniSliderChangeEvent) {
        this.main_axis_gap = e.detail.value
      },
      //仅自动化测试调用
shutao-dc's avatar
shutao-dc 已提交
215
      testModifyWaterflowProps() {
shutao-dc's avatar
shutao-dc 已提交
216 217 218
        this.cross_axis_count = 4
        this.main_axis_gap = 6
        this.cross_axis_gap = 6
shutao-dc's avatar
shutao-dc 已提交
219
        this.waterflowPadding = [5, 10, 5, 10] as Array<number>
shutao-dc's avatar
shutao-dc 已提交
220 221 222 223 224 225 226
      }
    }
  }
</script>

<template>
  <view class="main">
shutao-dc's avatar
shutao-dc 已提交
227
    <waterflow :cross-axis-count="cross_axis_count" :main-axis-gap="main_axis_gap" :cross-axis-gap="cross_axis_gap"
shutao-dc's avatar
shutao-dc 已提交
228 229 230 231 232
      :bounces="bounces_boolean" :upper-threshold="upper_threshold_input" :lower-threshold="lower_threshold_input"
      :scroll-top="scroll_top_input" :scroll-left="scroll_left_input" :show-scrollbar="show_scrollbar_boolean"
      :scroll-into-view="scrollIntoView" :scroll-with-animation="scroll_with_animation_boolean"
      :refresher-enabled="refresher_enabled_boolean" :refresher-background="refresher_background_input"
      :refresher-triggered="refresher_triggered_boolean" :refresher-default-style="refresher_default_style_input"
shutao-dc's avatar
shutao-dc 已提交
233 234 235 236 237 238 239
      @click="waterflow_click" @touchstart="waterflow_touchstart" @touchmove="waterflow_touchmove"
      @touchcancel="waterflow_touchcancel" @touchend="waterflow_touchend" @tap="waterflow_tap"
      @longpress="waterflow_longpress" @refresherpulling="waterflow_refresherpulling"
      @refresherrefresh="waterflow_refresherrefresh" @refresherrestore="waterflow_refresherrestore"
      @refresherabort="waterflow_refresherabort" @scrolltoupper="waterflow_scrolltoupper" ref="waterflow"
      @scrolltolower="waterflow_scrolltolower" @scroll="waterflow_scroll" @scrollend="waterflow_scrollend"
      style="width:100%; " id="waterflow" :padding="waterflowPadding">
shutao-dc's avatar
shutao-dc 已提交
240

shutao-dc's avatar
shutao-dc 已提交
241 242 243 244
      <flow-item v-for="(item, index) in scrollData" :key="index" :id="item.text" @click="flow_item_click"
        :style="{'height' : item.height}" @touchstart="flow_item_touchstart" @touchmove="flow_item_touchmove"
        @touchcancel="flow_item_touchcancel" @touchend="flow_item_touchend" @tap="flow_item_tap"
        @longpress="flow_item_longpress" class="flow-item" type=1>
shutao-dc's avatar
shutao-dc 已提交
245
        <text>{{item.text}}</text>
shutao-dc's avatar
shutao-dc 已提交
246
      </flow-item>
shutao-dc's avatar
shutao-dc 已提交
247

shutao-dc's avatar
shutao-dc 已提交
248
      <flow-item slot="refresher" id="refresher" type=2 class="refresh-box">
shutao-dc's avatar
shutao-dc 已提交
249
        <text class="tip-text">{{text[state]}}</text>
shutao-dc's avatar
shutao-dc 已提交
250 251
      </flow-item>
      <flow-item v-show="isLoadMore" slot="load-more" id="loadmore" type=6 class="load-more-box">
shutao-dc's avatar
shutao-dc 已提交
252
        <text>加载更多</text>
shutao-dc's avatar
shutao-dc 已提交
253 254
      </flow-item>
    </waterflow>
shutao-dc's avatar
shutao-dc 已提交
255 256 257 258 259 260 261 262 263 264 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
  </view>

  <scroll-view style="flex:1" direction="vertical">
    <view class="content">
      <boolean-data :defaultValue="false" title="设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发"
        @change="change_refresher_triggered_boolean"></boolean-data>
      <boolean-data :defaultValue="false" title="开启下拉刷新" @change="change_refresher_enabled_boolean"></boolean-data>
      <boolean-data :defaultValue="false" title="开启自定义样式" @change="change_refresher_style_boolean"></boolean-data>
      <boolean-data :defaultValue="false" title="是否在设置滚动条位置时使用滚动动画,设置false没有滚动动画"
        @change="change_scroll_with_animation_boolean"></boolean-data>
      <boolean-data :defaultValue="false" title="控制是否出现滚动条" @change="change_show_scrollbar_boolean"></boolean-data>
      <boolean-data :defaultValue="true" title="控制是否回弹效果" @change="change_bounces_boolean"></boolean-data>
      <boolean-data :defaultValue="true" title="是否显示加载更多" @change="change_load_more_boolean"></boolean-data>
      <input-data defaultValue="2" title="设置cross-axis-count,触发 scrolltoupper 事件" type="number"
        @confirm="handleChangeCrossAxisCount"></input-data>
      <view class="uni-slider uni-list-cell-padding">
        <text>拖动设置cross-axis-gap</text>
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <slider :max="20" :min="0" :step="1" :value="cross_axis_gap" :show-value="true"
          @change="handleChangeCrossAxisGap" />
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <text>拖动设置main-axis-gap</text>
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <slider :max="20" :min="0" :step="1" :value="main_axis_gap" :show-value="true"
          @change="handleChangeMainAxisGap" />
      </view>
      <input-data defaultValue="50" title="距顶部/左边多远时(单位px),触发 scrolltoupper 事件" type="number"
        @confirm="confirm_upper_threshold_input"></input-data>
      <input-data defaultValue="50" title="距底部/右边多远时(单位px),触发 scrolltolower 事件" type="number"
        @confirm="confirm_lower_threshold_input"></input-data>
      <input-data defaultValue="0" title="设置竖向滚动条位置" type="number" @confirm="confirm_scroll_top_input"></input-data>
      <input-data defaultValue="#FFF" title="设置下拉刷新区域背景颜色" type="text"
        @confirm="confirm_refresher_background_input"></input-data>
      <enum-data :items="size_enum" title="通过id位置跳转" @change="item_change_size_enum"></enum-data>
    </view>
  </scroll-view>
</template>

<style>
  .main {
    max-height: 250px;
    padding: 5px 0;
    border-bottom: 1px solid rgba(0, 0, 0, .06);
    flex-direction: row;
    justify-content: center;
  }

shutao-dc's avatar
shutao-dc 已提交
305
  .flow-item {
shutao-dc's avatar
shutao-dc 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    width: 100%;
    height: 200px;
    border: 1px solid #666;
    background-color: #66ccff;
    align-items: center;
    justify-content: center;
  }

  .tip-text {
    color: #888;
    font-size: 12px;
  }

  .refresh-box {
    justify-content: center;
    align-items: center;
    flex-direction: row;
    height: 45px;
    width: 100%;
  }

  .load-more-box {
    justify-content: center;
    align-items: center;
    flex-direction: row;
    height: 45px;
    width: 100%;
    border-style: solid;
  }

  .button {
    margin-top: 15px;
  }
</style>