array.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * DevExtreme (ui/file_manager/file_provider/array.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _common = require("../../../core/utils/common");
  11. var _data = require("../../../core/utils/data");
  12. var _iterator = require("../../../core/utils/iterator");
  13. var _type = require("../../../core/utils/type");
  14. var _type2 = _interopRequireDefault(_type);
  15. var _errors = require("../../../data/errors");
  16. var _file_provider = require("./file_provider");
  17. var _uiFile_manager = require("../ui.file_manager.common");
  18. var _uiFile_manager2 = require("../ui.file_manager.utils");
  19. function _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {
  21. "default": obj
  22. }
  23. }
  24. function _typeof(obj) {
  25. "@babel/helpers - typeof";
  26. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  27. return typeof obj
  28. } : function(obj) {
  29. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  30. }, _typeof(obj)
  31. }
  32. function _classCallCheck(instance, Constructor) {
  33. if (!(instance instanceof Constructor)) {
  34. throw new TypeError("Cannot call a class as a function")
  35. }
  36. }
  37. function _defineProperties(target, props) {
  38. for (var i = 0; i < props.length; i++) {
  39. var descriptor = props[i];
  40. descriptor.enumerable = descriptor.enumerable || false;
  41. descriptor.configurable = true;
  42. if ("value" in descriptor) {
  43. descriptor.writable = true
  44. }
  45. Object.defineProperty(target, descriptor.key, descriptor)
  46. }
  47. }
  48. function _createClass(Constructor, protoProps, staticProps) {
  49. if (protoProps) {
  50. _defineProperties(Constructor.prototype, protoProps)
  51. }
  52. if (staticProps) {
  53. _defineProperties(Constructor, staticProps)
  54. }
  55. Object.defineProperty(Constructor, "prototype", {
  56. writable: false
  57. });
  58. return Constructor
  59. }
  60. function _inherits(subClass, superClass) {
  61. if ("function" !== typeof superClass && null !== superClass) {
  62. throw new TypeError("Super expression must either be null or a function")
  63. }
  64. subClass.prototype = Object.create(superClass && superClass.prototype, {
  65. constructor: {
  66. value: subClass,
  67. writable: true,
  68. configurable: true
  69. }
  70. });
  71. Object.defineProperty(subClass, "prototype", {
  72. writable: false
  73. });
  74. if (superClass) {
  75. _setPrototypeOf(subClass, superClass)
  76. }
  77. }
  78. function _setPrototypeOf(o, p) {
  79. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  80. o.__proto__ = p;
  81. return o
  82. };
  83. return _setPrototypeOf(o, p)
  84. }
  85. function _createSuper(Derived) {
  86. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  87. return function() {
  88. var result, Super = _getPrototypeOf(Derived);
  89. if (hasNativeReflectConstruct) {
  90. var NewTarget = _getPrototypeOf(this).constructor;
  91. result = Reflect.construct(Super, arguments, NewTarget)
  92. } else {
  93. result = Super.apply(this, arguments)
  94. }
  95. return _possibleConstructorReturn(this, result)
  96. }
  97. }
  98. function _possibleConstructorReturn(self, call) {
  99. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  100. return call
  101. } else {
  102. if (void 0 !== call) {
  103. throw new TypeError("Derived constructors may only return object or undefined")
  104. }
  105. }
  106. return _assertThisInitialized(self)
  107. }
  108. function _assertThisInitialized(self) {
  109. if (void 0 === self) {
  110. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  111. }
  112. return self
  113. }
  114. function _isNativeReflectConstruct() {
  115. if ("undefined" === typeof Reflect || !Reflect.construct) {
  116. return false
  117. }
  118. if (Reflect.construct.sham) {
  119. return false
  120. }
  121. if ("function" === typeof Proxy) {
  122. return true
  123. }
  124. try {
  125. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  126. return true
  127. } catch (e) {
  128. return false
  129. }
  130. }
  131. function _getPrototypeOf(o) {
  132. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  133. return o.__proto__ || Object.getPrototypeOf(o)
  134. };
  135. return _getPrototypeOf(o)
  136. }
  137. var ArrayFileProvider = function(_FileProvider) {
  138. _inherits(ArrayFileProvider, _FileProvider);
  139. var _super = _createSuper(ArrayFileProvider);
  140. function ArrayFileProvider(options) {
  141. var _this;
  142. _classCallCheck(this, ArrayFileProvider);
  143. options = (0, _common.ensureDefined)(options, {});
  144. _this = _super.call(this, options);
  145. var initialArray = options.data;
  146. if (initialArray && !Array.isArray(initialArray)) {
  147. throw _errors.errors.Error("E4006")
  148. }
  149. var itemsExpr = options.itemsExpr || "items";
  150. _this._subFileItemsGetter = (0, _data.compileGetter)(itemsExpr);
  151. _this._subFileItemsSetter = _type2.default.isFunction(itemsExpr) ? itemsExpr : (0, _data.compileSetter)(itemsExpr);
  152. var nameExpr = _this._getNameExpr(options);
  153. _this._nameSetter = _type2.default.isFunction(nameExpr) ? nameExpr : (0, _data.compileSetter)(nameExpr);
  154. var isDirExpr = _this._getIsDirExpr(options);
  155. _this._getIsDirSetter = _type2.default.isFunction(isDirExpr) ? isDirExpr : (0, _data.compileSetter)(isDirExpr);
  156. _this._data = initialArray || [];
  157. return _this
  158. }
  159. _createClass(ArrayFileProvider, [{
  160. key: "getItems",
  161. value: function(path, itemType) {
  162. return this._getItems(path, itemType)
  163. }
  164. }, {
  165. key: "renameItem",
  166. value: function(item, name) {
  167. item.dataItem.name = name
  168. }
  169. }, {
  170. key: "createFolder",
  171. value: function(parentFolder, name) {
  172. var newItem = {};
  173. this._nameSetter(newItem, name);
  174. this._getIsDirSetter(newItem, true);
  175. var array = this._getChildrenArray(parentFolder.dataItem);
  176. array.push(newItem)
  177. }
  178. }, {
  179. key: "deleteItems",
  180. value: function(items) {
  181. var _this2 = this;
  182. (0, _iterator.each)(items, function(_, item) {
  183. return _this2._deleteItem(item)
  184. })
  185. }
  186. }, {
  187. key: "moveItems",
  188. value: function(items, destinationDir) {
  189. var _this3 = this;
  190. var array = this._getChildrenArray(destinationDir.dataItem);
  191. (0, _iterator.each)(items, function(_, item) {
  192. _this3._checkAbilityToMoveOrCopyItem(item, destinationDir);
  193. _this3._deleteItem(item);
  194. array.push(item.dataItem)
  195. })
  196. }
  197. }, {
  198. key: "copyItems",
  199. value: function(items, destinationDir) {
  200. var _this4 = this;
  201. var array = this._getChildrenArray(destinationDir.dataItem);
  202. (0, _iterator.each)(items, function(_, item) {
  203. _this4._checkAbilityToMoveOrCopyItem(item, destinationDir);
  204. var copiedItem = _this4._createCopy(item.dataItem);
  205. array.push(copiedItem)
  206. })
  207. }
  208. }, {
  209. key: "_checkAbilityToMoveOrCopyItem",
  210. value: function(item, destinationDir) {
  211. var newItemPath = (0, _uiFile_manager2.pathCombine)(destinationDir.relativeName, item.name);
  212. if (0 === newItemPath.indexOf(item.relativeName)) {
  213. throw {
  214. errorId: _uiFile_manager.ErrorCode.Other,
  215. fileItem: item
  216. }
  217. }
  218. }
  219. }, {
  220. key: "_createCopy",
  221. value: function(dataObj) {
  222. var _this5 = this;
  223. var copyObj = {};
  224. this._nameSetter(copyObj, this._nameGetter(dataObj));
  225. this._getIsDirSetter(copyObj, this._isDirGetter(dataObj));
  226. var items = this._subFileItemsGetter(dataObj);
  227. if (Array.isArray(items)) {
  228. var itemsCopy = [];
  229. (0, _iterator.each)(items, function(_, childItem) {
  230. var childCopy = _this5._createCopy(childItem);
  231. itemsCopy.push(childCopy)
  232. });
  233. this._subFileItemsSetter(copyObj, itemsCopy)
  234. }
  235. return copyObj
  236. }
  237. }, {
  238. key: "_deleteItem",
  239. value: function(_ref) {
  240. var parentPath = _ref.parentPath,
  241. dataItem = _ref.dataItem;
  242. var array = this._data;
  243. if ("" !== parentPath) {
  244. var folder = this._findItem(parentPath);
  245. array = this._subFileItemsGetter(folder)
  246. }
  247. var index = array.indexOf(dataItem);
  248. array.splice(index, 1)
  249. }
  250. }, {
  251. key: "_getChildrenArray",
  252. value: function(dataItem) {
  253. if (!dataItem) {
  254. return this._data
  255. }
  256. var subItems = this._subFileItemsGetter(dataItem);
  257. if (!Array.isArray(subItems)) {
  258. subItems = [];
  259. this._subFileItemsSetter(dataItem, subItems)
  260. }
  261. return subItems
  262. }
  263. }, {
  264. key: "_getItems",
  265. value: function(path, itemType) {
  266. if ("" === path || void 0 === path) {
  267. return this._convertDataObjectsToFileItems(this._data, "", itemType)
  268. }
  269. var folderEntry = this._findItem(path);
  270. var entries = folderEntry && this._subFileItemsGetter(folderEntry) || [];
  271. return this._convertDataObjectsToFileItems(entries, path, itemType)
  272. }
  273. }, {
  274. key: "_findItem",
  275. value: function(path) {
  276. var _this6 = this;
  277. if ("" === path) {
  278. return null
  279. }
  280. var result = null;
  281. var data = this._data;
  282. var parts = path.split("/");
  283. var _loop = function(i) {
  284. var part = parts[i];
  285. result = data.filter(function(entry) {
  286. return _this6._isDirGetter(entry) && _this6._nameGetter(entry) === part
  287. })[0];
  288. if (result) {
  289. var children = _this6._subFileItemsGetter(result);
  290. if (children) {
  291. data = children
  292. } else {
  293. if (i !== parts.length - 1) {
  294. return {
  295. v: null
  296. }
  297. }
  298. }
  299. } else {
  300. return {
  301. v: null
  302. }
  303. }
  304. };
  305. for (var i = 0; i < parts.length; i++) {
  306. var _ret = _loop(i);
  307. if ("object" === _typeof(_ret)) {
  308. return _ret.v
  309. }
  310. }
  311. return result
  312. }
  313. }, {
  314. key: "_hasSubDirs",
  315. value: function(dataObj) {
  316. var subItems = (0, _common.ensureDefined)(this._subFileItemsGetter(dataObj), []);
  317. if (!Array.isArray(subItems)) {
  318. return true
  319. }
  320. for (var i = 0; i < subItems.length; i++) {
  321. if (true === this._isDirGetter(subItems[i])) {
  322. return true
  323. }
  324. }
  325. return false
  326. }
  327. }]);
  328. return ArrayFileProvider
  329. }(_file_provider.FileProvider);
  330. module.exports = ArrayFileProvider;