|
10 | 10 | * |
11 | 11 | */ |
12 | 12 |
|
13 | | -function handleErrors (msg) { |
| 13 | +function handleErrors(msg) { |
14 | 14 | throw new Error(msg); |
15 | 15 | } |
16 | 16 |
|
17 | | -function isArray (obj) { |
| 17 | +function isArray(obj) { |
18 | 18 | return Object.prototype.toString.call(obj) === '[object Array]'; |
19 | 19 | } |
20 | 20 |
|
21 | | -function isObject (obj) { |
| 21 | +function isObject(obj) { |
22 | 22 | return obj !== null && typeof obj === 'object'; |
23 | 23 | } |
24 | 24 |
|
25 | | -function isString (obj) { |
| 25 | +function isString(obj) { |
26 | 26 | return typeof obj === 'string' || obj instanceof String; |
27 | 27 | } |
28 | 28 |
|
29 | | -function isUndefined (obj) { |
30 | | - return typeof obj == "undefined"; |
| 29 | +function isUndefined(obj) { |
| 30 | + return typeof obj === 'undefined'; |
31 | 31 | } |
32 | 32 |
|
33 | | -angular |
34 | | - .module('fileSaver', []) |
35 | | - .factory('SaveAs', ['$window', SaveAs]); |
| 33 | +function SaveAs($window) { |
| 34 | + var saveAs = $window.saveAs; |
| 35 | + var Blob = $window.Blob; |
36 | 36 |
|
37 | | - function SaveAs ($window) { |
38 | | - var saveAs = $window.saveAs; |
39 | | - var Blob = $window.Blob; |
| 37 | + if (isUndefined(saveAs)) { |
| 38 | + handleErrors('saveAs is not supported. Please include saveAs polyfill'); |
| 39 | + } |
40 | 40 |
|
41 | | - if (isUndefined(saveAs)) { |
42 | | - handleErrors('saveAs is not supported. Please include saveAs polyfill'); |
43 | | - } |
| 41 | + if (isUndefined(Blob)) { |
| 42 | + handleErrors('Blob is not supported. Please include blob polyfill'); |
| 43 | + } |
44 | 44 |
|
45 | | - if (isUndefined(Blob)) { |
46 | | - handleErrors('Blob is not supported. Please include blob polyfill'); |
47 | | - } |
| 45 | + function isBlobInstance(obj) { |
| 46 | + return obj instanceof Blob; |
| 47 | + } |
48 | 48 |
|
49 | | - function isBlobInstance (obj) { |
50 | | - return obj instanceof Blob; |
| 49 | + function save(blob, filename) { |
| 50 | + try { |
| 51 | + saveAs(blob, filename); |
| 52 | + } catch(err) { |
| 53 | + handleErrors(err.message); |
51 | 54 | } |
| 55 | + } |
52 | 56 |
|
53 | | - function save(blob, filename) { |
54 | | - try { |
55 | | - saveAs(blob, filename); |
56 | | - } catch(err) { |
57 | | - handleErrors(err.message); |
| 57 | + return { |
| 58 | + |
| 59 | + /** |
| 60 | + * download - Immediately starts saving a file, returns undefined. |
| 61 | + * |
| 62 | + * @param {array|Blob} data Represented as an array or a Blob object |
| 63 | + * @param {string} filename |
| 64 | + * @param {object} options Set of Blob constructor options. |
| 65 | + * Optional parameter, if Blob object is passed as first argument |
| 66 | + * @return {undefined} |
| 67 | + */ |
| 68 | + |
| 69 | + download: function(data, filename, options) { |
| 70 | + if (!isArray(data) || !isBlobInstance(data)) { |
| 71 | + handleErrors('Data argument should be represented as an array or Blob instance'); |
58 | 72 | } |
59 | | - } |
60 | 73 |
|
61 | | - return { |
62 | | - |
63 | | - /** |
64 | | - * download - Immediately starts saving a file, returns undefined. |
65 | | - * |
66 | | - * @param {array|Blob} data Represented as an array or a Blob object |
67 | | - * @param {string} filename |
68 | | - * @param {object} options Set of Blob constructor options. |
69 | | - * Optional parameter, if Blob object is passed as first argument |
70 | | - * @return {undefined} |
71 | | - */ |
72 | | - |
73 | | - download: function (data, filename, options) { |
74 | | - if (!isArray(data) || !isBlobInstance(data)) { |
75 | | - handleErrors('Data argument should be represented as an array or Blob instance'); |
76 | | - } |
77 | | - |
78 | | - if (!isString(filename)) { |
79 | | - handleErrors('Filename argument should be a string'); |
80 | | - } |
81 | | - |
82 | | - if (!isObject(options)) { |
83 | | - handleErrors('Options argument should be an object'); |
84 | | - } |
85 | | - |
86 | | - if (isBlobInstance(data)) { |
87 | | - return save(data, filename); |
88 | | - } |
89 | | - |
90 | | - var blob = new Blob(data, options); |
91 | | - return save(blob, filename); |
| 74 | + if (!isString(filename)) { |
| 75 | + handleErrors('Filename argument should be a string'); |
92 | 76 | } |
93 | | - }; |
94 | | - } |
| 77 | + |
| 78 | + if (!isObject(options)) { |
| 79 | + handleErrors('Options argument should be an object'); |
| 80 | + } |
| 81 | + |
| 82 | + if (isBlobInstance(data)) { |
| 83 | + return save(data, filename); |
| 84 | + } |
| 85 | + |
| 86 | + var blob = new Blob(data, options); |
| 87 | + return save(blob, filename); |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +angular |
| 93 | + .module('fileSaver', []) |
| 94 | + .factory('SaveAs', ['$window', SaveAs]); |
0 commit comments