-
Notifications
You must be signed in to change notification settings - Fork 16
/
nuxeo-delete-blob-button.js
197 lines (177 loc) · 5.95 KB
/
nuxeo-delete-blob-button.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
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
/**
@license
©2023 Hyland Software, Inc. and its affiliates. All rights reserved.
All Hyland product names are registered or unregistered trademarks of Hyland Software, Inc. or its affiliates.
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.
*/
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js';
import '@polymer/iron-icon/iron-icon.js';
import '@polymer/iron-icons/iron-icons.js';
import '@nuxeo/nuxeo-elements/nuxeo-element.js';
import '@nuxeo/nuxeo-elements/nuxeo-operation.js';
import '@polymer/paper-button/paper-button.js';
import '@polymer/paper-icon-button/paper-icon-button.js';
import '@polymer/polymer/lib/elements/dom-if.js';
import { FiltersBehavior } from '../nuxeo-filters-behavior.js';
import { I18nBehavior } from '../nuxeo-i18n-behavior.js';
import '../nuxeo-icons.js';
import '../widgets/nuxeo-dialog.js';
import '../widgets/nuxeo-tooltip.js';
import './nuxeo-action-button-styles.js';
import '../nuxeo-button-styles.js';
{
/**
* A button element for deleting a file blob from a document.
*
* Example:
*
* <nuxeo-delete-blob-button document="[[document]]"></nuxeo-delete-blob-button>
*
* @appliesMixin Nuxeo.I18nBehavior
* @appliesMixin Nuxeo.FiltersBehavior
* @memberof Nuxeo
* @demo demo/nuxeo-delete-blob-button/index.html
*/
class DeleteBlobButton extends mixinBehaviors([I18nBehavior, FiltersBehavior], Nuxeo.Element) {
static get template() {
return html`
<style include="nuxeo-action-button-styles nuxeo-button-styles"></style>
<nuxeo-operation
id="operation"
op="Blob.RemoveFromDocument"
input="[[document.uid]]"
params="[[_params(xpath)]]"
>
</nuxeo-operation>
<dom-if if="[[_isAvailable(document)]]">
<template>
<div class="action" on-click="_toggleDialog">
<paper-icon-button icon="[[icon]]" noink aria-labelledby="label"></paper-icon-button>
<span class="label" hidden$="[[!showLabel]]" id="label">[[_label]]</span>
<nuxeo-tooltip>[[_label]]</nuxeo-tooltip>
</div>
</template>
</dom-if>
<nuxeo-dialog id="dialog" with-backdrop>
<h2>[[i18n('deleteBlobButton.dialog.heading')]]</h2>
<div>[[i18n('deleteBlobButton.dialog.message')]]</div>
<div class="buttons">
<paper-button dialog-dismiss class="secondary">[[i18n('deleteBlobButton.dialog.no')]]</paper-button>
<paper-button dialog-confirm on-click="_remove" class="primary"
>[[i18n('deleteBlobButton.dialog.yes')]]</paper-button
>
</div>
</nuxeo-dialog>
<nuxeo-connection id="nx" connection-id="[[connectionId]]"></nuxeo-connection>
<nuxeo-resource id="blobRequest"></nuxeo-resource>
`;
}
static get is() {
return 'nuxeo-delete-blob-button';
}
static get properties() {
return {
connectionId: {
type: String,
value: 'nx',
},
/**
* Input document.
*/
document: Object,
/**
* Path of the file object to delete.
* For example `xpath="files:files/0/file"`.
*/
xpath: {
type: String,
value: 'file:content',
},
/**
* Icon to use (iconset_name:icon_name).
*/
icon: {
type: String,
value: 'nuxeo:remove',
},
/**
* `true` if the action should display the label, `false` otherwise.
*/
showLabel: {
type: Boolean,
value: false,
},
_label: {
type: String,
computed: '_computeLabel(i18n)',
},
};
}
_isAvailable(doc) {
return (
doc &&
this.hasPermission(doc, 'WriteProperties') &&
!this.isImmutable(doc) &&
!this.hasType(doc, 'Root') &&
!this.isTrashed(doc) &&
!(doc.isRecord && this.xpath !== 'file:content') &&
!(this.isUnderRetentionOrLegalHold(doc) && this.xpath === 'file:content')
);
}
_computeLabel() {
return this.i18n('deleteBlobButton.tooltip');
}
_toggleDialog() {
this.$.dialog.toggle();
}
_params(xpath) {
return {
xpath: xpath.startsWith('files:') ? xpath.split('/file')[0] : xpath,
};
}
_remove() {
const rowNo = this.xpath.split('/')[1];
const { 'upload-batch': uploadBatch, 'upload-fileId': uploadFileId } = this.document.properties[
'monschema:mesdonnees'
][rowNo].fichier;
if (uploadBatch && uploadFileId) {
this.$.blobRequest.data = {};
this.$.blobRequest.path = `upload/${uploadBatch}/${uploadFileId}`;
this.$.blobRequest
.remove()
.then((response) => {
this._dispatchEvent('file-deleted', response);
})
.catch((error) => {
this._dispatchEvent('error', error);
});
} else {
this.$.operation.execute().then((response) => {
this._dispatchEvent('file-deleted', response);
});
}
}
_dispatchEvent(eventName, response) {
this.dispatchEvent(
new CustomEvent(eventName, {
composed: true,
bubbles: true,
detail: {
response,
},
}),
);
}
}
customElements.define(DeleteBlobButton.is, DeleteBlobButton);
Nuxeo.DeleteBlobButton = DeleteBlobButton;
}