5a12bbfe47c614240e61db4e21661f7d2b869ee9
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / help / help-module / src / main / resources / help / help.controller.ts
1 declare var angular: angular.IAngularStatic;
2
3 import * as marked from "./lib/marked";  
4
5 import "./help.tree";
6 import "./help.service";
7
8 import { IHelpService , TocNodeCollection } from "./help.service";
9 import { resolvePath } from './help.utilities';
10 import { Node } from  "./help.tree";
11
12 let currentPath = '/help';
13 let currentDoc = '';
14
15 const renderer = new marked.Renderer();
16
17 renderer.link = (href: string, title: string, text: string) => {
18   // check if href is rel or abs
19   const absUrlMatch = href.trim().match(/^https?:\/\//i);
20   return `<a href="${absUrlMatch ? href : resolvePath('#/help/', currentDoc, href)}" title="${title}" >${text}</a>`
21 };
22
23 renderer.image = (href: string, title: string, text: string) => {
24   return `<img src="${resolvePath(currentPath, href)}" alt="${title}" />`
25 };
26
27 marked.setOptions({
28   renderer: renderer,
29   gfm: true,
30   tables: true,
31   breaks: false,
32   pedantic: false,
33   sanitize: false,
34   smartLists: true,
35   smartypants: false,
36   xhtml: false
37 });
38
39 const help = angular.module('app.help');
40
41 const HelpDirective = () => {
42
43   return {
44     restrict: 'AE',
45     template: `<div class="global-help" ><a href="{{ '#/help/'+ link }}"><i class="fa fa-question-circle"></i> Help</a></div>`,
46     scope: {
47       link: '@'
48     },
49     replace: true
50   };
51 }
52
53 help.directive('help', [HelpDirective]);
54
55 const MarkdownItDirective = ($sanitize: angular.sanitize.ISanitizeService) => {
56   const attribute = 'markdownIt';
57   
58   const render = function (value) {
59     const md2html = (marked instanceof Function) ? marked : marked.default;
60     return value
61       ? $sanitize(md2html(value.trim()))
62       : '';
63   };
64
65   return {
66     restrict: 'AE',
67     scope: {
68       markdownIt: '='
69     },
70     replace: true,
71     link: function (scope, element, attrs) {
72       if (attrs[attribute]) {
73         scope.$watch(attribute, function (value) {
74           element.html(render(value));
75         });
76       } else {
77         element.html(render(element.text()));
78       }
79     }
80   };
81 }
82
83 help.directive('markdownIt', ['$sanitize', MarkdownItDirective]);
84
85 interface IHelpScope extends angular.IScope {
86   toc: TocNodeCollection
87 }
88
89 const mapNode = (tocNode: TocNodeCollection): Node[] => {
90   return tocNode && Object.keys(tocNode).map(key => {
91     return {
92       ...tocNode[key],
93       href: `#/help/${tocNode[key]['versions']['current']['path']}`,
94       nodes: mapNode(tocNode[key].nodes)
95     };
96   });
97 };
98
99 class HelpController {
100   private _content = "## Loading";
101   private _path = '';
102   private _toc : Node = { }; 
103
104   constructor(private $scope: IHelpScope, private $state, private $timeout: angular.ITimeoutService, private helpService: IHelpService) {
105   
106     helpService.getTableOfContents().then(toc => {
107       this._toc = { nodes: mapNode(toc) } ;
108       if (!$state.params.path) {
109         $state.go('main.help', { path: toc['sdnr']['versions']['current']['path'] })
110       } else {
111         this.navigateTo($state.params.path);
112       }
113     });
114   }
115
116   public get content() { return this._content; }
117   public get path() { return this._path; }
118   public get toc() { return this._toc; }
119
120   private navigateTo(path: string): void {
121     this.helpService.getDocument(path).then((result) => {
122       currentDoc = path;
123       currentPath = result.basePath;
124       this._content = result.document;
125     });
126   }
127
128 }
129
130 help.controller('helpCtrl', ['$scope', '$state', '$timeout', 'helpService', HelpController ]);