jquery.fileupload-audio.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * jQuery File Upload Audio Preview Plugin
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, require */
  12. (function (factory) {
  13. 'use strict';
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['jquery', 'load-image', './jquery.fileupload-process'], factory);
  17. } else if (typeof exports === 'object') {
  18. // Node/CommonJS:
  19. factory(
  20. require('jquery'),
  21. require('blueimp-load-image/js/load-image'),
  22. require('./jquery.fileupload-process')
  23. );
  24. } else {
  25. // Browser globals:
  26. factory(window.jQuery, window.loadImage);
  27. }
  28. })(function ($, loadImage) {
  29. 'use strict';
  30. // Prepend to the default processQueue:
  31. $.blueimp.fileupload.prototype.options.processQueue.unshift(
  32. {
  33. action: 'loadAudio',
  34. // Use the action as prefix for the "@" options:
  35. prefix: true,
  36. fileTypes: '@',
  37. maxFileSize: '@',
  38. disabled: '@disableAudioPreview'
  39. },
  40. {
  41. action: 'setAudio',
  42. name: '@audioPreviewName',
  43. disabled: '@disableAudioPreview'
  44. }
  45. );
  46. // The File Upload Audio Preview plugin extends the fileupload widget
  47. // with audio preview functionality:
  48. $.widget('blueimp.fileupload', $.blueimp.fileupload, {
  49. options: {
  50. // The regular expression for the types of audio files to load,
  51. // matched against the file type:
  52. loadAudioFileTypes: /^audio\/.*$/
  53. },
  54. _audioElement: document.createElement('audio'),
  55. processActions: {
  56. // Loads the audio file given via data.files and data.index
  57. // as audio element if the browser supports playing it.
  58. // Accepts the options fileTypes (regular expression)
  59. // and maxFileSize (integer) to limit the files to load:
  60. loadAudio: function (data, options) {
  61. if (options.disabled) {
  62. return data;
  63. }
  64. var file = data.files[data.index],
  65. url,
  66. audio;
  67. if (
  68. this._audioElement.canPlayType &&
  69. this._audioElement.canPlayType(file.type) &&
  70. ($.type(options.maxFileSize) !== 'number' ||
  71. file.size <= options.maxFileSize) &&
  72. (!options.fileTypes || options.fileTypes.test(file.type))
  73. ) {
  74. url = loadImage.createObjectURL(file);
  75. if (url) {
  76. audio = this._audioElement.cloneNode(false);
  77. audio.src = url;
  78. audio.controls = true;
  79. data.audio = audio;
  80. return data;
  81. }
  82. }
  83. return data;
  84. },
  85. // Sets the audio element as a property of the file object:
  86. setAudio: function (data, options) {
  87. if (data.audio && !options.disabled) {
  88. data.files[data.index][options.name || 'preview'] = data.audio;
  89. }
  90. return data;
  91. }
  92. }
  93. });
  94. });