components.js 43.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { createElementVNode, defineComponent, createVNode, mergeProps, getCurrentInstance, provide, watch, onUnmounted, shallowRef, reactive, watchEffect, ref, inject, onBeforeUnmount, computed, Text, isVNode, Fragment, onMounted } from "vue";
fxy060608's avatar
fxy060608 已提交
2
import { hasOwn, extend, isPlainObject } from "@vue/shared";
fxy060608's avatar
fxy060608 已提交
3
import { cacheStringFunction } from "@dcloudio/uni-shared";
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const OPEN_TYPES = [
  "navigate",
  "redirect",
  "switchTab",
  "reLaunch",
  "navigateBack"
];
const navigatorProps = {
  hoverClass: {
    type: String,
    default: "navigator-hover"
  },
  url: {
    type: String,
    default: ""
  },
  openType: {
    type: String,
    default: "navigate",
    validator(value) {
      return Boolean(~OPEN_TYPES.indexOf(value));
fxy060608's avatar
fxy060608 已提交
25
    }
fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  },
  delta: {
    type: Number,
    default: 1
  },
  hoverStartTime: {
    type: [Number, String],
    default: 50
  },
  hoverStayTime: {
    type: [Number, String],
    default: 600
  },
  exists: {
    type: String,
    default: ""
  },
  hoverStopPropagation: {
    type: Boolean,
    default: false
  }
};
fxy060608's avatar
fxy060608 已提交
48
function createNavigatorOnClick(props) {
fxy060608's avatar
fxy060608 已提交
49
  return () => {
fxy060608's avatar
fxy060608 已提交
50
    if (props.openType !== "navigateBack" && !props.url) {
fxy060608's avatar
fxy060608 已提交
51 52
      console.error("<navigator/> should have url attribute when using navigateTo, redirectTo, reLaunch or switchTab");
      return;
fxy060608's avatar
fxy060608 已提交
53
    }
fxy060608's avatar
fxy060608 已提交
54
    switch (props.openType) {
fxy060608's avatar
fxy060608 已提交
55 56
      case "navigate":
        uni.navigateTo({
fxy060608's avatar
fxy060608 已提交
57
          url: props.url
fxy060608's avatar
fxy060608 已提交
58 59 60 61
        });
        break;
      case "redirect":
        uni.redirectTo({
fxy060608's avatar
fxy060608 已提交
62 63
          url: props.url,
          exists: props.exists
fxy060608's avatar
fxy060608 已提交
64 65 66 67
        });
        break;
      case "switchTab":
        uni.switchTab({
fxy060608's avatar
fxy060608 已提交
68
          url: props.url
fxy060608's avatar
fxy060608 已提交
69 70 71 72
        });
        break;
      case "reLaunch":
        uni.reLaunch({
fxy060608's avatar
fxy060608 已提交
73
          url: props.url
fxy060608's avatar
fxy060608 已提交
74 75 76 77
        });
        break;
      case "navigateBack":
        uni.navigateBack({
fxy060608's avatar
fxy060608 已提交
78
          delta: props.delta
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83
        });
        break;
    }
  };
}
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88
function useHoverClass(props) {
  if (props.hoverClass && props.hoverClass !== "none") {
    const hoverAttrs = { hoverClass: props.hoverClass };
    if (hasOwn(props, "hoverStartTime")) {
      hoverAttrs.hoverStartTime = props.hoverStartTime;
fxy060608's avatar
fxy060608 已提交
89
    }
fxy060608's avatar
fxy060608 已提交
90 91
    if (hasOwn(props, "hoverStayTime")) {
      hoverAttrs.hoverStayTime = props.hoverStayTime;
fxy060608's avatar
fxy060608 已提交
92
    }
fxy060608's avatar
fxy060608 已提交
93 94
    if (hasOwn(props, "hoverStopPropagation")) {
      hoverAttrs.hoverStopPropagation = props.hoverStopPropagation;
fxy060608's avatar
fxy060608 已提交
95 96 97 98
    }
    return hoverAttrs;
  }
  return {};
fxy060608's avatar
fxy060608 已提交
99
}
fxy060608's avatar
fxy060608 已提交
100 101 102
function createNVueTextVNode(text, attrs) {
  return createElementVNode("u-text", extend({ appendAsTree: true }, attrs), text);
}
fxy060608's avatar
fxy060608 已提交
103 104 105 106 107 108 109 110 111 112
const navigatorStyles = [{
  "navigator-hover": {
    backgroundColor: "rgba(0,0,0,0.1)",
    opacity: 0.7
  }
}];
var Navigator = defineComponent({
  name: "Navigator",
  props: navigatorProps,
  styles: navigatorStyles,
fxy060608's avatar
fxy060608 已提交
113
  setup(props, {
fxy060608's avatar
fxy060608 已提交
114 115
    slots
  }) {
fxy060608's avatar
fxy060608 已提交
116
    const onClick = createNavigatorOnClick(props);
fxy060608's avatar
fxy060608 已提交
117
    return () => {
fxy060608's avatar
fxy060608 已提交
118
      return createVNode("view", mergeProps(useHoverClass(props), {
fxy060608's avatar
fxy060608 已提交
119 120 121 122 123
        "onClick": onClick
      }), [slots.default && slots.default()]);
    };
  }
});
fxy060608's avatar
fxy060608 已提交
124 125 126 127 128 129
function PolySymbol(name) {
  return Symbol(process.env.NODE_ENV !== "production" ? "[uni-app]: " + name : name);
}
function useCurrentPageId() {
  return getCurrentInstance().root.proxy.$page.id;
}
fxy060608's avatar
fxy060608 已提交
130
const labelProps = {
fxy060608's avatar
fxy060608 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  for: {
    type: String,
    default: ""
  }
};
const uniLabelKey = PolySymbol(process.env.NODE_ENV !== "production" ? "uniLabel" : "ul");
function useProvideLabel() {
  const handlers = [];
  provide(uniLabelKey, {
    addHandler(handler) {
      handlers.push(handler);
    },
    removeHandler(handler) {
      handlers.splice(handlers.indexOf(handler), 1);
    }
  });
  return handlers;
}
var Label = /* @__PURE__ */ defineComponent({
  name: "Label",
fxy060608's avatar
fxy060608 已提交
151
  props: labelProps,
fxy060608's avatar
fxy060608 已提交
152
  styles: [],
fxy060608's avatar
fxy060608 已提交
153
  setup(props, {
fxy060608's avatar
fxy060608 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167
    slots
  }) {
    const pageId = useCurrentPageId();
    const handlers = useProvideLabel();
    const _onClick = ($event) => {
      const EventTarget = $event.target;
      const dataType = EventTarget.attr.dataUncType || "";
      let stopPropagation = /^uni-(checkbox|radio|switch)-/.test(dataType);
      if (!stopPropagation) {
        stopPropagation = /^uni-(checkbox|radio|switch|button)$/i.test(dataType);
      }
      if (stopPropagation) {
        return;
      }
fxy060608's avatar
fxy060608 已提交
168 169
      if (props.for) {
        UniViewJSBridge.emit(`uni-label-click-${pageId}-${props.for}`, $event, true);
fxy060608's avatar
fxy060608 已提交
170 171 172 173
      } else {
        handlers.length && handlers[0]($event, true);
      }
    };
fxy060608's avatar
fxy060608 已提交
174
    return () => createVNode("view", {
fxy060608's avatar
fxy060608 已提交
175 176 177 178
      "onClick": _onClick
    }, [slots.default && slots.default()]);
  }
});
fxy060608's avatar
fxy060608 已提交
179 180 181
function useListeners(props, listeners) {
  _addListeners(props.id, listeners);
  watch(() => props.id, (newId, oldId) => {
fxy060608's avatar
fxy060608 已提交
182 183 184 185
    _removeListeners(oldId, listeners, true);
    _addListeners(newId, listeners, true);
  });
  onUnmounted(() => {
fxy060608's avatar
fxy060608 已提交
186
    _removeListeners(props.id, listeners);
fxy060608's avatar
fxy060608 已提交
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  });
}
function _addListeners(id, listeners, watch2) {
  const pageId = useCurrentPageId();
  if (watch2 && !id) {
    return;
  }
  if (!isPlainObject(listeners)) {
    return;
  }
  Object.keys(listeners).forEach((name) => {
    if (watch2) {
      if (name.indexOf("@") !== 0 && name.indexOf("uni-") !== 0) {
        UniViewJSBridge.on(`uni-${name}-${pageId}-${id}`, listeners[name]);
      }
    } else {
      if (name.indexOf("uni-") === 0) {
        UniViewJSBridge.on(name, listeners[name]);
      } else if (id) {
        UniViewJSBridge.on(`uni-${name}-${pageId}-${id}`, listeners[name]);
      }
    }
  });
}
function _removeListeners(id, listeners, watch2) {
  const pageId = useCurrentPageId();
  if (watch2 && !id) {
    return;
  }
  if (!isPlainObject(listeners)) {
    return;
  }
  Object.keys(listeners).forEach((name) => {
    if (watch2) {
      if (name.indexOf("@") !== 0 && name.indexOf("uni-") !== 0) {
        UniViewJSBridge.off(`uni-${name}-${pageId}-${id}`, listeners[name]);
      }
    } else {
      if (name.indexOf("uni-") === 0) {
        UniViewJSBridge.off(name, listeners[name]);
      } else if (id) {
        UniViewJSBridge.off(`uni-${name}-${pageId}-${id}`, listeners[name]);
      }
    }
  });
}
fxy060608's avatar
fxy060608 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
function entries(obj) {
  return Object.keys(obj).map((key) => [key, obj[key]]);
}
const DEFAULT_EXCLUDE_KEYS = ["class", "style"];
const LISTENER_PREFIX = /^on[A-Z]+/;
const useAttrs = (params = {}) => {
  const { excludeListeners = false, excludeKeys = [] } = params;
  const instance = getCurrentInstance();
  const attrs = shallowRef({});
  const listeners = shallowRef({});
  const excludeAttrs = shallowRef({});
  const allExcludeKeys = excludeKeys.concat(DEFAULT_EXCLUDE_KEYS);
  instance.attrs = reactive(instance.attrs);
  watchEffect(() => {
    const res = entries(instance.attrs).reduce((acc, [key, val]) => {
      if (allExcludeKeys.includes(key)) {
        acc.exclude[key] = val;
      } else if (LISTENER_PREFIX.test(key)) {
        if (!excludeListeners) {
          acc.attrs[key] = val;
        }
        acc.listeners[key] = val;
      } else {
        acc.attrs[key] = val;
      }
      return acc;
    }, {
      exclude: {},
      attrs: {},
      listeners: {}
    });
    attrs.value = res.attrs;
    listeners.value = res.listeners;
    excludeAttrs.value = res.exclude;
  });
  return { $attrs: attrs, $listeners: listeners, $excludeAttrs: excludeAttrs };
};
fxy060608's avatar
fxy060608 已提交
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 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
const buttonProps = {
  id: {
    type: String,
    default: ""
  },
  hoverClass: {
    type: String,
    default: "button-hover"
  },
  hoverStartTime: {
    type: [Number, String],
    default: 20
  },
  hoverStayTime: {
    type: [Number, String],
    default: 70
  },
  hoverStopPropagation: {
    type: Boolean,
    default: false
  },
  disabled: {
    type: [Boolean, String],
    default: false
  },
  formType: {
    type: String,
    default: ""
  },
  openType: {
    type: String,
    default: ""
  },
  loading: {
    type: [Boolean, String],
    default: false
  },
  plain: {
    type: [Boolean, String],
    default: false
  }
};
const buttonStyle = [{
  ub: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    position: "relative",
    paddingLeft: "5",
    paddingRight: "5",
    overflow: "hidden",
    color: "#000000",
    backgroundColor: "#f8f8f8",
    borderRadius: "5",
    borderStyle: "solid",
    borderWidth: "1",
    borderColor: "#dbdbdb"
  },
  "ub-t": {
    color: "#000000",
    fontSize: "18",
    textDecoration: "none",
    lineHeight: "46"
  },
  "ub-d": {
    backgroundColor: "#f8f8f8"
  },
  "ub-p": {
    backgroundColor: "#007aff",
    borderColor: "#0062cc"
  },
  "ub-w": {
    backgroundColor: "#e64340",
    borderColor: "#b83633"
  },
  "ub-d-t": {
    color: "#000000"
  },
  "ub-p-t": {
    color: "#ffffff"
  },
  "ub-w-t": {
    color: "#ffffff"
  },
  "ub-d-d": {
    backgroundColor: "#f7f7f7"
  },
  "ub-p-d": {
    backgroundColor: "#63acfc",
    borderColor: "#4f8aca"
  },
  "ub-w-d": {
    backgroundColor: "#ec8b89",
    borderColor: "#bd6f6e"
  },
  "ub-d-t-d": {
    color: "#cccccc"
  },
  "ub-p-t-d": {
    color: "rgba(255,255,255,0.6)"
  },
  "ub-w-t-d": {
    color: "rgba(255,255,255,0.6)"
  },
  "ub-d-plain": {
    borderColor: "#353535",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-p-plain": {
    borderColor: "#007aff",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-w-plain": {
    borderColor: "#e64340",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-d-t-plain": {
    color: "#353535"
  },
  "ub-p-t-plain": {
    color: "#007aff"
  },
  "ub-w-t-plain": {
    color: "#e64340"
  },
  "ub-d-d-plain": {
    borderColor: "#c6c6c6",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-p-d-plain": {
    borderColor: "#c6c6c6",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-w-d-plain": {
    borderColor: "#c6c6c6",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-d-t-d-plain": {
    color: "rgba(0,0,0,0.2)"
  },
  "ub-p-t-d-plain": {
    color: "rgba(0,0,0,0.2)"
  },
  "ub-w-t-d-plain": {
    color: "rgba(0,0,0,0.2)"
  },
  "ub-mini": {
    lineHeight: "30",
    fontSize: "13",
    paddingTop: 0,
    paddingRight: "17.5",
    paddingBottom: 0,
    paddingLeft: "17.5"
  },
  "ub-loading": {
    width: "18",
    height: "18",
    marginRight: "10"
  },
  "ub-d-loading": {
    color: "rgba(255,255,255,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-p-loading": {
    color: "rgba(255,255,255,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-w-loading": {
    color: "rgba(255,255,255,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-d-loading-plain": {
    color: "#353535"
  },
  "ub-p-loading-plain": {
    color: "#007aff",
    backgroundColor: "#0062cc"
  },
  "ub-w-loading-plain": {
    color: "#e64340",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-d-hover": {
    opacity: 0.8,
    backgroundColor: "#dedede"
  },
  "ub-p-hover": {
    opacity: 0.8,
    backgroundColor: "#0062cc"
  },
  "ub-w-hover": {
    opacity: 0.8,
    backgroundColor: "#ce3c39"
  },
  "ub-d-t-hover": {
    color: "rgba(0,0,0,0.6)"
  },
  "ub-p-t-hover": {
    color: "rgba(255,255,255,0.6)"
  },
  "ub-w-t-hover": {
    color: "rgba(255,255,255,0.6)"
  },
  "ub-d-hover-plain": {
    color: "rgba(53,53,53,0.6)",
    borderColor: "rgba(53,53,53,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-p-hover-plain": {
    color: "rgba(26,173,25,0.6)",
    borderColor: "rgba(0,122,255,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  },
  "ub-w-hover-plain": {
    color: "rgba(230,67,64,0.6)",
    borderColor: "rgba(230,67,64,0.6)",
    backgroundColor: "rgba(0,0,0,0)"
  }
}];
const TYPES = {
  default: "d",
  primary: "p",
  warn: "w"
};
var Button = defineComponent({
fxy060608's avatar
fxy060608 已提交
495
  inheritAttrs: false,
fxy060608's avatar
fxy060608 已提交
496 497 498 499 500 501 502 503 504 505 506 507
  name: "Button",
  props: extend(buttonProps, {
    type: {
      type: String,
      default: "default"
    },
    size: {
      type: String,
      default: "default"
    }
  }),
  styles: buttonStyle,
fxy060608's avatar
fxy060608 已提交
508
  setup(props, {
fxy060608's avatar
fxy060608 已提交
509 510 511
    slots,
    attrs
  }) {
fxy060608's avatar
fxy060608 已提交
512 513 514 515 516 517 518
    const {
      $attrs,
      $excludeAttrs,
      $listeners
    } = useAttrs({
      excludeListeners: true
    });
fxy060608's avatar
fxy060608 已提交
519
    const type = props.type;
fxy060608's avatar
fxy060608 已提交
520 521
    const rootRef = ref(null);
    const onClick = (e2, isLabelClick) => {
fxy060608's avatar
fxy060608 已提交
522 523
      const _onClick = $listeners.value.onClick || (() => {
      });
fxy060608's avatar
fxy060608 已提交
524
      if (props.disabled) {
fxy060608's avatar
fxy060608 已提交
525 526
        return;
      }
fxy060608's avatar
fxy060608 已提交
527
      _onClick(e2);
fxy060608's avatar
fxy060608 已提交
528 529 530
    };
    const _getClass = (t2) => {
      let cl = "ub-" + TYPES[type] + t2;
fxy060608's avatar
fxy060608 已提交
531 532 533
      props.disabled && (cl += "-d");
      props.plain && (cl += "-plain");
      props.size === "mini" && t2 === "-t" && (cl += " ub-mini");
fxy060608's avatar
fxy060608 已提交
534 535 536
      return cl;
    };
    const _getHoverClass = (t2) => {
fxy060608's avatar
fxy060608 已提交
537
      if (props.disabled) {
fxy060608's avatar
fxy060608 已提交
538 539 540
        return "";
      }
      let cl = "ub-" + TYPES[type] + t2 + "-hover";
fxy060608's avatar
fxy060608 已提交
541
      props.plain && (cl += "-plain");
fxy060608's avatar
fxy060608 已提交
542 543 544 545 546 547 548 549 550
      return cl;
    };
    const uniLabel = inject(uniLabelKey, false);
    if (uniLabel) {
      uniLabel.addHandler(onClick);
      onBeforeUnmount(() => {
        uniLabel.removeHandler(onClick);
      });
    }
fxy060608's avatar
fxy060608 已提交
551
    useListeners(props, {
fxy060608's avatar
fxy060608 已提交
552 553
      "label-click": onClick
    });
fxy060608's avatar
fxy060608 已提交
554 555 556 557 558 559 560 561 562
    const _listeners = computed(() => {
      const obj = {};
      for (const eventName in $listeners.value) {
        const event = $listeners.value[eventName];
        if (eventName !== "onClick")
          obj[eventName] = event;
      }
      return obj;
    });
fxy060608's avatar
fxy060608 已提交
563 564 565
    const wrapSlots = () => {
      if (!slots.default)
        return [];
fxy060608's avatar
fxy060608 已提交
566 567 568 569 570 571 572
      const vnodes = slots.default();
      if (vnodes.length === 1 && vnodes[0].type === Text) {
        return [createNVueTextVNode(vnodes[0].children, {
          class: "ub-t " + _getClass("-t")
        })];
      }
      return vnodes;
fxy060608's avatar
fxy060608 已提交
573 574
    };
    return () => {
fxy060608's avatar
fxy060608 已提交
575 576 577
      const _attrs = extend({}, useHoverClass(props), {
        hoverClass: _getHoverClass("")
      }, $attrs.value, $excludeAttrs.value, _listeners.value);
D
DCloud_LXH 已提交
578
      return createVNode("view", mergeProps({
fxy060608's avatar
fxy060608 已提交
579
        "ref": rootRef,
fxy060608's avatar
fxy060608 已提交
580
        "class": ["ub", _getClass("")],
fxy060608's avatar
fxy060608 已提交
581
        "onClick": onClick
fxy060608's avatar
fxy060608 已提交
582
      }, _attrs), [props.loading ? createVNode("loading-indicator", mergeProps({
fxy060608's avatar
fxy060608 已提交
583 584 585 586 587 588 589 590
        "class": ["ub-loading", `ub-${TYPES[type]}-loading`]
      }, {
        arrow: "false",
        animating: "true"
      }), null) : null, ...wrapSlots()]);
    };
  }
});
fxy060608's avatar
fxy060608 已提交
591
const movableAreaProps = {
fxy060608's avatar
fxy060608 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  scaleArea: {
    type: Boolean,
    default: false
  }
};
function flatVNode(nodes) {
  const array = [];
  if (Array.isArray(nodes)) {
    nodes.forEach((vnode) => {
      if (isVNode(vnode)) {
        if (vnode.type === Fragment) {
          array.push(...flatVNode(vnode.children));
        } else {
          array.push(vnode);
        }
      } else if (Array.isArray(vnode)) {
        array.push(...flatVNode(vnode));
      }
    });
  }
  return array;
}
const getComponentSize = (el) => {
  const dom = weex.requireModule("dom");
  return new Promise((resolve) => {
    dom.getComponentRect(el, ({ size }) => {
      resolve(size);
    });
  });
};
var MovableArea = defineComponent({
  name: "MovableArea",
fxy060608's avatar
fxy060608 已提交
624
  props: movableAreaProps,
fxy060608's avatar
fxy060608 已提交
625 626 627 628 629 630
  styles: [{
    "uni-movable-area": {
      width: "10px",
      height: "10px"
    }
  }],
fxy060608's avatar
fxy060608 已提交
631
  setup(props, {
fxy060608's avatar
fxy060608 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    slots
  }) {
    const width = ref(0);
    const height = ref(0);
    const top = ref(0);
    const left = ref(0);
    const _isMounted = ref(false);
    const rootRef = ref(null);
    const originMovableViewContexts = [];
    let touchMovableView = null;
    const setTouchMovableViewContext = (movableview) => {
      touchMovableView = movableview;
    };
    const _getWH = () => {
      return getComponentSize(rootRef.value).then(({
        width: _width,
        height: _height,
        top: _top,
        left: _left
      }) => {
        width.value = _width;
        height.value = _height;
        top.value = _top;
        left.value = _left;
      });
    };
    const _resize = () => {
      _getWH().then(() => {
        originMovableViewContexts.forEach(function(item) {
          item.setParent();
        });
      });
    };
    onMounted(() => {
      setTimeout(() => {
        _isMounted.value = true;
        _resize();
      }, 200);
    });
    const listeners = {
      onPanstart(e2) {
        touchMovableView && touchMovableView.touchstart(e2);
      },
      onPanmove(e2) {
        e2.stopPropagation();
        touchMovableView && touchMovableView.touchmove(e2);
      },
      onPanend(e2) {
        touchMovableView && touchMovableView.touchend(e2);
      }
    };
    const addMovableViewContext = (movableViewContext) => {
      originMovableViewContexts.push(movableViewContext);
    };
    const removeMovableViewContext = (movableViewContext) => {
      const index = originMovableViewContexts.indexOf(movableViewContext);
      if (index >= 0) {
        originMovableViewContexts.splice(index, 1);
      }
    };
    provide("_isMounted", _isMounted);
    provide("parentSize", {
      width,
      height,
      top,
      left
    });
    provide("addMovableViewContext", addMovableViewContext);
    provide("removeMovableViewContext", removeMovableViewContext);
    provide("setTouchMovableViewContext", setTouchMovableViewContext);
    return () => {
      const defaultSlots = slots.default && slots.default();
      const movableViewItems = flatVNode(defaultSlots);
fxy060608's avatar
fxy060608 已提交
705 706
      return createVNode("div", mergeProps({
        "ref": rootRef,
fxy060608's avatar
fxy060608 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
        "class": "uni-movable-area"
      }, listeners), [movableViewItems]);
    };
  }
});
const __event = {};
function callback(type, $event) {
  if (__event[type]) {
    __event[type]($event);
  }
}
function addListener(type, callback2) {
  __event[type] = function($event) {
    if (typeof callback2 === "function") {
      $event.touches = $event.changedTouches;
      if (callback2($event) === false) {
        $event.stopPropagation();
      }
    }
  };
}
function touchstart($event) {
  callback("touchstart", $event);
}
function touchmove($event) {
  callback("touchmove", $event);
}
function touchend($event) {
  callback("touchend", $event);
}
function useTouchtrack(method) {
  let x0 = 0;
  let y0 = 0;
  let x1 = 0;
  let y1 = 0;
  const fn = function($event, state, x, y) {
    if (method({
      target: $event.target,
      currentTarget: $event.currentTarget,
      stopPropagation: $event.stopPropagation.bind($event),
      touches: $event.touches,
      changedTouches: $event.changedTouches,
      detail: {
        state,
        x,
        y,
        dx: x - x0,
        dy: y - y0,
        ddx: x - x1,
        ddy: y - y1,
        timeStamp: $event.timeStamp || Date.now()
      }
    }) === false) {
      return false;
    }
  };
  let $eventOld = null;
  addListener("touchstart", function($event) {
    if (!$eventOld) {
      $eventOld = $event;
      x0 = x1 = $event.touches[0].pageX;
      y0 = y1 = $event.touches[0].pageY;
      return fn($event, "start", x0, y0);
    }
  });
  addListener("touchmove", function($event) {
    if ($eventOld) {
      const res = fn($event, "move", $event.touches[0].pageX, $event.touches[0].pageY);
      x1 = $event.touches[0].pageX;
      y1 = $event.touches[0].pageY;
      return res;
    }
  });
  addListener("touchend", function($event) {
    if ($eventOld) {
      $eventOld = null;
      return fn($event, "end", $event.changedTouches[0].pageX, $event.changedTouches[0].pageY);
    }
  });
}
function useCustomEvent(ref2, emit) {
  return (name, detail) => {
    if (ref2.value) {
      emit(name, normalizeCustomEvent(name, ref2.value, detail || {}));
    }
  };
}
function normalizeCustomEvent(name, target, detail = {}) {
  target = processTarget(target);
  return {
    type: name,
    timeStamp: Date.now(),
    target,
    currentTarget: target,
    detail
  };
}
const firstLetterToLowerCase = cacheStringFunction((str) => {
  return str.charAt(0).toLowerCase() + str.slice(1);
});
function processTarget(weexTarget) {
  const { offsetLeft, offsetTop } = weexTarget;
  const attr = weexTarget.attr;
  const dataset = {};
  Object.keys(attr || {}).forEach((key) => {
    if (key.indexOf("data") === 0) {
      dataset[firstLetterToLowerCase(key.replace("data", ""))] = attr[key];
    }
  });
  return {
    id: attr && attr.id || "",
    dataset,
    offsetLeft: offsetLeft || 0,
    offsetTop: offsetTop || 0
  };
}
function e(e2, t2, n) {
  return e2 > t2 - n && e2 < t2 + n;
}
function t(t2, n) {
  return e(t2, 0, n);
}
function Decline() {
}
Decline.prototype.x = function(e2) {
  return Math.sqrt(e2);
};
function Friction(e2, t2) {
  this._m = e2;
  this._f = 1e3 * t2;
  this._startTime = 0;
  this._v = 0;
}
Friction.prototype.setV = function(x, y) {
  const n = Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5);
  this._x_v = x;
  this._y_v = y;
  this._x_a = -this._f * this._x_v / n;
  this._y_a = -this._f * this._y_v / n;
  this._t = Math.abs(x / this._x_a) || Math.abs(y / this._y_a);
  this._lastDt = null;
  this._startTime = new Date().getTime();
};
Friction.prototype.setS = function(x, y) {
  this._x_s = x;
  this._y_s = y;
};
Friction.prototype.s = function(t2) {
  if (t2 === void 0) {
    t2 = (new Date().getTime() - this._startTime) / 1e3;
  }
  if (t2 > this._t) {
    t2 = this._t;
    this._lastDt = t2;
  }
  let x = this._x_v * t2 + 0.5 * this._x_a * Math.pow(t2, 2) + this._x_s;
  let y = this._y_v * t2 + 0.5 * this._y_a * Math.pow(t2, 2) + this._y_s;
  if (this._x_a > 0 && x < this._endPositionX || this._x_a < 0 && x > this._endPositionX) {
    x = this._endPositionX;
  }
  if (this._y_a > 0 && y < this._endPositionY || this._y_a < 0 && y > this._endPositionY) {
    y = this._endPositionY;
  }
  return {
    x,
    y
  };
};
Friction.prototype.ds = function(t2) {
  if (t2 === void 0) {
    t2 = (new Date().getTime() - this._startTime) / 1e3;
  }
  if (t2 > this._t) {
    t2 = this._t;
  }
  return {
    dx: this._x_v + this._x_a * t2,
    dy: this._y_v + this._y_a * t2
  };
};
Friction.prototype.delta = function() {
  return {
    x: -1.5 * Math.pow(this._x_v, 2) / this._x_a || 0,
    y: -1.5 * Math.pow(this._y_v, 2) / this._y_a || 0
  };
};
Friction.prototype.dt = function() {
  return -this._x_v / this._x_a;
};
Friction.prototype.done = function() {
  const t2 = e(this.s().x, this._endPositionX) || e(this.s().y, this._endPositionY) || this._lastDt === this._t;
  this._lastDt = null;
  return t2;
};
Friction.prototype.setEnd = function(x, y) {
  this._endPositionX = x;
  this._endPositionY = y;
};
Friction.prototype.reconfigure = function(m, f) {
  this._m = m;
  this._f = 1e3 * f;
};
function Spring(m, k, c) {
  this._m = m;
  this._k = k;
  this._c = c;
  this._solution = null;
  this._endPosition = 0;
  this._startTime = 0;
}
Spring.prototype._solve = function(e2, t2) {
  const n = this._c;
  const i = this._m;
  const r = this._k;
  const o = n * n - 4 * i * r;
  if (o === 0) {
    const a = -n / (2 * i);
    const s = e2;
    const l = t2 / (a * e2);
    return {
      x: function(e3) {
        return (s + l * e3) * Math.pow(Math.E, a * e3);
      },
      dx: function(e3) {
        const t3 = Math.pow(Math.E, a * e3);
        return a * (s + l * e3) * t3 + l * t3;
      }
    };
  }
  if (o > 0) {
    const c = (-n - Math.sqrt(o)) / (2 * i);
    const u = (-n + Math.sqrt(o)) / (2 * i);
    const d = (t2 - c * e2) / (u - c);
    const h = e2 - d;
    return {
      x: function(e3) {
        let t3;
        let n2;
        if (e3 === this._t) {
          t3 = this._powER1T;
          n2 = this._powER2T;
        }
        this._t = e3;
        if (!t3) {
          t3 = this._powER1T = Math.pow(Math.E, c * e3);
        }
        if (!n2) {
          n2 = this._powER2T = Math.pow(Math.E, u * e3);
        }
        return h * t3 + d * n2;
      },
      dx: function(e3) {
        let t3;
        let n2;
        if (e3 === this._t) {
          t3 = this._powER1T;
          n2 = this._powER2T;
        }
        this._t = e3;
        if (!t3) {
          t3 = this._powER1T = Math.pow(Math.E, c * e3);
        }
        if (!n2) {
          n2 = this._powER2T = Math.pow(Math.E, u * e3);
        }
        return h * c * t3 + d * u * n2;
      }
    };
  }
  const p = Math.sqrt(4 * i * r - n * n) / (2 * i);
  const f = -n / 2 * i;
  const v2 = e2;
  const g2 = (t2 - f * e2) / p;
  return {
    x: function(e3) {
      return Math.pow(Math.E, f * e3) * (v2 * Math.cos(p * e3) + g2 * Math.sin(p * e3));
    },
    dx: function(e3) {
      const t3 = Math.pow(Math.E, f * e3);
      const n2 = Math.cos(p * e3);
      const i2 = Math.sin(p * e3);
      return t3 * (g2 * p * n2 - v2 * p * i2) + f * t3 * (g2 * i2 + v2 * n2);
    }
  };
};
Spring.prototype.x = function(e2) {
  if (e2 === void 0) {
    e2 = (new Date().getTime() - this._startTime) / 1e3;
  }
  return this._solution ? this._endPosition + this._solution.x(e2) : 0;
};
Spring.prototype.dx = function(e2) {
  if (e2 === void 0) {
    e2 = (new Date().getTime() - this._startTime) / 1e3;
  }
  return this._solution ? this._solution.dx(e2) : 0;
};
Spring.prototype.setEnd = function(e2, n, i) {
  if (!i) {
    i = new Date().getTime();
  }
  if (e2 !== this._endPosition || !t(n, 0.1)) {
    n = n || 0;
    let r = this._endPosition;
    if (this._solution) {
      if (t(n, 0.1)) {
        n = this._solution.dx((i - this._startTime) / 1e3);
      }
      r = this._solution.x((i - this._startTime) / 1e3);
      if (t(n, 0.1)) {
        n = 0;
      }
      if (t(r, 0.1)) {
        r = 0;
      }
      r += this._endPosition;
    }
    if (!(this._solution && t(r - e2, 0.1) && t(n, 0.1))) {
      this._endPosition = e2;
      this._solution = this._solve(r - this._endPosition, n);
      this._startTime = i;
    }
  }
};
Spring.prototype.snap = function(e2) {
  this._startTime = new Date().getTime();
  this._endPosition = e2;
  this._solution = {
    x: function() {
      return 0;
    },
    dx: function() {
      return 0;
    }
  };
};
Spring.prototype.done = function(n) {
  if (!n) {
    n = new Date().getTime();
  }
  return e(this.x(), this._endPosition, 0.1) && t(this.dx(), 0.1);
};
Spring.prototype.reconfigure = function(m, t2, c) {
  this._m = m;
  this._k = t2;
  this._c = c;
  if (!this.done()) {
    this._solution = this._solve(this.x() - this._endPosition, this.dx());
    this._startTime = new Date().getTime();
  }
};
Spring.prototype.springConstant = function() {
  return this._k;
};
Spring.prototype.damping = function() {
  return this._c;
};
Spring.prototype.configuration = function() {
  function e2(e3, t3) {
    e3.reconfigure(1, t3, e3.damping());
  }
  function t2(e3, t3) {
    e3.reconfigure(1, e3.springConstant(), t3);
  }
  return [
    {
      label: "Spring Constant",
      read: this.springConstant.bind(this),
      write: e2.bind(this, this),
      min: 100,
      max: 1e3
    },
    {
      label: "Damping",
      read: this.damping.bind(this),
      write: t2.bind(this, this),
      min: 1,
      max: 500
    }
  ];
};
function STD(e2, t2, n) {
  this._springX = new Spring(e2, t2, n);
  this._springY = new Spring(e2, t2, n);
  this._springScale = new Spring(e2, t2, n);
  this._startTime = 0;
}
STD.prototype.setEnd = function(e2, t2, n, i) {
  const r = new Date().getTime();
  this._springX.setEnd(e2, i, r);
  this._springY.setEnd(t2, i, r);
  this._springScale.setEnd(n, i, r);
  this._startTime = r;
};
STD.prototype.x = function() {
  const e2 = (new Date().getTime() - this._startTime) / 1e3;
  return {
    x: this._springX.x(e2),
    y: this._springY.x(e2),
    scale: this._springScale.x(e2)
  };
};
STD.prototype.done = function() {
  const e2 = new Date().getTime();
  return this._springX.done(e2) && this._springY.done(e2) && this._springScale.done(e2);
};
STD.prototype.reconfigure = function(e2, t2, n) {
  this._springX.reconfigure(e2, t2, n);
  this._springY.reconfigure(e2, t2, n);
  this._springScale.reconfigure(e2, t2, n);
};
fxy060608's avatar
fxy060608 已提交
1118
const movableViewProps = {
fxy060608's avatar
fxy060608 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
  direction: {
    type: String,
    default: "none"
  },
  inertia: {
    type: [Boolean, String],
    default: false
  },
  outOfBounds: {
    type: [Boolean, String],
    default: false
  },
  x: {
    type: [Number, String],
    default: 0
  },
  y: {
    type: [Number, String],
    default: 0
  },
  damping: {
    type: [Number, String],
    default: 20
  },
  friction: {
    type: [Number, String],
    default: 2
  },
  disabled: {
    type: [Boolean, String],
    default: false
  },
  scale: {
    type: [Boolean, String],
    default: false
  },
  scaleMin: {
    type: [Number, String],
    default: 0.5
  },
  scaleMax: {
    type: [Number, String],
    default: 10
  },
  scaleValue: {
    type: [Number, String],
    default: 1
  },
  animation: {
    type: [Boolean, String],
    default: true
  }
};
function v(a, b) {
  return +((1e3 * a - 1e3 * b) / 1e3).toFixed(1);
}
function g(friction, execute, endCallback) {
  let record = {
    id: 0,
    cancelled: false
  };
  let cancel = function(record2) {
    if (record2 && record2.id) {
      cancelAnimationFrame(record2.id);
    }
    if (record2) {
      record2.cancelled = true;
    }
  };
  function fn(record2, friction2, execute2, endCallback2) {
    if (!record2 || !record2.cancelled) {
      execute2(friction2);
      let isDone = friction2.done();
      if (!isDone) {
        if (!record2.cancelled) {
          record2.id = requestAnimationFrame(fn.bind(null, record2, friction2, execute2, endCallback2));
        }
      }
      if (isDone && endCallback2) {
        endCallback2(friction2);
      }
    }
  }
  fn(record, friction, execute, endCallback);
  return {
    cancel: cancel.bind(null, record),
    model: friction
  };
}
let requesting = false;
function _requestAnimationFrame(e2) {
  if (!requesting) {
    requesting = true;
    requestAnimationFrame(function() {
      e2();
      requesting = false;
    });
  }
}
function requestAnimationFrame(callback2) {
  return setTimeout(callback2, 16);
}
function cancelAnimationFrame(id) {
  clearTimeout(id);
}
const animation = weex.requireModule("animation");
var MovableView = defineComponent({
  name: "MovableView",
fxy060608's avatar
fxy060608 已提交
1227
  props: movableViewProps,
fxy060608's avatar
fxy060608 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  emits: ["change", "scale"],
  styles: [{
    "uni-movable-view": {
      position: "absolute",
      top: "0px",
      left: "0px",
      width: "10px",
      height: "10px"
    }
  }],
fxy060608's avatar
fxy060608 已提交
1238
  setup(props, {
fxy060608's avatar
fxy060608 已提交
1239 1240 1241 1242 1243 1244 1245
    emit,
    slots
  }) {
    const rootRef = ref(null);
    const trigger = useCustomEvent(rootRef, emit);
    const setTouchMovableViewContext = inject("setTouchMovableViewContext", () => {
    });
fxy060608's avatar
fxy060608 已提交
1246
    useMovableViewState(props, trigger, rootRef);
fxy060608's avatar
fxy060608 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    const touchStart = () => {
      setTouchMovableViewContext({
        touchstart,
        touchmove,
        touchend
      });
    };
    return () => {
      const attrs = {
        preventGesture: true
      };
fxy060608's avatar
fxy060608 已提交
1258
      return createVNode("view", mergeProps({
fxy060608's avatar
fxy060608 已提交
1259 1260 1261 1262 1263 1264 1265 1266
        "ref": rootRef,
        "onTouchstart": touchStart,
        "class": "uni-movable-view",
        "style": "transform-origin: center;"
      }, attrs), [slots.default && slots.default()]);
    };
  }
});
fxy060608's avatar
fxy060608 已提交
1267
function useMovableViewState(props, trigger, rootRef) {
fxy060608's avatar
fxy060608 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
  const _isMounted = inject("_isMounted", ref(false));
  const parentSize = inject("parentSize", {
    width: ref(0),
    height: ref(0),
    top: ref(0),
    left: ref(0)
  });
  const addMovableViewContext = inject("addMovableViewContext", () => {
  });
  const removeMovableViewContext = inject("removeMovableViewContext", () => {
  });
  function _getPx(val) {
    return Number(val) || 0;
  }
  function _getScaleNumber(val) {
    val = Number(val);
    return isNaN(val) ? 1 : val;
  }
fxy060608's avatar
fxy060608 已提交
1286 1287 1288
  const xSync = ref(_getPx(props.x));
  const ySync = ref(_getPx(props.y));
  const scaleValueSync = ref(_getScaleNumber(Number(props.scaleValue)));
fxy060608's avatar
fxy060608 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  const width = ref(0);
  const height = ref(0);
  const minX = ref(0);
  const minY = ref(0);
  const maxX = ref(0);
  const maxY = ref(0);
  let _SFA = null;
  let _FA = null;
  const _offset = {
    x: 0,
    y: 0
  };
  const _scaleOffset = {
    x: 0,
    y: 0
  };
  let _scale = 1;
  let _translateX = 0;
  let _translateY = 0;
  let _isTouching = false;
  let __baseX;
  let __baseY;
  let _checkCanMove = null;
  let _firstMoveDirection = null;
  let _rect = {
    top: 0,
    left: 0,
    width: 0,
    height: 0
  };
  const _declineX = new Decline();
  const _declineY = new Decline();
  const __touchInfo = {
    historyX: [0, 0],
    historyY: [0, 0],
    historyT: [0, 0]
  };
  const dampingNumber = computed(() => {
fxy060608's avatar
fxy060608 已提交
1327
    let val = Number(props.damping);
fxy060608's avatar
fxy060608 已提交
1328 1329 1330
    return isNaN(val) ? 20 : val;
  });
  const frictionNumber = computed(() => {
fxy060608's avatar
fxy060608 已提交
1331
    let val = Number(props.friction);
fxy060608's avatar
fxy060608 已提交
1332 1333 1334
    return isNaN(val) || val <= 0 ? 2 : val;
  });
  const scaleMinNumber = computed(() => {
fxy060608's avatar
fxy060608 已提交
1335
    let val = Number(props.scaleMin);
fxy060608's avatar
fxy060608 已提交
1336 1337 1338
    return isNaN(val) ? 0.5 : val;
  });
  const scaleMaxNumber = computed(() => {
fxy060608's avatar
fxy060608 已提交
1339
    let val = Number(props.scaleMax);
fxy060608's avatar
fxy060608 已提交
1340 1341
    return isNaN(val) ? 10 : val;
  });
fxy060608's avatar
fxy060608 已提交
1342 1343
  const xMove = computed(() => props.direction === "all" || props.direction === "horizontal");
  const yMove = computed(() => props.direction === "all" || props.direction === "vertical");
fxy060608's avatar
fxy060608 已提交
1344 1345
  const _STD = new STD(1, 9 * Math.pow(dampingNumber.value, 2) / 40, dampingNumber.value);
  const _friction = new Friction(1, frictionNumber.value);
fxy060608's avatar
fxy060608 已提交
1346
  watch(() => props.x, (val) => {
fxy060608's avatar
fxy060608 已提交
1347 1348
    xSync.value = _getPx(val);
  });
fxy060608's avatar
fxy060608 已提交
1349
  watch(() => props.y, (val) => {
fxy060608's avatar
fxy060608 已提交
1350 1351
    ySync.value = _getPx(val);
  });
fxy060608's avatar
fxy060608 已提交
1352
  watch(() => props.scaleValue, (val) => {
fxy060608's avatar
fxy060608 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    scaleValueSync.value = _getScaleNumber(Number(val));
  });
  watch(xSync, _setX);
  watch(ySync, _setY);
  watch(scaleValueSync, _setScaleValue);
  watch(scaleMinNumber, _setScaleMinOrMax);
  watch(scaleMaxNumber, _setScaleMinOrMax);
  function FAandSFACancel() {
    if (_FA) {
      _FA.cancel();
    }
    if (_SFA) {
      _SFA.cancel();
    }
  }
  function _setX(val) {
    if (xMove.value) {
      if (val + _scaleOffset.x === _translateX) {
        return _translateX;
      } else {
        if (_SFA) {
          _SFA.cancel();
        }
        _animationTo(val + _scaleOffset.x, ySync.value + _scaleOffset.y, _scale);
      }
    }
    return val;
  }
  function _setY(val) {
    if (yMove.value) {
      if (val + _scaleOffset.y === _translateY) {
        return _translateY;
      } else {
        if (_SFA) {
          _SFA.cancel();
        }
        _animationTo(xSync.value + _scaleOffset.x, val + _scaleOffset.y, _scale);
      }
    }
    return val;
  }
  function _setScaleMinOrMax() {
fxy060608's avatar
fxy060608 已提交
1395
    if (!props.scale) {
fxy060608's avatar
fxy060608 已提交
1396 1397 1398 1399 1400
      return false;
    }
    _updateScale(_scale, true);
  }
  function _setScaleValue(scale) {
fxy060608's avatar
fxy060608 已提交
1401
    if (!props.scale) {
fxy060608's avatar
fxy060608 已提交
1402 1403 1404 1405 1406 1407 1408 1409
      return false;
    }
    scale = _adjustScale(scale);
    _updateScale(scale, true);
    return scale;
  }
  function __handleTouchStart() {
    {
fxy060608's avatar
fxy060608 已提交
1410
      if (!props.disabled) {
fxy060608's avatar
fxy060608 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
        FAandSFACancel();
        __touchInfo.historyX = [0, 0];
        __touchInfo.historyY = [0, 0];
        __touchInfo.historyT = [0, 0];
        if (xMove.value) {
          __baseX = _translateX;
        }
        if (yMove.value) {
          __baseY = _translateY;
        }
        _checkCanMove = null;
        _firstMoveDirection = null;
        _isTouching = true;
      }
    }
  }
  function __handleTouchMove(event) {
fxy060608's avatar
fxy060608 已提交
1428
    if (!props.disabled && _isTouching) {
fxy060608's avatar
fxy060608 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
      let x = _translateX;
      let y = _translateY;
      if (_firstMoveDirection === null) {
        _firstMoveDirection = Math.abs(event.detail.dx / event.detail.dy) > 1 ? "htouchmove" : "vtouchmove";
      }
      if (xMove.value) {
        x = event.detail.dx + __baseX;
        __touchInfo.historyX.shift();
        __touchInfo.historyX.push(x);
        if (!yMove.value && _checkCanMove === null) {
          _checkCanMove = Math.abs(event.detail.dx / event.detail.dy) < 1;
        }
      }
      if (yMove.value) {
        y = event.detail.dy + __baseY;
        __touchInfo.historyY.shift();
        __touchInfo.historyY.push(y);
        if (!xMove.value && _checkCanMove === null) {
          _checkCanMove = Math.abs(event.detail.dy / event.detail.dx) < 1;
        }
      }
      __touchInfo.historyT.shift();
      __touchInfo.historyT.push(event.detail.timeStamp);
      if (!_checkCanMove) {
        let source = "touch";
        if (x < minX.value) {
fxy060608's avatar
fxy060608 已提交
1455
          if (props.outOfBounds) {
fxy060608's avatar
fxy060608 已提交
1456 1457 1458 1459 1460 1461
            source = "touch-out-of-bounds";
            x = minX.value - _declineX.x(minX.value - x);
          } else {
            x = minX.value;
          }
        } else if (x > maxX.value) {
fxy060608's avatar
fxy060608 已提交
1462
          if (props.outOfBounds) {
fxy060608's avatar
fxy060608 已提交
1463 1464 1465 1466 1467 1468 1469
            source = "touch-out-of-bounds";
            x = maxX.value + _declineX.x(x - maxX.value);
          } else {
            x = maxX.value;
          }
        }
        if (y < minY.value) {
fxy060608's avatar
fxy060608 已提交
1470
          if (props.outOfBounds) {
fxy060608's avatar
fxy060608 已提交
1471 1472 1473 1474 1475 1476 1477
            source = "touch-out-of-bounds";
            y = minY.value - _declineY.x(minY.value - y);
          } else {
            y = minY.value;
          }
        } else {
          if (y > maxY.value) {
fxy060608's avatar
fxy060608 已提交
1478
            if (props.outOfBounds) {
fxy060608's avatar
fxy060608 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
              source = "touch-out-of-bounds";
              y = maxY.value + _declineY.x(y - maxY.value);
            } else {
              y = maxY.value;
            }
          }
        }
        _requestAnimationFrame(function() {
          _setTransform(x, y, _scale, source);
        });
      }
    }
  }
  function __handleTouchEnd() {
fxy060608's avatar
fxy060608 已提交
1493
    if (!props.disabled && _isTouching) {
fxy060608's avatar
fxy060608 已提交
1494
      _isTouching = false;
fxy060608's avatar
fxy060608 已提交
1495
      if (!_checkCanMove && !_revise("out-of-bounds") && props.inertia) {
fxy060608's avatar
fxy060608 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
        const xv = 1e3 * (__touchInfo.historyX[1] - __touchInfo.historyX[0]) / (__touchInfo.historyT[1] - __touchInfo.historyT[0]);
        const yv = 1e3 * (__touchInfo.historyY[1] - __touchInfo.historyY[0]) / (__touchInfo.historyT[1] - __touchInfo.historyT[0]);
        _friction.setV(xv, yv);
        _friction.setS(_translateX, _translateY);
        const x0 = _friction.delta().x;
        const y0 = _friction.delta().y;
        let x = x0 + _translateX;
        let y = y0 + _translateY;
        if (x < minX.value) {
          x = minX.value;
          y = _translateY + (minX.value - _translateX) * y0 / x0;
        } else {
          if (x > maxX.value) {
            x = maxX.value;
            y = _translateY + (maxX.value - _translateX) * y0 / x0;
          }
        }
        if (y < minY.value) {
          y = minY.value;
          x = _translateX + (minY.value - _translateY) * x0 / y0;
        } else {
          if (y > maxY.value) {
            y = maxY.value;
            x = _translateX + (maxY.value - _translateY) * x0 / y0;
          }
        }
        _friction.setEnd(x, y);
        _FA = g(_friction, function() {
          let t2 = _friction.s();
          let x2 = t2.x;
          let y2 = t2.y;
          _setTransform(x2, y2, _scale, "friction");
        }, function() {
          _FA.cancel();
        });
      }
    }
  }
  function _getLimitXY(x, y) {
    let outOfBounds = false;
    if (x > maxX.value) {
      x = maxX.value;
      outOfBounds = true;
    } else {
      if (x < minX.value) {
        x = minX.value;
        outOfBounds = true;
      }
    }
    if (y > maxY.value) {
      y = maxY.value;
      outOfBounds = true;
    } else {
      if (y < minY.value) {
        y = minY.value;
        outOfBounds = true;
      }
    }
    return {
      x,
      y,
      outOfBounds
    };
  }
  function _updateOffset() {
    _offset.x = _rect.left - parentSize.left.value;
    _offset.y = _rect.top - parentSize.top.value;
  }
  function _updateWH(scale) {
    scale = scale || _scale;
    scale = _adjustScale(scale);
    height.value = _rect.height / _scale;
    width.value = _rect.width / _scale;
    let _height = height.value * scale;
    let _width = width.value * scale;
    _scaleOffset.x = (_width - width.value) / 2;
    _scaleOffset.y = (_height - height.value) / 2;
  }
  function _updateBoundary() {
    let x = 0 - _offset.x + _scaleOffset.x;
    let _width = parentSize.width.value - width.value - _offset.x - _scaleOffset.x;
    minX.value = Math.min(x, _width);
    maxX.value = Math.max(x, _width);
    let y = 0 - _offset.y + _scaleOffset.y;
    let _height = parentSize.height.value - height.value - _offset.y - _scaleOffset.y;
    minY.value = Math.min(y, _height);
    maxY.value = Math.max(y, _height);
  }
  function _updateScale(scale, animat) {
fxy060608's avatar
fxy060608 已提交
1585
    if (props.scale) {
fxy060608's avatar
fxy060608 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      scale = _adjustScale(scale);
      _updateWH(scale);
      _updateBoundary();
      const limitXY = _getLimitXY(_translateX, _translateY);
      const x = limitXY.x;
      const y = limitXY.y;
      if (animat) {
        _animationTo(x, y, scale, "", true, true);
      } else {
        _requestAnimationFrame(function() {
          _setTransform(x, y, scale, "", true, true);
        });
      }
    }
  }
  function _adjustScale(scale) {
    scale = Math.max(0.5, scaleMinNumber.value, scale);
    scale = Math.min(10, scaleMaxNumber.value, scale);
    return scale;
  }
  function _animationTo(x, y, scale, source, r, o) {
    FAandSFACancel();
    if (!xMove.value) {
      x = _translateX;
    }
    if (!yMove.value) {
      y = _translateY;
    }
fxy060608's avatar
fxy060608 已提交
1614
    if (!props.scale) {
fxy060608's avatar
fxy060608 已提交
1615 1616 1617 1618 1619
      scale = _scale;
    }
    let limitXY = _getLimitXY(x, y);
    x = limitXY.x;
    y = limitXY.y;
fxy060608's avatar
fxy060608 已提交
1620
    if (!props.animation) {
fxy060608's avatar
fxy060608 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
      _setTransform(x, y, scale, source, r, o);
      return;
    }
    _STD._springX._solution = null;
    _STD._springY._solution = null;
    _STD._springScale._solution = null;
    _STD._springX._endPosition = _translateX;
    _STD._springY._endPosition = _translateY;
    _STD._springScale._endPosition = _scale;
    _STD.setEnd(x, y, scale, 1);
    _SFA = g(_STD, function() {
      let data = _STD.x();
      let x2 = data.x;
      let y2 = data.y;
      let scale2 = data.scale;
      _setTransform(x2, y2, scale2, source, r, o);
    }, function() {
      _SFA.cancel();
    });
  }
  function _revise(source) {
    let limitXY = _getLimitXY(_translateX, _translateY);
    let x = limitXY.x;
    let y = limitXY.y;
    let outOfBounds = limitXY.outOfBounds;
    if (outOfBounds) {
      _animationTo(x, y, _scale, source);
    }
    return outOfBounds;
  }
  function _setTransform(x, y, scale, source = "", r, o) {
    if (!(x !== null && x.toString() !== "NaN" && typeof x === "number")) {
      x = _translateX || 0;
    }
    if (!(y !== null && y.toString() !== "NaN" && typeof y === "number")) {
      y = _translateY || 0;
    }
    x = Number(x.toFixed(1));
    y = Number(y.toFixed(1));
    scale = Number(scale.toFixed(1));
    if (!(_translateX === x && _translateY === y)) {
      if (!r) {
        trigger("change", {
          x: v(x, _scaleOffset.x),
          y: v(y, _scaleOffset.y),
          source
        });
      }
    }
fxy060608's avatar
fxy060608 已提交
1670
    if (!props.scale) {
fxy060608's avatar
fxy060608 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
      scale = _scale;
    }
    scale = _adjustScale(scale);
    scale = +scale.toFixed(3);
    if (o && scale !== _scale) {
      trigger("scale", {
        x,
        y,
        scale
      });
    }
    const transform = `translate(${x}px, ${y}px) scale(${scale})`;
    animation.transition(rootRef.value, {
      styles: {
        transform
      },
      duration: 0,
      delay: 0
    });
    _translateX = x;
    _translateY = y;
    _scale = scale;
  }
  function _updateRect() {
    return getComponentSize(rootRef.value).then((rect) => {
      _rect = rect;
    });
  }
  function setParent() {
    if (!_isMounted.value) {
      return;
    }
    FAandSFACancel();
fxy060608's avatar
fxy060608 已提交
1704
    let scale = props.scale ? scaleValueSync.value : 1;
fxy060608's avatar
fxy060608 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
    _updateOffset();
    _updateWH(scale);
    _updateBoundary();
    _translateX = xSync.value + _scaleOffset.x;
    _translateY = ySync.value + _scaleOffset.y;
    let limitXY = _getLimitXY(_translateX, _translateY);
    let x = limitXY.x;
    let y = limitXY.y;
    _setTransform(x, y, scale, "", true);
  }
  onMounted(() => {
    useTouchtrack((event) => {
      switch (event.detail.state) {
        case "start":
          __handleTouchStart();
          break;
        case "move":
          __handleTouchMove(event);
          break;
        case "end":
          __handleTouchEnd();
      }
    });
    setTimeout(() => {
      _updateRect().then(() => {
        setParent();
      });
    }, 100);
    _friction.reconfigure(1, frictionNumber.value);
    _STD.reconfigure(1, 9 * Math.pow(dampingNumber.value, 2) / 40, dampingNumber.value);
    const context = {
      setParent
    };
    addMovableViewContext(context);
    onUnmounted(() => {
      removeMovableViewContext(context);
    });
  });
  onUnmounted(() => {
    FAandSFACancel();
  });
}
fxy060608's avatar
fxy060608 已提交
1747
var components = {
fxy060608's avatar
fxy060608 已提交
1748 1749 1750 1751 1752
  Navigator,
  Label,
  Button,
  MovableArea,
  MovableView
fxy060608's avatar
fxy060608 已提交
1753 1754
};
export { components as default };