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
|
/* Initiliaze several plugins. */
$(document).ready(function(){
$('.dropdown-toggle').dropdown();
$('input[type=file]').bootstrapFileInput();
});
/* Value of upload button. */
var buttonId = "";
/* Function to toggle button to disabled status. */
function toggleButton(id, enable, btnId){
if(enable == "enable"){
text = getBtnValue(btnId)
$(id).prop('enable', true);
$(id).removeClass('disabled');
$(btnId).text(text);
} else {
$(id).prop('disabled', true);
$(id).addClass('disabled');
$(btnId).html('<i class="fa fa-spinner fa-spin"></i> Uploading...');
}
};
/* Get the correct value from the button. */
function getBtnValue(buttonId){
switch(buttonId) {
case "#imageSubmit":
return "Upload!";
break;
case "#urlSubmit":
return "Fetch the image!";
break;
case "#base64Submit":
return "Decode the image!";
break;
}
}
/* Resets DOM for bootstrap modal. */
$('.modal').on('hidden.bs.modal', function(e){
// $('.btn-upload').addClass('hidden');
$("#imageLink").text('');
$('#created_img').remove();
$('.error-msg').text('');
toggleButton('.btn-upload', "enable", buttonId);
});
/* Do all the AJAX stuff. */
function doAjaxStuff(id, target, isFile){
var client = new XMLHttpRequest();
var file = document.getElementById(id);
/* Create a FormData instance */
var formData = new FormData();
/* formData is a file to upload. */
if(isFile == "isFile"){
if(file.files[0] === null) {
toggleButton('.btn-upload', "enable", buttonId);
alert("Please add a photo first.");
} else {
/* Add the file */
formData.append("image", file.files[0]);
}
} else {
if(file.value === null) {
toggleButton('.btn-upload', "enable", buttonId);
$(buttonId).text('Try again!');
alert("Please add a photo first.");
} else {
formData.append("image", file.value);
}
}
client.open("post", target, true);
/* Send to server */
client.send(formData);
/* Check the response status */
client.onreadystatechange = function() {
if (client.readyState == 4) {
if(client.status == 200){
var r = JSON.parse(client.responseText);
var imageLink = '<a id="created_img_link" title="' + r.data.link + '" href="' + r.data.link + '"> ' + r.data.link + '</a>';
$("#imageLink").append(imageLink);
var img = $('<img id="created_img" class="img-responsive">');
img.attr('src', r.data.link)
img.attr('alt', r.data.name)
img.appendTo('#imageObj');
$('#created_img').wrap(function() {
return "<a href='" + r.data.link + "'/>";
});
//$('.btn-upload').html('<i class="fa fa-check"></i> Uploaded!');
$(buttonId).html('<i class="fa fa-check"></i> Uploaded!');
$('#uploaded-dialog').modal('show');
} else if (client.status == 500) {
if (client.responseText == "") {
var errorMsg = "There was an error. That's everything we know.";
} else {
var r = JSON.parse(client.responseText);
var errorMsg = r.data.error;
}
$('.error-msg').text(errorMsg);
toggleButton('.btn-upload', 'enable', buttonId);
$(buttonId).text('Try again!');
$('#error-dialog').modal('show');
} else {
var errorMsg = "There was an error. That's everything we know.";
$('.error-msg').text(errorMsg);
toggleButton('.btn-upload', 'enable', buttonId);
$(buttonId).text('Try again!');
$('#error-dialog').modal('show');
}
}
}
};
/* Prevent the form submit when pressing 'enter'. */
$('form').submit(function(event){
event.preventDefault();
});
/* Detects change on input #file. */
$(function() {
$("#file").change(function (){
//var fileName = $(this).val();
//$(".filename").html(fileName);
//console.log(fileName)
//$('#imageSubmit').trigger('click');
$('.btn-upload').addClass('hidden');
$('#imageSubmit').removeClass('hidden');
});
});
/* Start file upload via AJAX. */
$("#imageSubmit").click( function(event){
event.preventDefault();
buttonId = "#imageSubmit";
toggleButton('.btn-upload', "disable", buttonId);
doAjaxStuff("file", "/api/v1/file", "isFile");
});
/* Show upload button for urlBar on click. */
$("#urlBar").click(function (){
$('.btn-upload').addClass('hidden');
$('#urlSubmit').removeClass("hidden");
});
/* Start upload over url via AJAX. */
$("#urlSubmit").click( function(event){
event.preventDefault();
buttonId = "#urlSubmit";
toggleButton('.btn-upload', "disable");
doAjaxStuff("urlBar", "/api/v1/url", "isNoFile");
});
/* Show upload button for base64Bar on click. */
$("#base64Bar").click(function (){
$('.btn-upload').addClass('hidden');
$('#base64Submit').removeClass("hidden");
});
/* Start upload over base64 encoded image via AJAX. */
$("#base64Submit").click( function(event){
event.preventDefault();
buttonId = "#base64Submit";
toggleButton('.btn-upload', "disable");
doAjaxStuff("urlBar", "/api/v1/base64", "isNoFile");
});
|