-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-svg-file.amd.js
184 lines (160 loc) · 4.88 KB
/
load-svg-file.amd.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
/**
* @file Load SVG files over XHR and embed the SVG content into the DOM.
* @version 1.0.1
* @copyright Richard Szakacs, richardszkcs@gmail.com, www.richardszkcs.com
* @license MIT
*/
define(function () {
'use strict';
/**
* The options of the SVG content loading function.
*
* @typedef {Object} optionsType
*
* @property {string} [class=''] - The class of the element, in which the content of the SVG file will be loaded.
* @property {boolean} [hide=true] - Hide the element to prevent showing the content of the loaded SVG file.
*/
/**
* The SVG content loading callback.
*
* @callback callbackType
*
* @param {Error} error - The error, that caused the failure.
*/
/**
* Loads an SVG file using XHR and embeds its content into the DOM.
*
* @function loadSvgFile
*
* @param {string} url - The URL of the SVG file to load.
* The .svg extension can be omitted.
* @param {optionsType|callbackType} [optionsOrCallback] - The options of the SVG file loader or the callback.
* @param {callbackType} [callback] - The result callback
*
* @returns {Promise|null} Promise, where available, otherwise null.
*
* @example
* // Loading an SVG file.
* loadSvgFile('images/icons.svg')
*
* @example
* // Loading an SVG file with options.
* loadSvgFile('images/icons.svg', {
* class: 'custom-class', // custom class on the container element
* hide: false // don't hide the container element
* })
*
* @example
* // Loading an SVG file with callback.
* loadSvgFile('images/icons.svg', function (error) {
* if (error) {
* throw error
* }
* console.log('SVG Loaded successfully')
* })
*
* @example
* // Loading an SVG file with options and callback.
* loadSvgFile(
* 'images/icons.svg',
* {
* class: 'custom-class', // custom class on the container element
* hide: false // don't hide the container element
* },
* function (error) {
* if (error) {
* throw error
* }
* console.log('SVG Loaded successfully')
* }
* )
*
* @example
* // Loading an SVG file using Promise - ES2015.
* loadSvgFile('images/icons.svg')
* .then(() => console.log('SVG Loaded successfully'))
* .catch(error => console.log(error))
*
* @example
* // Loading an SVG file using async/await - ES2017.
* await loadSvgFile('images/icons.svg')
*
* @example
* // Loading an SVG file, omitting the extension.
* loadSvgFile('images/icons')
*
* @example
* // Using in Node.js (CommonJS).
* const loadSvgFile = require('load-svg-file')
*
* @example
* // Using in ES6.
* import loadSvgFile from 'load-svg-file'
*
* @example
* // Using with RequireJS (AMD).
* require(['loadSvgFile'], function(loadSvgFile) {
* console.log('loadSvgFile ready')
* })
*/
function loadSvgFile(url, optionsOrCallback, callback) {
var type = typeof url;
if (!url || type !== 'string') {
type = type !== 'string' ? type : 'empty ' + type;
throw new Error('The url must be a non-empty string, got: "' + type + '".');
}
// check if the url has the .svg extension, otherwise append to it automatically
if (url.lastIndexOf('.svg') !== url.length - 4) {
url += '.svg';
}
var options = optionsOrCallback || {};
type = typeof optionsOrCallback;
if (optionsOrCallback && type === 'object') {
options = optionsOrCallback;
} else if (type === 'function') {
options = {};
callback = optionsOrCallback;
}
var promise = void 0;
var res = void 0;
var rej = void 0;
if (Promise) {
promise = new Promise(function (resolve, reject) {
res = resolve;
rej = reject;
});
}
var xhr = new XMLHttpRequest();
xhr.onloadend = xhr.onerror = function () {
if (xhr.status === 200) {
var element = document.createElement('div');
element.innerHTML = xhr.response;
var elementClass = typeof options.class === 'string' ? options.class : '';
if (elementClass.length > 0) {
element.className = elementClass;
}
var hideElement = typeof options.hide === 'boolean' ? options.hide : true;
if (hideElement) {
element.style.display = 'none';
}
document.body.appendChild(element);
if (callback) {
callback();
} else if (promise) {
res();
}
} else {
var error = new Error('Cannot load SVG file: "' + url + '".');
if (callback) {
callback(error);
} else if (promise) {
rej(error);
}
}
};
xhr.open('get', url, true);
xhr.send(null);
return promise || null;
}
return loadSvgFile;
});