|
14 | 14 | ;(function ($) { |
15 | 15 | 'use strict' |
16 | 16 |
|
| 17 | + var urlAPI = $.URL || $.webkitURL |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates an object URL for a given File object. |
| 21 | + * |
| 22 | + * @param {File|Blob} blob File or Blob object |
| 23 | + * @returns {string|boolean} Returns object URL if API exists, else false. |
| 24 | + */ |
| 25 | + function createObjectURL(blob) { |
| 26 | + return urlAPI ? urlAPI.createObjectURL(blob) : false |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Revokes a given object URL. |
| 31 | + * |
| 32 | + * @param {string} url Blob object URL |
| 33 | + * @returns {undefined|boolean} Returns undefined if API exists, else false. |
| 34 | + */ |
| 35 | + function revokeObjectURL(url) { |
| 36 | + return urlAPI ? urlAPI.revokeObjectURL(url) : false |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Helper function to revoke an object URL |
| 41 | + * |
| 42 | + * @param {string} url Blob Object URL |
| 43 | + * @param {object} [options] Options object |
| 44 | + */ |
| 45 | + function revokeHelper(url, options) { |
| 46 | + if (url && url.slice(0, 5) === 'blob:' && !(options && options.noRevoke)) { |
| 47 | + revokeObjectURL(url) |
| 48 | + } |
| 49 | + } |
| 50 | + |
17 | 51 | /** |
18 | 52 | * Loads an image for a given File object. |
19 | 53 | * |
|
63 | 97 | if (err && $.console) console.log(err) // eslint-disable-line no-console |
64 | 98 | if (blob && loadImage.isInstanceOf('Blob', blob)) { |
65 | 99 | file = blob // eslint-disable-line no-param-reassign |
66 | | - url = loadImage.createObjectURL(file) |
| 100 | + url = createObjectURL(file) |
67 | 101 | } else { |
68 | 102 | url = file |
69 | 103 | if (options && options.crossOrigin) { |
|
91 | 125 | // (Firefox 3.6) support the File API but not Blobs: |
92 | 126 | loadImage.isInstanceOf('File', file) |
93 | 127 | ) { |
94 | | - url = loadImage.createObjectURL(file) |
| 128 | + url = createObjectURL(file) |
95 | 129 | if (url) { |
96 | 130 | img.src = url |
97 | 131 | return img |
|
113 | 147 | return executor(callback, callback) |
114 | 148 | } |
115 | 149 |
|
116 | | - var urlAPI = $.URL || $.webkitURL |
117 | | - |
118 | | - /** |
119 | | - * Helper function to revoke an object URL |
120 | | - * |
121 | | - * @param {string} url Blob Object URL |
122 | | - * @param {object} [options] Options object |
123 | | - */ |
124 | | - function revokeHelper(url, options) { |
125 | | - if (url && url.slice(0, 5) === 'blob:' && !(options && options.noRevoke)) { |
126 | | - loadImage.revokeObjectURL(url) |
127 | | - } |
128 | | - } |
129 | | - |
130 | 150 | // Determines if metadata should be loaded automatically. |
131 | 151 | // Requires the load image meta extension to load metadata. |
132 | 152 | loadImage.requiresMetaData = function (options) { |
|
169 | 189 | } |
170 | 190 | } |
171 | 191 |
|
172 | | - loadImage.createObjectURL = function (file) { |
173 | | - return urlAPI ? urlAPI.createObjectURL(file) : false |
174 | | - } |
175 | | - |
176 | | - loadImage.revokeObjectURL = function (url) { |
177 | | - return urlAPI ? urlAPI.revokeObjectURL(url) : false |
178 | | - } |
179 | | - |
180 | 192 | // Loads a given File object via FileReader interface, |
181 | 193 | // invokes the callback with the event object (load or error). |
182 | 194 | // The result can be read via event.target.result: |
|
195 | 207 | } |
196 | 208 |
|
197 | 209 | loadImage.global = $ |
| 210 | + loadImage.createObjectURL = createObjectURL |
| 211 | + loadImage.revokeObjectURL = revokeObjectURL |
198 | 212 |
|
199 | 213 | if (typeof define === 'function' && define.amd) { |
200 | 214 | define(function () { |
|
0 commit comments