@scaleflex/widget-provider-views 0.0.1

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 (35) hide show
  1. package/CHANGELOG.md +8103 -0
  2. package/LICENSE +21 -0
  3. package/README.md +44 -0
  4. package/dist/style.css +671 -0
  5. package/dist/style.min.css +1 -0
  6. package/lib/ProviderViews.Styled.js +22 -0
  7. package/lib/components/AuthView.js +35 -0
  8. package/lib/components/AuthView.styled.js +21 -0
  9. package/lib/components/Browser.js +58 -0
  10. package/lib/components/Items/AssetsSection/AssetsSectionTableHeader.js +41 -0
  11. package/lib/components/Items/AssetsSection/AssetsSectionTableRow.js +147 -0
  12. package/lib/components/Items/AssetsSection/index.js +65 -0
  13. package/lib/components/Items/FoldersSection/FoldersSectionTableHeader.js +41 -0
  14. package/lib/components/Items/FoldersSection/FoldersSectionTableRow.js +102 -0
  15. package/lib/components/Items/FoldersSection/index.js +64 -0
  16. package/lib/components/Items/Items.styled.js +156 -0
  17. package/lib/components/Items/List.js +96 -0
  18. package/lib/components/Items/index.js +172 -0
  19. package/lib/components/SearchProviderView/FooterActions.js +25 -0
  20. package/lib/components/SearchProviderView/SearchFilterInput.js +76 -0
  21. package/lib/components/SearchProviderView/SearchProviderBrowser.js +74 -0
  22. package/lib/components/SearchProviderView/SearchProviderBrowser.styled.js +65 -0
  23. package/lib/components/SearchProviderView/SearchProviderView.js +248 -0
  24. package/lib/components/SearchProviderView/index.js +1 -0
  25. package/lib/context/index.js +2 -0
  26. package/lib/hooks/useBrowserContext.js +5 -0
  27. package/lib/index.js +850 -0
  28. package/lib/style/filerobot-ProviderBrowser-layoutType--grid.scss +233 -0
  29. package/lib/style/filerobot-ProviderBrowser-layoutType--list.scss +190 -0
  30. package/lib/style/filerobot-ProviderBrowserItem-details.scss +207 -0
  31. package/lib/style.scss +309 -0
  32. package/lib/utils/View.js +219 -0
  33. package/lib/utils/remoteFileObjToLocal.js +13 -0
  34. package/package.json +36 -0
  35. package/types/index.d.ts +28 -0
package/lib/index.js ADDED
@@ -0,0 +1,850 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var _excluded = ["children"];
3
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
4
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
5
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
7
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
8
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
9
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
10
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
11
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
12
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
13
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
14
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
15
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
16
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
17
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
19
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
20
+ import { useEffect } from 'react';
21
+ import { LoaderView } from '@scaleflex/widget-common';
22
+ import generateFileId from '@scaleflex/widget-utils/lib/generateFileId';
23
+ import isFolder from '@scaleflex/widget-utils/lib/isFolder';
24
+ import { LIST_VIEW_COLS_IDS, LAYOUTS_IDS, LIST_VIEW_FOLDERS_COLS_IDS, PLUGINS_IDS } from '@scaleflex/widget-utils/lib/constants';
25
+ import AuthView from './components/AuthView';
26
+ import Browser from './components/Browser';
27
+ // TODO: find a way to show version of the current plugin
28
+ // why solution below isn't good?
29
+ // first import doesn't work with webpack 5 as it was deprecated
30
+ // second import fixes webpack 5 issue as it was mentioned in their docs
31
+ // but it exposes our package.json to the client and it is mentioned as security rist in mutiple places
32
+ // https://github.com/axelpale/genversion
33
+ // https://stackoverflow.com/questions/64993118/error-should-not-import-the-named-export-version-imported-as-version
34
+ // https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#10855054
35
+ // import { version } from '../package.json'
36
+ // import packageInfo from '../package.json'
37
+
38
+ /**
39
+ * Array.prototype.findIndex ponyfill for old browsers.
40
+ */
41
+ import { jsx as _jsx } from "react/jsx-runtime";
42
+ function findIndex(array, predicate) {
43
+ for (var i = 0; i < array.length; i++) {
44
+ if (predicate(array[i])) return i;
45
+ }
46
+ return -1;
47
+ }
48
+
49
+ // location.origin does not exist in IE
50
+ function getOrigin() {
51
+ if ('origin' in location) {
52
+ return location.origin; // eslint-disable-line compat/compat
53
+ }
54
+ return "".concat(location.protocol, "//").concat(location.hostname).concat(location.port ? ":".concat(location.port) : '');
55
+ }
56
+ var CloseWrapper = function CloseWrapper(_ref) {
57
+ var children = _ref.children,
58
+ props = _objectWithoutProperties(_ref, _excluded);
59
+ useEffect(function () {
60
+ return props.onUnmount;
61
+ }, []);
62
+ return children;
63
+ };
64
+
65
+ /**
66
+ * Class to easily generate generic views for Provider plugins
67
+ */
68
+ var ProviderView = /*#__PURE__*/function () {
69
+ // static VERSION = packageInfo.version
70
+
71
+ /**
72
+ * @param {object} plugin instance of the plugin
73
+ * @param {object} opts
74
+ */
75
+ function ProviderView(plugin, opts) {
76
+ var _this = this;
77
+ _classCallCheck(this, ProviderView);
78
+ /**
79
+ * Toggles file/folder checkbox to on/off state while updating files list.
80
+ *
81
+ * Note that some extra complexity comes from supporting shift+click to
82
+ * toggle multiple checkboxes at once, which is done by getting all files
83
+ * in between last checked file and current one.
84
+ */
85
+ _defineProperty(this, "toggleCheckbox", function (e, item) {
86
+ var isMultiple = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
87
+ e.stopPropagation();
88
+ e.preventDefault();
89
+ e.currentTarget.focus();
90
+ var _this$plugin$getPlugi = _this.plugin.getPluginCommonState(),
91
+ folders = _this$plugin$getPlugi.folders,
92
+ files = _this$plugin$getPlugi.files;
93
+ var items = _this.filterItems(folders.concat(files));
94
+ var isFolderItem = isFolder(item);
95
+
96
+ // Shift-clicking selects a single consecutive list of items
97
+ // starting at the previous click and deselects everything else.
98
+ if (_this.lastCheckbox && e.shiftKey) {
99
+ var currentSelection;
100
+ var prevIndex = findIndex(items, function (i) {
101
+ return i.id === _this.lastCheckbox;
102
+ });
103
+ var currentIndex = findIndex(items, function (i) {
104
+ return i.id === item.id;
105
+ });
106
+ if (prevIndex < currentIndex) {
107
+ currentSelection = items.slice(prevIndex, currentIndex + 1);
108
+ } else {
109
+ currentSelection = items.slice(currentIndex, prevIndex + 1);
110
+ }
111
+ var _currentSelection$red = currentSelection.reduce(function (selections, currentItem) {
112
+ var key = isFolder(currentItem) ? 'selectedFolders' : 'selectedFiles';
113
+ return _objectSpread(_objectSpread({}, selections), {}, _defineProperty({}, key, [].concat(_toConsumableArray(selections[key]), [currentItem])));
114
+ }, {
115
+ selectedFolders: [],
116
+ selectedFiles: []
117
+ }),
118
+ selectedFolders = _currentSelection$red.selectedFolders,
119
+ selectedFiles = _currentSelection$red.selectedFiles;
120
+ // We need the current files & the file to be added, so that we are subtracting file of the bulk selected ones and reusing it as separate file.
121
+ if (_this.plugin.filerobot.isFileRestrictedToGetAdded(selectedFiles.slice(1), selectedFiles[0], false)) {
122
+ return;
123
+ }
124
+ var _isAllFoldersSelected = _this.checkIfAllSelected(selectedFolders, folders);
125
+ var _isAllFilesSelected = _this.checkIfAllSelected(selectedFiles, files);
126
+ _this.plugin.setPluginCommonState({
127
+ currentSelection: currentSelection,
128
+ isAllFoldersSelected: _isAllFoldersSelected,
129
+ isAllFilesSelected: _isAllFilesSelected
130
+ });
131
+ return currentSelection;
132
+ } else if (isMultiple || e.ctrlKey || e.metaKey) {
133
+ _this.lastCheckbox = item.id;
134
+ var _this$plugin$getPlugi2 = _this.plugin.getPluginCommonState(),
135
+ _currentSelection = _this$plugin$getPlugi2.currentSelection;
136
+ var updatedSelection = _this.isChecked(item) ? _currentSelection.filter(function (i) {
137
+ return item.id !== i.id;
138
+ }) : _currentSelection.concat([item]);
139
+ var updates = {
140
+ currentSelection: updatedSelection
141
+ };
142
+ if (isFolderItem) {
143
+ updates.isAllFoldersSelected = _this.checkIfAllSelected(updatedSelection.filter(function (item) {
144
+ return isFolder(item);
145
+ }), folders);
146
+ } else {
147
+ if (_this.plugin.filerobot.isFileRestrictedToGetAdded(_currentSelection, item, false)) {
148
+ return;
149
+ }
150
+ updates.isAllFilesSelected = _this.checkIfAllSelected(updatedSelection.filter(function (item) {
151
+ return !isFolder(item);
152
+ }), files);
153
+ }
154
+ _this.plugin.setPluginCommonState(updates);
155
+ return updatedSelection;
156
+ }
157
+
158
+ // If only 1 file/folder is available and it's selected means we have selected all the files/folders.
159
+ var isAllFoldersSelected = false;
160
+ var isAllFilesSelected = false;
161
+ if (isFolderItem && folders.length === 1) {
162
+ isAllFoldersSelected = true;
163
+ } else if (!isFolderItem && files.length === 1) {
164
+ isAllFilesSelected = true;
165
+ }
166
+ if (!isFolderItem && _this.plugin.filerobot.isFileRestrictedToGetAdded([], item, false)) {
167
+ return;
168
+ }
169
+ _this.lastCheckbox = item.id;
170
+ _this.plugin.setPluginCommonState({
171
+ currentSelection: [item],
172
+ disableDownloadButton: !!isFolderItem,
173
+ isAllFoldersSelected: isAllFoldersSelected,
174
+ isAllFilesSelected: isAllFilesSelected
175
+ });
176
+ return [item];
177
+ });
178
+ _defineProperty(this, "toggleAllFilesSelection", function () {
179
+ var _this$plugin$getPlugi3 = _this.plugin.getPluginCommonState(),
180
+ _this$plugin$getPlugi4 = _this$plugin$getPlugi3.currentSelection,
181
+ currentSelection = _this$plugin$getPlugi4 === void 0 ? [] : _this$plugin$getPlugi4,
182
+ files = _this$plugin$getPlugi3.files,
183
+ _this$plugin$getPlugi5 = _this$plugin$getPlugi3.isAllFilesSelected,
184
+ isAllFilesSelected = _this$plugin$getPlugi5 === void 0 ? false : _this$plugin$getPlugi5;
185
+ var selectedFolders = currentSelection.filter(function (item) {
186
+ return isFolder(item);
187
+ });
188
+ var newSelections = isAllFilesSelected ? selectedFolders : selectedFolders.concat(files);
189
+ _this.plugin.setPluginCommonState({
190
+ currentSelection: newSelections,
191
+ isAllFilesSelected: !isAllFilesSelected
192
+ });
193
+ });
194
+ _defineProperty(this, "toggleAllFoldersSelection", function () {
195
+ var _this$plugin$getPlugi6 = _this.plugin.getPluginCommonState(),
196
+ _this$plugin$getPlugi7 = _this$plugin$getPlugi6.currentSelection,
197
+ currentSelection = _this$plugin$getPlugi7 === void 0 ? [] : _this$plugin$getPlugi7,
198
+ folders = _this$plugin$getPlugi6.folders,
199
+ _this$plugin$getPlugi8 = _this$plugin$getPlugi6.isAllFoldersSelected,
200
+ isAllFoldersSelected = _this$plugin$getPlugi8 === void 0 ? false : _this$plugin$getPlugi8;
201
+ var selectedFiles = currentSelection.filter(function (item) {
202
+ return !isFolder(item);
203
+ });
204
+ var newSelections = isAllFoldersSelected ? selectedFiles : selectedFiles.concat(folders);
205
+ _this.plugin.setPluginCommonState({
206
+ currentSelection: newSelections,
207
+ isAllFoldersSelected: !isAllFoldersSelected
208
+ });
209
+ });
210
+ _defineProperty(this, "checkIfAllSelected", function (currentSelection, items) {
211
+ return currentSelection.length === items.length;
212
+ });
213
+ this.id = PLUGINS_IDS.PROVIDER_VIEWS;
214
+ this.plugin = plugin;
215
+ this.provider = opts.provider;
216
+
217
+ // set default options
218
+ var defaultOptions = {
219
+ layoutType: LAYOUTS_IDS.LIST,
220
+ showTitles: true,
221
+ showFilter: false,
222
+ showBreadcrumbs: true,
223
+ showSort: false,
224
+ is3rdPartyData: true,
225
+ showLogout: true,
226
+ showUsername: true,
227
+ selectedListViewCols: [LIST_VIEW_COLS_IDS.TYPE, LIST_VIEW_COLS_IDS.VISIBILITY,
228
+ // LIST_VIEW_COLS_IDS.STATUS,
229
+ LIST_VIEW_COLS_IDS.TAGS, LIST_VIEW_COLS_IDS.LABELS, LIST_VIEW_COLS_IDS.LAST_MODIFY],
230
+ selectedListViewFoldersCols: [LIST_VIEW_FOLDERS_COLS_IDS.VISIBILITY, LIST_VIEW_FOLDERS_COLS_IDS.AMOUNT, LIST_VIEW_FOLDERS_COLS_IDS.LAST_MODIFY]
231
+ };
232
+
233
+ // merge default options with the ones set by user
234
+ this.opts = _objectSpread(_objectSpread({}, defaultOptions), opts);
235
+
236
+ // Logic
237
+ this.addFile = this.addFile.bind(this);
238
+ this.filterItems = this.filterItems.bind(this);
239
+ this.filterQuery = this.filterQuery.bind(this);
240
+ this.toggleSearch = this.toggleSearch.bind(this);
241
+ this.getFolder = this.getFolder.bind(this);
242
+ this.getNextFolder = this.getNextFolder.bind(this);
243
+ this.logout = this.logout.bind(this);
244
+ this.preFirstRender = this.preFirstRender.bind(this);
245
+ this.handleAuth = this.handleAuth.bind(this);
246
+ this.sortByTitle = this.sortByTitle.bind(this);
247
+ this.sortByDate = this.sortByDate.bind(this);
248
+ this.isActiveRow = this.isActiveRow.bind(this);
249
+ this.isChecked = this.isChecked.bind(this);
250
+ this.toggleCheckbox = this.toggleCheckbox.bind(this);
251
+ this.handleError = this.handleError.bind(this);
252
+ this.handleScroll = this.handleScroll.bind(this);
253
+ this.listAllFiles = this.listAllFiles.bind(this);
254
+ this.donePicking = this.donePicking.bind(this);
255
+ this.cancelPicking = this.cancelPicking.bind(this);
256
+ this.clearSelection = this.clearSelection.bind(this);
257
+ this.addSelectedItems = this.addSelectedItems.bind(this);
258
+
259
+ // Visual
260
+ this.render = this.render.bind(this);
261
+ this.clearSelection();
262
+
263
+ // Set default state for the plugin
264
+ this.plugin.setPluginCommonState({
265
+ authenticated: false,
266
+ files: [],
267
+ folders: [],
268
+ openedFolders: [],
269
+ selectedFolders: {},
270
+ activeRow: -1,
271
+ filterInput: '',
272
+ isSearchVisible: false
273
+ });
274
+ }
275
+ return _createClass(ProviderView, [{
276
+ key: "_updateFilesAndFolders",
277
+ value: function _updateFilesAndFolders(res, files, folders) {
278
+ this.nextPagePath = res.nextPagePath;
279
+ res.items.forEach(function (item) {
280
+ if (item.isFolder) {
281
+ folders.push(item);
282
+ } else {
283
+ files.push(item);
284
+ }
285
+ });
286
+ this.plugin.setPluginCommonState({
287
+ folders: folders,
288
+ files: files
289
+ });
290
+ }
291
+
292
+ /**
293
+ * Called only the first time the provider view is rendered.
294
+ * Kind of like an init function.
295
+ */
296
+ }, {
297
+ key: "preFirstRender",
298
+ value: function preFirstRender() {
299
+ this.plugin.setPluginCommonState({
300
+ didFirstRender: true
301
+ });
302
+ this.plugin.onFirstRender();
303
+ }
304
+
305
+ /**
306
+ * Based on folder ID, fetch a new folder and update it to state
307
+ * used from the companion-client
308
+ *
309
+ * @param {string} id Folder id
310
+ * @returns {Promise} Folders/files in folder
311
+ */
312
+ }, {
313
+ key: "getFolder",
314
+ value: function getFolder(id, name) {
315
+ var _this2 = this;
316
+ return this._loaderWrapper(this.provider.list(id), function (res) {
317
+ var folders = [];
318
+ var files = [];
319
+ var openedFolders;
320
+ var state = _this2.plugin.getPluginCommonState();
321
+ var index = findIndex(state.openedFolders, function (folder) {
322
+ return id === folder.id;
323
+ });
324
+ if (index !== -1) {
325
+ openedFolders = state.openedFolders.slice(0, index + 1);
326
+ } else {
327
+ openedFolders = state.openedFolders.concat([{
328
+ id: id,
329
+ title: name
330
+ }]);
331
+ }
332
+ _this2.username = _this2.username ? _this2.username : res.username;
333
+ _this2._updateFilesAndFolders(res, files, folders);
334
+ _this2.plugin.setPluginCommonState({
335
+ openedFolders: openedFolders
336
+ });
337
+ }, this.handleError);
338
+ }
339
+
340
+ /**
341
+ * Fetches new folder
342
+ *
343
+ * @param {object} folder
344
+ */
345
+ }, {
346
+ key: "getNextFolder",
347
+ value: function getNextFolder(folder) {
348
+ if (folder) {
349
+ this.getFolder(folder.requestPath || folder.id, folder.name || folder.title);
350
+ this.lastCheckbox = undefined;
351
+ if (this.opts.is3rdPartyData) {
352
+ this.plugin.setPluginCommonState({
353
+ currentSelection: []
354
+ });
355
+ }
356
+ }
357
+ }
358
+ }, {
359
+ key: "addFile",
360
+ value: function addFile(file) {
361
+ var tagFile = {
362
+ id: this.providerFileToId(file),
363
+ source: this.plugin.id,
364
+ data: file,
365
+ name: file.name || file.id,
366
+ type: file.mimeType,
367
+ isRemote: true,
368
+ body: {
369
+ fileId: file.id
370
+ },
371
+ remote: {
372
+ companionUrl: this.plugin.provider.companionUrl,
373
+ url: "".concat(this.provider.fileUrl(file.requestPath)),
374
+ body: {
375
+ fileId: file.id
376
+ },
377
+ providerOptions: this.provider.opts
378
+ }
379
+ };
380
+ if (file.thumbnail || file.preview) {
381
+ tagFile.preview = file.thumbnail || file.preview;
382
+ }
383
+ this.plugin.filerobot.log('Adding remote file');
384
+ try {
385
+ this.plugin.filerobot.addFile(tagFile);
386
+ } catch (err) {
387
+ if (!err.isRestriction) {
388
+ this.plugin.filerobot.log(err);
389
+ }
390
+ }
391
+ }
392
+ }, {
393
+ key: "removeFile",
394
+ value: function removeFile(id) {
395
+ var _this$plugin$getPlugi9 = this.plugin.getPluginCommonState(),
396
+ currentSelection = _this$plugin$getPlugi9.currentSelection;
397
+ this.plugin.setPluginCommonState({
398
+ currentSelection: currentSelection.filter(function (file) {
399
+ return file.id !== id;
400
+ })
401
+ });
402
+ }
403
+
404
+ /**
405
+ * Removes session token on client side.
406
+ */
407
+ }, {
408
+ key: "logout",
409
+ value: function logout() {
410
+ var _this3 = this;
411
+ this.provider.logout().then(function (res) {
412
+ if (res.ok) {
413
+ if (!res.revoked) {
414
+ var message = _this3.plugin.filerobot.i18n('providerViewCompanionUnauthorizeMessage', {
415
+ provider: _this3.plugin.title,
416
+ url: res.manual_revoke_url
417
+ });
418
+ _this3.plugin.filerobot.info(message, 'info', 7000);
419
+ }
420
+ var newState = {
421
+ authenticated: false,
422
+ files: [],
423
+ folders: [],
424
+ openedFolders: []
425
+ };
426
+ _this3.plugin.setPluginCommonState(newState);
427
+ }
428
+ })["catch"](this.handleError);
429
+ }
430
+ }, {
431
+ key: "filterQuery",
432
+ value: function filterQuery(e) {
433
+ var state = this.plugin.getPluginCommonState();
434
+ this.plugin.setPluginCommonState(_extends({}, state, {
435
+ filterInput: e ? e.target.value : ''
436
+ }));
437
+ }
438
+ }, {
439
+ key: "toggleSearch",
440
+ value: function toggleSearch(inputEl) {
441
+ var state = this.plugin.getPluginCommonState();
442
+ this.plugin.setPluginCommonState({
443
+ isSearchVisible: !state.isSearchVisible,
444
+ filterInput: ''
445
+ });
446
+ }
447
+ }, {
448
+ key: "filterItems",
449
+ value: function filterItems(items) {
450
+ var state = this.plugin.getPluginCommonState();
451
+ if (!state.filterInput || state.filterInput === '') {
452
+ return items;
453
+ }
454
+ return items.filter(function (folder) {
455
+ return folder.name.toLowerCase().indexOf(state.filterInput.toLowerCase()) !== -1;
456
+ });
457
+ }
458
+ }, {
459
+ key: "sortByTitle",
460
+ value: function sortByTitle() {
461
+ var state = _extends({}, this.plugin.getPluginCommonState());
462
+ var files = state.files,
463
+ folders = state.folders,
464
+ sorting = state.sorting;
465
+ var sortedFiles = files.sort(function (fileA, fileB) {
466
+ if (sorting === 'titleDescending') {
467
+ return fileB.name.localeCompare(fileA.name);
468
+ }
469
+ return fileA.name.localeCompare(fileB.name);
470
+ });
471
+ var sortedFolders = folders.sort(function (folderA, folderB) {
472
+ if (sorting === 'titleDescending') {
473
+ return folderB.name.localeCompare(folderA.name);
474
+ }
475
+ return folderA.name.localeCompare(folderB.name);
476
+ });
477
+ this.plugin.setPluginCommonState(_extends({}, state, {
478
+ files: sortedFiles,
479
+ folders: sortedFolders,
480
+ sorting: sorting === 'titleDescending' ? 'titleAscending' : 'titleDescending'
481
+ }));
482
+ }
483
+ }, {
484
+ key: "sortByDate",
485
+ value: function sortByDate() {
486
+ var state = _extends({}, this.plugin.getPluginCommonState());
487
+ var files = state.files,
488
+ folders = state.folders,
489
+ sorting = state.sorting;
490
+ var sortedFiles = files.sort(function (fileA, fileB) {
491
+ var a = new Date(fileA.modifiedDate);
492
+ var b = new Date(fileB.modifiedDate);
493
+ if (sorting === 'dateDescending') {
494
+ return a > b ? -1 : a < b ? 1 : 0;
495
+ }
496
+ return a > b ? 1 : a < b ? -1 : 0;
497
+ });
498
+ var sortedFolders = folders.sort(function (folderA, folderB) {
499
+ var a = new Date(folderA.modifiedDate);
500
+ var b = new Date(folderB.modifiedDate);
501
+ if (sorting === 'dateDescending') {
502
+ return a > b ? -1 : a < b ? 1 : 0;
503
+ }
504
+ return a > b ? 1 : a < b ? -1 : 0;
505
+ });
506
+ this.plugin.setPluginCommonState(_extends({}, state, {
507
+ files: sortedFiles,
508
+ folders: sortedFolders,
509
+ sorting: sorting === 'dateDescending' ? 'dateAscending' : 'dateDescending'
510
+ }));
511
+ }
512
+ }, {
513
+ key: "sortBySize",
514
+ value: function sortBySize() {
515
+ var state = _extends({}, this.plugin.getPluginCommonState());
516
+ var files = state.files,
517
+ sorting = state.sorting;
518
+
519
+ // check that plugin supports file sizes
520
+ if (!files.length || !this.plugin.getItemData(files[0]).size) {
521
+ return;
522
+ }
523
+ var sortedFiles = files.sort(function (fileA, fileB) {
524
+ var a = fileA.size;
525
+ var b = fileB.size;
526
+ if (sorting === 'sizeDescending') {
527
+ return a > b ? -1 : a < b ? 1 : 0;
528
+ }
529
+ return a > b ? 1 : a < b ? -1 : 0;
530
+ });
531
+ this.plugin.setPluginCommonState(_extends({}, state, {
532
+ files: sortedFiles,
533
+ sorting: sorting === 'sizeDescending' ? 'sizeAscending' : 'sizeDescending'
534
+ }));
535
+ }
536
+ }, {
537
+ key: "isActiveRow",
538
+ value: function isActiveRow(file) {
539
+ return this.plugin.getPluginCommonState().activeRow === this.plugin.getItemId(file);
540
+ }
541
+ }, {
542
+ key: "isChecked",
543
+ value: function isChecked(file) {
544
+ var _this$plugin$getPlugi10 = this.plugin.getPluginCommonState(),
545
+ currentSelection = _this$plugin$getPlugi10.currentSelection;
546
+ // comparing id instead of the file object, because the reference to the object
547
+ // changes when we switch folders, and the file list is updated
548
+ return currentSelection.some(function (item) {
549
+ return item.id === file.id;
550
+ });
551
+ }
552
+
553
+ /**
554
+ * Adds all files found inside of specified folder.
555
+ *
556
+ * Uses separated state while folder contents are being fetched and
557
+ * mantains list of selected folders, which are separated from files.
558
+ */
559
+ }, {
560
+ key: "addFolder",
561
+ value: function addFolder(folder) {
562
+ var _this4 = this;
563
+ var folderId = this.providerFileToId(folder);
564
+ var folders = _objectSpread({}, this.plugin.getPluginCommonState().selectedFolders);
565
+ if (folderId in folders && folders[folderId].loading) {
566
+ return;
567
+ }
568
+ folders[folderId] = {
569
+ loading: true,
570
+ files: []
571
+ };
572
+ this.plugin.setPluginCommonState({
573
+ selectedFolders: folders
574
+ });
575
+ return this._loaderWrapper(this.listAllFiles(folder.requestPath).then(function (files) {
576
+ files.forEach(function (file) {
577
+ _this4.addFile(file);
578
+ });
579
+ var ids = files.map(_this4.providerFileToId);
580
+ folders = _objectSpread(_objectSpread({}, _this4.plugin.getPluginCommonState().selectedFolders), {}, _defineProperty({}, folderId, {
581
+ loading: false,
582
+ files: ids
583
+ }));
584
+ _this4.plugin.setPluginCommonState({
585
+ selectedFolders: folders
586
+ });
587
+ var message;
588
+ if (files.length) {
589
+ message = _this4.plugin.filerobot.i18n('filerobotFolderAddedMessage', {
590
+ smart_count: files.length,
591
+ folder: folder.name
592
+ });
593
+ } else {
594
+ message = _this4.plugin.filerobot.i18n('filerobotEmptyFolderAddedMessage');
595
+ }
596
+ _this4.plugin.filerobot.info(message);
597
+ }), function () {}, function (e) {
598
+ folders = _objectSpread({}, _this4.plugin.getPluginCommonState().selectedFolders);
599
+ delete folders[folderId];
600
+ _this4.plugin.setPluginCommonState({
601
+ selectedFolders: folders
602
+ });
603
+ _this4.handleError(e);
604
+ });
605
+ }
606
+
607
+ /**
608
+ * gets all the current selection items adds them to the uploading panel
609
+ * if folder it would retrieve its files
610
+ * if files they would be added directly
611
+ *
612
+ */
613
+ }, {
614
+ key: "addSelectedItems",
615
+ value: function addSelectedItems() {
616
+ var _this5 = this;
617
+ var _this$plugin$getPlugi11 = this.plugin.getPluginCommonState(),
618
+ currentSelection = _this$plugin$getPlugi11.currentSelection;
619
+ currentSelection.forEach(function (item) {
620
+ if (item.isFolder) {
621
+ _this5.addFolder(item);
622
+ } else {
623
+ _this5.addFile(item);
624
+ }
625
+ });
626
+ this.plugin.setPluginCommonState({
627
+ isAllFoldersSelected: false,
628
+ isAllFilesSelected: false,
629
+ currentSelection: []
630
+ });
631
+ }
632
+ }, {
633
+ key: "providerFileToId",
634
+ value: function providerFileToId(file) {
635
+ return generateFileId({
636
+ data: file,
637
+ name: file.name || file.id,
638
+ type: file.mimeType
639
+ });
640
+ }
641
+ }, {
642
+ key: "handleAuth",
643
+ value: function handleAuth() {
644
+ var _this6 = this;
645
+ var authState = btoa(JSON.stringify({
646
+ origin: getOrigin()
647
+ }));
648
+ var clientVersion = encodeURIComponent("@scaleflex/widget-provider-views=".concat(ProviderView.VERSION));
649
+ var link = "".concat(this.provider.authUrl(), "?state=").concat(authState, "&filerobotVersions=").concat(clientVersion);
650
+ var authWindow = window.open(link, '_blank');
651
+ var _handleToken = function handleToken(e) {
652
+ if (e.source !== authWindow) {
653
+ _this6.plugin.filerobot.log("rejecting event from ".concat(e.origin));
654
+ return;
655
+ }
656
+
657
+ // Check if it's a string before doing the JSON.parse to maintain support
658
+ // for older Companion versions that used object references
659
+ var data = typeof e.data === 'string' ? JSON.parse(e.data) : e.data;
660
+ if (!data.token) {
661
+ _this6.plugin.filerobot.log('did not receive token from auth window');
662
+ return;
663
+ }
664
+ authWindow.close();
665
+ window.removeEventListener('message', _handleToken);
666
+ _this6.provider.setAuthToken(data.token);
667
+ _this6.preFirstRender();
668
+ };
669
+ window.addEventListener('message', _handleToken);
670
+ }
671
+ }, {
672
+ key: "handleError",
673
+ value: function handleError(error) {
674
+ var filerobot = this.plugin.filerobot;
675
+ filerobot.log(error.toString());
676
+ if (error.isAuthError) {
677
+ return;
678
+ }
679
+ var message = filerobot.i18n('providerViewCompanionError');
680
+ filerobot.info({
681
+ message: message,
682
+ details: error.toString()
683
+ }, 'error', 5000);
684
+ }
685
+ }, {
686
+ key: "handleScroll",
687
+ value: function handleScroll(e) {
688
+ var _this7 = this;
689
+ var scrollPos = e.target.scrollHeight - (e.target.scrollTop + e.target.offsetHeight);
690
+ var path = this.nextPagePath || null;
691
+ if (scrollPos < 50 && path && !this._isHandlingScroll) {
692
+ this.provider.list(path).then(function (res) {
693
+ var _this7$plugin$getPlug = _this7.plugin.getPluginCommonState(),
694
+ files = _this7$plugin$getPlug.files,
695
+ folders = _this7$plugin$getPlug.folders;
696
+ _this7._updateFilesAndFolders(res, files, folders);
697
+ })["catch"](this.handleError).then(function () {
698
+ _this7._isHandlingScroll = false;
699
+ }); // always called
700
+
701
+ this._isHandlingScroll = true;
702
+ }
703
+ }
704
+ }, {
705
+ key: "listAllFiles",
706
+ value: function listAllFiles(path) {
707
+ var _this8 = this;
708
+ var files = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
709
+ files = files || [];
710
+ return new Promise(function (resolve, reject) {
711
+ _this8.provider.list(path).then(function (res) {
712
+ res.items.forEach(function (item) {
713
+ if (!item.isFolder) {
714
+ files.push(item);
715
+ }
716
+ });
717
+ var moreFiles = res.nextPagePath || null;
718
+ if (moreFiles) {
719
+ return _this8.listAllFiles(moreFiles, files).then(function (files) {
720
+ return resolve(files);
721
+ })["catch"](function (e) {
722
+ return reject(e);
723
+ });
724
+ } else {
725
+ return resolve(files);
726
+ }
727
+ })["catch"](function (e) {
728
+ return reject(e);
729
+ });
730
+ });
731
+ }
732
+ }, {
733
+ key: "donePicking",
734
+ value: function donePicking() {
735
+ var _this9 = this;
736
+ var _this$plugin$getPlugi12 = this.plugin.getPluginCommonState(),
737
+ currentSelection = _this$plugin$getPlugi12.currentSelection;
738
+ var promises = currentSelection.map(function (file) {
739
+ if (file.isFolder) {
740
+ return _this9.addFolder(file);
741
+ } else {
742
+ return _this9.addFile(file);
743
+ }
744
+ });
745
+ this._loaderWrapper(Promise.all(promises), function () {
746
+ _this9.clearSelection();
747
+ }, function () {});
748
+ }
749
+ }, {
750
+ key: "cancelPicking",
751
+ value: function cancelPicking() {
752
+ this.clearSelection();
753
+ }
754
+ }, {
755
+ key: "clearSelection",
756
+ value: function clearSelection() {
757
+ this.plugin.setPluginCommonState({
758
+ currentSelection: []
759
+ });
760
+ }
761
+
762
+ // displays loader view while asynchronous request is being made.
763
+ }, {
764
+ key: "_loaderWrapper",
765
+ value: function _loaderWrapper(promise, then, catch_) {
766
+ var _this10 = this;
767
+ promise.then(function (result) {
768
+ _this10.plugin.setPluginCommonState({
769
+ loading: false
770
+ });
771
+ then(result);
772
+ })["catch"](function (err) {
773
+ _this10.plugin.setPluginCommonState({
774
+ loading: false
775
+ });
776
+ catch_(err);
777
+ });
778
+ this.plugin.setPluginCommonState({
779
+ loading: true
780
+ });
781
+ }
782
+ }, {
783
+ key: "render",
784
+ value: function render(state) {
785
+ var _this11 = this;
786
+ var viewOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
787
+ var _this$plugin$getPlugi13 = this.plugin.getPluginCommonState(),
788
+ authenticated = _this$plugin$getPlugi13.authenticated,
789
+ didFirstRender = _this$plugin$getPlugi13.didFirstRender;
790
+ if (!didFirstRender) {
791
+ this.preFirstRender();
792
+ }
793
+
794
+ // reload pluginState for "loading" attribute because it might
795
+ // have changed above.
796
+ if (this.plugin.getPluginCommonState().loading) {
797
+ return /*#__PURE__*/_jsx(CloseWrapper, {
798
+ onUnmount: this.clearSelection,
799
+ children: /*#__PURE__*/_jsx(LoaderView, {
800
+ loaderLabel: this.plugin.filerobot.i18n('loaderLoadingLabel')
801
+ })
802
+ });
803
+ }
804
+ if (!authenticated) {
805
+ return /*#__PURE__*/_jsx(CloseWrapper, {
806
+ onUnmount: this.clearSelection,
807
+ children: /*#__PURE__*/_jsx(AuthView, {
808
+ pluginName: this.plugin.title,
809
+ pluginIcon: this.plugin.icon,
810
+ handleAuth: this.handleAuth,
811
+ i18n: this.plugin.filerobot.i18n,
812
+ i18nArray: this.plugin.filerobot.i18nArray
813
+ })
814
+ });
815
+ }
816
+ var targetViewOptions = _objectSpread(_objectSpread({}, this.opts), viewOptions);
817
+ var browserProps = _extends({}, this.plugin.getPluginCommonState(), _objectSpread(_objectSpread({}, targetViewOptions), {}, {
818
+ username: this.username,
819
+ getNextFolder: this.getNextFolder,
820
+ getFolder: this.getFolder,
821
+ filterItems: this.filterItems,
822
+ filterQuery: this.filterQuery,
823
+ toggleSearch: this.toggleSearch,
824
+ sortByTitle: this.sortByTitle,
825
+ sortByDate: this.sortByDate,
826
+ logout: this.logout,
827
+ isActiveRow: this.isActiveRow,
828
+ isChecked: this.isChecked,
829
+ toggleCheckbox: this.toggleCheckbox,
830
+ handleScroll: this.handleScroll,
831
+ listAllFiles: this.listAllFiles,
832
+ done: this.donePicking,
833
+ cancel: this.cancelPicking,
834
+ title: this.plugin.title,
835
+ pluginIcon: this.plugin.icon,
836
+ i18n: this.plugin.filerobot.i18n,
837
+ updateFilesAndFolders: function updateFilesAndFolders() {
838
+ return _this11._updateFilesAndFolders.apply(_this11, arguments);
839
+ },
840
+ toggleAllFilesSelection: this.toggleAllFilesSelection,
841
+ toggleAllFoldersSelection: this.toggleAllFoldersSelection
842
+ }));
843
+ return /*#__PURE__*/_jsx(CloseWrapper, {
844
+ onUnmount: this.clearSelection,
845
+ children: /*#__PURE__*/_jsx(Browser, _objectSpread({}, browserProps))
846
+ });
847
+ }
848
+ }]);
849
+ }();
850
+ export { ProviderView as default };