-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbackend.js
152 lines (128 loc) · 4.37 KB
/
backend.js
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
import "./backend.css"
import "./_handler.js"
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.ContaoFineUploaderBackend = factory(root);
}
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
'use strict';
/**
* Default settings
*/
var defaults = {
ajaxActionName: 'fineuploader_reload'
};
/**
* Plugin constructor
* @param {object} container
* @param {object} config
* @param {object} options
* @constructor
*/
function Plugin(container, config, options) {
this.container = container;
this.config = config;
options = options || {};
// Clone the "defaults" object
this.settings = JSON.parse(JSON.stringify(defaults));
// Extend default with options
for (var item in options) {
if (options.hasOwnProperty(item)) {
this.settings[item] = options[item];
}
}
this.initUploader();
this.makeSortable();
}
Plugin.prototype = {
/**
* Initialize the uploader
*/
initUploader: function () {
this.config.configCallback = this.configCallback.bind(this);
this.uploader = new ContaoFineUploader(this.container, this.config);
this.uploader.initFineUploader();
},
/**
* Config callback
*
* @param {object} config
*/
configCallback: function (config) {
config.callbacks.onUpload = this.onUploadCallback;
config.callbacks.onComplete = this.onCompleteCallback.bind(this);
},
/**
* Make items sortable
*/
makeSortable: function () {
var sortable = this.uploader.ajaxContainer.querySelector('.sortable');
if (sortable === null) {
return;
}
var i;
var list = new Sortables(sortable, {
contstrain: true,
opacity: 0.6
}).addEvent('complete', function () {
var els = [],
lis = sortable.getChildren('li');
for (i = 0; i < lis.length; i++) {
els.push(lis[i].get('data-item-id'));
}
this.uploader.field.value = els.join(',');
}.bind(this));
list.fireEvent('complete'); // Initial sorting
},
/**
* Handle the onUpload callback
*/
onUploadCallback: function () {
AjaxRequest.displayBox(Contao.lang.loading + ' …');
},
/**
* Handle the onComplete callback
*
* @param {string} id
* @param {string} name
* @param {object} result
*/
onCompleteCallback: function (id, name, result) {
if (!result.success) {
AjaxRequest.hideBox();
return;
}
// Trigger the default callback
this.uploader.onComplete.apply(this.uploader, arguments);
// Return if there are still files in progress
if (this.uploader.fineUploader.getInProgress() > 0) {
return;
}
new Request.Contao({
field: this.uploader.field.name,
evalScripts: false,
onSuccess: function (txt, json) {
this.uploader.setAjaxContainerContent(json.content);
// Execute the scripts
json.javascript && Browser.exec(json.javascript);
// Hide the loading box
AjaxRequest.hideBox();
// Fire the ajax change event
window.fireEvent('ajax_change');
// Make the elements sortable
this.makeSortable();
}.bind(this)
}).post({
'action': this.settings.ajaxActionName,
'name': this.uploader.field.name,
'value': this.uploader.currentValue,
'REQUEST_TOKEN': this.uploader.field.form.querySelector('input[name="REQUEST_TOKEN"]').value
});
}
};
return Plugin;
});