/**
 * DevExtreme (ui/file_manager/ui.file_manager.files_tree_view.js)
 * Version: 21.1.0 (build 21016-0316)
 * Build date: Sat Jan 16 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";

function _typeof(obj) {
    if ("function" === typeof Symbol && "symbol" === typeof Symbol.iterator) {
        _typeof = function(obj) {
            return typeof obj
        }
    } else {
        _typeof = function(obj) {
            return obj && "function" === typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
        }
    }
    return _typeof(obj)
}
exports.default = void 0;
var _renderer = _interopRequireDefault(require("../../core/renderer"));
var _events_engine = _interopRequireDefault(require("../../events/core/events_engine"));
var _extend = require("../../core/utils/extend");
var _icon = require("../../core/utils/icon");
var _common = require("../../core/utils/common");
var _ui = _interopRequireDefault(require("../widget/ui.widget"));
var _uiTree_view = _interopRequireDefault(require("../tree_view/ui.tree_view.search"));
var _uiFile_manager = _interopRequireDefault(require("./ui.file_manager.file_actions_button"));
var _deferred = require("../../core/utils/deferred");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function")
    }
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    return Constructor
}

function _get(target, property, receiver) {
    if ("undefined" !== typeof Reflect && Reflect.get) {
        _get = Reflect.get
    } else {
        _get = function(target, property, receiver) {
            var base = _superPropBase(target, property);
            if (!base) {
                return
            }
            var desc = Object.getOwnPropertyDescriptor(base, property);
            if (desc.get) {
                return desc.get.call(receiver)
            }
            return desc.value
        }
    }
    return _get(target, property, receiver || target)
}

function _superPropBase(object, property) {
    while (!Object.prototype.hasOwnProperty.call(object, property)) {
        object = _getPrototypeOf(object);
        if (null === object) {
            break
        }
    }
    return object
}

function _inherits(subClass, superClass) {
    if ("function" !== typeof superClass && null !== superClass) {
        throw new TypeError("Super expression must either be null or a function")
    }
    subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
            value: subClass,
            writable: true,
            configurable: true
        }
    });
    if (superClass) {
        _setPrototypeOf(subClass, superClass)
    }
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf || function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}

function _createSuper(Derived) {
    var hasNativeReflectConstruct = _isNativeReflectConstruct();
    return function() {
        var result, Super = _getPrototypeOf(Derived);
        if (hasNativeReflectConstruct) {
            var NewTarget = _getPrototypeOf(this).constructor;
            result = Reflect.construct(Super, arguments, NewTarget)
        } else {
            result = Super.apply(this, arguments)
        }
        return _possibleConstructorReturn(this, result)
    }
}

function _possibleConstructorReturn(self, call) {
    if (call && ("object" === _typeof(call) || "function" === typeof call)) {
        return call
    }
    return _assertThisInitialized(self)
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _isNativeReflectConstruct() {
    if ("undefined" === typeof Reflect || !Reflect.construct) {
        return false
    }
    if (Reflect.construct.sham) {
        return false
    }
    if ("function" === typeof Proxy) {
        return true
    }
    try {
        Date.prototype.toString.call(Reflect.construct(Date, [], (function() {})));
        return true
    } catch (e) {
        return false
    }
}

function _getPrototypeOf(o) {
    _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(o) {
        return o.__proto__ || Object.getPrototypeOf(o)
    };
    return _getPrototypeOf(o)
}
var FILE_MANAGER_DIRS_TREE_CLASS = "dx-filemanager-dirs-tree";
var FILE_MANAGER_DIRS_TREE_FOCUSED_ITEM_CLASS = "dx-filemanager-focused-item";
var FILE_MANAGER_DIRS_TREE_ITEM_TEXT_CLASS = "dx-filemanager-dirs-tree-item-text";
var TREE_VIEW_ITEM_CLASS = "dx-treeview-item";
var FileManagerFilesTreeView = function(_Widget) {
    _inherits(FileManagerFilesTreeView, _Widget);
    var _super = _createSuper(FileManagerFilesTreeView);

    function FileManagerFilesTreeView() {
        _classCallCheck(this, FileManagerFilesTreeView);
        return _super.apply(this, arguments)
    }
    _createClass(FileManagerFilesTreeView, [{
        key: "_initMarkup",
        value: function() {
            var _this = this;
            this._initActions();
            this._getCurrentDirectory = this.option("getCurrentDirectory");
            this._createFileActionsButton = _common.noop;
            this._storeExpandedState = this.option("storeExpandedState") || false;
            var $treeView = (0, _renderer.default)("<div>").addClass(FILE_MANAGER_DIRS_TREE_CLASS).appendTo(this.$element());
            var treeViewOptions = {
                dataStructure: "plain",
                rootValue: "",
                createChildren: this._onFilesTreeViewCreateSubDirectories.bind(this),
                itemTemplate: this._createFilesTreeViewItemTemplate.bind(this),
                keyExpr: "getInternalKey",
                parentIdExpr: "parentDirectory.getInternalKey",
                displayExpr: function(itemInfo) {
                    return itemInfo.getDisplayName()
                },
                hasItemsExpr: "fileItem.hasSubDirectories",
                onItemClick: function(e) {
                    return _this._actions.onDirectoryClick(e)
                },
                onItemExpanded: function(e) {
                    return _this._onFilesTreeViewItemExpanded(e)
                },
                onItemCollapsed: function(e) {
                    return _this._onFilesTreeViewItemCollapsed(e)
                },
                onItemRendered: function(e) {
                    return _this._onFilesTreeViewItemRendered(e)
                },
                onContentReady: function() {
                    return _this._actions.onFilesTreeViewContentReady()
                }
            };
            if (this._contextMenu) {
                this._contextMenu.option("onContextMenuHidden", (function() {
                    return _this._onContextMenuHidden()
                }));
                treeViewOptions.onItemContextMenu = function(e) {
                    return _this._onFilesTreeViewItemContextMenu(e)
                };
                this._createFileActionsButton = function(element, options) {
                    return _this._createComponent(element, _uiFile_manager.default, options)
                }
            }
            this._filesTreeView = this._createComponent($treeView, _uiTree_view.default, treeViewOptions);
            _events_engine.default.on($treeView, "click", (function() {
                return _this._actions.onClick()
            }))
        }
    }, {
        key: "_initActions",
        value: function() {
            this._actions = {
                onClick: this._createActionByOption("onClick"),
                onDirectoryClick: this._createActionByOption("onDirectoryClick"),
                onFilesTreeViewContentReady: this._createActionByOption("onFilesTreeViewContentReady")
            }
        }
    }, {
        key: "_render",
        value: function() {
            _get(_getPrototypeOf(FileManagerFilesTreeView.prototype), "_render", this).call(this);
            var that = this;
            setTimeout((function() {
                that._updateFocusedElement()
            }))
        }
    }, {
        key: "_onFilesTreeViewCreateSubDirectories",
        value: function(rootItem) {
            var getDirectories = this.option("getDirectories");
            var directoryInfo = rootItem && rootItem.itemData || null;
            return getDirectories && getDirectories(directoryInfo, true)
        }
    }, {
        key: "_onFilesTreeViewItemRendered",
        value: function(_ref) {
            var itemData = _ref.itemData;
            var currentDirectory = this._getCurrentDirectory();
            if (currentDirectory && currentDirectory.fileItem.equals(itemData.fileItem)) {
                this._updateFocusedElement()
            }
        }
    }, {
        key: "_onFilesTreeViewItemExpanded",
        value: function(_ref2) {
            var itemData = _ref2.itemData;
            if (this._storeExpandedState) {
                itemData.expanded = true
            }
        }
    }, {
        key: "_onFilesTreeViewItemCollapsed",
        value: function(_ref3) {
            var itemData = _ref3.itemData;
            if (this._storeExpandedState) {
                itemData.expanded = false
            }
        }
    }, {
        key: "_createFilesTreeViewItemTemplate",
        value: function(itemData, itemIndex, itemElement) {
            var _this2 = this;
            var $itemElement = (0, _renderer.default)(itemElement);
            var $itemWrapper = $itemElement.closest(this._filesTreeViewItemSelector);
            $itemWrapper.data("item", itemData);
            var $image = (0, _icon.getImageContainer)(itemData.icon);
            var $text = (0, _renderer.default)("<span>").text(itemData.getDisplayName()).addClass(FILE_MANAGER_DIRS_TREE_ITEM_TEXT_CLASS);
            var $button = (0, _renderer.default)("<div>");
            $itemElement.append($image, $text, $button);
            this._createFileActionsButton($button, {
                onClick: function(e) {
                    return _this2._onFileItemActionButtonClick(e)
                }
            })
        }
    }, {
        key: "_onFilesTreeViewItemContextMenu",
        value: function(_ref4) {
            var itemElement = _ref4.itemElement,
                event = _ref4.event;
            event.preventDefault();
            var itemData = (0, _renderer.default)(itemElement).data("item");
            this._contextMenu.showAt([itemData], itemElement, event)
        }
    }, {
        key: "_onFileItemActionButtonClick",
        value: function(_ref5) {
            var component = _ref5.component,
                element = _ref5.element,
                event = _ref5.event;
            event.stopPropagation();
            var $item = component.$element().closest(this._filesTreeViewItemSelector);
            var item = $item.data("item");
            this._contextMenu.showAt([item], element);
            this._activeFileActionsButton = component;
            this._activeFileActionsButton.setActive(true)
        }
    }, {
        key: "_onContextMenuHidden",
        value: function() {
            if (this._activeFileActionsButton) {
                this._activeFileActionsButton.setActive(false)
            }
        }
    }, {
        key: "toggleNodeDisabledState",
        value: function(key, state) {
            var node = this._getNodeByKey(key);
            if (!node) {
                return
            }
            var items = this._filesTreeView.option("items");
            var itemIndex = items.map((function(item) {
                return item.getInternalKey()
            })).indexOf(node.getInternalKey());
            if (-1 !== itemIndex) {
                this._filesTreeView.option("items[".concat(itemIndex, "].disabled"), state)
            }
        }
    }, {
        key: "_updateFocusedElement",
        value: function() {
            var directoryInfo = this._getCurrentDirectory();
            var $element = this._getItemElementByKey(null === directoryInfo || void 0 === directoryInfo ? void 0 : directoryInfo.getInternalKey());
            if (this._$focusedElement) {
                this._$focusedElement.toggleClass(FILE_MANAGER_DIRS_TREE_FOCUSED_ITEM_CLASS, false)
            }
            this._$focusedElement = $element || (0, _renderer.default)();
            this._$focusedElement.toggleClass(FILE_MANAGER_DIRS_TREE_FOCUSED_ITEM_CLASS, true)
        }
    }, {
        key: "_getNodeByKey",
        value: function(key) {
            var _this$_filesTreeView;
            return null === (_this$_filesTreeView = this._filesTreeView) || void 0 === _this$_filesTreeView ? void 0 : _this$_filesTreeView._getNode(key)
        }
    }, {
        key: "_getItemElementByKey",
        value: function(key) {
            var node = this._getNodeByKey(key);
            if (node) {
                var $node = this._filesTreeView._getNodeElement(node);
                if ($node) {
                    return $node.children(this._filesTreeViewItemSelector)
                }
            }
            return null
        }
    }, {
        key: "_getDefaultOptions",
        value: function() {
            return (0, _extend.extend)(_get(_getPrototypeOf(FileManagerFilesTreeView.prototype), "_getDefaultOptions", this).call(this), {
                storeExpandedState: false,
                initialFolder: null,
                contextMenu: null,
                getItems: null,
                getCurrentDirectory: null,
                onDirectoryClick: null
            })
        }
    }, {
        key: "_optionChanged",
        value: function(args) {
            var name = args.name;
            switch (name) {
                case "storeExpandedState":
                    this._storeExpandedState = this.option(name);
                    break;
                case "getItems":
                case "rootFolderDisplayName":
                case "initialFolder":
                case "contextMenu":
                    this.repaint();
                    break;
                case "getCurrentDirectory":
                    this.getCurrentDirectory = this.option(name);
                    break;
                case "onClick":
                case "onDirectoryClick":
                case "onFilesTreeViewContentReady":
                    this._actions[name] = this._createActionByOption(name);
                    break;
                default:
                    _get(_getPrototypeOf(FileManagerFilesTreeView.prototype), "_optionChanged", this).call(this, args)
            }
        }
    }, {
        key: "toggleDirectoryExpandedState",
        value: function(directoryInfo, state) {
            var deferred = new _deferred.Deferred;
            var treeViewNode = this._getNodeByKey(null === directoryInfo || void 0 === directoryInfo ? void 0 : directoryInfo.getInternalKey());
            if (!treeViewNode) {
                return deferred.reject().promise()
            }
            if (treeViewNode.expanded === state || treeViewNode.itemsLoaded && !treeViewNode.fileItem.hasSubDirectories) {
                return deferred.resolve().promise()
            }
            var action = state ? "expandItem" : "collapseItem";
            return this._filesTreeView[action](directoryInfo.getInternalKey())
        }
    }, {
        key: "refresh",
        value: function() {
            this._$focusedElement = null;
            this._filesTreeView.option("dataSource", [])
        }
    }, {
        key: "updateCurrentDirectory",
        value: function() {
            if (this._disposed) {
                return
            }
            this._updateFocusedElement();
            this._storeExpandedState && this._updateExpandedStateToCurrentDirectory()
        }
    }, {
        key: "_updateExpandedStateToCurrentDirectory",
        value: function() {
            return this.toggleDirectoryExpandedStateRecursive(this._getCurrentDirectory(), true)
        }
    }, {
        key: "toggleDirectoryExpandedStateRecursive",
        value: function(directoryInfo, state) {
            var dirLine = [];
            for (var dirInfo = directoryInfo; dirInfo; dirInfo = dirInfo.parentDirectory) {
                dirLine.unshift(dirInfo)
            }
            return this.toggleDirectoryLineExpandedState(dirLine, state)
        }
    }, {
        key: "toggleDirectoryLineExpandedState",
        value: function(dirLine, state) {
            var _this3 = this;
            if (!dirLine.length) {
                return (new _deferred.Deferred).resolve().promise()
            }
            return this.toggleDirectoryExpandedState(dirLine.shift(), state).then((function() {
                return _this3.toggleDirectoryLineExpandedState(dirLine, state)
            }))
        }
    }, {
        key: "_filesTreeViewItemSelector",
        get: function() {
            return ".".concat(TREE_VIEW_ITEM_CLASS)
        }
    }, {
        key: "_contextMenu",
        get: function() {
            return this.option("contextMenu")
        }
    }]);
    return FileManagerFilesTreeView
}(_ui.default);
var _default = FileManagerFilesTreeView;
exports.default = _default;
module.exports = exports.default;
