forked from aldeed/meteor-cfs-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfs-autoform.js
199 lines (173 loc) · 6.1 KB
/
cfs-autoform.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
CfsAutoForm = CfsAutoForm || {};
CfsAutoForm.Util = Util;
CfsAutoForm.Hooks = Hooks;
CfsAutoForm.deleteUploadedFiles = function(template) {
template.$('.cfsaf-hidden').each(function () {
var uploadedFiles = $(this).data("cfsaf_uploaded-files") || [];
_.each(uploadedFiles, function ( f ) {
f.remove();
});
});
};
if (Meteor.isClient) {
// Adds a custom "cfs-file" input type that AutoForm will recognize
AutoForm.addInputType("cfs-file", {
template:"cfsFileField",
valueOut: function () {
return 'dummyId';
},
contextAdjust: function (context) {
context.atts.placeholder = context.atts.placeholder || "Click to upload a file or drop it here";
context.atts["class"] = (context.atts["class"] || "") + " cfsaf-field";
return context;
}
});
// Adds a custom "cfs-files" input type that AutoForm will recognize
AutoForm.addInputType("cfs-files", {
template:"cfsFilesField",
valueOut: function () {
return ["dummyId"];
},
contextAdjust: function (context) {
context.atts.placeholder = context.atts.placeholder || "Click to upload files or drop them here";
context.atts["class"] = (context.atts["class"] || "") + " cfsaf-field";
return context;
}
});
function textInputAtts() {
var atts = _.clone(this.atts);
delete atts.collection;
// we want the schema key tied to the hidden file field only
delete atts["data-schema-key"];
atts["class"] = (atts["class"] || "") + " form-control";
return atts;
}
function fileInputAtts() {
return {'data-schema-key': this.atts["data-schema-key"]};
}
Template.cfsFileField_bootstrap3.helpers({
textInputAtts: textInputAtts,
fileInputAtts: fileInputAtts,
value: function() {
var id = Template.currentData().value
if(id) {
return id
} else {
return null
}
},
available: function() {
// If there's a file attached
var id = Template.currentData().value
return id
},
url: function () {
// Get the id of the file
var id = Template.currentData().value
// If there's a file...
if (id) {
// Get the file and the file data
var collection = FS._collections[this.atts.collection]
var file = collection.findOne({ _id: id })
var fileType = file.type()
// If the file is a image return the url
if(fileType.match(/image/g)) {
return file.url()
} else {
return false
}
} else {
return false
}
},
});
Template.cfsFilesField_bootstrap3.helpers({
textInputAtts: textInputAtts,
fileInputAtts: fileInputAtts
});
var hookTracking = {};
Template.cfsFileField_bootstrap3.rendered =
Template.cfsFilesField_bootstrap3.rendered = function () {
var formId;
// backwards compatibility with pre 5.0 autoform
if (typeof AutoForm.find === 'function') {
formId = AutoForm.find().formId;
} else {
formId = AutoForm.getFormId();
}
// By adding hooks dynamically on render, hopefully any user hooks will have
// been added before so we won't disrupt expected behavior.
if (!hookTracking[formId]) {
hookTracking[formId] = true;
addAFHook(formId);
}
};
var singleHandler = function (event, template) {
var fileList = [];
FS.Utility.eachFile(event, function (f) {
fileList.push(f.name);
});
template.$('.cfsaf-field').val(fileList.join(", "));
var fileList = event.originalEvent.dataTransfer ? event.originalEvent.dataTransfer.files : event.currentTarget.files;
// Store the FileList on the file input for later
template.$('.cfsaf-hidden').data("cfsaf_files", fileList);
};
Template.cfsFileField_bootstrap3.events({
'click .cfsaf-field': function (event, template) {
template.$('.cfsaf-hidden').click();
},
'change .cfsaf-hidden': singleHandler,
'dropped .cfsaf-field': singleHandler,
'click .af-remove-file': function(event, template) {
// When the remove link is clicked...
event.preventDefault();
// Set the data attributes marking it for removal upon save
// Actual removing will happen only if saved through hooks
template.$('.cfsaf-hidden').data('remove', 'true');
// Remove the file preview and the remove link
template.$('.af-file-preview').fadeOut();
template.$(event.target).fadeOut();
}
});
var multipleHandler = function (event, template) {
// Get the FileList object from the event object
var fileList = event.originalEvent.dataTransfer ? event.originalEvent.dataTransfer.files : event.currentTarget.files;
// Store the FileList on the file input for later. We store an array of
// separate FileList objects. Browsers don't let you add/remove items from
// an existing FileList.
var fileListList = template.$('.cfsaf-hidden').data("cfsaf_files_multi") || [];
fileListList.push(fileList);
template.$('.cfsaf-hidden').data("cfsaf_files_multi", fileListList);
// Get full list of files to display in the visible, read-only field
var fullNameList = [];
_.each(fileListList, function (fileList) {
_.each(fileList, function (f) {
fullNameList.push(f.name);
});
});
template.$('.cfsaf-field').val(fullNameList.join(", "));
};
Template.cfsFilesField_bootstrap3.events({
'click .cfsaf-field': function (event, template) {
template.$('.cfsaf-hidden').click();
},
'change .cfsaf-hidden': multipleHandler,
'dropped .cfsaf-field': multipleHandler
});
function addAFHook(formId) {
AutoForm.addHooks(formId, {
before: {
// We add a before.insert hook to upload all the files in the form.
// This hook doesn't allow the form to continue submitting until
// all the files are successfully uploaded.
insert: CfsAutoForm.Hooks.beforeInsert,
update: CfsAutoForm.Hooks.beforeUpdate
},
after: {
// We add an after.insert hook to delete uploaded files if the doc insert fails.
insert: CfsAutoForm.Hooks.afterInsert,
update: CfsAutoForm.Hooks.afterUpdate
}
});
}
}