1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
(function($) {
Drupal.plupload = Drupal.plupload || {};
// Add Plupload events for autoupload and autosubmit.
Drupal.plupload.filesAddedCallback = function (up, files) {
setTimeout(function(){up.start()}, 100);
};
Drupal.plupload.uploadCompleteCallback = function(up, files) {
var $this = $("#"+up.settings.container);
// If there is submit_element trigger it.
var submit_element = window.Drupal.settings.plupload[$this.attr('id')].submit_element;
if (submit_element) {
$(submit_element).click();
}
// Otherwise submit default form.
else {
var $form = $this.parents('form');
$($form[0]).submit();
}
};
/**
* Attaches the Plupload behavior to each Plupload form element.
*/
Drupal.behaviors.plupload = {
attach: function (context, settings) {
$(".plupload-element", context).once('plupload-init', function () {
var $this = $(this);
// Merge the default settings and the element settings to get a full
// settings object to pass to the Plupload library for this element.
var id = $this.attr('id');
var defaultSettings = settings.plupload['_default'] ? settings.plupload['_default'] : {};
var elementSettings = (id && settings.plupload[id]) ? settings.plupload[id] : {};
var pluploadSettings = $.extend({}, defaultSettings, elementSettings);
// Process Plupload events.
if (elementSettings['init'] || false) {
if (!pluploadSettings.init) {
pluploadSettings.init = {};
}
for (var key in elementSettings['init']) {
var callback = elementSettings['init'][key].split('.');
var fn = window;
for (var j = 0; j < callback.length; j++) {
fn = fn[callback[j]];
}
if (typeof fn === 'function') {
pluploadSettings.init[key] = fn;
}
}
}
// Initialize Plupload for this element.
$this.pluploadQueue(pluploadSettings);
});
}
};
/**
* Attaches the Plupload behavior to each Plupload form element.
*/
Drupal.behaviors.pluploadform = {
attach: function(context, settings) {
$('form', context).once('plupload-form', function() {
if (0 < $(this).find('.plupload-element').length) {
var $form = $(this);
var originalFormAttributes = {
'method': $form.attr('method'),
'enctype': $form.attr('enctype'),
'action': $form.attr('action'),
'target': $form.attr('target')
};
$(this).submit(function(e) {
var completedPluploaders = 0;
var totalPluploaders = $(this).find('.plupload-element').length;
var errors = '';
$(this).find('.plupload-element').each( function(index){
var uploader = $(this).pluploadQueue();
var id = $(this).attr('id');
var defaultSettings = settings.plupload['_default'] ? settings.plupload['_default'] : {};
var elementSettings = (id && settings.plupload[id]) ? settings.plupload[id] : {};
var pluploadSettings = $.extend({}, defaultSettings, elementSettings);
//Only allow the submit to proceed if there are files and they've all
//completed uploading.
//TODO: Implement a setting for whether the field is required, rather
//than assuming that all are.
if (uploader.state == plupload.STARTED) {
errors += Drupal.t("Please wait while your files are being uploaded.");
}
else if (uploader.files.length == 0 && !pluploadSettings.required) {
completedPluploaders++;
}
else if (uploader.files.length == 0) {
errors += Drupal.t("@index: You must upload at least one file.\n",{'@index': (index + 1)});
}
else if (uploader.files.length > 0 && uploader.total.uploaded == uploader.files.length) {
completedPluploaders++;
}
else {
var stateChangedHandler = function() {
if (uploader.total.uploaded == uploader.files.length) {
uploader.unbind('StateChanged', stateChangedHandler);
completedPluploaders++;
if (completedPluploaders == totalPluploaders ) {
//Plupload's html4 runtime has a bug where it changes the
//attributes of the form to handle the file upload, but then
//fails to change them back after the upload is finished.
for (var attr in originalFormAttributes) {
$form.attr(attr, originalFormAttributes[attr]);
}
// Click a specific element if one is specified.
if (settings.plupload[id].submit_element) {
$(settings.plupload[id].submit_element).click();
}
else {
$form.submit();
}
return true;
}
}
};
uploader.bind('StateChanged', stateChangedHandler);
uploader.start();
}
});
if (completedPluploaders == totalPluploaders) {
//Plupload's html4 runtime has a bug where it changes the
//attributes of the form to handle the file upload, but then
//fails to change them back after the upload is finished.
for (var attr in originalFormAttributes) {
$form.attr(attr, originalFormAttributes[attr]);
}
return true;
}
else if (0 < errors.length){
alert(errors);
}
return false;
});
}
});
}
};
/**
* Helper function to compare version strings.
*
* Returns one of:
* - A negative integer if a < b.
* - A positive integer if a > b.
* - 0 if a == b.
*/
Drupal.plupload.compareVersions = function (a, b) {
a = a.split('.');
b = b.split('.');
// Return the most significant difference, if there is one.
for (var i=0; i < Math.min(a.length, b.length); i++) {
var compare = parseInt(a[i]) - parseInt(b[i]);
if (compare != 0) {
return compare;
}
}
// If the common parts of the two version strings are equal, the greater
// version number is the one with the most sections.
return a.length - b.length;
}
})(jQuery);
|