-
Notifications
You must be signed in to change notification settings - Fork 11
/
autoform-cloudinary.js
90 lines (74 loc) · 2 KB
/
autoform-cloudinary.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
AutoForm.addInputType('cloudinary', {
template: 'afCloudinary',
valueOut: function () {
return this.val();
}
});
Meteor.startup(function () {
Meteor.call('publicCredentials', function(err, res) {
if (res) {
$.cloudinary.config({
cloud_name: res.cloudName,
api_key: res.apiKey
});
} else {
$.cloudinary.config({
cloud_name: Meteor.settings.public.CLOUDINARY_CLOUD_NAME,
api_key: Meteor.settings.public.CLOUDINARY_API_KEY
});
}
});
});
var templates = ['afCloudinary', 'afCloudinary_bootstrap3'];
_.each(templates, function (tmpl) {
Template[tmpl].onCreated(function () {
var self = this;
self.url = new ReactiveVar();
self.initialValueChecked = false;
self.checkInitialValue = function () {
Tracker.nonreactive(function () {
if (! self.initialValueChecked && ! self.url.get() && self.data.value) {
self.url.set(self.data.value);
self.initialValueChecked = true;
}
});
};
});
Template[tmpl].onRendered(function () {
var self = this;
Meteor.call('afCloudinarySign', function (err, res) {
if (err) {
return console.log(err);
}
self.$('input[name=file]').cloudinary_fileupload({
formData: res
});
});
self.$('input[name=file]').on('fileuploaddone', function (e, data) {
self.url.set(data.result.secure_url);
Tracker.flush();
});
self.$(self.firstNode).closest('form').on('reset', function () {
self.url.set(null);
});
});
Template[tmpl].helpers({
url: function () {
var t = Template.instance();
t.checkInitialValue();
return t.url.get();
},
accept: function () {
return this.atts.accept || 'image/*';
}
});
Template[tmpl].events({
'click button': function (e, t) {
t.$('input[name=file]').click();
},
'click .js-remove': function (e, t) {
e.preventDefault();
t.url.set(null);
}
});
});