forked from manishthakur37/frappe-code-material
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattachment_perview.js
More file actions
301 lines (272 loc) · 8.19 KB
/
attachment_perview.js
File metadata and controls
301 lines (272 loc) · 8.19 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// MIT License. See license.txt
frappe.ui.form.Attachments = class Attachments {
constructor(opts) {
$.extend(this, opts);
this.attachments_page_length = 10; // show n attachments initially
this.show_all_attachments = false;
this.make();
}
make() {
var me = this;
this.parent.find(".add-attachment-btn").click(function () {
me.new_attachment();
});
this.parent.find(".explore-link").click(() => {
if (!this.frm.attachments.get_attachments()?.length) return;
frappe.open_in_new_tab = true;
frappe.set_route("List", "File", {
attached_to_doctype: this.frm.doctype,
attached_to_name: this.frm.docname,
});
});
this.add_attachment_wrapper = this.parent.find(".attachments-actions");
this.attachments_label = this.parent.find(".attachments-label");
}
max_reached(raise_exception = false) {
const attachment_count = Object.keys(this.get_attachments()).length;
const attachment_limit = this.frm.meta.max_attachments;
if (attachment_limit && attachment_count >= attachment_limit) {
if (raise_exception) {
frappe.throw({
title: __("Attachment Limit Reached"),
message: __("Maximum attachment limit of {0} has been reached.", [
cstr(attachment_limit).bold(),
]),
});
}
return true;
}
return false;
}
refresh() {
if (this.frm.doc.__islocal) {
this.parent.toggle(false);
return;
}
this.parent.toggle(true);
this.parent.find(".attachment-row").remove();
var max_reached = this.max_reached();
this.add_attachment_wrapper.find(".add-attachment-btn").toggle(!max_reached);
// add attachment objects
var attachments = this.get_attachments();
this.render_attachments(attachments);
this.setup_show_all_button(attachments);
}
setup_show_all_button(attachments) {
// show button if there is more to show and user has not clicked on "Show All"
let is_slicable = attachments.length > this.attachments_page_length;
let show = !this.show_all_attachments && is_slicable;
let show_all_btn = this.parent.find(".show-all-btn");
if (!show) {
show_all_btn.addClass("hidden");
return;
}
show_all_btn.removeClass("hidden");
show_all_btn.click(() => {
show_all_btn.addClass("hidden");
this.show_all_attachments = true;
this.refresh();
});
}
get_attachments() {
return this.frm.get_docinfo().attachments || [];
}
render_attachments(attachments) {
var me = this;
let attachments_to_render = attachments;
let is_slicable = attachments.length > this.attachments_page_length;
if (!this.show_all_attachments && is_slicable) {
// render last n attachments as they are at the top
let start = attachments.length - this.attachments_page_length;
attachments_to_render = attachments.slice(start, attachments.length);
}
if (attachments_to_render.length) {
let exists = {};
let unique_attachments = attachments_to_render.filter((attachment) => {
return Object.prototype.hasOwnProperty.call(exists, attachment.file_name)
? false
: (exists[attachment.file_name] = true);
});
unique_attachments.forEach((attachment) => {
me.add_attachment(attachment);
});
}
if (!attachments.length) {
// If no attachments in totality
this.attachments_label.removeClass("has-attachments");
}
}
add_attachment(attachment) {
var file_name = attachment.file_name;
var file_url = this.get_file_url(attachment);
var fileid = attachment.name;
if (!file_name) {
file_name = file_url;
}
var me = this;
let file_label = `
<a href="#" data-file-url="${file_url}" title="${frappe.utils.escape_html(file_name)}"
class="ellipsis attachment-preview-link" style="max-width: calc(100% - 43px);"
>
<span>${file_name}</span>
</a>`;
let remove_action = null;
if (frappe.model.can_write(this.frm.doctype, this.frm.name)) {
remove_action = function (target_id) {
frappe.confirm(__("Are you sure you want to delete the attachment?"), function () {
let target_attachment = me
.get_attachments()
.find((attachment) => attachment.name === target_id);
let to_be_removed = me
.get_attachments()
.filter(
(attachment) => attachment.file_name === target_attachment.file_name
);
to_be_removed.forEach((attachment) => me.remove_attachment(attachment.name));
});
return false;
};
}
const icon = `<a href="/app/file/${fileid}">
${frappe.utils.icon(attachment.is_private ? "es-line-lock" : "es-line-unlock", "sm ml-0")}
</a>`;
$(`<li class="attachment-row">`)
.append(frappe.get_data_pill(file_label, fileid, remove_action, icon))
.insertAfter(this.add_attachment_wrapper);
this.setup_attachment_preview();
}
setup_attachment_preview() {
this.parent.find(".attachment-preview-link").off("click").on("click", (e) => {
e.preventDefault();
let file_url = $(e.currentTarget).data("file-url");
this.show_preview(file_url);
});
}
show_preview(file_url) {
let me = this;
let preview_modal = new frappe.ui.Dialog({
title: __("Attachment Preview"),
width: 800,
no_cancel: true
});
let preview_content = `
<div style="text-align: center;">
<iframe src="${file_url}" frameborder="0" style="width: 100%; height: 600px;"></iframe>
</div>`;
preview_modal.$body.html(preview_content);
preview_modal.show();
}
get_file_url(attachment) {
var file_url = attachment.file_url;
if (!file_url) {
if (attachment.file_name.indexOf("files/") === 0) {
file_url = "/" + attachment.file_name;
} else {
file_url = "/files/" + attachment.file_name;
}
}
// hash is not escaped, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
return encodeURI(file_url).replace(/#/g, "%23");
}
get_file_id_from_file_url(file_url) {
var fid;
$.each(this.get_attachments(), function (i, attachment) {
if (attachment.file_url === file_url) {
fid = attachment.name;
return false;
}
});
return fid;
}
remove_attachment_by_filename(filename, callback) {
this.remove_attachment(this.get_file_id_from_file_url(filename), callback);
}
remove_attachment(fileid, callback) {
if (!fileid) {
if (callback) callback();
return;
}
var me = this;
return frappe.call({
method: "frappe.desk.form.utils.remove_attach",
type: "DELETE",
args: {
fid: fileid,
dt: me.frm.doctype,
dn: me.frm.docname,
},
callback: function (r, rt) {
if (r.exc) {
if (!r._server_messages) frappe.msgprint(__("There were errors"));
return;
}
me.remove_fileid(fileid);
me.frm.sidebar.reload_docinfo();
if (callback) callback();
},
});
}
new_attachment(fieldname) {
if (this.dialog) {
// remove upload dialog
this.dialog.$wrapper.remove();
}
const restrictions = {};
if (this.frm.meta.max_attachments) {
restrictions.max_number_of_files =
this.frm.meta.max_attachments - this.frm.attachments.get_attachments().length;
}
new frappe.ui.FileUploader({
doctype: this.frm.doctype,
docname: this.frm.docname,
frm: this.frm,
folder: "Home/Attachments",
on_success: (file_doc) => {
this.attachment_uploaded(file_doc);
},
restrictions,
make_attachments_public: this.frm.meta.make_attachments_public,
});
}
get_args() {
return {
from_form: 1,
doctype: this.frm.doctype,
docname: this.frm.docname,
};
}
attachment_uploaded(attachment) {
this.dialog && this.dialog.hide();
this.update_attachment(attachment);
this.frm.sidebar.reload_docinfo();
if (this.fieldname) {
this.frm.set_value(this.fieldname, attachment.file_url);
}
}
update_attachment(attachment) {
if (attachment.name) {
this.add_to_attachments(attachment);
this.refresh();
}
}
add_to_attachments(attachment) {
var form_attachments = this.get_attachments();
for (var i in form_attachments) {
// prevent duplicate
if (form_attachments[i]["name"] === attachment.name) return;
}
form_attachments.push(attachment);
}
remove_fileid(fileid) {
var attachments = this.get_attachments();
var new_attachments = [];
$.each(attachments, function (i, attachment) {
if (attachment.name != fileid) {
new_attachments.push(attachment);
}
});
this.frm.get_docinfo().attachments = new_attachments;
this.refresh();
}
};