2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
26 if (window.PunchOutRegistry) {
30 var queuedFactoryRequests = new Map();
31 var factoryPromises = new Map();
32 var instancePromises = new Map();
34 function loadOnBoarding(callback) {
36 if (factoryPromises.has("onboarding/vendor") && !queuedFactoryRequests.has("onboarding/vendor")) {
40 console.log("Load OnBoarding");
41 $.getScript("/onboarding/punch-outs_en.js").then(callback);
45 function registerFactory(name, factory) {
46 if (factoryPromises.has(name) && !queuedFactoryRequests.has(name)) {
47 // console.error("PunchOut \"" + name + "\" has been already registered");
50 if (queuedFactoryRequests.has(name)) {
51 var factoryRequest = queuedFactoryRequests.get(name);
52 factoryRequest(factory);
53 queuedFactoryRequests.delete(name);
55 factoryPromises.set(name, Promise.resolve(factory));
59 function getFactoryPromise(name) {
60 var factoryPromise = factoryPromises.get(name);
61 if (!factoryPromise) {
62 factoryPromise = new Promise(function (resolveFactory) {
63 queuedFactoryRequests.set(name, resolveFactory);
65 factoryPromises.set(name, factoryPromise);
67 return factoryPromise;
70 function getInstancePromise(name, element) {
72 var instancePromise = instancePromises.get(element);
73 if (!instancePromise) {
74 instancePromise = getFactoryPromise(name).then(function (factory) {
77 instancePromises.set(element, instancePromise);
79 return instancePromise;
82 function renderPunchOut(params, element) {
83 var name = params.name;
84 var options = params.options || {};
85 var onEvent = params.onEvent || function () {
88 getInstancePromise(name, element).then(function (punchOut) {
89 punchOut.render({options: options, onEvent: onEvent}, element);
93 function unmountPunchOut(element) {
94 if (!instancePromises.has(element)) {
95 console.error("There is no PunchOut in element", element);
98 instancePromises.get(element).then(function (punchOut) {
99 punchOut.unmount(element);
101 instancePromises.delete(element);
104 var PunchOutRegistry = Object.freeze({
105 register: registerFactory,
106 render: renderPunchOut,
107 unmount: unmountPunchOut,
108 loadOnBoarding: loadOnBoarding
111 window.PunchOutRegistry = PunchOutRegistry;