-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.js
207 lines (164 loc) · 6.49 KB
/
ui.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function init () {
var self = this;
// init controls
self.uploaderConfig = self.uploaderConfig || {};
self.uploaderConfig.controls = self.uploaderConfig.controls || {};
self.uploaderConfig.controls.select = self.uploaderConfig.controls.select || ".btn-upload-select";
self.uploaderConfig.controls.upload = self.uploaderConfig.controls.upload || ".btn-upload-start";
self.uploaderConfig.controls.fileName = self.uploaderConfig.controls.fileName || ".upload-file-name";
self.config.options = self.config.options || {};
self.config.options.ui = self.config.options.ui || {};
self.config.options.ui.upload = self.config.options.ui.upload || ".uploadBtn";
// listen for internal events
self.on("renderUi", prepareUi);
// reset controls after upload finished
self.on("fileUploaded", resetControls);
self.on("uploadFailed", resetControls);
self.on("reset", resetControls);
// add / remove upload controls if permission changes
self.on("rebuildUi", buildUi);
self.on("getDocuments", getDocuments);
}
function getDocuments (request, callback) {
var self = this;
if (!callback) {
return console.error("No callback was provided");
}
// check if a template exists
if (!request.template || !self.template) {
return callback("A valid template needs to be provided");
}
request.template = request.template || self.template;
// check if uploader value is provided
if (!request.uploader) {
return callback("A valid uploader value needs to be provided");
}
// fetch documents
self.link("getDocuments", { data: request }, callback);
}
function prepareUi () {
var self = this;
// do not render uploader controls if no configuration is present
if (!self.template) {
return;
}
if (!self.uploaderConfig.uploaders || !self.uploaderConfig.html) {
return console.error("Uploader configuration missing core properties");
}
// stop here if no uploaders configured
if (!Object.keys(self.uploaderConfig.uploaders).length) {
return;
}
// get upload template html if not in cache
self.uploadTemplateCache = self.uploadTemplateCache || {};
if (!self.uploadTemplateCache[self.template._id]) {
var htmlToGet = self.template.options.uploader.html;
// handle i18n
if (typeof htmlToGet === "object") {
htmlToGet = htmlToGet[M.getLocale()];
}
if (!htmlToGet) { return; }
// fetch html
self.link(htmlToGet, function (err, html) {
if (err || !html) {
return;
}
// cache html
self.uploadTemplateCache[self.template._id] = html;
// render uploaders controls
buildUi.call(self);
});
} else {
// render uploaders controls
buildUi.call(self);
}
}
function buildUi () {
var self = this;
self.uploaders = {};
// get permissions
self.link("getUploadPermissions", { data: { template: self.template, formData: self.formData } }, function (err, permissions) {
if (err) {
console.error(err);
return;
}
for (var key in self.uploaderConfig.uploaders) {
if (!self.uploaderConfig.uploaders[key].container) continue;
// get container
var $container = $(self.uploaderConfig.uploaders[key].container);
if (!$container.length) continue;
// add controls to container if user is allowed, else clean it
if (permissions[key]) {
$container.html($(self.uploadTemplateCache[self.template._id]));
self.uploaders[key] = {};
self.uploaders[key].ref = $container;
} else {
$container.html("");
delete self.uploaders[key];
}
}
setupControls.call(self);
self.emit("uploadersRendered");
});
}
function setupControls () {
var self = this;
var $lastUploadUsed = '';
// listen for uploader events
for (var uploader in self.uploaders) {
if (!self.uploaders.hasOwnProperty(uploader)) continue;
(function (uploader) {
self.uploaders[uploader].ref.find(self.uploaderConfig.controls.select).on("click", function () {
$("[type=file]", $("form", self.dom)).click();
// slect the filename in next to the select
$lastUploadUsed = $(self.uploaderConfig.controls.fileName, self.uploaders[uploader].ref.has($(this)));
// add uploader value to form
if ($(".hiddenUploaderValue", $("form", self.dom)).length) {
$(".hiddenUploaderValue", $("form", self.dom)).val(uploader);
} else {
var $input = $("<input class='hiddenUploaderValue hide' type='hidden' name='uploader'>");
$("form", self.dom).append($input);
$input.val(uploader);
}
});
self.uploaders[uploader].ref.find(self.uploaderConfig.controls.upload).on("click", function () {
$(self.config.options.ui.upload, $("form", self.dom)).click();
});
})(uploader);
}
// listen for file-name value change
$("[type=file]", $("form", self.dom)).on("change", function () {
if (!$(this).val()) {
return resetFileName($(self.uploaderConfig.controls.fileName));
}
if ($lastUploadUsed.length) {
resetFileName($(self.uploaderConfig.controls.fileName));
$lastUploadUsed.html(($(this).val()).replace(/^.*[\\\/]/, ''));
}
});
// disable upload buttons on submit
$(self.dom).on("submit", "form", function () {
$(self.uploaderConfig.controls.upload).prop('disabled', true);
});
}
function resetFileName ($element) {
$element.html({
de: "Keine Datei ausgewählt",
fr: "Aucun fichier sélectionné",
it: "Nessun file selezionato"
}[M.getLocale()]);
}
function resetControls () {
var self = this;
// reset file names
resetFileName($(self.uploaderConfig.controls.fileName));
// enable upload buttons
$(self.uploaderConfig.controls.upload).prop('disabled', false);
// reset upload value
$("[type=file]", self.dom).val("");
// clear the hidden input uploader value
if ($(".hiddenUploaderValue", $("form", self.dom)).length) {
$(".hiddenUploaderValue", $("form", self.dom)).val("");
}
}
module.exports = init;