-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfactory.js
135 lines (103 loc) · 2.67 KB
/
factory.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
125
126
127
128
129
130
131
132
133
134
/**
* The Factory class requires {@link https://olado.github.io/doT}
* @requires doT.js
*/
// Factory configurations
/**
* Store all badge's styles
* @constant {string[]}
*/
STYLENAMES = ['flat', 'flat-square', 'plastic', 'for-the-badge', 'social']
/**
* Base path to download template
* @constant {string}
*/
BASE_PATH = 'templates/'
/**
* Templates suffix
* @constant {string}
*/
SUFFIX = '-template.svg'
// Do not touch below
var canvas = document.createElement('canvas')
var context = canvas.getContext('2d')
context.font = '11px Verdana, "DejaVu Sans"'
/**
* Factory class for building badge
* @constructor
* @public
*/
Factory = function() {
/** @private */
this.count = 0
/** @private */
this.templates = {}
}
/**
* Download all templates
* @param {function} callback - Callback when templates are all downloaded
*/
Factory.prototype.downloadAll = function(callback) {
// For each name
STYLENAMES.forEach(function(name) {
// Path to template
var path = BASE_PATH + name + SUFFIX
// Keep scope
var that = this
// AJAX
var r = new XMLHttpRequest()
r.open('GET', path, true)
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return
// Compile with doT
var t = doT.template(r.responseText)
// Save it for later
that.templates[name] = t
// Template downloaded
++that.count
// Callback if ready
if (that.ready()) {
callback()
}
}
r.send()
}, this)
}
/**
* Check if templates are ready to use
* @return {boolean}
*/
Factory.prototype.ready = function() {
return this.count === STYLENAMES.length
}
/**
* Compile template to HTML
* @param {string} style - One of the stylenames
* @param {string} subject - The badge subject
* @param {string} value - The badge value
* @paran {string} color - The badge's value color
* @return {string} - HTML based on template
*/
Factory.prototype.compileBadge = function(style, subject, value, color) {
var sty = style
var func = this.templates[sty]
var sub = subject.replace(/_/g, ' ')
var val = value.replace(/_/g, ' ')
var subjectWidth = getCanvasSize(subject) + 10
var valueWidth = getCanvasSize(value) + 10
var html = func({colorB: color, widths: [subjectWidth, valueWidth], text: [sub, val], escapeXml: escapeXml, capitalize: capitalize, links: '', logoWidth: 0, logoPadding: 0})
return html
}
function getCanvasSize(text) {
return Math.ceil(context.measureText(text).width)
}
function escapeXml(s) {
return s.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
function capitalize(s) {
return s[0].toUpperCase() + s.slice(1)
}