aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/punch-out/punch-out.ts
blob: aa15f1f3e9ffc08dece3a6abdaca58103c64c476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
import {IUserProperties, IAppConfigurtaion} from "app/models";
let PunchOutRegistry = require('third-party/PunchOutRegistry.js');

export interface IPunchOutScope extends ng.IScope {
    name:string;
    data:any;
    user:IUserProperties;
    onEvent:Function;
}

export class PunchOutDirective implements ng.IDirective {

    constructor(private sdcConfig:IAppConfigurtaion) {
    }

    scope = {
        name: '=',
        data: '=',
        user: '=',
        onEvent: '&'
    };

    replace = false;
    restrict = 'E';

    link = (scope:IPunchOutScope, element:ng.IAugmentedJQuery):void => {
        // global registry object
        let PunchOutRegistry = window['PunchOutRegistry'];

        let render = ():void => {
            let cookieConfig = this.sdcConfig.cookie;
            let props = {
                name: scope.name,
                options: {
                    data: scope.data,
                    apiRoot: this.sdcConfig.api.root,
                    apiHeaders: {
                        userId: {
                            name: cookieConfig.userIdSuffix,
                            value: scope.user.userId
                        },
                        userFirstName: {
                            name: cookieConfig.userFirstName,
                            value: scope.user.firstName
                        },
                        userLastName: {
                            name: cookieConfig.userLastName,
                            value: scope.user.lastName
                        },
                        userEmail: {
                            name: cookieConfig.userEmail,
                            value: scope.user.email
                        }
                    }
                },
                onEvent: (...args) => {
                    scope.$apply(() => {
                        scope.onEvent().apply(null, args);
                    });
                }
            };
            PunchOutRegistry.render(props, element[0]);
        };

        let unmount = ():void => {
            PunchOutRegistry.unmount(element[0]);
        };

        scope.$watch('data', render);
        element.on('$destroy', unmount);
    };

    public static factory = (sdcConfig:IAppConfigurtaion) => {
        return new PunchOutDirective(sdcConfig);
    };

}

PunchOutDirective.factory.$inject = ['sdcConfig'];