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