@watermarkinsights/ripple 3.6.0-2 → 3.6.0-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/{functions-0cf2d639.js → functions-068b9a56.js} +442 -442
  3. package/dist/cjs/{global-b6602785.js → global-4e77664e.js} +65 -65
  4. package/dist/cjs/{http-service-494d81de.js → http-service-9e8c4dd5.js} +49 -49
  5. package/dist/cjs/{interfaces-a3338581.js → interfaces-30a74c1f.js} +29 -29
  6. package/dist/cjs/loader.cjs.js +2 -2
  7. package/dist/cjs/priv-chart-popover.cjs.entry.js +78 -78
  8. package/dist/cjs/priv-datepicker.cjs.entry.js +667 -667
  9. package/dist/cjs/priv-navigator-button.cjs.entry.js +18 -18
  10. package/dist/cjs/priv-navigator-item.cjs.entry.js +23 -23
  11. package/dist/cjs/ripple.cjs.js +2 -2
  12. package/dist/cjs/wm-action-menu_2.cjs.entry.js +341 -341
  13. package/dist/cjs/wm-button.cjs.entry.js +218 -218
  14. package/dist/cjs/wm-chart-slice.cjs.entry.js +13 -13
  15. package/dist/cjs/wm-chart.cjs.entry.js +474 -474
  16. package/dist/cjs/wm-datepicker.cjs.entry.js +256 -256
  17. package/dist/cjs/wm-input.cjs.entry.js +134 -134
  18. package/dist/cjs/wm-modal-footer.cjs.entry.js +31 -31
  19. package/dist/cjs/wm-modal-header.cjs.entry.js +34 -34
  20. package/dist/cjs/wm-modal.cjs.entry.js +161 -161
  21. package/dist/cjs/wm-navigator.cjs.entry.js +268 -268
  22. package/dist/cjs/wm-network-uploader.cjs.entry.js +420 -421
  23. package/dist/cjs/wm-option_2.cjs.entry.js +494 -494
  24. package/dist/cjs/wm-pagination.cjs.entry.js +167 -167
  25. package/dist/cjs/wm-search.cjs.entry.js +231 -231
  26. package/dist/cjs/wm-snackbar.cjs.entry.js +170 -170
  27. package/dist/cjs/wm-tab-item_3.cjs.entry.js +300 -300
  28. package/dist/cjs/wm-tag-input.cjs.entry.js +570 -570
  29. package/dist/cjs/wm-timepicker.cjs.entry.js +380 -380
  30. package/dist/cjs/wm-toggletip.cjs.entry.js +124 -124
  31. package/dist/cjs/wm-uploader.cjs.entry.js +347 -360
  32. package/dist/cjs/wm-wrapper.cjs.entry.js +12 -12
  33. package/dist/collection/components/wm-action-menu/wm-action-menu.js +460 -460
  34. package/dist/collection/components/wm-button/wm-button.js +485 -485
  35. package/dist/collection/components/wm-chart/priv-chart-popover/priv-chart-popover.js +232 -232
  36. package/dist/collection/components/wm-chart/wm-chart-slice.js +64 -64
  37. package/dist/collection/components/wm-chart/wm-chart.js +719 -719
  38. package/dist/collection/components/wm-datepicker/priv-datepicker/priv-datepicker.js +1015 -1015
  39. package/dist/collection/components/wm-datepicker/wm-datepicker.js +462 -462
  40. package/dist/collection/components/wm-input/wm-input.js +424 -424
  41. package/dist/collection/components/wm-menuitem/wm-menuitem.js +444 -444
  42. package/dist/collection/components/wm-modal/wm-modal-footer.js +141 -141
  43. package/dist/collection/components/wm-modal/wm-modal-header.js +92 -92
  44. package/dist/collection/components/wm-modal/wm-modal.js +459 -459
  45. package/dist/collection/components/wm-navigator/priv-navigator-button/priv-navigator-button.js +97 -97
  46. package/dist/collection/components/wm-navigator/priv-navigator-item/priv-navigator-item.js +114 -114
  47. package/dist/collection/components/wm-navigator/wm-navigator.js +468 -468
  48. package/dist/collection/components/wm-option/wm-option.js +422 -422
  49. package/dist/collection/components/wm-pagination/wm-pagination.js +368 -368
  50. package/dist/collection/components/wm-search/wm-search.js +479 -479
  51. package/dist/collection/components/wm-select/wm-select.js +720 -720
  52. package/dist/collection/components/wm-snackbar/wm-snackbar.js +297 -297
  53. package/dist/collection/components/wm-tabs/wm-tab-item/wm-tab-item.js +219 -219
  54. package/dist/collection/components/wm-tabs/wm-tab-list/wm-tab-list.js +331 -331
  55. package/dist/collection/components/wm-tabs/wm-tab-panel/wm-tab-panel.js +104 -104
  56. package/dist/collection/components/wm-tag-input/wm-tag-input.js +801 -801
  57. package/dist/collection/components/wm-timepicker/wm-timepicker.js +580 -580
  58. package/dist/collection/components/wm-toggletip/wm-toggletip.js +241 -241
  59. package/dist/collection/components/wm-uploader/wm-network-uploader/wm-network-uploader.js +753 -772
  60. package/dist/collection/components/wm-uploader/wm-uploader.css +11 -18
  61. package/dist/collection/components/wm-uploader/wm-uploader.js +748 -794
  62. package/dist/collection/components/wm-wrapper/wm-wrapper.js +27 -27
  63. package/dist/collection/dev/scripts.js +20 -20
  64. package/dist/collection/global/__mocks__/functions.js +6 -6
  65. package/dist/collection/global/functions.js +445 -445
  66. package/dist/collection/global/global.js +72 -72
  67. package/dist/collection/global/interfaces.js +49 -49
  68. package/dist/collection/global/services/__mocks__/http-service.js +130 -130
  69. package/dist/collection/global/services/http-service.js +50 -50
  70. package/dist/collection/lang/lang.js +5 -5
  71. package/dist/collection/lang/missing.js +39 -39
  72. package/dist/collection/lang/piglatin.js +93 -93
  73. package/dist/esm/{functions-223fb0dc.js → functions-122ab496.js} +442 -442
  74. package/dist/esm/{global-f9c17bf1.js → global-0fd112f7.js} +65 -65
  75. package/dist/esm/{http-service-3dc3b3e7.js → http-service-5d037e16.js} +49 -49
  76. package/dist/esm/{interfaces-2b97fab2.js → interfaces-61c6305b.js} +29 -29
  77. package/dist/esm/loader.js +2 -2
  78. package/dist/esm/polyfills/core-js.js +0 -0
  79. package/dist/esm/polyfills/dom.js +0 -0
  80. package/dist/esm/polyfills/es5-html-element.js +0 -0
  81. package/dist/esm/polyfills/index.js +0 -0
  82. package/dist/esm/polyfills/system.js +0 -0
  83. package/dist/esm/priv-chart-popover.entry.js +78 -78
  84. package/dist/esm/priv-datepicker.entry.js +667 -667
  85. package/dist/esm/priv-navigator-button.entry.js +18 -18
  86. package/dist/esm/priv-navigator-item.entry.js +23 -23
  87. package/dist/esm/ripple.js +2 -2
  88. package/dist/esm/wm-action-menu_2.entry.js +341 -341
  89. package/dist/esm/wm-button.entry.js +218 -218
  90. package/dist/esm/wm-chart-slice.entry.js +13 -13
  91. package/dist/esm/wm-chart.entry.js +474 -474
  92. package/dist/esm/wm-datepicker.entry.js +256 -256
  93. package/dist/esm/wm-input.entry.js +134 -134
  94. package/dist/esm/wm-modal-footer.entry.js +31 -31
  95. package/dist/esm/wm-modal-header.entry.js +34 -34
  96. package/dist/esm/wm-modal.entry.js +161 -161
  97. package/dist/esm/wm-navigator.entry.js +268 -268
  98. package/dist/esm/wm-network-uploader.entry.js +420 -421
  99. package/dist/esm/wm-option_2.entry.js +494 -494
  100. package/dist/esm/wm-pagination.entry.js +167 -167
  101. package/dist/esm/wm-search.entry.js +231 -231
  102. package/dist/esm/wm-snackbar.entry.js +170 -170
  103. package/dist/esm/wm-tab-item_3.entry.js +300 -300
  104. package/dist/esm/wm-tag-input.entry.js +570 -570
  105. package/dist/esm/wm-timepicker.entry.js +380 -380
  106. package/dist/esm/wm-toggletip.entry.js +124 -124
  107. package/dist/esm/wm-uploader.entry.js +348 -361
  108. package/dist/esm/wm-wrapper.entry.js +12 -12
  109. package/dist/ripple/{p-e26c9858.entry.js → p-067909ce.entry.js} +1 -1
  110. package/dist/ripple/{p-cf075b3b.js → p-08e67884.js} +0 -0
  111. package/dist/ripple/{p-6b28f3fe.entry.js → p-1c23f228.entry.js} +1 -1
  112. package/dist/ripple/{p-c637686e.entry.js → p-2aa79663.entry.js} +1 -1
  113. package/dist/ripple/{p-d5938de0.entry.js → p-2b3f75eb.entry.js} +1 -1
  114. package/dist/ripple/{p-a84de435.entry.js → p-3ff67fd5.entry.js} +1 -1
  115. package/dist/ripple/{p-6d0b3f08.entry.js → p-4629470d.entry.js} +1 -1
  116. package/dist/ripple/p-4b1f14bb.entry.js +1 -0
  117. package/dist/ripple/{p-07f557b0.entry.js → p-4b72f4eb.entry.js} +1 -1
  118. package/dist/ripple/{p-1fdd85ea.entry.js → p-54c39a0b.entry.js} +1 -1
  119. package/dist/ripple/p-7da00f33.entry.js +1 -0
  120. package/dist/ripple/{p-addbfa91.entry.js → p-803b19aa.entry.js} +1 -1
  121. package/dist/ripple/{p-43f1298b.js → p-888bec42.js} +0 -0
  122. package/dist/ripple/{p-b14062a7.entry.js → p-888c18c3.entry.js} +1 -1
  123. package/dist/ripple/{p-94f584c5.entry.js → p-97884327.entry.js} +1 -1
  124. package/dist/ripple/{p-03f14a00.entry.js → p-9a8fbfad.entry.js} +1 -1
  125. package/dist/ripple/{p-f23d6211.entry.js → p-a2b02aec.entry.js} +1 -1
  126. package/dist/ripple/{p-c8655e6c.entry.js → p-a61b07de.entry.js} +1 -1
  127. package/dist/ripple/{p-fd8070fb.js → p-a6d6eae7.js} +0 -0
  128. package/dist/ripple/{p-c5d718c7.entry.js → p-e36853fd.entry.js} +1 -1
  129. package/dist/ripple/{p-0b1cdd4e.entry.js → p-ecbb441f.entry.js} +1 -1
  130. package/dist/ripple/{p-792a78d7.js → p-f2a1d8b1.js} +1 -1
  131. package/dist/ripple/{p-0663e9fc.entry.js → p-f6bed262.entry.js} +1 -1
  132. package/dist/ripple/{p-2d7891e9.entry.js → p-f95c2283.entry.js} +1 -1
  133. package/dist/ripple/ripple.esm.js +1 -1
  134. package/dist/types/components/wm-action-menu/wm-action-menu.d.ts +48 -48
  135. package/dist/types/components/wm-button/wm-button.d.ts +44 -44
  136. package/dist/types/components/wm-chart/priv-chart-popover/priv-chart-popover.d.ts +23 -23
  137. package/dist/types/components/wm-chart/wm-chart-slice.d.ts +8 -8
  138. package/dist/types/components/wm-chart/wm-chart.d.ts +82 -82
  139. package/dist/types/components/wm-datepicker/priv-datepicker/priv-datepicker.d.ts +76 -76
  140. package/dist/types/components/wm-datepicker/wm-datepicker.d.ts +41 -41
  141. package/dist/types/components/wm-input/wm-input.d.ts +46 -46
  142. package/dist/types/components/wm-menuitem/wm-menuitem.d.ts +34 -34
  143. package/dist/types/components/wm-modal/wm-modal-footer.d.ts +15 -15
  144. package/dist/types/components/wm-modal/wm-modal-header.d.ts +12 -12
  145. package/dist/types/components/wm-modal/wm-modal.d.ts +41 -41
  146. package/dist/types/components/wm-navigator/priv-navigator-button/priv-navigator-button.d.ts +10 -10
  147. package/dist/types/components/wm-navigator/priv-navigator-item/priv-navigator-item.d.ts +13 -13
  148. package/dist/types/components/wm-navigator/wm-navigator.d.ts +61 -61
  149. package/dist/types/components/wm-option/wm-option.d.ts +32 -32
  150. package/dist/types/components/wm-pagination/wm-pagination.d.ts +32 -32
  151. package/dist/types/components/wm-search/wm-search.d.ts +86 -86
  152. package/dist/types/components/wm-select/wm-select.d.ts +66 -66
  153. package/dist/types/components/wm-snackbar/wm-snackbar.d.ts +35 -35
  154. package/dist/types/components/wm-tabs/wm-tab-item/wm-tab-item.d.ts +38 -38
  155. package/dist/types/components/wm-tabs/wm-tab-list/wm-tab-list.d.ts +53 -53
  156. package/dist/types/components/wm-tabs/wm-tab-panel/wm-tab-panel.d.ts +20 -20
  157. package/dist/types/components/wm-tag-input/wm-tag-input.d.ts +92 -92
  158. package/dist/types/components/wm-timepicker/wm-timepicker.d.ts +61 -61
  159. package/dist/types/components/wm-toggletip/wm-toggletip.d.ts +27 -27
  160. package/dist/types/components/wm-uploader/wm-network-uploader/wm-network-uploader.d.ts +85 -86
  161. package/dist/types/components/wm-uploader/wm-uploader.d.ts +80 -84
  162. package/dist/types/components/wm-wrapper/wm-wrapper.d.ts +7 -7
  163. package/dist/types/components.d.ts +24 -30
  164. package/dist/types/global/__mocks__/functions.d.ts +6 -6
  165. package/dist/types/global/functions.d.ts +40 -40
  166. package/dist/types/global/global.d.ts +1 -1
  167. package/dist/types/global/interfaces.d.ts +34 -34
  168. package/dist/types/global/services/__mocks__/http-service.d.ts +6 -6
  169. package/dist/types/global/services/http-service.d.ts +4 -4
  170. package/dist/types/lang/lang.d.ts +5 -5
  171. package/package.json +47 -47
  172. package/dist/ripple/p-a1fc8e81.entry.js +0 -1
  173. package/dist/ripple/p-fb90d334.entry.js +0 -1
@@ -1,772 +1,753 @@
1
- import { Component, Element, h, Host, Prop, State, Listen, Watch, Event } from "@stencil/core";
2
- import { forceUpdate } from "@stencil/core";
3
- import { getRequest, postRequest, putRequest, deleteRequest } from "../../../global/services/http-service";
4
- import { intl, generateId, hasValidType } from "../../../global/functions";
5
- export class NetworkUploader {
6
- constructor() {
7
- this.isTabbing = false;
8
- this.fileList = [];
9
- this.inProgressList = [];
10
- this.errorList = []; // for errors on a particular file
11
- this.snackbar = [];
12
- this.buttonType = "secondary";
13
- this.fileTypes = "pdf txt log xml doc docx xls xlsx ppt pptx gif jpg jpeg png csv";
14
- this.maxSize = 100; // in Mb
15
- this.sortBy = "date"; // name is descending (a -> z), date is ascending (earlier -> later)
16
- this.inputId = generateId();
17
- }
18
- toggleTabbingOn() {
19
- this.isTabbing = true;
20
- }
21
- toggleTabbingOff() {
22
- this.isTabbing = false;
23
- }
24
- handleUploadProgress(ev) {
25
- this.inProgressList.map((i) => {
26
- if (i.name === ev.detail.file_name) {
27
- i.progress = ev.detail.progress;
28
- forceUpdate(this.el);
29
- }
30
- });
31
- if (ev.detail.progress === 100) {
32
- // inProgressList is not a perfect reflection of how many files are uploading
33
- // because even after progress reached 100% the item should remain in the array
34
- // (should only be removed once it appears in fileList)
35
- this.numFilesInProgress -= 1;
36
- }
37
- // aggregate the percentages for SR to announce progress
38
- if (!this.srProgress && this.numFilesInProgress) {
39
- const total = this.numFilesInProgress; // if we don't reassign this number changes when a file is done and the percentage becomes wrong
40
- this.srProgress = setInterval(() => {
41
- const aggregate = this.inProgressList.reduce((avg, item) => (item.progress ? avg + item.progress : avg), 0);
42
- if (this.liveRegion) {
43
- this.liveRegion.innerHTML = intl.formatMessage({
44
- id: "uploader.uploadProgress",
45
- defaultMessage: "Upload progress: {percentage}%",
46
- }, { percentage: Math.floor(aggregate / total) });
47
- }
48
- }, 5000);
49
- }
50
- }
51
- handleFinishedSnack(ev) {
52
- this.snackbar = this.snackbar.filter((n) => n.id !== ev.detail.id);
53
- }
54
- setMaxSize() {
55
- // Max size can't be set above 100Mb
56
- this.maxSize = this.maxSize > 100 ? 100 : this.maxSize;
57
- }
58
- uploadNotifications(newNum, oldNum) {
59
- if (oldNum > 0 && newNum === 0) {
60
- setTimeout(() => {
61
- this.notif = null;
62
- }, 20 * 1000);
63
- const message = intl.formatMessage({
64
- id: "uploader.finishedUploading",
65
- defaultMessage: "Finished uploading",
66
- });
67
- this.notif = { id: 5, message };
68
- if (this.liveRegion) {
69
- this.liveRegion.innerHTML = message;
70
- }
71
- this.numFilesInProgress = null;
72
- clearInterval(this.srProgress);
73
- forceUpdate(this.el);
74
- }
75
- else if (newNum > 0 && newNum != oldNum) {
76
- const message = intl.formatMessage({
77
- id: "uploader.filesUploading",
78
- defaultMessage: "{num, plural, one {1 file uploading} other {# files uploading}}",
79
- }, { num: newNum });
80
- this.notif = { id: 6, message };
81
- if (this.liveRegion) {
82
- this.liveRegion.innerHTML = message;
83
- }
84
- forceUpdate(this.el);
85
- }
86
- }
87
- emitFileListEvent() {
88
- this.wmNetworkUploaderFilesChanged.emit(this.fileList);
89
- }
90
- formatDate(isoDate, locale, timezone) {
91
- const timestamp = Date.parse(isoDate);
92
- if (locale.match(/^[a-zA-Z]*(-[a-zA-Z0-9]*)*$/g)) {
93
- // make sure locale is valid, otherwise set default locale
94
- try {
95
- //@ts-ignore
96
- Intl.getCanonicalLocales(locale);
97
- }
98
- catch (_a) {
99
- locale = "en-us";
100
- }
101
- }
102
- else {
103
- return isoDate;
104
- }
105
- if (!!timestamp) {
106
- const date = new Date(timestamp);
107
- const year = date.getFullYear();
108
- const month = ("0" + (date.getMonth() + 1)).substr(-2);
109
- const day = ("0" + date.getDate()).substr(-2);
110
- const time = date.toLocaleTimeString([locale, "en-us"], {
111
- timeZone: timezone,
112
- hour: "numeric",
113
- minute: "numeric",
114
- });
115
- return `${year}/${month}/${day} ${time}`;
116
- }
117
- else {
118
- return isoDate;
119
- }
120
- }
121
- isValidSize(size) {
122
- // https://stackoverflow.com/questions/2365100/converting-bytes-to-megabytes
123
- return size / 1024 / 1024 <= this.maxSize;
124
- }
125
- isDuplicate(name) {
126
- return this.fileList.find((f) => f.file_name === name);
127
- }
128
- isEmpty() {
129
- return !this.fileList.length && !this.inProgressList.length && !this.errorList.length;
130
- }
131
- isDisabled() {
132
- const isMax = !!this.maxFiles && this.fileList.length >= this.maxFiles;
133
- if (!this.notif) {
134
- if (isMax) {
135
- const errMessage = intl.formatMessage({
136
- id: "uploader.fileUploadLimit",
137
- defaultMessage: "You have reached the file upload limit.",
138
- });
139
- this.notif = { id: 1, message: errMessage };
140
- if (this.liveRegion) {
141
- this.liveRegion.innerHTML = errMessage;
142
- }
143
- }
144
- else {
145
- this.notif = null;
146
- }
147
- }
148
- else if (this.notif.id === 1) {
149
- // to make sure SR don't repeat the error, don't reset it if it already is showing
150
- // just dismiss it if isMax is not true anymore
151
- if (!isMax) {
152
- this.notif = null;
153
- }
154
- // finally do nothing if error 2 is showing (special case when user selects too many files)
155
- }
156
- // disable if is the uploader is maxed out or if something is uploading
157
- return isMax || this.numFilesInProgress > 0;
158
- }
159
- purgeProgressList(item) {
160
- // If a file is retrieved from the server because it was just uploaded
161
- // it should be removed from inProgressList
162
- this.inProgressList = this.inProgressList.filter((i) => i.progress !== 100 && i.name !== item.file_name);
163
- }
164
- getExistingFiles() {
165
- getRequest(this.getPath)
166
- .then((reply) => {
167
- reply.map((item) => this.purgeProgressList(item));
168
- if (this.sortBy === "name") {
169
- reply.sort((a, b) => a.file_name.localeCompare(b.file_name));
170
- }
171
- this.fileList = reply;
172
- })
173
- .catch((errStatus) => {
174
- console.error(errStatus);
175
- });
176
- }
177
- clearErrors() {
178
- this.errorList = [];
179
- this.notif = null;
180
- }
181
- handleFiles(ev) {
182
- const files = Array.from(ev.target.files);
183
- const validTypes = this.fileTypes.split(" ").join(", ");
184
- if (!!this.maxFiles && this.fileList.length + files.length > this.maxFiles) {
185
- const errMessage = intl.formatMessage({
186
- id: "uploader.fileUploadExceeded",
187
- defaultMessage: "The number of selected files exceeds the limit.",
188
- });
189
- this.notif = { id: 2, message: errMessage };
190
- if (this.liveRegion) {
191
- this.liveRegion.innerHTML = errMessage;
192
- }
193
- }
194
- else {
195
- // map to create inProgress items so they can be displayed immediately
196
- files &&
197
- files.map((file) => {
198
- if (!hasValidType(file, this.fileTypes)) {
199
- const error = {
200
- file_name: file.name,
201
- message: intl.formatMessage({
202
- id: "uploader.invalidFileType",
203
- defaultMessage: "The file type is invalid. Accepted types: {validTypes}",
204
- }, { validTypes }),
205
- };
206
- this.errorList.push(error);
207
- }
208
- else if (!this.isValidSize(file.size)) {
209
- const error = {
210
- file_name: file.name,
211
- message: intl.formatMessage({
212
- id: "uploader.fileTooLarge",
213
- defaultMessage: "The selected file is too large. Maximum file size is {size}Mb",
214
- }, { size: this.maxSize }),
215
- };
216
- this.errorList.push(error);
217
- }
218
- else if (this.isDuplicate(file.name)) {
219
- const error = {
220
- file_name: file.name,
221
- message: intl.formatMessage({
222
- id: "uploader.duplicateName",
223
- defaultMessage: "A file with this name already exists.",
224
- }),
225
- };
226
- this.errorList.push(error);
227
- }
228
- else {
229
- this.inProgressList.push(file);
230
- }
231
- forceUpdate(this.el);
232
- });
233
- if (this.inProgressList.length > 0) {
234
- this.numFilesInProgress = this.inProgressList.length;
235
- // map over "uploadable" files for actual upload
236
- // this happens separately otherwise at each pass we'd have to wait for the requests to complete before displaying things
237
- this.inProgressList.map((file, i) => {
238
- this.uploadFile(file, i); // = input.files file
239
- });
240
- }
241
- }
242
- ev.target.value = ""; // reset to ensure that onChange is triggered when the same file is selected a second time
243
- }
244
- uploadFile(file, i) {
245
- // get upload link
246
- getRequest(this.requestUploadPath + i.toString())
247
- .then(async (reply) => {
248
- const assData = JSON.parse(this.associatedData);
249
- const fileInfo = Object.assign(Object.assign({}, assData), { file_name: file.name, mime_type: file.type, file_path: reply.path, document_uuid: reply.guid });
250
- // onProgress is passed as a callback to the ev listener inside putRequest (http service),
251
- // so we can't change component things directly => custom event to pass data to uploader component.
252
- const onProgress = (ev) => {
253
- const progress = ev.lengthComputable && ev.total !== 0 ? (ev.loaded / ev.total) * 100 : 0;
254
- const event = new CustomEvent("wmUploadProgress", {
255
- detail: { file_name: file.name, progress },
256
- });
257
- this.el.dispatchEvent(event);
258
- };
259
- // upload to AWS s3 to url we just fetched
260
- await putRequest(reply.url, file, onProgress);
261
- // create the db entry
262
- await postRequest(this.uploadPath, fileInfo);
263
- // get files from server again
264
- this.getExistingFiles();
265
- })
266
- .catch((err) => {
267
- // do not show toast on error
268
- this.numFilesInProgress = null;
269
- // do not show in progress item on error
270
- this.inProgressList = [];
271
- this.notif = {
272
- id: 3,
273
- message: intl.formatMessage({
274
- id: "global.genericError",
275
- defaultMessage: "An error occurred. Please try again.",
276
- }),
277
- };
278
- console.error(err);
279
- });
280
- }
281
- downloadFile(uuid, path) {
282
- this.clearErrors();
283
- // POST request to get temporary download link
284
- const payload = {
285
- file_path: path,
286
- file_uuid: uuid,
287
- };
288
- postRequest(this.requestDownloadPath, payload)
289
- .then((reply) => {
290
- // we got the download link, now download the file
291
- window.location.assign(reply.url);
292
- })
293
- .catch((err) => {
294
- console.error(err);
295
- });
296
- }
297
- deleteFile(file) {
298
- this.clearErrors();
299
- this.snackbar.push({
300
- message: intl.formatMessage({
301
- id: "uploader.fileDeleted",
302
- defaultMessage: "{fileName} was deleted.",
303
- }, { fileName: file.file_name }),
304
- id: file.uuid,
305
- link: "",
306
- });
307
- // rm file from list immediately.
308
- this.fileList = this.fileList.filter((i) => i.uuid !== file.uuid);
309
- deleteRequest(`${this.deletePath}/${file.uuid}`).catch((err) => {
310
- this.notif = {
311
- id: 4,
312
- message: intl.formatMessage({
313
- id: "uploader.fileNotDeleted",
314
- defaultMessage: "{fileName} could not be deleted.",
315
- }, { fileName: file.file_name }),
316
- };
317
- this.getExistingFiles();
318
- console.error(err);
319
- });
320
- }
321
- componentWillLoad() {
322
- if (!this.getPath) {
323
- throw new Error("wm-network-uploader: get-path is a required prop");
324
- }
325
- if (!this.requestUploadPath) {
326
- throw new Error("wm-network-uploader: request-upload-path is a required prop");
327
- }
328
- if (!this.uploadPath) {
329
- throw new Error("wm-network-uploader: upload-path is a required prop");
330
- }
331
- if (!this.requestDownloadPath) {
332
- throw new Error("wm-network-uploader: request-download-path is a required prop");
333
- }
334
- if (!this.deletePath) {
335
- throw new Error("wm-network-uploader: delete-path is a required prop");
336
- }
337
- if (!this.buttonText) {
338
- throw new Error("wm-network-uploader: button-text is a required prop");
339
- }
340
- if (!this.emptyStateText) {
341
- throw new Error("wm-network-uploader: empty-state-text is a required prop");
342
- }
343
- if (!this.associatedData) {
344
- throw new Error("wm-network-uploader: associated-data is a required prop");
345
- }
346
- this.setMaxSize();
347
- this.clearErrors();
348
- }
349
- componentDidLoad() {
350
- this.getExistingFiles();
351
- // awkward way to handle Edge bug where file input dialog doesn't open on Enter key
352
- // first, detect polyfill by checking the class name that it adds to the component
353
- // (it's important NOT to execute the code below in normal browsers as it would open the dialog twice)
354
- this.el.classList.contains("sc-wm-uploader-h") &&
355
- // then add an even listener to force a click event when Enter key is pressed
356
- this.inputEl &&
357
- this.inputEl.addEventListener("keydown", (ev) => {
358
- if (this.inputEl && ev.keyCode === 13) {
359
- this.inputEl.click();
360
- }
361
- });
362
- if (document.body.classList.contains("wmcl-user-is-tabbing")) {
363
- this.toggleTabbingOn();
364
- }
365
- }
366
- renderUploadSvg() {
367
- return (h("svg", { class: "upload-file", width: "91", height: "84", viewBox: "0 0 91 84" },
368
- h("path", { transform: "rotate(-180.000000) translate(-452, -255.5)", fill: "#eae8ec", d: "M361,249.681818 L361,197.318182 C361,194.118182 365.555061,191.5 371.122358,191.5 L391.316463,191.5 L391.286495,200.562515 L369.871211,200.600292 L369.885404,200.622146 L369.886423,246.655947 L443.657038,246.840767 L443.657038,200.622146 L421.683537,200.622146 L421.683537,191.5 L441.978865,191.5 C447.546162,191.5 452,194.118182 452,197.318182 L452,249.681818 C452,252.881818 447.546162,255.5 441.978865,255.5 L371.122358,255.5 C365.504449,255.5 361,252.881818 361,249.681818 Z M391.5,207 L406,221 L420.5,207 L409.625,207 L409.625,172 L402.375,172 L402.375,207 L391.5,207 Z" })));
369
- }
370
- renderIcon() {
371
- // mdi attachment icon (paper clip) is horizontal but we want it vertical
372
- const rotate = this.icon && this.icon === "f066";
373
- return (this.icon && (h("span", { class: `mdi ${rotate ? "rotate" : ""}` }, String.fromCodePoint(parseInt(`0x${this.icon}`)))));
374
- }
375
- renderNotif() {
376
- return this.notif ? h("span", { class: "notif" }, this.notif.message) : "";
377
- }
378
- renderUploadButton(classes) {
379
- return (h("div", { class: "notif-wrapper" },
380
- h("input", { ref: (el) => (this.inputEl = el), name: this.inputId, id: this.inputId, class: "sr-only", type: "file", multiple: true, onClick: () => this.clearErrors(), onChange: (e) => this.handleFiles(e), disabled: this.isDisabled() }),
381
- h("label", { htmlFor: this.inputId, class: `wm-button ${classes}${this.isTabbing ? " user-is-tabbing" : ""}` },
382
- this.renderIcon(),
383
- this.buttonText),
384
- this.renderNotif()));
385
- }
386
- renderWithItems() {
387
- return (h("div", null,
388
- h("div", { class: "header" },
389
- h("div", { class: "descr" },
390
- h("slot", null)),
391
- this.renderUploadButton(`${this.buttonType} ${this.isDisabled() ? "disabled" : ""}`)),
392
- this.renderErrorList(),
393
- this.renderInProgressList(),
394
- this.renderFileList()));
395
- }
396
- renderEmptyState() {
397
- return (h("div", null,
398
- h("div", { class: "header" },
399
- h("div", { class: "descr" },
400
- h("slot", null))),
401
- h("div", { class: "empty-block" },
402
- this.renderUploadSvg(),
403
- h("div", { class: "empty-message" }, this.emptyStateText),
404
- this.renderUploadButton("-primary empty-button"))));
405
- }
406
- renderErrorList() {
407
- if (!!this.errorList.length) {
408
- if (this.liveRegion) {
409
- let srMessage = this.errorList.reduce(function (acc, err) {
410
- return acc + `${err.file_name}: ${err.message} `;
411
- }, "");
412
- this.liveRegion.innerHTML = srMessage;
413
- }
414
- return (h("ul", { class: "error-list" }, this.errorList.map((err) => (h("li", null,
415
- h("div", { class: "file --error" },
416
- h("div", { class: "file-info" },
417
- h("span", { class: "filename" }, err.file_name))),
418
- h("div", { class: "error-message" }, err.message))))));
419
- }
420
- }
421
- renderInProgressList() {
422
- return (h("ul", { class: "inprogress-list" }, this.inProgressList.map((item) => {
423
- return (h("li", null,
424
- h("div", { class: "file --progress" },
425
- h("div", { class: "progress", style: {
426
- background: `linear-gradient(to right, rgba(67, 126, 142, 1) ${item.progress}%, transparent ${item.progress}%)`,
427
- } }),
428
- h("div", { class: "file-info" },
429
- h("span", { class: "filename" },
430
- item.name,
431
- " ",
432
- h("span", { class: "sr-only" }, "uploading"))))));
433
- })));
434
- }
435
- renderFile(item) {
436
- return (h("li", null,
437
- h("div", { class: "file --saved" },
438
- h("div", { class: "file-info" },
439
- h("span", { class: "filename" }, item.file_name),
440
- h("span", { class: "filedate" }, this.formatDate(item.updated_at, intl.locale, Intl.DateTimeFormat().resolvedOptions().timeZone))),
441
- h("div", { class: "file-controls" },
442
- h("wm-button", { class: "download-button", "button-type": "icononly", tooltip: "download", "label-for-identical-buttons": `download ${item.file_name}`, icon: "f1da", onClick: () => this.downloadFile(item.uuid, item.file_path) }),
443
- h("wm-button", { class: "delete-button", "button-type": "icononly", tooltip: "remove", "label-for-identical-buttons": `remove ${item.file_name}`, icon: "f1c0", onClick: () => this.deleteFile(item) })))));
444
- }
445
- renderFileList() {
446
- return h("ul", { class: "file-list" }, this.fileList.map((item) => this.renderFile(item)));
447
- }
448
- render() {
449
- return (h(Host, null,
450
- this.isEmpty() ? this.renderEmptyState() : this.renderWithItems(),
451
- h("wm-snackbar", { notifications: JSON.stringify(this.snackbar) }),
452
- h("div", { ref: (el) => (this.liveRegion = el), class: "live-region sr-only", "aria-live": "assertive" // since it's directly linked to the action the user took, it should be read in priority. With polite, some messages get dismissed in favor of the page title
453
- , "aria-atomic": "true" })));
454
- }
455
- static get is() { return "wm-network-uploader"; }
456
- static get encapsulation() { return "shadow"; }
457
- static get originalStyleUrls() { return {
458
- "$": ["wm-network-uploader.scss"]
459
- }; }
460
- static get styleUrls() { return {
461
- "$": ["wm-network-uploader.css"]
462
- }; }
463
- static get properties() { return {
464
- "getPath": {
465
- "type": "string",
466
- "mutable": false,
467
- "complexType": {
468
- "original": "string",
469
- "resolved": "string",
470
- "references": {}
471
- },
472
- "required": true,
473
- "optional": false,
474
- "docs": {
475
- "tags": [],
476
- "text": ""
477
- },
478
- "attribute": "get-path",
479
- "reflect": false
480
- },
481
- "requestUploadPath": {
482
- "type": "string",
483
- "mutable": false,
484
- "complexType": {
485
- "original": "string",
486
- "resolved": "string",
487
- "references": {}
488
- },
489
- "required": true,
490
- "optional": false,
491
- "docs": {
492
- "tags": [],
493
- "text": ""
494
- },
495
- "attribute": "request-upload-path",
496
- "reflect": false
497
- },
498
- "uploadPath": {
499
- "type": "string",
500
- "mutable": false,
501
- "complexType": {
502
- "original": "string",
503
- "resolved": "string",
504
- "references": {}
505
- },
506
- "required": true,
507
- "optional": false,
508
- "docs": {
509
- "tags": [],
510
- "text": ""
511
- },
512
- "attribute": "upload-path",
513
- "reflect": false
514
- },
515
- "requestDownloadPath": {
516
- "type": "string",
517
- "mutable": false,
518
- "complexType": {
519
- "original": "string",
520
- "resolved": "string",
521
- "references": {}
522
- },
523
- "required": true,
524
- "optional": false,
525
- "docs": {
526
- "tags": [],
527
- "text": ""
528
- },
529
- "attribute": "request-download-path",
530
- "reflect": false
531
- },
532
- "deletePath": {
533
- "type": "string",
534
- "mutable": false,
535
- "complexType": {
536
- "original": "string",
537
- "resolved": "string",
538
- "references": {}
539
- },
540
- "required": true,
541
- "optional": false,
542
- "docs": {
543
- "tags": [],
544
- "text": ""
545
- },
546
- "attribute": "delete-path",
547
- "reflect": false
548
- },
549
- "associatedData": {
550
- "type": "string",
551
- "mutable": false,
552
- "complexType": {
553
- "original": "string",
554
- "resolved": "string | undefined",
555
- "references": {}
556
- },
557
- "required": false,
558
- "optional": true,
559
- "docs": {
560
- "tags": [],
561
- "text": ""
562
- },
563
- "attribute": "associated-data",
564
- "reflect": false
565
- },
566
- "buttonText": {
567
- "type": "string",
568
- "mutable": false,
569
- "complexType": {
570
- "original": "string",
571
- "resolved": "string",
572
- "references": {}
573
- },
574
- "required": true,
575
- "optional": false,
576
- "docs": {
577
- "tags": [],
578
- "text": ""
579
- },
580
- "attribute": "button-text",
581
- "reflect": false
582
- },
583
- "buttonType": {
584
- "type": "string",
585
- "mutable": false,
586
- "complexType": {
587
- "original": "\"primary\" | \"secondary\"",
588
- "resolved": "\"primary\" | \"secondary\"",
589
- "references": {}
590
- },
591
- "required": false,
592
- "optional": false,
593
- "docs": {
594
- "tags": [],
595
- "text": ""
596
- },
597
- "attribute": "button-type",
598
- "reflect": false,
599
- "defaultValue": "\"secondary\""
600
- },
601
- "emptyStateText": {
602
- "type": "string",
603
- "mutable": false,
604
- "complexType": {
605
- "original": "string",
606
- "resolved": "string",
607
- "references": {}
608
- },
609
- "required": true,
610
- "optional": false,
611
- "docs": {
612
- "tags": [],
613
- "text": ""
614
- },
615
- "attribute": "empty-state-text",
616
- "reflect": false
617
- },
618
- "icon": {
619
- "type": "string",
620
- "mutable": false,
621
- "complexType": {
622
- "original": "string",
623
- "resolved": "string",
624
- "references": {}
625
- },
626
- "required": true,
627
- "optional": false,
628
- "docs": {
629
- "tags": [],
630
- "text": ""
631
- },
632
- "attribute": "icon",
633
- "reflect": false
634
- },
635
- "fileTypes": {
636
- "type": "string",
637
- "mutable": false,
638
- "complexType": {
639
- "original": "string",
640
- "resolved": "string",
641
- "references": {}
642
- },
643
- "required": false,
644
- "optional": false,
645
- "docs": {
646
- "tags": [],
647
- "text": ""
648
- },
649
- "attribute": "file-types",
650
- "reflect": false,
651
- "defaultValue": "\"pdf txt log xml doc docx xls xlsx ppt pptx gif jpg jpeg png csv\""
652
- },
653
- "maxSize": {
654
- "type": "number",
655
- "mutable": false,
656
- "complexType": {
657
- "original": "number",
658
- "resolved": "number",
659
- "references": {}
660
- },
661
- "required": false,
662
- "optional": false,
663
- "docs": {
664
- "tags": [],
665
- "text": ""
666
- },
667
- "attribute": "max-size",
668
- "reflect": false,
669
- "defaultValue": "100"
670
- },
671
- "maxFiles": {
672
- "type": "number",
673
- "mutable": false,
674
- "complexType": {
675
- "original": "number",
676
- "resolved": "number | undefined",
677
- "references": {}
678
- },
679
- "required": false,
680
- "optional": true,
681
- "docs": {
682
- "tags": [],
683
- "text": ""
684
- },
685
- "attribute": "max-files",
686
- "reflect": false
687
- },
688
- "sortBy": {
689
- "type": "string",
690
- "mutable": false,
691
- "complexType": {
692
- "original": "\"name\" | \"date\"",
693
- "resolved": "\"date\" | \"name\"",
694
- "references": {}
695
- },
696
- "required": false,
697
- "optional": false,
698
- "docs": {
699
- "tags": [],
700
- "text": ""
701
- },
702
- "attribute": "sort-by",
703
- "reflect": false,
704
- "defaultValue": "\"date\""
705
- }
706
- }; }
707
- static get states() { return {
708
- "isTabbing": {},
709
- "fileList": {},
710
- "inProgressList": {},
711
- "errorList": {},
712
- "notif": {},
713
- "snackbar": {},
714
- "numFilesInProgress": {}
715
- }; }
716
- static get events() { return [{
717
- "method": "wmNetworkUploaderFilesChanged",
718
- "name": "wmNetworkUploaderFilesChanged",
719
- "bubbles": true,
720
- "cancelable": true,
721
- "composed": true,
722
- "docs": {
723
- "tags": [],
724
- "text": ""
725
- },
726
- "complexType": {
727
- "original": "Object[]",
728
- "resolved": "Object[]",
729
- "references": {
730
- "Object": {
731
- "location": "global"
732
- }
733
- }
734
- }
735
- }]; }
736
- static get elementRef() { return "el"; }
737
- static get watchers() { return [{
738
- "propName": "maxSize",
739
- "methodName": "setMaxSize"
740
- }, {
741
- "propName": "numFilesInProgress",
742
- "methodName": "uploadNotifications"
743
- }, {
744
- "propName": "fileList",
745
- "methodName": "emitFileListEvent"
746
- }]; }
747
- static get listeners() { return [{
748
- "name": "wmUserIsTabbing",
749
- "method": "toggleTabbingOn",
750
- "target": "window",
751
- "capture": false,
752
- "passive": false
753
- }, {
754
- "name": "wmUserIsNotTabbing",
755
- "method": "toggleTabbingOff",
756
- "target": "window",
757
- "capture": false,
758
- "passive": false
759
- }, {
760
- "name": "wmUploadProgress",
761
- "method": "handleUploadProgress",
762
- "target": undefined,
763
- "capture": false,
764
- "passive": false
765
- }, {
766
- "name": "userFinishedSnack",
767
- "method": "handleFinishedSnack",
768
- "target": undefined,
769
- "capture": false,
770
- "passive": false
771
- }]; }
772
- }
1
+ import { Component, Element, h, Host, Prop, State, Listen, Watch, Event } from "@stencil/core";
2
+ import { forceUpdate } from "@stencil/core";
3
+ import { getRequest, postRequest, putRequest, deleteRequest } from "../../../global/services/http-service";
4
+ import { intl, generateId, hasValidType } from "../../../global/functions";
5
+ export class NetworkUploader {
6
+ constructor() {
7
+ this.isTabbing = false;
8
+ this.fileList = [];
9
+ this.inProgressList = [];
10
+ this.errorList = []; // for errors on a particular file
11
+ this.snackbar = [];
12
+ this.fileTypes = "pdf txt log xml doc docx xls xlsx ppt pptx gif jpg jpeg png csv";
13
+ this.maxSize = 100; // in Mb
14
+ this.sortBy = "date"; // name is descending (a -> z), date is ascending (earlier -> later)
15
+ this.inputId = generateId();
16
+ }
17
+ toggleTabbingOn() {
18
+ this.isTabbing = true;
19
+ }
20
+ toggleTabbingOff() {
21
+ this.isTabbing = false;
22
+ }
23
+ handleUploadProgress(ev) {
24
+ this.inProgressList.map((i) => {
25
+ if (i.name === ev.detail.file_name) {
26
+ i.progress = ev.detail.progress;
27
+ forceUpdate(this.el);
28
+ }
29
+ });
30
+ if (ev.detail.progress === 100) {
31
+ // inProgressList is not a perfect reflection of how many files are uploading
32
+ // because even after progress reached 100% the item should remain in the array
33
+ // (should only be removed once it appears in fileList)
34
+ this.numFilesInProgress -= 1;
35
+ }
36
+ // aggregate the percentages for SR to announce progress
37
+ if (!this.srProgress && this.numFilesInProgress) {
38
+ const total = this.numFilesInProgress; // if we don't reassign this number changes when a file is done and the percentage becomes wrong
39
+ this.srProgress = setInterval(() => {
40
+ const aggregate = this.inProgressList.reduce((avg, item) => (item.progress ? avg + item.progress : avg), 0);
41
+ if (this.liveRegion) {
42
+ this.liveRegion.innerHTML = intl.formatMessage({
43
+ id: "uploader.uploadProgress",
44
+ defaultMessage: "Upload progress: {percentage}%",
45
+ }, { percentage: Math.floor(aggregate / total) });
46
+ }
47
+ }, 5000);
48
+ }
49
+ }
50
+ handleFinishedSnack(ev) {
51
+ this.snackbar = this.snackbar.filter((n) => n.id !== ev.detail.id);
52
+ }
53
+ setMaxSize() {
54
+ // Max size can't be set above 100Mb
55
+ this.maxSize = this.maxSize > 100 ? 100 : this.maxSize;
56
+ }
57
+ uploadNotifications(newNum, oldNum) {
58
+ if (oldNum > 0 && newNum === 0) {
59
+ setTimeout(() => {
60
+ this.notif = null;
61
+ }, 20 * 1000);
62
+ const message = intl.formatMessage({
63
+ id: "uploader.finishedUploading",
64
+ defaultMessage: "Finished uploading",
65
+ });
66
+ this.notif = { id: 5, message };
67
+ if (this.liveRegion) {
68
+ this.liveRegion.innerHTML = message;
69
+ }
70
+ this.numFilesInProgress = null;
71
+ clearInterval(this.srProgress);
72
+ forceUpdate(this.el);
73
+ }
74
+ else if (newNum > 0 && newNum != oldNum) {
75
+ const message = intl.formatMessage({
76
+ id: "uploader.filesUploading",
77
+ defaultMessage: "{num, plural, one {1 file uploading} other {# files uploading}}",
78
+ }, { num: newNum });
79
+ this.notif = { id: 6, message };
80
+ if (this.liveRegion) {
81
+ this.liveRegion.innerHTML = message;
82
+ }
83
+ forceUpdate(this.el);
84
+ }
85
+ }
86
+ emitFileListEvent() {
87
+ this.wmNetworkUploaderFilesChanged.emit(this.fileList);
88
+ }
89
+ formatDate(isoDate, locale, timezone) {
90
+ const timestamp = Date.parse(isoDate);
91
+ if (locale.match(/^[a-zA-Z]*(-[a-zA-Z0-9]*)*$/g)) {
92
+ // make sure locale is valid, otherwise set default locale
93
+ try {
94
+ //@ts-ignore
95
+ Intl.getCanonicalLocales(locale);
96
+ }
97
+ catch (_a) {
98
+ locale = "en-us";
99
+ }
100
+ }
101
+ else {
102
+ return isoDate;
103
+ }
104
+ if (!!timestamp) {
105
+ const date = new Date(timestamp);
106
+ const year = date.getFullYear();
107
+ const month = ("0" + (date.getMonth() + 1)).substr(-2);
108
+ const day = ("0" + date.getDate()).substr(-2);
109
+ const time = date.toLocaleTimeString([locale, "en-us"], {
110
+ timeZone: timezone,
111
+ hour: "numeric",
112
+ minute: "numeric",
113
+ });
114
+ return `${year}/${month}/${day} ${time}`;
115
+ }
116
+ else {
117
+ return isoDate;
118
+ }
119
+ }
120
+ isValidSize(size) {
121
+ // https://stackoverflow.com/questions/2365100/converting-bytes-to-megabytes
122
+ return size / 1024 / 1024 <= this.maxSize;
123
+ }
124
+ isDuplicate(name) {
125
+ return this.fileList.find((f) => f.file_name === name);
126
+ }
127
+ isEmpty() {
128
+ return !this.fileList.length && !this.inProgressList.length && !this.errorList.length;
129
+ }
130
+ isDisabled() {
131
+ const isMax = !!this.maxFiles && this.fileList.length >= this.maxFiles;
132
+ if (!this.notif) {
133
+ if (isMax) {
134
+ const errMessage = intl.formatMessage({
135
+ id: "uploader.fileUploadLimit",
136
+ defaultMessage: "You have reached the file upload limit.",
137
+ });
138
+ this.notif = { id: 1, message: errMessage };
139
+ if (this.liveRegion) {
140
+ this.liveRegion.innerHTML = errMessage;
141
+ }
142
+ }
143
+ else {
144
+ this.notif = null;
145
+ }
146
+ }
147
+ else if (this.notif.id === 1) {
148
+ // to make sure SR don't repeat the error, don't reset it if it already is showing
149
+ // just dismiss it if isMax is not true anymore
150
+ if (!isMax) {
151
+ this.notif = null;
152
+ }
153
+ // finally do nothing if error 2 is showing (special case when user selects too many files)
154
+ }
155
+ // disable if is the uploader is maxed out or if something is uploading
156
+ return isMax || this.numFilesInProgress > 0;
157
+ }
158
+ purgeProgressList(item) {
159
+ // If a file is retrieved from the server because it was just uploaded
160
+ // it should be removed from inProgressList
161
+ this.inProgressList = this.inProgressList.filter((i) => i.progress !== 100 && i.name !== item.file_name);
162
+ }
163
+ getExistingFiles() {
164
+ getRequest(this.getPath)
165
+ .then((reply) => {
166
+ reply.map((item) => this.purgeProgressList(item));
167
+ if (this.sortBy === "name") {
168
+ reply.sort((a, b) => a.file_name.localeCompare(b.file_name));
169
+ }
170
+ this.fileList = reply;
171
+ })
172
+ .catch((errStatus) => {
173
+ console.error(errStatus);
174
+ });
175
+ }
176
+ clearErrors() {
177
+ this.errorList = [];
178
+ this.notif = null;
179
+ }
180
+ handleFiles(ev) {
181
+ const files = Array.from(ev.target.files);
182
+ const validTypes = this.fileTypes.split(" ").join(", ");
183
+ if (!!this.maxFiles && this.fileList.length + files.length > this.maxFiles) {
184
+ const errMessage = intl.formatMessage({
185
+ id: "uploader.fileUploadExceeded",
186
+ defaultMessage: "The number of selected files exceeds the limit.",
187
+ });
188
+ this.notif = { id: 2, message: errMessage };
189
+ if (this.liveRegion) {
190
+ this.liveRegion.innerHTML = errMessage;
191
+ }
192
+ }
193
+ else {
194
+ // map to create inProgress items so they can be displayed immediately
195
+ files &&
196
+ files.map((file) => {
197
+ if (!hasValidType(file, this.fileTypes)) {
198
+ const error = {
199
+ file_name: file.name,
200
+ message: intl.formatMessage({
201
+ id: "uploader.invalidFileType",
202
+ defaultMessage: "The file type is invalid. Accepted types: {validTypes}",
203
+ }, { validTypes }),
204
+ };
205
+ this.errorList.push(error);
206
+ }
207
+ else if (!this.isValidSize(file.size)) {
208
+ const error = {
209
+ file_name: file.name,
210
+ message: intl.formatMessage({
211
+ id: "uploader.fileTooLarge",
212
+ defaultMessage: "The selected file is too large. Maximum file size is {size}Mb",
213
+ }, { size: this.maxSize }),
214
+ };
215
+ this.errorList.push(error);
216
+ }
217
+ else if (this.isDuplicate(file.name)) {
218
+ const error = {
219
+ file_name: file.name,
220
+ message: intl.formatMessage({
221
+ id: "uploader.duplicateName",
222
+ defaultMessage: "A file with this name already exists.",
223
+ }),
224
+ };
225
+ this.errorList.push(error);
226
+ }
227
+ else {
228
+ this.inProgressList.push(file);
229
+ }
230
+ forceUpdate(this.el);
231
+ });
232
+ if (this.inProgressList.length > 0) {
233
+ this.numFilesInProgress = this.inProgressList.length;
234
+ // map over "uploadable" files for actual upload
235
+ // this happens separately otherwise at each pass we'd have to wait for the requests to complete before displaying things
236
+ this.inProgressList.map((file, i) => {
237
+ this.uploadFile(file, i); // = input.files file
238
+ });
239
+ }
240
+ }
241
+ ev.target.value = ""; // reset to ensure that onChange is triggered when the same file is selected a second time
242
+ }
243
+ uploadFile(file, i) {
244
+ // get upload link
245
+ getRequest(this.requestUploadPath + i.toString())
246
+ .then(async (reply) => {
247
+ const assData = JSON.parse(this.associatedData);
248
+ const fileInfo = Object.assign(Object.assign({}, assData), { file_name: file.name, mime_type: file.type, file_path: reply.path, document_uuid: reply.guid });
249
+ // onProgress is passed as a callback to the ev listener inside putRequest (http service),
250
+ // so we can't change component things directly => custom event to pass data to uploader component.
251
+ const onProgress = (ev) => {
252
+ const progress = ev.lengthComputable && ev.total !== 0 ? (ev.loaded / ev.total) * 100 : 0;
253
+ const event = new CustomEvent("wmUploadProgress", {
254
+ detail: { file_name: file.name, progress },
255
+ });
256
+ this.el.dispatchEvent(event);
257
+ };
258
+ // upload to AWS s3 to url we just fetched
259
+ await putRequest(reply.url, file, onProgress);
260
+ // create the db entry
261
+ await postRequest(this.uploadPath, fileInfo);
262
+ // get files from server again
263
+ this.getExistingFiles();
264
+ })
265
+ .catch((err) => {
266
+ // do not show toast on error
267
+ this.numFilesInProgress = null;
268
+ // do not show in progress item on error
269
+ this.inProgressList = [];
270
+ this.notif = {
271
+ id: 3,
272
+ message: intl.formatMessage({
273
+ id: "global.genericError",
274
+ defaultMessage: "An error occurred. Please try again.",
275
+ }),
276
+ };
277
+ console.error(err);
278
+ });
279
+ }
280
+ downloadFile(uuid, path) {
281
+ this.clearErrors();
282
+ // POST request to get temporary download link
283
+ const payload = {
284
+ file_path: path,
285
+ file_uuid: uuid,
286
+ };
287
+ postRequest(this.requestDownloadPath, payload)
288
+ .then((reply) => {
289
+ // we got the download link, now download the file
290
+ window.location.assign(reply.url);
291
+ })
292
+ .catch((err) => {
293
+ console.error(err);
294
+ });
295
+ }
296
+ deleteFile(file) {
297
+ this.clearErrors();
298
+ this.snackbar.push({
299
+ message: intl.formatMessage({
300
+ id: "uploader.fileDeleted",
301
+ defaultMessage: "{fileName} was deleted.",
302
+ }, { fileName: file.file_name }),
303
+ id: file.uuid,
304
+ link: "",
305
+ });
306
+ // rm file from list immediately.
307
+ this.fileList = this.fileList.filter((i) => i.uuid !== file.uuid);
308
+ deleteRequest(`${this.deletePath}/${file.uuid}`).catch((err) => {
309
+ this.notif = {
310
+ id: 4,
311
+ message: intl.formatMessage({
312
+ id: "uploader.fileNotDeleted",
313
+ defaultMessage: "{fileName} could not be deleted.",
314
+ }, { fileName: file.file_name }),
315
+ };
316
+ this.getExistingFiles();
317
+ console.error(err);
318
+ });
319
+ }
320
+ componentWillLoad() {
321
+ if (!this.getPath) {
322
+ throw new Error("wm-network-uploader: get-path is a required prop");
323
+ }
324
+ if (!this.requestUploadPath) {
325
+ throw new Error("wm-network-uploader: request-upload-path is a required prop");
326
+ }
327
+ if (!this.uploadPath) {
328
+ throw new Error("wm-network-uploader: upload-path is a required prop");
329
+ }
330
+ if (!this.requestDownloadPath) {
331
+ throw new Error("wm-network-uploader: request-download-path is a required prop");
332
+ }
333
+ if (!this.deletePath) {
334
+ throw new Error("wm-network-uploader: delete-path is a required prop");
335
+ }
336
+ if (!this.buttonText) {
337
+ throw new Error("wm-network-uploader: button-text is a required prop");
338
+ }
339
+ if (!this.emptyStateText) {
340
+ throw new Error("wm-network-uploader: empty-state-text is a required prop");
341
+ }
342
+ if (!this.associatedData) {
343
+ throw new Error("wm-network-uploader: associated-data is a required prop");
344
+ }
345
+ this.setMaxSize();
346
+ this.clearErrors();
347
+ }
348
+ componentDidLoad() {
349
+ this.getExistingFiles();
350
+ // awkward way to handle Edge bug where file input dialog doesn't open on Enter key
351
+ // first, detect polyfill by checking the class name that it adds to the component
352
+ // (it's important NOT to execute the code below in normal browsers as it would open the dialog twice)
353
+ this.el.classList.contains("sc-wm-uploader-h") &&
354
+ // then add an even listener to force a click event when Enter key is pressed
355
+ this.inputEl &&
356
+ this.inputEl.addEventListener("keydown", (ev) => {
357
+ if (this.inputEl && ev.keyCode === 13) {
358
+ this.inputEl.click();
359
+ }
360
+ });
361
+ if (document.body.classList.contains("wmcl-user-is-tabbing")) {
362
+ this.toggleTabbingOn();
363
+ }
364
+ }
365
+ renderUploadSvg() {
366
+ return (h("svg", { class: "upload-file", width: "91", height: "84", viewBox: "0 0 91 84" },
367
+ h("path", { transform: "rotate(-180.000000) translate(-452, -255.5)", fill: "#eae8ec", d: "M361,249.681818 L361,197.318182 C361,194.118182 365.555061,191.5 371.122358,191.5 L391.316463,191.5 L391.286495,200.562515 L369.871211,200.600292 L369.885404,200.622146 L369.886423,246.655947 L443.657038,246.840767 L443.657038,200.622146 L421.683537,200.622146 L421.683537,191.5 L441.978865,191.5 C447.546162,191.5 452,194.118182 452,197.318182 L452,249.681818 C452,252.881818 447.546162,255.5 441.978865,255.5 L371.122358,255.5 C365.504449,255.5 361,252.881818 361,249.681818 Z M391.5,207 L406,221 L420.5,207 L409.625,207 L409.625,172 L402.375,172 L402.375,207 L391.5,207 Z" })));
368
+ }
369
+ renderIcon() {
370
+ // mdi attachment icon (paper clip) is horizontal but we want it vertical
371
+ const rotate = this.icon && this.icon === "f066";
372
+ return (this.icon && (h("span", { class: `mdi ${rotate ? "rotate" : ""}` }, String.fromCodePoint(parseInt(`0x${this.icon}`)))));
373
+ }
374
+ renderNotif() {
375
+ return this.notif ? h("span", { class: "notif" }, this.notif.message) : "";
376
+ }
377
+ renderUploadButton(classes) {
378
+ return (h("div", { class: "notif-wrapper" },
379
+ h("input", { ref: (el) => (this.inputEl = el), name: this.inputId, id: this.inputId, class: "sr-only", type: "file", multiple: true, onClick: () => this.clearErrors(), onChange: (e) => this.handleFiles(e), disabled: this.isDisabled() }),
380
+ h("label", { htmlFor: this.inputId, class: `wm-button ${classes}${this.isTabbing ? " user-is-tabbing" : ""}` },
381
+ this.renderIcon(),
382
+ this.buttonText),
383
+ this.renderNotif()));
384
+ }
385
+ renderWithItems() {
386
+ return (h("div", null,
387
+ h("div", { class: "header" },
388
+ h("div", { class: "descr" },
389
+ h("slot", null)),
390
+ this.renderUploadButton(this.isDisabled() ? "disabled" : "")),
391
+ this.renderErrorList(),
392
+ this.renderInProgressList(),
393
+ this.renderFileList()));
394
+ }
395
+ renderEmptyState() {
396
+ return (h("div", null,
397
+ h("div", { class: "header" },
398
+ h("div", { class: "descr" },
399
+ h("slot", null))),
400
+ h("div", { class: "empty-block" },
401
+ this.renderUploadSvg(),
402
+ h("div", { class: "empty-message" }, this.emptyStateText),
403
+ this.renderUploadButton("-primary empty-button"))));
404
+ }
405
+ renderErrorList() {
406
+ if (!!this.errorList.length) {
407
+ if (this.liveRegion) {
408
+ let srMessage = this.errorList.reduce(function (acc, err) {
409
+ return acc + `${err.file_name}: ${err.message} `;
410
+ }, "");
411
+ this.liveRegion.innerHTML = srMessage;
412
+ }
413
+ return (h("ul", { class: "error-list" }, this.errorList.map((err) => (h("li", null,
414
+ h("div", { class: "file --error" },
415
+ h("div", { class: "file-info" },
416
+ h("span", { class: "filename" }, err.file_name))),
417
+ h("div", { class: "error-message" }, err.message))))));
418
+ }
419
+ }
420
+ renderInProgressList() {
421
+ return (h("ul", { class: "inprogress-list" }, this.inProgressList.map((item) => {
422
+ return (h("li", null,
423
+ h("div", { class: "file --progress" },
424
+ h("div", { class: "progress", style: {
425
+ background: `linear-gradient(to right, rgba(67, 126, 142, 1) ${item.progress}%, transparent ${item.progress}%)`,
426
+ } }),
427
+ h("div", { class: "file-info" },
428
+ h("span", { class: "filename" },
429
+ item.name,
430
+ " ",
431
+ h("span", { class: "sr-only" }, "uploading"))))));
432
+ })));
433
+ }
434
+ renderFile(item) {
435
+ return (h("li", null,
436
+ h("div", { class: "file --saved" },
437
+ h("div", { class: "file-info" },
438
+ h("span", { class: "filename" }, item.file_name),
439
+ h("span", { class: "filedate" }, this.formatDate(item.updated_at, intl.locale, Intl.DateTimeFormat().resolvedOptions().timeZone))),
440
+ h("div", { class: "file-controls" },
441
+ h("wm-button", { class: "download-button", "button-type": "icononly", tooltip: "download", "label-for-identical-buttons": `download ${item.file_name}`, icon: "f1da", onClick: () => this.downloadFile(item.uuid, item.file_path) }),
442
+ h("wm-button", { class: "delete-button", "button-type": "icononly", tooltip: "remove", "label-for-identical-buttons": `remove ${item.file_name}`, icon: "f1c0", onClick: () => this.deleteFile(item) })))));
443
+ }
444
+ renderFileList() {
445
+ return h("ul", { class: "file-list" }, this.fileList.map((item) => this.renderFile(item)));
446
+ }
447
+ render() {
448
+ return (h(Host, null,
449
+ this.isEmpty() ? this.renderEmptyState() : this.renderWithItems(),
450
+ h("wm-snackbar", { notifications: JSON.stringify(this.snackbar) }),
451
+ h("div", { ref: (el) => (this.liveRegion = el), class: "live-region sr-only", "aria-live": "assertive" // since it's directly linked to the action the user took, it should be read in priority. With polite, some messages get dismissed in favor of the page title
452
+ , "aria-atomic": "true" })));
453
+ }
454
+ static get is() { return "wm-network-uploader"; }
455
+ static get encapsulation() { return "shadow"; }
456
+ static get originalStyleUrls() { return {
457
+ "$": ["wm-network-uploader.scss"]
458
+ }; }
459
+ static get styleUrls() { return {
460
+ "$": ["wm-network-uploader.css"]
461
+ }; }
462
+ static get properties() { return {
463
+ "getPath": {
464
+ "type": "string",
465
+ "mutable": false,
466
+ "complexType": {
467
+ "original": "string",
468
+ "resolved": "string",
469
+ "references": {}
470
+ },
471
+ "required": true,
472
+ "optional": false,
473
+ "docs": {
474
+ "tags": [],
475
+ "text": ""
476
+ },
477
+ "attribute": "get-path",
478
+ "reflect": false
479
+ },
480
+ "requestUploadPath": {
481
+ "type": "string",
482
+ "mutable": false,
483
+ "complexType": {
484
+ "original": "string",
485
+ "resolved": "string",
486
+ "references": {}
487
+ },
488
+ "required": true,
489
+ "optional": false,
490
+ "docs": {
491
+ "tags": [],
492
+ "text": ""
493
+ },
494
+ "attribute": "request-upload-path",
495
+ "reflect": false
496
+ },
497
+ "uploadPath": {
498
+ "type": "string",
499
+ "mutable": false,
500
+ "complexType": {
501
+ "original": "string",
502
+ "resolved": "string",
503
+ "references": {}
504
+ },
505
+ "required": true,
506
+ "optional": false,
507
+ "docs": {
508
+ "tags": [],
509
+ "text": ""
510
+ },
511
+ "attribute": "upload-path",
512
+ "reflect": false
513
+ },
514
+ "requestDownloadPath": {
515
+ "type": "string",
516
+ "mutable": false,
517
+ "complexType": {
518
+ "original": "string",
519
+ "resolved": "string",
520
+ "references": {}
521
+ },
522
+ "required": true,
523
+ "optional": false,
524
+ "docs": {
525
+ "tags": [],
526
+ "text": ""
527
+ },
528
+ "attribute": "request-download-path",
529
+ "reflect": false
530
+ },
531
+ "deletePath": {
532
+ "type": "string",
533
+ "mutable": false,
534
+ "complexType": {
535
+ "original": "string",
536
+ "resolved": "string",
537
+ "references": {}
538
+ },
539
+ "required": true,
540
+ "optional": false,
541
+ "docs": {
542
+ "tags": [],
543
+ "text": ""
544
+ },
545
+ "attribute": "delete-path",
546
+ "reflect": false
547
+ },
548
+ "associatedData": {
549
+ "type": "string",
550
+ "mutable": false,
551
+ "complexType": {
552
+ "original": "string",
553
+ "resolved": "string | undefined",
554
+ "references": {}
555
+ },
556
+ "required": false,
557
+ "optional": true,
558
+ "docs": {
559
+ "tags": [],
560
+ "text": ""
561
+ },
562
+ "attribute": "associated-data",
563
+ "reflect": false
564
+ },
565
+ "buttonText": {
566
+ "type": "string",
567
+ "mutable": false,
568
+ "complexType": {
569
+ "original": "string",
570
+ "resolved": "string",
571
+ "references": {}
572
+ },
573
+ "required": true,
574
+ "optional": false,
575
+ "docs": {
576
+ "tags": [],
577
+ "text": ""
578
+ },
579
+ "attribute": "button-text",
580
+ "reflect": false
581
+ },
582
+ "emptyStateText": {
583
+ "type": "string",
584
+ "mutable": false,
585
+ "complexType": {
586
+ "original": "string",
587
+ "resolved": "string",
588
+ "references": {}
589
+ },
590
+ "required": true,
591
+ "optional": false,
592
+ "docs": {
593
+ "tags": [],
594
+ "text": ""
595
+ },
596
+ "attribute": "empty-state-text",
597
+ "reflect": false
598
+ },
599
+ "icon": {
600
+ "type": "string",
601
+ "mutable": false,
602
+ "complexType": {
603
+ "original": "string",
604
+ "resolved": "string",
605
+ "references": {}
606
+ },
607
+ "required": true,
608
+ "optional": false,
609
+ "docs": {
610
+ "tags": [],
611
+ "text": ""
612
+ },
613
+ "attribute": "icon",
614
+ "reflect": false
615
+ },
616
+ "fileTypes": {
617
+ "type": "string",
618
+ "mutable": false,
619
+ "complexType": {
620
+ "original": "string",
621
+ "resolved": "string",
622
+ "references": {}
623
+ },
624
+ "required": false,
625
+ "optional": false,
626
+ "docs": {
627
+ "tags": [],
628
+ "text": ""
629
+ },
630
+ "attribute": "file-types",
631
+ "reflect": false,
632
+ "defaultValue": "\"pdf txt log xml doc docx xls xlsx ppt pptx gif jpg jpeg png csv\""
633
+ },
634
+ "maxSize": {
635
+ "type": "number",
636
+ "mutable": false,
637
+ "complexType": {
638
+ "original": "number",
639
+ "resolved": "number",
640
+ "references": {}
641
+ },
642
+ "required": false,
643
+ "optional": false,
644
+ "docs": {
645
+ "tags": [],
646
+ "text": ""
647
+ },
648
+ "attribute": "max-size",
649
+ "reflect": false,
650
+ "defaultValue": "100"
651
+ },
652
+ "maxFiles": {
653
+ "type": "number",
654
+ "mutable": false,
655
+ "complexType": {
656
+ "original": "number",
657
+ "resolved": "number | undefined",
658
+ "references": {}
659
+ },
660
+ "required": false,
661
+ "optional": true,
662
+ "docs": {
663
+ "tags": [],
664
+ "text": ""
665
+ },
666
+ "attribute": "max-files",
667
+ "reflect": false
668
+ },
669
+ "sortBy": {
670
+ "type": "string",
671
+ "mutable": false,
672
+ "complexType": {
673
+ "original": "\"name\" | \"date\"",
674
+ "resolved": "\"date\" | \"name\"",
675
+ "references": {}
676
+ },
677
+ "required": false,
678
+ "optional": false,
679
+ "docs": {
680
+ "tags": [],
681
+ "text": ""
682
+ },
683
+ "attribute": "sort-by",
684
+ "reflect": false,
685
+ "defaultValue": "\"date\""
686
+ }
687
+ }; }
688
+ static get states() { return {
689
+ "isTabbing": {},
690
+ "fileList": {},
691
+ "inProgressList": {},
692
+ "errorList": {},
693
+ "notif": {},
694
+ "snackbar": {},
695
+ "numFilesInProgress": {}
696
+ }; }
697
+ static get events() { return [{
698
+ "method": "wmNetworkUploaderFilesChanged",
699
+ "name": "wmNetworkUploaderFilesChanged",
700
+ "bubbles": true,
701
+ "cancelable": true,
702
+ "composed": true,
703
+ "docs": {
704
+ "tags": [],
705
+ "text": ""
706
+ },
707
+ "complexType": {
708
+ "original": "Object[]",
709
+ "resolved": "Object[]",
710
+ "references": {
711
+ "Object": {
712
+ "location": "global"
713
+ }
714
+ }
715
+ }
716
+ }]; }
717
+ static get elementRef() { return "el"; }
718
+ static get watchers() { return [{
719
+ "propName": "maxSize",
720
+ "methodName": "setMaxSize"
721
+ }, {
722
+ "propName": "numFilesInProgress",
723
+ "methodName": "uploadNotifications"
724
+ }, {
725
+ "propName": "fileList",
726
+ "methodName": "emitFileListEvent"
727
+ }]; }
728
+ static get listeners() { return [{
729
+ "name": "wmUserIsTabbing",
730
+ "method": "toggleTabbingOn",
731
+ "target": "window",
732
+ "capture": false,
733
+ "passive": false
734
+ }, {
735
+ "name": "wmUserIsNotTabbing",
736
+ "method": "toggleTabbingOff",
737
+ "target": "window",
738
+ "capture": false,
739
+ "passive": false
740
+ }, {
741
+ "name": "wmUploadProgress",
742
+ "method": "handleUploadProgress",
743
+ "target": undefined,
744
+ "capture": false,
745
+ "passive": false
746
+ }, {
747
+ "name": "userFinishedSnack",
748
+ "method": "handleFinishedSnack",
749
+ "target": undefined,
750
+ "capture": false,
751
+ "passive": false
752
+ }]; }
753
+ }