Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 /*
A 2  * jQuery File Upload Video 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: 'loadVideo',
38             // Use the action as prefix for the "@" options:
39             prefix: true,
40             fileTypes: '@',
41             maxFileSize: '@',
42             disabled: '@disableVideoPreview'
43         },
44         {
45             action: 'setVideo',
46             name: '@videoPreviewName',
47             disabled: '@disableVideoPreview'
48         }
49     );
50
51     // The File Upload Video Preview plugin extends the fileupload widget
52     // with video preview functionality:
53     $.widget('blueimp.fileupload', $.blueimp.fileupload, {
54
55         options: {
56             // The regular expression for the types of video files to load,
57             // matched against the file type:
58             loadVideoFileTypes: /^video\/.*$/
59         },
60
61         _videoElement: document.createElement('video'),
62
63         processActions: {
64
65             // Loads the video file given via data.files and data.index
66             // as video 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             loadVideo: function (data, options) {
70                 if (options.disabled) {
71                     return data;
72                 }
73                 var file = data.files[data.index],
74                     url,
75                     video;
76                 if (this._videoElement.canPlayType &&
77                         this._videoElement.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                         video = this._videoElement.cloneNode(false);
85                         video.src = url;
86                         video.controls = true;
87                         data.video = video;
88                         return data;
89                     }
90                 }
91                 return data;
92             },
93
94             // Sets the video element as a property of the file object:
95             setVideo: function (data, options) {
96                 if (data.video && !options.disabled) {
97                     data.files[data.index][options.name || 'preview'] = data.video;
98                 }
99                 return data;
100             }
101
102         }
103
104     });
105
106 }));