This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTemplateFactory.js
124 lines (124 loc) · 4.34 KB
/
TemplateFactory.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./StringUtil"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var StringUtil_1 = require("./StringUtil");
/**
* A helper class to provide a convenient and consistent way to render templates.
*
* @class TemplateFactory
* @module StructureJS
* @submodule util
* @requires StringUtil
* @requires Handlebars
* @author Robert S. (www.codeBelt.com)
* @static
*/
var TemplateFactory = (function () {
function TemplateFactory() {
throw new Error('[TemplateFactory] Do not instantiate the TemplateFactory class because it is a static class.');
}
/**
* Creates a template.
*
* @method create
* @param templatePath {any}
* @param [data=any]
* @returns {string}
* @public
* @static
* @example
* TemplateFactory.create('templateName', {some: 'data'});
*/
TemplateFactory.create = function (templatePath, data) {
if (data === void 0) { data = null; }
//Checks the first character to see if it is a '.' or '#'.
var regex = /^([.#])(.+)/;
var template = null;
var isFunctionTemplate = typeof templatePath === 'function';
var isClassOrIdName = regex.test(templatePath);
if (isFunctionTemplate) {
template = templatePath(data);
}
else if (isClassOrIdName) {
// Remove pound sign from the id name.
templatePath = templatePath.substring(1);
var htmlString = document.getElementById(templatePath).innerHTML;
htmlString = StringUtil_1.default.removeLeadingTrailingWhitespace(htmlString);
if (TemplateFactory.templateEngine == TemplateFactory.UNDERSCORE) {
// Underscore Template:
var templateMethod = window['_'].template(htmlString);
template = templateMethod(data);
}
else {
// Handlebars Template
var templateMethod = Handlebars.compile(htmlString);
template = templateMethod(data);
}
}
else {
var templateObj = window[TemplateFactory.templateNamespace];
if (!templateObj) {
// Returns null because the template namespace is not found.
return null;
}
var templateFunction = templateObj[templatePath];
if (templateFunction) {
// The templatePath gets a function storage in the associative array.
// We call the function by passing in the data as the argument.
template = templateFunction(data);
}
}
return template;
};
return TemplateFactory;
}());
/**
* A constant value for using Underscore or Lodash templates.
*
* @property UNDERSCORE
* @type {string}
* @public
* @final
* @static
*/
TemplateFactory.UNDERSCORE = 'underscore';
/**
* A constant value for using Handlebars templates. This is the default template engine.
*
* @property HANDLEBARS
* @type {string}
* @public
* @final
* @static
*/
TemplateFactory.HANDLEBARS = 'handlebars';
/**
* Sets the template engine type for this TemplateFactory class. The default is TemplateFactory.HANDLEBARS
*
* @property templateEngine
* @type {string}
* @default TemplateFactory.HANDLEBARS
* @public
* @static
*/
TemplateFactory.templateEngine = TemplateFactory.HANDLEBARS;
/**
* The global namespace for pre-compiled templates.
*
* @property templateNamespace
* @type {string}
* @default 'JST'
* @public
* @static
*/
TemplateFactory.templateNamespace = 'JST';
exports.default = TemplateFactory;
});