Blame view

src/directives/transfer-dom.js 2.97 KB
713bd3d7   梁灏   fixed #583
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  // Thanks to: https://github.com/airyland/vux/blob/v2/src/directives/transfer-dom/index.js
  // Thanks to: https://github.com/calebroseland/vue-dom-portal
  
  /**
   * Get target DOM Node
   * @param {(Node|string|Boolean)} [node=document.body] DOM Node, CSS selector, or Boolean
   * @return {Node} The target that the el will be appended to
   */
  function getTarget (node) {
      if (node === void 0) {
          node = document.body
      }
      if (node === true) { return document.body }
      return node instanceof window.Node ? node : document.querySelector(node)
  }
  
  const directive = {
548eac43   梁灏   fixed #1387 and u...
18
19
      inserted (el, { value }, vnode) {
          if (el.dataset.transfer !== 'true') return false;
713bd3d7   梁灏   fixed #583
20
21
          el.className = el.className ? el.className + ' v-transfer-dom' : 'v-transfer-dom';
          const parentNode = el.parentNode;
dce5a3ea   Aresn   update Modal
22
          if (!parentNode) return;
713bd3d7   梁灏   fixed #583
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
          const home = document.createComment('');
          let hasMovedOut = false;
  
          if (value !== false) {
              parentNode.replaceChild(home, el); // moving out, el is no longer in the document
              getTarget(value).appendChild(el); // moving into new place
              hasMovedOut = true
          }
          if (!el.__transferDomData) {
              el.__transferDomData = {
                  parentNode: parentNode,
                  home: home,
                  target: getTarget(value),
                  hasMovedOut: hasMovedOut
              }
          }
      },
548eac43   梁灏   fixed #1387 and u...
40
41
      componentUpdated (el, { value }) {
          if (el.dataset.transfer !== 'true') return false;
713bd3d7   梁灏   fixed #583
42
43
          // need to make sure children are done updating (vs. `update`)
          const ref$1 = el.__transferDomData;
dce5a3ea   Aresn   update Modal
44
          if (!ref$1) return;
713bd3d7   梁灏   fixed #583
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
          // homes.get(el)
          const parentNode = ref$1.parentNode;
          const home = ref$1.home;
          const hasMovedOut = ref$1.hasMovedOut; // recall where home is
  
          if (!hasMovedOut && value) {
              // remove from document and leave placeholder
              parentNode.replaceChild(home, el);
              // append to target
              getTarget(value).appendChild(el);
              el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: true, target: getTarget(value) });
          } else if (hasMovedOut && value === false) {
              // previously moved, coming back home
              parentNode.replaceChild(el, home);
              el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: false, target: getTarget(value) });
          } else if (value) {
              // already moved, going somewhere else
              getTarget(value).appendChild(el);
          }
      },
548eac43   梁灏   fixed #1387 and u...
65
66
      unbind (el) {
          if (el.dataset.transfer !== 'true') return false;
dce5a3ea   Aresn   update Modal
67
68
69
          el.className = el.className.replace('v-transfer-dom', '');
          const ref$1 = el.__transferDomData;
          if (!ref$1) return;
713bd3d7   梁灏   fixed #583
70
71
72
73
74
75
76
77
          if (el.__transferDomData.hasMovedOut === true) {
              el.__transferDomData.parentNode && el.__transferDomData.parentNode.appendChild(el)
          }
          el.__transferDomData = null
      }
  };
  
  export default directive;