Skip to content
Permalink
Newer
Older
100644 84 lines (65 sloc) 2.3 KB
May 7, 2013 01:46
1
define(function () {
November 19, 2013 14:06
2
3
'use strict';
4
May 22, 2013 20:54
5
var icon = {};
May 7, 2013 01:46
6
7
function changeColors(iconData, fillColor, strokeColor) {
8
var re;
9
10
if (fillColor) {
11
re = /(<!ENTITY fill_color ")(.*)(">)/;
12
iconData = iconData.replace(re, "$1" + fillColor + "$3");
13
}
14
15
if (strokeColor) {
16
re = /(<!ENTITY stroke_color ")(.*)(">)/;
17
iconData = iconData.replace(re, "$1" + strokeColor + "$3");
18
}
19
20
return iconData;
21
}
22
May 22, 2013 20:54
23
icon.load = function (iconInfo, callback) {
May 11, 2013 21:42
24
var source;
25
var dataHeader = "data:image/svg+xml,";
26
May 7, 2013 01:46
27
if ("uri" in iconInfo) {
28
source = iconInfo.uri;
May 25, 2013 19:16
29
} else if ("name" in iconInfo) {
May 30, 2013 11:14
30
source = "lib/graphics/icons/" + iconInfo.name + ".svg";
May 7, 2013 01:46
31
}
32
May 11, 2013 21:42
33
var fillColor = iconInfo.fillColor;
34
var strokeColor = iconInfo.strokeColor;
May 7, 2013 01:46
35
36
// If source is already a data uri, read it instead of doing
37
// the XMLHttpRequest
38
if (source.substring(0, 4) == 'data') {
39
var iconData = unescape(source.slice(dataHeader.length));
40
var newData = changeColors(iconData, fillColor, strokeColor);
41
callback(dataHeader + escape(newData));
42
return;
43
}
44
May 11, 2013 21:42
45
var client = new XMLHttpRequest();
May 7, 2013 01:46
46
47
client.onload = function () {
48
var iconData = this.responseText;
49
var newData = changeColors(iconData, fillColor, strokeColor);
50
callback(dataHeader + escape(newData));
May 7, 2013 01:46
51
};
52
53
client.open("GET", source);
54
client.send();
55
};
56
57
function getBackgroundURL(elem) {
58
var style = elem.currentStyle || window.getComputedStyle(elem, '');
59
// Remove prefix 'url(' and suffix ')' before return
60
return style.backgroundImage.slice(4, -1);
61
}
62
63
function setBackgroundURL(elem, url) {
64
elem.style.backgroundImage = "url('" + url + "')";
65
}
66
67
icon.colorize = function (elem, colors, callback) {
68
var iconInfo = {
69
"uri": getBackgroundURL(elem),
70
"strokeColor": colors.stroke,
71
"fillColor": colors.fill
72
};
73
May 22, 2013 20:54
74
icon.load(iconInfo, function (url) {
75
setBackgroundURL(elem, url);
76
if (callback) {
77
callback();
78
}
79
});
80
81
};
82
May 22, 2013 20:54
83
return icon;
May 7, 2013 01:46
84
});