]> git.proxmox.com Git - pmg-gui.git/blame - js/AttachmentGrid.js
attachment grid: simply filter to avoid code bloat
[pmg-gui.git] / js / AttachmentGrid.js
CommitLineData
c87d46fb 1Ext.define('PMG.grid.AttachmentGrid', {
0023ef6a
DC
2 extend: 'Ext.grid.GridPanel',
3 xtype: 'pmgAttachmentGrid',
1a96ec32
DC
4 mixins: ['Proxmox.Mixin.CBind'],
5
6 showDownloads: true,
0023ef6a 7
38229fc8
TL
8 title: gettext('Attachments'),
9 iconCls: 'fa fa-paperclip',
10
ecab0d0e
TL
11 minHeight: 50,
12 maxHeight: 250,
13 scrollable: true,
db3264f2 14 collapsed: true,
ecab0d0e 15
db3264f2
DC
16 tools: [
17 {
18 xtype: 'checkbox',
19 boxLabel: gettext('show all parts'),
20 boxLabelAlgign: 'before',
21 listeners: {
22 change: function(cb, value) {
23 let grid = this.up('pmgAttachmentGrid');
24 let store = grid.getStore();
25 store.clearFilter();
26 if (!value) {
27 store.filter({
28 property: 'content-disposition',
29 value: 'attachment',
30 });
31 }
32 },
33 },
34 },
35 {
36 type: 'down',
37 handler: function() {
38 let me = this;
39 let type = me.type === 'up' ? 'down' : 'up';
40 me.up('pmgAttachmentGrid').toggleCollapse();
41 me.setType(type);
42 },
43 },
44 ],
45
46 header: {
47 padding: '6 10 6 10', // make same height as normal panel
48 },
38229fc8 49
0023ef6a
DC
50 store: {
51 autoDestroy: true,
db3264f2 52 fields: ['name', 'content-type', 'size', 'content-disposition'],
0023ef6a
DC
53 proxy: {
54 type: 'proxmox',
c87d46fb 55 },
db3264f2
DC
56 filters: {
57 property: 'content-disposition',
58 value: 'attachment',
59 },
0023ef6a
DC
60 },
61
550ec9a8
TL
62 controller: {
63 xclass: 'Ext.app.ViewController',
64 init: function(view) {
65 view.store.on('load', this.onLoad, this);
66 },
67 onLoad: function(store, records, success) {
68 let me = this;
69 let view = me.getView();
70 if (!success) {
71 view.updateTitleStats(-1);
72 return;
73 }
17589a84
TL
74 let attachments = records.filter(({ data }) => data['content-disposition'] === 'attachment');
75 let totalSize = attachments.reduce((sum, { data }) => sum + data.size, 0);
76 view.updateTitleStats(attachments.length, totalSize);
550ec9a8
TL
77 },
78 },
79
80 updateTitleStats: function(count, totalSize) {
81 let me = this;
82 let title;
83 if (count > 0) {
86c2fdb6 84 title = Ext.String.format(gettext('{0} Attachments'), count);
550ec9a8 85 title += ` (${Proxmox.Utils.format_size(totalSize)})`;
db3264f2 86 me.expand();
550ec9a8
TL
87 } else {
88 title = gettext('No Attachments');
db3264f2 89 me.collapse();
550ec9a8
TL
90 }
91 me.setTitle(title);
92 },
93
0023ef6a
DC
94 setID: function(rec) {
95 var me = this;
96 if (!rec || !rec.data || !rec.data.id) {
97 me.getStore().removeAll();
98 return;
99 }
100 var url = '/api2/json/quarantine/listattachments?id=' + rec.data.id;
101 me.mailid = rec.data.id;
102 me.store.proxy.setUrl(url);
103 me.store.load();
104 },
105
106 emptyText: gettext('No Attachments'),
107
108 download: function() {
28eb60c0 109 Ext.Msg.alert(arguments);
0023ef6a
DC
110 },
111
112 columns: [
113 {
114 text: gettext('Filename'),
115 dataIndex: 'name',
116 flex: 1,
117 },
118 {
119 text: gettext('Filetype'),
120 dataIndex: 'content-type',
121 renderer: PMG.Utils.render_filetype,
122 flex: 1,
123 },
124 {
125 text: gettext('Size'),
ff23b1b3 126 renderer: Proxmox.Utils.render_size,
0023ef6a
DC
127 dataIndex: 'size',
128 flex: 1,
129 },
130 {
131 header: gettext('Download'),
1a96ec32
DC
132 cbind: {
133 hidden: '{!showDownloads}',
134 },
0023ef6a
DC
135 renderer: function(value, mD, rec) {
136 var me = this;
28eb60c0
TL
137 let url = `/api2/json/quarantine/download?mailid=${me.mailid}&attachmentid=${rec.data.id}`;
138 return `<a target='_blank' class='download' download='${rec.data.name}' href='${url}'>
139 <i class='fa fa-fw fa-download'</i>
140 </a>`;
0023ef6a 141 },
c87d46fb
TL
142 },
143 ],
0023ef6a 144});