44c07ef6889145d62408a28ece4ba1319bcec90b
[sdc.git] / catalog-ui / src / app / directives / punch-out / punch-out.ts
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 'use strict';
22 import {IUserProperties, IAppConfigurtaion} from "app/models";
23 let PunchOutRegistry = require('third-party/PunchOutRegistry.js');
24
25 export interface IPunchOutScope extends ng.IScope {
26     name:string;
27     data:any;
28     user:IUserProperties;
29     onEvent:Function;
30 }
31
32 export class PunchOutDirective implements ng.IDirective {
33
34     constructor(private sdcConfig:IAppConfigurtaion) {
35     }
36
37     scope = {
38         name: '=',
39         data: '=',
40         user: '=',
41         onEvent: '&'
42     };
43
44     replace = false;
45     restrict = 'E';
46
47     link = (scope:IPunchOutScope, element:ng.IAugmentedJQuery):void => {
48         // global registry object
49         let PunchOutRegistry = window['PunchOutRegistry'];
50
51         let render = ():void => {
52             let cookieConfig = this.sdcConfig.cookie;
53             let props = {
54                 name: scope.name,
55                 options: {
56                     data: scope.data,
57                     apiRoot: this.sdcConfig.api.root,
58                     apiHeaders: {
59                         userId: {
60                             name: cookieConfig.userIdSuffix,
61                             value: scope.user.userId
62                         },
63                         userFirstName: {
64                             name: cookieConfig.userFirstName,
65                             value: scope.user.firstName
66                         },
67                         userLastName: {
68                             name: cookieConfig.userLastName,
69                             value: scope.user.lastName
70                         },
71                         userEmail: {
72                             name: cookieConfig.userEmail,
73                             value: scope.user.email
74                         }
75                     }
76                 },
77                 onEvent: (...args) => {
78                     scope.$apply(() => {
79                         scope.onEvent().apply(null, args);
80                     });
81                 }
82             };
83             PunchOutRegistry.render(props, element[0]);
84         };
85
86         let unmount = ():void => {
87             PunchOutRegistry.unmount(element[0]);
88         };
89
90         scope.$watch('data', render);
91         element.on('$destroy', unmount);
92     };
93
94     public static factory = (sdcConfig:IAppConfigurtaion) => {
95         return new PunchOutDirective(sdcConfig);
96     };
97
98 }
99
100 PunchOutDirective.factory.$inject = ['sdcConfig'];