-
Notifications
You must be signed in to change notification settings - Fork 3
/
gloomy.js
120 lines (94 loc) · 3.13 KB
/
gloomy.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
/*!
* Gloomy v1.0.0
* A simple and semantic substitution template engine for the browser.
* Project: https://github.com/pazguille/gloomy
* Copyright (c) 2013, by @pazguille (http://pazguille.me)
* Released under the MIT license.
*/
(function (window) {
'use strict';
var regexp = new RegExp('\\:\\[([a-zA-Z0-9.]+)\\]', 'gm');
function render(tpl, data) {
return tpl.innerHTML.replace(regexp, function () {
var key = arguments[1],
keys = key.split('.'),
value;
function defineValue(k) {
value = value[k];
}
if (keys.length > 1) {
value = data;
keys.forEach(defineValue);
}
value = (data[key] !== undefined) ? data[key] : value;
return value;
});
}
/**
* Creates a new instance of `Gloomy`.
* @param {HTMLElement} [template] - A given gloomy template.
* @param {HTMLElement} [container] - A given HTMLElement to insert the result of a template rendered with the data.
* @param {Array} [data] - A given model data to render.
* @returns {gloomy} Returns a new instance of Gloomy.
*/
function Gloomy(template, container, data) {
if (template) {
this.use(template);
}
if (container) {
this.into(container);
}
if (data) {
this.render(data);
}
return this;
}
/**
* Sets what gloomy template would you to use.
* @param {HTMLElement} [template] - A given gloomy template.
* @returns {gloomy} Returns an instance of Gloomy.
*/
Gloomy.prototype.use = function (template) {
this.template = template;
return this;
};
/**
* Sets a container to insert the result of a template rendered with the data.
* @param {HTMLElement} [container] - A given HTMLElement to insert the result of a template rendered with the data.
* @returns {gloomy} Returns an instance of Gloomy.
*/
Gloomy.prototype.into = function (container) {
this.container = container;
return this;
};
/**
* Render the template with a given data and inserts the output in a container.
* @param {Array} data - A given model data to render.
* @returns {gloomy} Returns an instance of Gloomy.
*/
Gloomy.prototype.render = function (data) {
var that = this,
html = [];
data = Array.isArray(data) ? data : [data];
data.forEach(function (data) {
html.push(render(that.template.cloneNode(true), data));
});
this.container.innerHTML = html.join('');
return this;
};
/**
* Expose Gloomy
*/
// AMD suppport
if (typeof window.define === 'function' && window.define.amd !== undefined) {
window.define('Gloomy', [], function () {
return Gloomy;
});
// CommonJS suppport
} else if (typeof module !== 'undefined' && module.exports !== undefined) {
module.exports = Gloomy;
// Default
} else {
window.Gloomy = Gloomy;
}
}(this));