-
Notifications
You must be signed in to change notification settings - Fork 116
/
upload.js
executable file
·133 lines (116 loc) · 4.57 KB
/
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
/*
* Copyright 2015 OSBI Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Module License Upload
*/
var upload = (function($, window, document, undefined) {
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
'use strict';
var module = {
_baseURL: window.location.origin + '/saiku/rest/saiku/api/license',
formChange: function() {
$('#file-chooser').change(function() {
$('.form-upload p').text(this.files.length + ' file selected');
});
},
_notifyUser: function(alertType, msg) {
$('#notification').removeClass();
$('#notification').addClass(alertType);
$('#notification').slideDown();
module._setNotificationMessage(msg);
setTimeout(function() {
$('#notification').slideUp();
}, 3000);
},
_setNotificationMessage: function(msg) {
$('#notification p').text(msg);
},
_clearInputFile: function() {
var inputFile = $('#file-chooser');
inputFile.replaceWith(inputFile.val('').clone(true));
},
_loadingButton: function(isLoadBtn) {
if (isLoadBtn) {
$('#btn-sendfile').attr('disabled', 'disabled');
$('#btn-sendfile').text('Loading file...');
}
else {
$('#btn-sendfile').removeAttr('disabled');
$('#btn-sendfile').text('Upload');
}
},
_backLogin: function(isShow) {
if (isShow) {
$('.back-login').show();
$('#btn-back-login').on('click', function() {
var url = window.location.origin;
window.open(url, '_self');
});
}
else {
$('.back-login').hide();
}
},
_xhr: function(options, callback) {
$.ajax({
url: options.url,
type: options.type,
data: options.data,
contentType: 'application/x-java-serialized-object',
processData: false,
success: function(data, status, jqXHR) {
module._clearInputFile();
module._loadingButton(false);
module._backLogin(true);
callback('alert-success', data);
},
error: function(jqXHR, status, errorThrown) {
module._clearInputFile();
module._loadingButton(false);
// module._backLogin(false);
callback('alert-danger', 'Error while uploading the file: (' + errorThrown + ')');
}
});
},
sendFile: function() {
$('#btn-sendfile').on('click', function() {
var file = $('#file-chooser')[0].files[0];
if (file !== undefined) {
module._loadingButton(true);
module._xhr({type: 'POST', url: module._baseURL, data: file}, module._notifyUser);
$('.form-upload p').text('Drag your license or click in this area.');
}
else {
// module._backLogin(false);
module._notifyUser('alert-danger', 'Oops... Select a file!');
}
});
},
init: function() {
module.formChange();
module.sendFile();
}
};
return {
init: module.init
}
}(jQuery, window, document));