blob: 506a785c79b55b17ff156c24c2f1da45981e315a [file] [log] [blame]
Michael Landoed64b5e2017-06-09 03:19:04 +03001/*-
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 Zivf5854fd2017-07-31 15:50:46 +030021
22
23(function (window) {
Michael Landoed64b5e2017-06-09 03:19:04 +030024 "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 Zivf5854fd2017-07-31 15:50:46 +030034 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 Landoed64b5e2017-06-09 03:19:04 +030045 function registerFactory(name, factory) {
46 if (factoryPromises.has(name) && !queuedFactoryRequests.has(name)) {
Avi Zivf5854fd2017-07-31 15:50:46 +030047 // console.error("PunchOut \"" + name + "\" has been already registered");
Michael Landoed64b5e2017-06-09 03:19:04 +030048 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 Zivf5854fd2017-07-31 15:50:46 +030074 instancePromise = getFactoryPromise(name).then(function (factory) {
Michael Landoed64b5e2017-06-09 03:19:04 +030075 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 Zivf5854fd2017-07-31 15:50:46 +030085 var onEvent = params.onEvent || function () {
86 };
Michael Landoed64b5e2017-06-09 03:19:04 +030087
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 Zivf5854fd2017-07-31 15:50:46 +030098 instancePromises.get(element).then(function (punchOut) {
Michael Landoed64b5e2017-06-09 03:19:04 +030099 punchOut.unmount(element);
100 });
101 instancePromises.delete(element);
102 }
103
104 var PunchOutRegistry = Object.freeze({
105 register: registerFactory,
106 render: renderPunchOut,
Avi Zivf5854fd2017-07-31 15:50:46 +0300107 unmount: unmountPunchOut,
108 loadOnBoarding: loadOnBoarding
Michael Landoed64b5e2017-06-09 03:19:04 +0300109 });
110
111 window.PunchOutRegistry = PunchOutRegistry;
112
113})(window);