1 declare var angular: angular.IAngularStatic;
3 import * as marked from "./lib/marked";
6 import "./help.service";
8 import { IHelpService , TocNodeCollection } from "./help.service";
9 import { resolvePath } from './help.utilities';
10 import { Node } from "./help.tree";
12 let currentPath = '/help';
15 const renderer = new marked.Renderer();
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>`
23 renderer.image = (href: string, title: string, text: string) => {
24 return `<img src="${resolvePath(currentPath, href)}" alt="${title}" />`
39 const help = angular.module('app.help');
41 const HelpDirective = () => {
45 template: `<div class="global-help" ><a href="{{ '#/help/'+ link }}"><i class="fa fa-question-circle"></i> Help</a></div>`,
53 help.directive('help', [HelpDirective]);
55 const MarkdownItDirective = ($sanitize: angular.sanitize.ISanitizeService) => {
56 const attribute = 'markdownIt';
58 const render = function (value) {
59 const md2html = (marked instanceof Function) ? marked : marked.default;
61 ? $sanitize(md2html(value.trim()))
71 link: function (scope, element, attrs) {
72 if (attrs[attribute]) {
73 scope.$watch(attribute, function (value) {
74 element.html(render(value));
77 element.html(render(element.text()));
83 help.directive('markdownIt', ['$sanitize', MarkdownItDirective]);
85 interface IHelpScope extends angular.IScope {
86 toc: TocNodeCollection
89 const mapNode = (tocNode: TocNodeCollection): Node[] => {
90 return tocNode && Object.keys(tocNode).map(key => {
93 href: `#/help/${tocNode[key]['versions']['current']['path']}`,
94 nodes: mapNode(tocNode[key].nodes)
99 class HelpController {
100 private _content = "## Loading";
102 private _toc : Node = { };
104 constructor(private $scope: IHelpScope, private $state, private $timeout: angular.ITimeoutService, private helpService: IHelpService) {
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'] })
111 this.navigateTo($state.params.path);
116 public get content() { return this._content; }
117 public get path() { return this._path; }
118 public get toc() { return this._toc; }
120 private navigateTo(path: string): void {
121 this.helpService.getDocument(path).then((result) => {
123 currentPath = result.basePath;
124 this._content = result.document;
130 help.controller('helpCtrl', ['$scope', '$state', '$timeout', 'helpService', HelpController ]);