jquery.fileupload-audio.js 3.5 KB

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