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