-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.interject.js
More file actions
167 lines (151 loc) · 5.41 KB
/
jquery.interject.js
File metadata and controls
167 lines (151 loc) · 5.41 KB
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
/*!
* Interject - Inline SVG injector
*/
;
(function ($, window, undefined) {
if (!$.Interject) {
$.Interject = {};
}
// localStorage Cache
var cache_localStorage = {
_values: false,
_keys: [],
remove: function (key) {
var index = this._keys.indexOf(key);
if (index > -1) {
localStorage.removeItem(key);
this._keys.splice(index, 1);
}
},
exist: function (key) {
if (this._keys.indexOf(key) > -1) {
return true;
}
else if (localStorage.getItem(key) !== null) {
this._keys.push(key);
return true;
}
return false;
},
get: function (key) {
if (this._keys.indexOf(key) == -1) this._keys.push(key);
return localStorage.getItem(key);
},
set: function (key, value, callback) {
if (value instanceof SVGSVGElement) {
var stringified = new XMLSerializer().serializeToString(value);
localStorage.setItem(key, stringified);
this._keys.push(key);
if (callback instanceof Function) callback(value);
return;
}
throw new Error(value + " is not of type [object SVGSVGElement].");
},
clear: function () {
this._keys.forEach(function (key) {
localStorage.removeItem(key);
});
this._keys = [];
}
},
// If no localStorage is present, cache in RAM. Not as useful.
// http://stackoverflow.com/questions/17104265/caching-a-jquery-ajax-response-in-javascript-browser
cache_ram = {
_values: [],
_keys: [],
remove: function (key) {
var index = this._keys.indexOf(key);
if (index > -1) {
this._values.splice(index, 1);
this._keys.splice(index, 1);
}
},
exist: function (key) {
return this._keys.indexOf(key) > -1;
},
get: function (key) {
var index = this._keys.indexOf(key);
if (index > -1) {
return this._values[index];
}
return null;
},
set: function (key, value, callback) {
this.remove(key);
this._keys.push(key);
this._values.push(value);
if (callback instanceof Function) callback(value);
},
clear: function () {
this._keys = [];
this._values = [];
}
},
localStorageSupported = false;
// Check if localstorage is supported
try {
localStorageSupported = 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
}
$.Interject.Cache = localStorageSupported ? cache_localStorage : cache_ram;
$.Interject.Svg = function (el, options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("Interject.Svg", base);
base.init = function () {
base.options = $.extend({}, $.Interject.Options, options);
};
// Run initializer
base.init();
var $img = base.$el;
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
$.ajax({
url: imgURL,
async: true,
cache: base.options.cache,
success: function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Add replaced image's ID to the new SVG
if (typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if (typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass + ' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
if (base.options.cache) $.Interject.Cache.set(imgURL, $svg[0]);
},
beforeSend: function () {
if (base.options.cache && $.Interject.Cache.exist(imgURL)) {
$img.replaceWith($.Interject.Cache.get(imgURL));
return false;
}
return true;
}
});
};
$.Interject.Options = {
cache: true
};
$.fn.interject_svg = function (options) {
return this.each(function () {
(new $.Interject.Svg(this, options));
});
};
$.fn.interject_clear_cache = function () {
$.Interject.Cache.clear();
return this;
};
}(jQuery, window));