Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 21 | |
| 22 | |
| 23 | (function (window) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 24 | "use strict"; |
| 25 | |
| 26 | if (window.PunchOutRegistry) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | var queuedFactoryRequests = new Map(); |
| 31 | var factoryPromises = new Map(); |
| 32 | var instancePromises = new Map(); |
| 33 | |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 34 | function loadOnBoarding(callback) { |
| 35 | |
| 36 | if (factoryPromises.has("onboarding/vendor") && !queuedFactoryRequests.has("onboarding/vendor")) { |
| 37 | callback(); |
| 38 | } |
| 39 | else { |
| 40 | console.log("Load OnBoarding"); |
| 41 | $.getScript("/onboarding/punch-outs_en.js").then(callback); |
| 42 | } |
| 43 | } |
| 44 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 45 | function registerFactory(name, factory) { |
| 46 | if (factoryPromises.has(name) && !queuedFactoryRequests.has(name)) { |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 47 | // console.error("PunchOut \"" + name + "\" has been already registered"); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | if (queuedFactoryRequests.has(name)) { |
| 51 | var factoryRequest = queuedFactoryRequests.get(name); |
| 52 | factoryRequest(factory); |
| 53 | queuedFactoryRequests.delete(name); |
| 54 | } else { |
| 55 | factoryPromises.set(name, Promise.resolve(factory)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function getFactoryPromise(name) { |
| 60 | var factoryPromise = factoryPromises.get(name); |
| 61 | if (!factoryPromise) { |
| 62 | factoryPromise = new Promise(function (resolveFactory) { |
| 63 | queuedFactoryRequests.set(name, resolveFactory); |
| 64 | }); |
| 65 | factoryPromises.set(name, factoryPromise); |
| 66 | } |
| 67 | return factoryPromise; |
| 68 | } |
| 69 | |
| 70 | function getInstancePromise(name, element) { |
| 71 | var factoryPromise; |
| 72 | var instancePromise = instancePromises.get(element); |
| 73 | if (!instancePromise) { |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 74 | instancePromise = getFactoryPromise(name).then(function (factory) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 75 | return factory(); |
| 76 | }); |
| 77 | instancePromises.set(element, instancePromise); |
| 78 | } |
| 79 | return instancePromise; |
| 80 | } |
| 81 | |
| 82 | function renderPunchOut(params, element) { |
| 83 | var name = params.name; |
| 84 | var options = params.options || {}; |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 85 | var onEvent = params.onEvent || function () { |
| 86 | }; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 87 | |
| 88 | getInstancePromise(name, element).then(function (punchOut) { |
| 89 | punchOut.render({options: options, onEvent: onEvent}, element); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | function unmountPunchOut(element) { |
| 94 | if (!instancePromises.has(element)) { |
| 95 | console.error("There is no PunchOut in element", element); |
| 96 | return; |
| 97 | } |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 98 | instancePromises.get(element).then(function (punchOut) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 99 | punchOut.unmount(element); |
| 100 | }); |
| 101 | instancePromises.delete(element); |
| 102 | } |
| 103 | |
| 104 | var PunchOutRegistry = Object.freeze({ |
| 105 | register: registerFactory, |
| 106 | render: renderPunchOut, |
Avi Ziv | f5854fd | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 107 | unmount: unmountPunchOut, |
| 108 | loadOnBoarding: loadOnBoarding |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 109 | }); |
| 110 | |
| 111 | window.PunchOutRegistry = PunchOutRegistry; |
| 112 | |
| 113 | })(window); |