This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile-upload.js
118 lines (104 loc) · 3.51 KB
/
file-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
/** On click at the menuitem, open dialog */
$(".uploadFile").on('click', function() {
//dialog name & file type
var dialogName = "Upload "+$(this).children().html();
var fileType = $(this).attr('id').replace("upload-","");
//dialog's HTML options
var htmlOptions = html_options(fileType)
//open dialog
open_dialog(dialogName, htmlOptions);
});
/** Get HTML options according to a fileType {json,ea,refontouml} */
function html_options(fileType)
{
//clone template
var htmlFileChooser = document.importNode(fileChooserTemplate, true);
//accept file type
var htmlFileInput = htmlFileChooser.querySelector('#file-input');
if(fileType==="ea") htmlFileInput.accept = "."+"xml";
else htmlFileInput.accept = "."+fileType;
htmlFileInput.addEventListener('change', function(event){
if($(this)[0].files[0]===null) return
htmlFileChooser.querySelector('#file-path').innerHTML = " "+$(this)[0].files[0].name+"";
});
//button click event
var htmlButton = htmlFileChooser.querySelector('#browse-button');
htmlButton.addEventListener('click', function(event){
if(event.target.id != 'browse-button'){
this.querySelector('#file-input').click();
}
});
//HTML result
var htmlResult = document.createElement("div");
htmlResult.appendChild(htmlFileChooser);
if(fileType==="refontouml") htmlResult.appendChild(document.importNode(refOptionsTemplate, true));
if(fileType==="ea") htmlResult.appendChild(document.importNode(eaOptionsTemplate, true));
return htmlResult
}
/** Open upload dialog */
function open_dialog(dialogName, htmlContent)
{
var dialog = new BootstrapDialog({
type: BootstrapDialog.TYPE_DEFAULT,
size: BootstrapDialog.SIZE_NORMAL,
title: dialogName,
animate:false,
autospin: true,
message: htmlContent,
hotkey:13,//enter
buttons: [{label: 'Upload', autospin: true, cssClass: 'btn-primary',
action: function(dialog){ upload(dialog); dialog.close();}
}]
});
dialog.open();
dialog.getModal().css('background-color','rgba(255,255,255,0.3)');
}
/** Upload file with options to the server via ajax */
function upload(dialog)
{
//set url
var fileExtension = $("#file-input").prop('accept').replace(".","")
if(fileExtension==="xml") fileExtension = "ea";
var url = ADDR+"api/upload/"+fileExtension
//get file
var file = $("#file-input")[0].files[0];
if(file===null) return;
//create form
var form = new FormData();
form.append("file", file);
//upload form
if(fileExtension==='json') ajax_post(url,form,load_tree);
else {
append_options_to_form(fileExtension, form);
ajax_post(url,form,load_tree);
}
}
function append_options_to_form(ext, form)
{
if(ext==='refontouml'){
var ignorePackages = $("#refontouml-ignore-package").is(':checked');
var classesAsEvents = $("#refontouml-classes-as-events").is(':checked');
form.append("ignore-package",ignorePackages);
form.append("classes-as-events",classesAsEvents);
}
if(ext==='ea'){
var unnamed = $("#ignore-unnamed-eatypes").is(':checked');
var mult = $("#set-1-eamultiplicities").is(':checked');
form.append("unnamed-types",unnamed);
form.append("default-multiplicities",mult);
}
}
function ajax_post(url, formData, callback)
{
$.ajax({url: url, type: "POST", data: formData, processData: false, contentType: false,
success: function(response) {
callback();
console_append(response);
//show_success("File has been successfully uploaded.");
},
error: function(jqXHR, textStatus, errorMessage) {
console_append(errorMessage);
show_error("File could not be uploaded.");
}
});
}