|
1 | | -import Vue from 'vue' |
2 | | - |
3 | | -let globalVue = null |
4 | | - |
5 | | -// Detect environment (browser, module, etc.) |
6 | | -if (typeof window !== 'undefined' && window.Vue !== undefined) { |
7 | | - globalVue = window.Vue |
8 | | -} else if (typeof global !== 'undefined') { |
9 | | - globalVue = global.Vue |
10 | | -} |
11 | | - |
12 | | -if (!globalVue) { |
13 | | - globalVue = Vue |
14 | | - globalVue.config.productionTip = false |
15 | | -} |
16 | | - |
17 | | -// Attach the passed Vue component to DOM |
18 | | -export function attachViewToDom(parentElement, vueComponent, propsData, classes) { |
19 | | - // Create an appropriate wrapper for the component |
20 | | - const wrapper = document.createElement(shouldWrapInIonPage(parentElement) ? 'ion-page' : 'div') |
| 1 | +export default class Delegate { |
| 2 | + constructor(Vue) { |
| 3 | + this.Vue = Vue |
| 4 | + } |
21 | 5 |
|
22 | | - parentElement.appendChild(wrapper) |
| 6 | + // Attach the passed Vue component to DOM |
| 7 | + attachViewToDom(parentElement, component, opts, classes) { |
| 8 | + // Get the Vue controller |
| 9 | + return this.vueController(component).then(controller => { |
| 10 | + const vueComponent = this.vueComponent(controller, opts) |
23 | 11 |
|
24 | | - // Create a Vue component constructor |
25 | | - const vueElement = globalVue.extend(vueComponent) |
| 12 | + // Add any classes to the Vue component's root element |
| 13 | + addClasses(vueComponent.$el, classes) |
26 | 14 |
|
27 | | - // Create a new instance of the Vue component |
28 | | - const page = new vueElement({ propsData }).$mount(wrapper) |
29 | | - |
30 | | - // Add any classes the Vue component's root element |
31 | | - if (classes) { |
32 | | - for (const cls of classes) { |
33 | | - page.$el.classList.add(cls) |
34 | | - } |
| 15 | + // Append the Vue component to DOM |
| 16 | + parentElement.appendChild(vueComponent.$el) |
| 17 | + return vueComponent.$el |
| 18 | + }) |
35 | 19 | } |
36 | 20 |
|
37 | | - // Resolve the Vue component element |
38 | | - return Promise.resolve(page.$el) |
39 | | -} |
| 21 | + // Remove the earlier created Vue component from DOM |
| 22 | + removeViewFromDom(parentElement, childElement) { |
| 23 | + // Destroy the Vue component instance |
| 24 | + if (childElement.__vue__) { |
| 25 | + childElement.__vue__.$destroy() |
| 26 | + } |
40 | 27 |
|
41 | | -// Remove the earlier created Vue component from DOM |
42 | | -export function removeViewFromDom(parentElement, childElement) { |
43 | | - // Destroy the Vue component instance |
44 | | - if (childElement.hasOwnProperty('__vue__')) { |
45 | | - childElement.__vue__.$destroy() |
| 28 | + return Promise.resolve() |
46 | 29 | } |
47 | 30 |
|
48 | | - // Remove from DOM |
49 | | - parentElement.removeChild(childElement) |
50 | | - |
51 | | - return Promise.resolve() |
52 | | -} |
| 31 | + // Handle creation of sync and async components |
| 32 | + vueController(component) { |
| 33 | + return Promise.resolve( |
| 34 | + typeof component === 'function' && component.cid === undefined |
| 35 | + ? component().then(c => this.Vue.extend(isESModule(c) ? c.default : c)) |
| 36 | + : this.Vue.extend(component) |
| 37 | + ) |
| 38 | + } |
53 | 39 |
|
54 | | -const Delegate = { |
55 | | - attachViewToDom, |
56 | | - removeViewFromDom, |
| 40 | + // Create a new instance of the Vue component |
| 41 | + vueComponent(controller, opts) { |
| 42 | + return new controller(opts).$mount() |
| 43 | + } |
57 | 44 | } |
58 | 45 |
|
59 | | -export { Delegate } |
| 46 | +const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol' |
60 | 47 |
|
61 | | -// Detect wrapper to be used |
62 | | -function shouldWrapInIonPage(element) { |
63 | | - return isElementModal(element) || isElementNav(element) |
| 48 | +function isESModule(obj) { |
| 49 | + return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module') |
64 | 50 | } |
65 | 51 |
|
66 | | -// Check if element is ION-NAV |
67 | | -function isElementNav(element) { |
68 | | - return element.tagName.toUpperCase() === 'ION-NAV' |
69 | | -} |
70 | | - |
71 | | -// Check if element has modal-wrapper class |
72 | | -function isElementModal(element) { |
73 | | - return element.classList.contains('modal-wrapper') |
| 52 | +function addClasses(element, classes = []) { |
| 53 | + for (const cls of classes) { |
| 54 | + element.classList.add(cls) |
| 55 | + } |
74 | 56 | } |
0 commit comments