blob: db8e6875d6bc0ea8551ec0a3cb69fc21f6bec200 [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
21(function(window) {
22 "use strict";
23
24 if (window.PunchOutRegistry) {
25 return;
26 }
27
28 var queuedFactoryRequests = new Map();
29 var factoryPromises = new Map();
30 var instancePromises = new Map();
31
32 function registerFactory(name, factory) {
33 if (factoryPromises.has(name) && !queuedFactoryRequests.has(name)) {
34 console.error("PunchOut \"" + name + "\" has been already registered");
35 return;
36 }
37 if (queuedFactoryRequests.has(name)) {
38 var factoryRequest = queuedFactoryRequests.get(name);
39 factoryRequest(factory);
40 queuedFactoryRequests.delete(name);
41 } else {
42 factoryPromises.set(name, Promise.resolve(factory));
43 }
44 }
45
46 function getFactoryPromise(name) {
47 var factoryPromise = factoryPromises.get(name);
48 if (!factoryPromise) {
49 factoryPromise = new Promise(function (resolveFactory) {
50 queuedFactoryRequests.set(name, resolveFactory);
51 });
52 factoryPromises.set(name, factoryPromise);
53 }
54 return factoryPromise;
55 }
56
57 function getInstancePromise(name, element) {
58 var factoryPromise;
59 var instancePromise = instancePromises.get(element);
60 if (!instancePromise) {
61 instancePromise = getFactoryPromise(name).then(function(factory) {
62 return factory();
63 });
64 instancePromises.set(element, instancePromise);
65 }
66 return instancePromise;
67 }
68
69 function renderPunchOut(params, element) {
70 var name = params.name;
71 var options = params.options || {};
72 var onEvent = params.onEvent || function () {};
73
74 getInstancePromise(name, element).then(function (punchOut) {
75 punchOut.render({options: options, onEvent: onEvent}, element);
76 });
77 }
78
79 function unmountPunchOut(element) {
80 if (!instancePromises.has(element)) {
81 console.error("There is no PunchOut in element", element);
82 return;
83 }
84 instancePromises.get(element).then(function(punchOut) {
85 punchOut.unmount(element);
86 });
87 instancePromises.delete(element);
88 }
89
90 var PunchOutRegistry = Object.freeze({
91 register: registerFactory,
92 render: renderPunchOut,
93 unmount: unmountPunchOut
94 });
95
96 window.PunchOutRegistry = PunchOutRegistry;
97
98})(window);