Skip to content

Commit d59fb21

Browse files
committed
refactor js which handles file uploading function
1 parent 2001edc commit d59fb21

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

src/views/script.blade.php

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,27 @@
2828
});
2929
3030
$('#upload-btn').click(function () {
31-
var options = {
32-
beforeSubmit: showRequest,
33-
success: showResponse,
34-
error: showError
35-
};
36-
37-
function showRequest(formData, jqForm, options) {
38-
$('#upload-btn').html("<i class='fa fa-refresh fa-spin'></i> {{ $lang['btn-uploading'] }}");
39-
return true;
40-
}
31+
$(this).html('')
32+
.append($('<i>').addClass('fa fa-refresh fa-spin'))
33+
.append(" {{ $lang['btn-uploading'] }}")
34+
.addClass('disabled');
4135
42-
function showResponse(responseText, statusText, xhr, $form) {
36+
function resetUploadForm() {
4337
$('#uploadModal').modal('hide');
44-
$('#upload-btn').html("{{ $lang['btn-upload'] }}");
45-
if (responseText != 'OK'){
46-
notify(responseText);
47-
}
38+
$('#upload-btn').html("{{ $lang['btn-upload'] }}").removeClass('disabled');
4839
$('input#upload').val('');
49-
loadItems();
5040
}
5141
52-
function showError(jqXHR, textStatus, errorThrown) {
53-
$('#upload-btn').html("{{ $lang['btn-upload'] }}");
54-
if (jqXHR.status == 413) {
55-
notify("{{ $lang['error-too-large'] }}");
56-
} else if (textStatus == 'error') {
57-
notify("{{ $lang['error-other'] }}" + errorThrown);
58-
} else {
59-
notify("{{ $lang['error-other'] }}" + textStatus + '<br>' + errorThrown);
42+
$('#uploadForm').ajaxSubmit({
43+
success: function (data, statusText, xhr, $form) {
44+
resetUploadForm();
45+
refreshFoldersAndItems(data);
46+
},
47+
error: function () {
48+
resetUploadForm();
49+
notify('Action failed, due to server error.');
6050
}
61-
}
62-
63-
$('#uploadForm').ajaxSubmit(options);
64-
return false;
51+
});
6552
});
6653
6754
$('#thumbnail-display').click(function () {
@@ -108,6 +95,39 @@ function setOpenFolders() {
10895
// == Ajax actions ==
10996
// ====================
11097
98+
function performLfmRequest(url, parameter, type) {
99+
var data = defaultParameters();
100+
101+
if (parameter != null) {
102+
$.each(parameter, function (key, value) {
103+
data[key] = value;
104+
});
105+
}
106+
107+
return $.ajax({
108+
type: 'GET',
109+
dataType: type || 'text',
110+
url: url,
111+
data: data,
112+
cache: false
113+
}).fail(function () {
114+
notify('Action failed, due to server error.');
115+
});
116+
}
117+
118+
var refreshFoldersAndItems = function (data) {
119+
if (data == success_response) {
120+
loadFolders();
121+
} else {
122+
notify(data);
123+
}
124+
};
125+
126+
var hideNavAndShowEditor = function (data) {
127+
$('#nav-buttons').addClass('hidden');
128+
$('#content').html(data);
129+
}
130+
111131
function loadFolders() {
112132
performLfmRequest('{{ route("unisharp.lfm.getFolders") }}', {}, 'html')
113133
.done(function (data) {
@@ -154,37 +174,6 @@ function trash(item_name) {
154174
});
155175
}
156176
157-
function performLfmRequest(url, parameter, type) {
158-
var data = defaultParameters();
159-
160-
if (parameter != null) {
161-
$.each(parameter, function (key, value) {
162-
data[key] = value;
163-
});
164-
}
165-
166-
return $.ajax({
167-
type: 'GET',
168-
dataType: type || 'text',
169-
url: url,
170-
data: data,
171-
cache: false
172-
});
173-
}
174-
175-
var refreshFoldersAndItems = function (data) {
176-
if (data == 'OK') {
177-
loadFolders();
178-
} else {
179-
notify(data);
180-
}
181-
};
182-
183-
var hideNavAndShowEditor = function (data) {
184-
$('#nav-buttons').addClass('hidden');
185-
$('#content').html(data);
186-
}
187-
188177
function cropImage(image_name) {
189178
performLfmRequest('{{ route("unisharp.lfm.getCrop") }}', {img: image_name})
190179
.done(hideNavAndShowEditor);

0 commit comments

Comments
 (0)