Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

104 wiersze
3.0 KiB

  1. /*
  2. * JavaScript Load Image Fetch
  3. * https://github.com/blueimp/JavaScript-Load-Image
  4. *
  5. * Copyright 2017, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, module, require, Promise */
  12. ;(function (factory) {
  13. 'use strict'
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['jquery/fileUploader/vendor/blueimp-load-image/js/load-image'], factory)
  17. } else if (typeof module === 'object' && module.exports) {
  18. factory(require('jquery/fileUploader/vendor/blueimp-load-image/js/load-image'))
  19. } else {
  20. // Browser globals:
  21. factory(window.loadImage)
  22. }
  23. })(function (loadImage) {
  24. 'use strict'
  25. var global = loadImage.global
  26. if (
  27. global.fetch &&
  28. global.Request &&
  29. global.Response &&
  30. global.Response.prototype.blob
  31. ) {
  32. loadImage.fetchBlob = function (url, callback, options) {
  33. /**
  34. * Fetch response handler.
  35. *
  36. * @param {Response} response Fetch response
  37. * @returns {Blob} Fetched Blob.
  38. */
  39. function responseHandler(response) {
  40. return response.blob()
  41. }
  42. if (global.Promise && typeof callback !== 'function') {
  43. return fetch(new Request(url, callback)).then(responseHandler)
  44. }
  45. fetch(new Request(url, options))
  46. .then(responseHandler)
  47. .then(callback)
  48. [
  49. // Avoid parsing error in IE<9, where catch is a reserved word.
  50. // eslint-disable-next-line dot-notation
  51. 'catch'
  52. ](function (err) {
  53. callback(null, err)
  54. })
  55. }
  56. } else if (
  57. global.XMLHttpRequest &&
  58. // https://xhr.spec.whatwg.org/#the-responsetype-attribute
  59. new XMLHttpRequest().responseType === ''
  60. ) {
  61. loadImage.fetchBlob = function (url, callback, options) {
  62. /**
  63. * Promise executor
  64. *
  65. * @param {Function} resolve Resolution function
  66. * @param {Function} reject Rejection function
  67. */
  68. function executor(resolve, reject) {
  69. options = options || {} // eslint-disable-line no-param-reassign
  70. var req = new XMLHttpRequest()
  71. req.open(options.method || 'GET', url)
  72. if (options.headers) {
  73. Object.keys(options.headers).forEach(function (key) {
  74. req.setRequestHeader(key, options.headers[key])
  75. })
  76. }
  77. req.withCredentials = options.credentials === 'include'
  78. req.responseType = 'blob'
  79. req.onload = function () {
  80. resolve(req.response)
  81. }
  82. req.onerror = req.onabort = req.ontimeout = function (err) {
  83. if (resolve === reject) {
  84. // Not using Promises
  85. reject(null, err)
  86. } else {
  87. reject(err)
  88. }
  89. }
  90. req.send(options.body)
  91. }
  92. if (global.Promise && typeof callback !== 'function') {
  93. options = callback // eslint-disable-line no-param-reassign
  94. return new Promise(executor)
  95. }
  96. return executor(callback, callback)
  97. }
  98. }
  99. })