Commit 713bd3d75dcc6988065b368775e1b8ae36c456fe
1 parent
891f61d8
fixed #583
Showing
4 changed files
with
80 additions
and
13 deletions
Show diff stats
examples/routers/modal.vue
examples/routers/page.vue
| 1 | 1 | <template> |
| 2 | 2 | <div> |
| 3 | - <Page :total="1000" show-sizer show-elevator show-total class="classr" :style="{float: 'right'}" @on-page-size-change="pc"></Page> | |
| 4 | - <br><br> | |
| 5 | - <Page :total="1000" show-sizer show-elevator show-total size="small" class="classr2"></Page> | |
| 6 | - <br><br> | |
| 7 | - <Page :current="2" :total="50" simple></Page> | |
| 3 | + <Page :total="100" :current="current"></Page> | |
| 4 | + {{ current }} | |
| 5 | + <Button @click="current = 1">set current</Button> | |
| 8 | 6 | </div> |
| 9 | 7 | </template> |
| 10 | 8 | <script> |
| 11 | - import { Page } from 'iview'; | |
| 12 | 9 | export default { |
| 13 | - components: { Page }, | |
| 14 | - methods: { | |
| 15 | - pc (page) { | |
| 16 | - console.log(page) | |
| 10 | + data () { | |
| 11 | + return { | |
| 12 | + current: 2 | |
| 17 | 13 | } |
| 18 | 14 | } |
| 19 | 15 | } | ... | ... |
src/components/modal/modal.vue
| 1 | 1 | <template> |
| 2 | - <span> | |
| 2 | + <div v-transfer-dom> | |
| 3 | 3 | <transition :name="transitionNames[1]"> |
| 4 | 4 | <div :class="maskClasses" v-show="visible" @click="mask"></div> |
| 5 | 5 | </transition> |
| ... | ... | @@ -24,11 +24,12 @@ |
| 24 | 24 | </div> |
| 25 | 25 | </transition> |
| 26 | 26 | </div> |
| 27 | - </span> | |
| 27 | + </div> | |
| 28 | 28 | </template> |
| 29 | 29 | <script> |
| 30 | 30 | import Icon from '../icon'; |
| 31 | 31 | import iButton from '../button/button.vue'; |
| 32 | + import TransferDom from '../../directives/transfer-dom'; | |
| 32 | 33 | import { getScrollBarSize } from '../../utils/assist'; |
| 33 | 34 | import Locale from '../../mixins/locale'; |
| 34 | 35 | |
| ... | ... | @@ -38,6 +39,7 @@ |
| 38 | 39 | name: 'Modal', |
| 39 | 40 | mixins: [ Locale ], |
| 40 | 41 | components: { Icon, iButton }, |
| 42 | + directives: { TransferDom }, | |
| 41 | 43 | props: { |
| 42 | 44 | value: { |
| 43 | 45 | type: Boolean, | ... | ... |
| 1 | +// Thanks to: https://github.com/airyland/vux/blob/v2/src/directives/transfer-dom/index.js | |
| 2 | +// Thanks to: https://github.com/calebroseland/vue-dom-portal | |
| 3 | + | |
| 4 | +/** | |
| 5 | + * Get target DOM Node | |
| 6 | + * @param {(Node|string|Boolean)} [node=document.body] DOM Node, CSS selector, or Boolean | |
| 7 | + * @return {Node} The target that the el will be appended to | |
| 8 | + */ | |
| 9 | +function getTarget (node) { | |
| 10 | + if (node === void 0) { | |
| 11 | + node = document.body | |
| 12 | + } | |
| 13 | + if (node === true) { return document.body } | |
| 14 | + return node instanceof window.Node ? node : document.querySelector(node) | |
| 15 | +} | |
| 16 | + | |
| 17 | +const directive = { | |
| 18 | + inserted (el, { value }, vnode) { | |
| 19 | + el.className = el.className ? el.className + ' v-transfer-dom' : 'v-transfer-dom'; | |
| 20 | + const parentNode = el.parentNode; | |
| 21 | + const home = document.createComment(''); | |
| 22 | + let hasMovedOut = false; | |
| 23 | + | |
| 24 | + if (value !== false) { | |
| 25 | + parentNode.replaceChild(home, el); // moving out, el is no longer in the document | |
| 26 | + getTarget(value).appendChild(el); // moving into new place | |
| 27 | + hasMovedOut = true | |
| 28 | + } | |
| 29 | + if (!el.__transferDomData) { | |
| 30 | + el.__transferDomData = { | |
| 31 | + parentNode: parentNode, | |
| 32 | + home: home, | |
| 33 | + target: getTarget(value), | |
| 34 | + hasMovedOut: hasMovedOut | |
| 35 | + } | |
| 36 | + } | |
| 37 | + }, | |
| 38 | + componentUpdated (el, { value }) { | |
| 39 | + // need to make sure children are done updating (vs. `update`) | |
| 40 | + const ref$1 = el.__transferDomData; | |
| 41 | + // homes.get(el) | |
| 42 | + const parentNode = ref$1.parentNode; | |
| 43 | + const home = ref$1.home; | |
| 44 | + const hasMovedOut = ref$1.hasMovedOut; // recall where home is | |
| 45 | + | |
| 46 | + if (!hasMovedOut && value) { | |
| 47 | + // remove from document and leave placeholder | |
| 48 | + parentNode.replaceChild(home, el); | |
| 49 | + // append to target | |
| 50 | + getTarget(value).appendChild(el); | |
| 51 | + el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: true, target: getTarget(value) }); | |
| 52 | + } else if (hasMovedOut && value === false) { | |
| 53 | + // previously moved, coming back home | |
| 54 | + parentNode.replaceChild(el, home); | |
| 55 | + el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: false, target: getTarget(value) }); | |
| 56 | + } else if (value) { | |
| 57 | + // already moved, going somewhere else | |
| 58 | + getTarget(value).appendChild(el); | |
| 59 | + } | |
| 60 | + }, | |
| 61 | + unbind: function unbind (el, binding) { | |
| 62 | + el.className = el.className.replace('v-transfer-dom', '') | |
| 63 | + if (el.__transferDomData.hasMovedOut === true) { | |
| 64 | + el.__transferDomData.parentNode && el.__transferDomData.parentNode.appendChild(el) | |
| 65 | + } | |
| 66 | + el.__transferDomData = null | |
| 67 | + } | |
| 68 | +}; | |
| 69 | + | |
| 70 | +export default directive; | |
| 0 | 71 | \ No newline at end of file | ... | ... |