Merge "remove neo4j classes" into release-1.1.0
[sdc.git] / catalog-ui / src / third-party / PunchOutRegistry.js
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
21
22
23 (function (window) {
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
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
45     function registerFactory(name, factory) {
46         if (factoryPromises.has(name) && !queuedFactoryRequests.has(name)) {
47             // console.error("PunchOut \"" + name + "\" has been already registered");
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) {
74             instancePromise = getFactoryPromise(name).then(function (factory) {
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 || {};
85         var onEvent = params.onEvent || function () {
86             };
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         }
98         instancePromises.get(element).then(function (punchOut) {
99             punchOut.unmount(element);
100         });
101         instancePromises.delete(element);
102     }
103
104     var PunchOutRegistry = Object.freeze({
105         register: registerFactory,
106         render: renderPunchOut,
107         unmount: unmountPunchOut,
108         loadOnBoarding: loadOnBoarding
109     });
110
111     window.PunchOutRegistry = PunchOutRegistry;
112
113 })(window);