/
modaldialog.js
159 lines (134 loc) · 5.85 KB
/
modaldialog.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
window.uploadModalObjects = window.uploadModalObjects || {};
class UploadQuestionController {
constructor(fieldname) {
this.fieldname = fieldname
this.$el = $('#upload_'+fieldname);
this.$modalEl = $('#file-upload-modal-' + this.fieldname);
this.show_title = this.$el.data('showtitle');
this.show_comment = this.$el.data('showcomment');
}
prepareOpenUploadModalDialog() {
const buttonsOpts = {};
buttonsOpts[uploadLang.returnTxt] = () => {
this.$el.dialog("close");
};
$(document).off('shown.bs.modal.lsuploadquestion', '#file-upload-modal-' + this.fieldname);
$(document).on('shown.bs.modal.lsuploadquestion', '#file-upload-modal-' + this.fieldname, () => {
const uploadFrame = $('#uploader' + this.fieldname);
uploadFrame.load(uploadFrame.data('src'));
this.updateMaxHeightModalbody(this.$el);
});
this.$modalEl.off('hide.bs.modal.lsuploadquestion');
this.$modalEl.on('hide.bs.modal.lsuploadquestion', () => {
const uploadFrame = $('#uploader' + this.fieldname);
window.currentUploadHandler.saveAndExit(this.fieldname, this.show_title, this.show_comment, 1);
uploadFrame.html('');
return true;
});
this.$el.off('click.lsuploadquestion');
this.$el.on('click.lsuploadquestion', (e) => {
console.ls.log("File upload modal opening");
this.$modalEl.modal('show');
});
}
/* Function to update upload frame
*
* @param frameName name of the frame (here it's id too :) )
* @param integer heigth
*/
updateUploadFrame(frameName, heigth) {
$("#" + frameName).innerHeight(heigth);
}
/* Function to update modal body max height
*
* @param modal jquery object : the modal
*/
updateMaxHeightModalbody(modal) {
const modalHeader = $(modal).find(".modal-header").outerHeight();
const modalFooter = $(modal).find(".modal-footer").outerHeight();
const finalMaxHeight = Math.max(150, $(window).height() - (modalHeader + modalFooter + 16)); // Not less than 150px
console.ls.log([$(window).height(), modalHeader, modalFooter, (modalHeader + modalFooter)]);
$(modal).find(".modal-body").css("max-height", finalMaxHeight);
}
getQueryVariable(variable, url) {
const vars1 = url.split("/");
for (let i = 0; i < vars1.length; i++) {
if (vars1[i] == variable) {
return vars1[i + 1];
}
}
// If not found try with ?
// TODO : replace by a regexp
const vars2 = url.replace(/\&/g, '&').split("&");
for (let i = 0; i < vars.length; i++) {
const pair = vars2[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return null;
}
isValueInArray(arr, val) {
inArray = false;
for (let i = 0; i < arr.length; i++) {
if (val.toLowerCase() == arr[i].toLowerCase()) {
inArray = true;
}
}
return inArray;
}
displayUploadedFiles(filecount, fieldname, show_title, show_comment) {
const jsonstring = $("#" + fieldname).val();
let display = '';
if (jsonstring == '[]' || jsonstring == '') {
$('#' + this.fieldname + '_uploadedfiles').addClass('hidden');
$('#' + this.fieldname + '_uploadedfiles').find('table>tbody').html('');
return;
}
if (jsonstring !== '') {
let jsonobj = [];
try {
jsonobj = JSON.parse(jsonstring);
} catch (e) {};
$('#' + this.fieldname + '_uploadedfiles').removeClass('hidden');
$('#' + this.fieldname + '_uploadedfiles').find('table>tbody').html('');
const image_extensions = new Array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'jp2', 'iff', 'bmp', 'xbm', 'ico');
const templateHtml = $('#filerowtemplate_'+this.fieldname).html();
jsonobj.forEach((item, iterator) => {
let imageOrPlaceholder, imageOrPlaceholderHtml, title, comment, name, filepointer;
if (this.isValueInArray(image_extensions, item.ext)) {
imageOrPlaceholder = "image";
imageOrPlaceholderHtml = '<img src="'+uploadurl+'/filegetcontents/'+decodeURIComponent(item.filename)+'" class="uploaded" />';
} else {
imageOrPlaceholder = "placeholder";
imageOrPlaceholderHtml = '<div class="upload-placeholder"></div>';
}
title = (show_title != 0) ? item.title : '';
comment = (show_comment != 0) ? item.comment : '';
name = item.name;
filepointer = iterator;
const rowHtml = this.replaceWithObject(templateHtml, {imageOrPlaceholder, imageOrPlaceholderHtml, title, comment, name, filepointer});
$('#' + this.fieldname + '_uploadedfiles').find('table>tbody').append(rowHtml)
});
$('.trigger_edit_upload_'+this.fieldname).off('click.lsuploadquestion');
$('.trigger_edit_upload_'+this.fieldname).on('click.lsuploadquestion',
() => {
this.$modalEl.modal('show');
});
}
};
replaceWithObject(templateString, objectWithReplacements) {
let outString = templateString;
for( let key in objectWithReplacements) {
outString = outString.replace(new RegExp(`\{${key}\}`), objectWithReplacements[key]);
}
return outString;
}
showBasic() {
$('#basic').show();
}
hideBasic() {
$('#basic').hide();
}
}
window.UploadQuestionController = UploadQuestionController;