|
1 |
| -import { Delegate } from './framework-delegate' |
| 1 | +import ProxyController from './proxy-controller' |
| 2 | +import ProxyMenuController from './proxy-menu-controller' |
2 | 3 |
|
3 |
| -const api = { |
4 |
| - // Create or return a NavController instance and set root to a Vue component |
5 |
| - newNavController(root) { |
6 |
| - return Promise.resolve( |
7 |
| - initComponent('ion-nav', 'ion-app').then(ctrl => { |
8 |
| - ctrl.root = root |
9 |
| - return ctrl |
10 |
| - }) |
11 |
| - ) |
12 |
| - }, |
13 |
| - // Create or return an AlertController instance and set it's properties |
14 |
| - newAlertController(props) { |
15 |
| - return this.newAbstractController('ion-alert-controller', props) |
16 |
| - }, |
17 |
| - // Create or return a LoadingController instance and set it's properties |
18 |
| - newLoadingController(props) { |
19 |
| - return this.newAbstractController('ion-loading-controller', props) |
20 |
| - }, |
21 |
| - // Create or return a ModalController instance and set it's properties |
22 |
| - newModalController(props) { |
23 |
| - return this.newAbstractController('ion-modal-controller', props) |
24 |
| - }, |
25 |
| - // Initialize an Ionic component and set it's properties |
26 |
| - newAbstractController(tag, props) { |
27 |
| - const controller = initComponent(tag).then(ctrl => ctrl.create(props)) |
28 |
| - return Promise.resolve(controller) |
29 |
| - }, |
| 4 | +export default class Api { |
| 5 | + // Create or return a ActionSheetController instance |
| 6 | + get actionSheetController() { |
| 7 | + return getOrCreateController('_actionSheetController', 'ion-action-sheet-controller') |
| 8 | + } |
| 9 | + |
| 10 | + // Create or return an AlertController instance |
| 11 | + get alertController() { |
| 12 | + return getOrCreateController('_alertController', 'ion-alert-controller') |
| 13 | + } |
| 14 | + |
| 15 | + // Create or return a LoadingController instance |
| 16 | + get loadingController() { |
| 17 | + return getOrCreateController('_loadingController', 'ion-loading-controller') |
| 18 | + } |
| 19 | + |
| 20 | + // Create or return a MenuController instance |
| 21 | + get menuController() { |
| 22 | + return getOrCreateController('_menuController', 'ion-menu-controller', ProxyMenuController) |
| 23 | + } |
| 24 | + |
| 25 | + // Create or return a ModalController instance |
| 26 | + get modalController() { |
| 27 | + return getOrCreateController('_modalController', 'ion-modal-controller') |
| 28 | + } |
| 29 | + |
| 30 | + // Create or return a PopoverController instance |
| 31 | + get popoverController() { |
| 32 | + return getOrCreateController('_popoverController', 'ion-popover-controller') |
| 33 | + } |
| 34 | + |
| 35 | + // Create or return a ToastController instance |
| 36 | + get toastController() { |
| 37 | + return getOrCreateController('_toastController', 'ion-toast-controller') |
| 38 | + } |
30 | 39 | }
|
31 | 40 |
|
32 |
| -export default api |
| 41 | +// Cached controllers |
| 42 | +Api._actionSheetController = null |
| 43 | +Api._alertController = null |
| 44 | +Api._loadingController = null |
| 45 | +Api._menuController = null |
| 46 | +Api._modalController = null |
| 47 | +Api._popoverController = null |
| 48 | +Api._toastController = null |
33 | 49 |
|
34 |
| -api.install = function(Vue) { |
| 50 | +Api.install = function(Vue) { |
35 | 51 | // If installed - skip
|
36 |
| - if (api.install.installed) { |
| 52 | + if (Api.install.installed) { |
37 | 53 | return
|
38 | 54 | }
|
39 | 55 |
|
40 |
| - api.install.installed = true |
| 56 | + Api.install.installed = true |
41 | 57 |
|
42 | 58 | // Ignore Ionic custom elements
|
43 | 59 | Vue.config.ignoredElements.push(/^ion-/)
|
44 | 60 |
|
45 | 61 | // Give access to the API methods
|
46 | 62 | Object.defineProperty(Vue.prototype, '$ionic', {
|
47 | 63 | get() {
|
48 |
| - return api |
| 64 | + return new Api() |
49 | 65 | },
|
50 | 66 | })
|
51 | 67 | }
|
52 | 68 |
|
53 |
| -// Initialize an Ionic component and append it to DOM |
54 |
| -function initComponent(tag, wrapper = 'body') { |
55 |
| - // If wrapper doesn't exist use body as fall-back |
56 |
| - const wrapperEl = document.querySelector(wrapper) || document.body |
57 |
| - const element = getOrAppendElement(tag, wrapperEl) |
58 |
| - |
59 |
| - // Set the framework-specific implementations for Ionic's internals |
60 |
| - element.delegate = Delegate |
61 |
| - |
62 |
| - // Return a Promise |
63 |
| - return element.componentOnReady() |
64 |
| -} |
65 |
| - |
66 |
| -// Return existing Element (tag) or create a new one |
67 |
| -function getOrAppendElement(tag, wrapper) { |
68 |
| - let element = document.querySelector(tag) |
69 |
| - |
70 |
| - if (element) { |
71 |
| - return element |
| 69 | +// Get existing controller instance or initialize a new one |
| 70 | +function getOrCreateController(cache, tag, proxy = ProxyController) { |
| 71 | + if (!Api[cache]) { |
| 72 | + Api[cache] = new proxy(tag) |
72 | 73 | }
|
73 | 74 |
|
74 |
| - return wrapper.appendChild(document.createElement(tag)) |
| 75 | + return Api[cache] |
75 | 76 | }
|
0 commit comments