-
Notifications
You must be signed in to change notification settings - Fork 2k
/
image-upload.js
192 lines (170 loc) · 5.7 KB
/
image-upload.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
/* Image Upload
*
*/
this.ckan.module('image-upload', function($, _) {
return {
/* options object can be extended using data-module-* attributes */
options: {
is_url: true,
is_upload: false,
field_upload: 'image_upload',
field_url: 'image_url',
field_clear: 'clear_upload',
upload_label: '',
i18n: {
upload: _('Upload'),
url: _('Link'),
remove: _('Remove'),
upload_label: _('Image'),
upload_tooltip: _('Upload a file on your computer'),
url_tooltip: _('Link to a URL on the internet (you can also link to a API)'),
remove_tooltip: _('Reset this')
},
template: [
''
].join("\n")
},
state: {
attached: 1,
blank: 2,
web: 3
},
/* Initialises the module setting up elements and event listeners.
*
* Returns nothing.
*/
initialize: function () {
$.proxyAll(this, /_on/);
var options = this.options;
// firstly setup the fields
var field_upload = 'input[name="' + options.field_upload + '"]';
var field_url = 'input[name="' + options.field_url + '"]';
var field_clear = 'input[name="' + options.field_clear + '"]';
this.input = $(field_upload, this.el);
this.field_url = $(field_url, this.el).parents('.control-group');
this.field_image = this.input.parents('.control-group');
// Is there a clear checkbox on the form already?
var checkbox = $(field_clear, this.el);
if (checkbox.length > 0) {
options.is_upload = true;
checkbox.parents('.control-group').remove();
}
// Adds the hidden clear input to the form
this.field_clear = $('<input type="hidden" name="clear_upload">')
.appendTo(this.el);
// Button to set the field to be a URL
this.button_url = $('<a href="javascript:;" class="btn"><i class="icon-globe"></i> '+this.i18n('url')+'</a>')
.prop('title', this.i18n('url_tooltip'))
.on('click', this._onFromWeb)
.insertAfter(this.input);
// Button to attach local file to the form
this.button_upload = $('<a href="javascript:;" class="btn"><i class="icon-cloud-upload"></i>'+this.i18n('upload')+'</a>')
.insertAfter(this.input);
// Button to reset the form back to the first from when there is a image uploaded
this.button_remove = $('<a href="javascript:;" class="btn btn-danger" />')
.text(this.i18n('remove'))
.on('click', this._onRemove)
.insertAfter(this.button_upload);
// Button for resetting the form when there is a URL set
$('<a href="javascript:;" class="btn btn-danger btn-remove-url"><i class="icon-remove"></i></a>')
.prop('title', this.i18n('remove_tooltip'))
.on('click', this._onRemove)
.insertBefore($('input', this.field_url));
// Update the main label
$('label[for="field-image-upload"]').text(options.upload_label || this.i18n('upload_label'));
// Setup the file input
this.input
.on('mouseover', this._onInputMouseOver)
.on('mouseout', this._onInputMouseOut)
.on('change', this._onInputChange)
.prop('title', this.i18n('upload_tooltip'))
.css('width', this.button_upload.outerWidth());
// Fields storage. Used in this.changeState
this.fields = $('<i />')
.add(this.button_remove)
.add(this.button_upload)
.add(this.button_url)
.add(this.input)
.add(this.field_url)
.add(this.field_image);
// Setup the initial state
if (options.is_url) {
this.changeState(this.state.web);
} else if (options.is_upload) {
this.changeState(this.state.attached);
} else {
this.changeState(this.state.blank);
}
},
/* Method to change the display state of the image fields
*
* state - Pseudo constant for passing the state we should be in now
*
* Examples
*
* this.changeState(this.state.web); // Sets the state in URL mode
*
* Returns nothing.
*/
changeState: function(state) {
this.fields.hide();
if (state == this.state.blank) {
this.button_upload
.add(this.field_image)
.add(this.button_url)
.add(this.input)
.show();
} else if (state == this.state.attached) {
this.button_remove
.add(this.field_image)
.show();
} else if (state == this.state.web) {
this.field_url
.show();
}
},
/* Event listener for when someone sets the field to URL mode
*
* Returns nothing.
*/
_onFromWeb: function() {
this.changeState(this.state.web);
$('input', this.field_url).focus();
if (this.options.is_upload) {
this.field_clear.val('true');
}
},
/* Event listener for resetting the field back to the blank state
*
* Returns nothing.
*/
_onRemove: function() {
this.changeState(this.state.blank);
$('input', this.field_url).val('');
this.field_clear.val('true');
},
/* Event listener for when someone chooses a file to upload
*
* Returns nothing.
*/
_onInputChange: function() {
this.file_name = this.input.val();
this.field_clear.val('');
this.changeState(this.state.attached);
},
/* Event listener for when a user mouseovers the hidden file input
*
* Returns nothing.
*/
_onInputMouseOver: function() {
this.button_upload.addClass('hover');
},
/* Event listener for when a user mouseouts the hidden file input
*
* Returns nothing.
*/
_onInputMouseOut: function() {
this.button_upload.removeClass('hover');
}
}
});