1 | /* global YUI */
|
2 |
|
3 | /**
|
4 | * The firedoc module
|
5 | * @module firedoc
|
6 | */
|
7 |
|
8 | const _ = require('underscore');
|
9 | const path = require('path');
|
10 | const Handlebars = require('handlebars');
|
11 |
|
12 | /**
|
13 | * View class borrowed from [Selleck](https://github.com/rgrove/selleck)
|
14 | * The view class is a **`handlebars`** template helper.
|
15 | *
|
16 | * @class DocView
|
17 | * @constructor
|
18 | * @param {Object} data Meta data to use in this template
|
19 | * @param {String} templateName The name of the template file to render.
|
20 | **/
|
21 | function DocView (data, templateName, cwd) {
|
22 | this.templateName = templateName;
|
23 | this.cwd = path.join(cwd || '');
|
24 | this.assets = path.join(cwd || '', 'assets');
|
25 | _.extend(this, data);
|
26 |
|
27 | // register helpers
|
28 | var self = this;
|
29 | Handlebars.registerHelper('relink', function (item, options) {
|
30 | item = item || '';
|
31 | if (self.project.local) {
|
32 | return '//' + self.project.root + '/' + item;
|
33 | } else {
|
34 | return self.project.baseurl + '/' + item;
|
35 | }
|
36 | });
|
37 |
|
38 | Handlebars.registerHelper('equal', function(lvalue, rvalue, options) {
|
39 | if (arguments.length < 3)
|
40 | throw new Error("Handlebars Helper equal needs 2 parameters");
|
41 | if( lvalue!=rvalue ) {
|
42 | return options.inverse(this);
|
43 | } else {
|
44 | return options.fn(this);
|
45 | }
|
46 | });
|
47 |
|
48 | Handlebars.registerHelper('is_not', function(lvalue, rvalue, options) {
|
49 | if (arguments.length < 3)
|
50 | throw new Error("Handlebars Helper equal needs 2 parameters");
|
51 | if( lvalue==rvalue ) {
|
52 | return options.inverse(this);
|
53 | } else {
|
54 | return options.fn(this);
|
55 | }
|
56 | });
|
57 |
|
58 | // no need, but keep for helper template
|
59 | // Handlebars.registerHelper('is_in', function(name, collection, options) {
|
60 | // if (arguments.length < 2)
|
61 | // throw new Error("Handlebars Helper is_in needs 2 parameters");
|
62 | // for (var i = 0; i < collection.length; ++i) {
|
63 | // if (collection[i].name === name) return options.fn(this);
|
64 | // }
|
65 | // return options.inverse(this);
|
66 | // });
|
67 |
|
68 | // Handlebars.registerHelper('fromClass', function(name, collection) {
|
69 | // if (arguments.length < 2)
|
70 | // throw new Error("Handlebars Helper is_in needs 2 parameters");
|
71 | // for (var i = 0; i < collection.length; ++i) {
|
72 | // if (collection[i].name === name) {
|
73 | // return collection[i].clazz;
|
74 | // }
|
75 | // }
|
76 | // return '';
|
77 | // });
|
78 | }
|
79 |
|
80 | DocView.prototype = {
|
81 | /**
|
82 | * **Mustache** `lambda` method for setting the HTML title
|
83 | * @method htmlTitle
|
84 | */
|
85 | htmlTitle: function () {
|
86 | var name = this.name;
|
87 | var title = name;
|
88 | try {
|
89 | if (title) {
|
90 | if (this.project.name) {
|
91 | title += ' - ' + this.project.name;
|
92 | }
|
93 | } else {
|
94 | title = this.project.name;
|
95 | }
|
96 | } catch (e) {}
|
97 | return title;
|
98 | },
|
99 |
|
100 | /**
|
101 | * **Mustache** `lambda` method for setting the title
|
102 | * @method title
|
103 | */
|
104 | title: function () {
|
105 | var name = this.name;
|
106 | var title = name;
|
107 | try {
|
108 | title = this.project.name;
|
109 | if (name) {
|
110 | title += ': ' + name;
|
111 | }
|
112 | } catch (e) {}
|
113 | return title;
|
114 | }
|
115 |
|
116 | };
|
117 |
|
118 | exports.DocView = DocView;
|