-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjquery.svgInject.js
219 lines (173 loc) · 6.66 KB
/
jquery.svgInject.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
svgInject - v1.0.0
jQuery plugin for replacing img-tags with SVG content
by Robert Bue (@robert_bue)
Dual licensed under MIT and GPL.
*/
;(function($, window, document, undefined) {
var pluginName = 'svgInject';
/**
* Cache that helps to reduce requests
*/
function Cache(){
this._entries = {};
}
/**
* Cache prototype extension for inheritance
* @type {Object}
*/
Cache.prototype = {
/**
* Checks if there is already an entry with that URL
* @param {String} url
* @return {Boolean}
*/
isFileInCache : function( url ){
return (typeof this._entries[url] !== 'undefined');
},
/**
* Returns the entry of the given url if it exists. If not, false is returned;
* @param {String} url
* @return {Object||Boolean} Entry or false if there is no entry with that url
*/
getEntry : function( url ){
return this._entries[url] || false;
},
/**
* Adds an entry to the cache
* @param {String} url
* @param {Function} callback
*/
addEntry : function( url, callback ){
// check if the given callback is a function
var isCallbackFunction = (typeof callback === 'function');
// check if the entry is already in the cache
if(this._entries[url]){
// if the callback is a function and the data is loaded, we execute it instantly with the cached data
if(isCallbackFunction && !this._entries[url].isLoading){
callback(this._entries[url].data);
}else if(isCallbackFunction){ // if the callback is a function and the data is still loading, we push in into the callback array
this._entries[url].callbacks.push(callback);
}
return this._entries[url];
}else{
var callbacks = [];
if(isCallbackFunction){
callbacks.push(callback);
}
// put the entry into the cache
this._entries[url] = {
isLoading : true,
callbacks : callbacks
};
}
},
/**
* Updates the entry after the data is loaded and executes all the callbacks for that entry
* @param {String} url
* @param {*} data
*/
addEntryData : function( url, data ){
var entry = this._entries[url];
if(typeof entry !== 'undefined'){
entry.data = data;
entry.isLoading = false;
this.executeEntryCallbacks(url);
}
},
/**
* Executes all callback for the entry with the given url
* @param {String} url
*/
executeEntryCallbacks : function( url ){
var entry = this._entries[url];
if(typeof entry !== 'undefined'){
for(var i = 0, j = entry.callbacks.length; i < j; i++){
entry.callbacks.shift()(entry.data);
i--;
j--;
}
}
}
};
function Plugin(element, options) {
this.element = element;
this.$element = $(element);
this.callback = options;
this._name = pluginName;
this._cache = Plugin._cache;
this.init();
}
Plugin._cache = new Cache();
Plugin.prototype = {
init: function() {
this.$element.css('visibility', 'hidden');
this.injectSVG(this.$element, this.callback);
},
injectSVG: function( $el, callback) {
var imgURL = $el.attr('src');
var imgID = $el.attr('id');
var imgClass = $el.attr('class');
var imgData = $el.clone(true).data();
var dimensions = {
w: $el.attr('width'),
h: $el.attr('height')
};
var _this = this;
// If the file is not in the cache, we request it
if(!this._cache.isFileInCache(imgURL)){
$.get(imgURL, function(data) {
var svg = $(data).find('svg');
// File is put into the cache
_this._cache.addEntryData( imgURL, svg );
});
}
// We add an entry to the cache with the given image url
this._cache.addEntry(imgURL , function( svg ){
// When the entry is loaded, the image gets replaces by the clone of the loaded svg
_this.replaceIMGWithSVG($el, svg.clone(), imgID, imgClass, imgURL, imgData, dimensions, callback);
});
},
replaceIMGWithSVG : function( $el, svg, imgID, imgClass, imgURL, imgData, dimensions, callback ){
if (typeof imgID !== undefined) {
svg = svg.attr('id', imgID);
}
if (typeof imgClass !== undefined) {
var cls = (svg.attr('class') !== undefined) ? svg.attr('class') : '';
svg = svg.attr('class', imgClass + ' ' + cls + ' replaced-svg');
}
if (typeof imgURL !== undefined) {
svg = svg.attr('data-url', imgURL);
}
$.each(imgData, function(name, value) {
svg[0].setAttribute('data-' + name, value);
});
svg = svg.removeAttr('xmlns:a');
var ow = parseFloat(svg.attr('width'));
var oh = parseFloat(svg.attr('height'));
if (dimensions.w && dimensions.h) {
$(svg).attr('width', dimensions.w);
$(svg).attr('height', dimensions.h);
} else if (dimensions.w) {
$(svg).attr('width', dimensions.w);
$(svg).attr('height', (oh / ow) * dimensions.w);
} else if (dimensions.h) {
$(svg).attr('height', dimensions.h);
$(svg).attr('width', (ow / oh) * dimensions.h);
}
$el.replaceWith(svg);
var js = new Function(svg.find('script').text());
js();
if ( typeof callback === 'function' ) {
callback();
}
}
};
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);