<?php
// Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<div id="fm" class="objects-management-box objects-management-box-collapsible fm clearfix">
    <div id="objects-list" class="objects-list">
        <div class="objects-content">
            <div id="objects-list-content-area" class="objects-content-area">
                <div id="fm-tree" class="tree"></div>
            </div>
        </div>
    </div>
    <div id="fm-frame-switcher" class="frame-switcher"><div class="frame-switcher-wrap"><span></span></div></div>
    <div id="objects-details" class="objects-details">
        <div id="objects-warnings" class="warning-block" style="display: none; "></div>
        <div id="fm-content"></div>
    </div>
</div>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {

    Smb.FileManager.initSidebarVisibility();

    var copyMoveTreeConfig = {
        applyTo: 'fm-modal-dialog-box-files-tree',
        data: <?php echo Zend_Json::encode($this->directoriesTree); ?>,
        dataUrl: '/file-manager/tree-data',
        rootNodeTitle: <?php echo $this->filesList->jsLmsg('rootDirectory'); ?>,
        waitMessage: <?php echo $this->filesList->jsLmsg('waitMessage'); ?>
    };

    var filesList = new Smb.FileManager.Explorer({
        applyTo: 'fm-content',
        data: <?php echo Zend_Json::encode($this->filesList->fetchData()); ?>,
        dataUrl: '/file-manager/list-data',
        pageable: false,
        tree: {
            id: 'fm-tree',
            data: <?php echo Zend_Json::encode($this->directoriesTree); ?>,
            dataUrl: '/file-manager/tree-data',
            rootNodeTitle: <?php echo $this->filesList->jsLmsg('rootDirectory'); ?>,
            onNodeClick: function (directoryPath) {
                Jsw.getComponent('fm-content').changeDirectory(directoryPath, true);
            }
        },
        isDisabledItem: function(item) {
            return ('..' == item.name);
        },
        onContentTableClick: function(event) {
            var content = Jsw.getComponent('fm-content');
            var dir = event.target.hasAttribute('data-dir') ? event.target : event.target.up('[data-dir]');
            var file = event.target.hasAttribute('data-file') ? event.target : event.target.up('[data-file]');
            if (dir) {
                content.changeDirectory(dir.getAttribute('data-dir'));
            } else
            if (file) {
                content.callDefaultItemAction(file.getAttribute('data-file'));
            }
        },
        columns: [
            Object.extend(Object.clone(Jsw.list.COLUMN_SELECTION), { cls: 'select first', headerCls: 'select first', hideDisabled: true }),
            {
                header: <?php echo $this->filesList->jsLmsg('name');?>,
                sortable: true,
                dataIndex: 'name',
                cls: 'js-file-name fm-file-name',
                renderer: function(item) {
                    var icon = '<i class="icon"><img src="' + getIcon(item) + '"></i> ';

                    var linkStartTag = item.isDirectory
                        ? '<a class="i-link fm-file-name-link" data-dir="' + Jsw.escapeAttribute(item.name) + '" href="#">'
                        : '<a class="i-link fm-file-name-link" data-file="' + Jsw.escapeAttribute(item.id) + '" href="#">';

                    var itemTitle = '' +
                        linkStartTag +
                            icon +
                            '<span>' + item.name.escapeHTML() + '</span>' +
                        '</a>';

                    return itemTitle;
                }
            }, {
                header: <?php echo $this->filesList->jsLmsg('changeDate');?>,
                sortable: true,
                dataIndex: 'modificationDate',
                cls: 'minor number'
            }, {
                header: <?php echo $this->filesList->jsLmsg('size');?>,
                sortable: true,
                dataIndex: 'formatedSize',
                cls: 't-r number js-file-size <?php echo $this->additionalColumns ? '' : 'last'; ?>',
                headerCls: '<?php echo $this->additionalColumns ? '' : 'last'; ?>'
            },
            <?php if ($this->additionalColumns): ?>
            {
                header: <?php echo $this->filesList->jsLmsg('permissions');?>,
                sortable: false,
                dataIndex: 'filePerms',
                cls: 'nowrap',
                renderer: function(item) {
                    if (item.actions) {
                        var actionInfo = Jsw.getComponent('fm-content').getActionByName(item, 'permissions');
                        if (actionInfo) {
                            return '<a href="' + Jsw.prepareUrl(actionInfo.href) + '">' + item.filePerms + '</a>';
                        }
                    }
                    return item.filePerms;
                },
                isVisible: function() {
                    return !Jsw.Cookie.get('fm-hidden-permissions-column');
                }
            }, {
                header: <?php echo $this->filesList->jsLmsg('user');?>,
                sortable: true,
                dataIndex: 'user',
                isVisible: function() {
                    return !Jsw.Cookie.get('fm-hidden-user-column');
                }
            }, {
                header: <?php echo $this->filesList->jsLmsg('group');?>,
                sortable: true,
                dataIndex: 'group',
                cls: 'last',
                headerCls: 'last',
                isVisible: function() {
                    return !Jsw.Cookie.get('fm-hidden-group-column');
                }
            },
            <?php endif; ?>
            Jsw.list.COLUMN_ACTIONS
        ],
        itemActions: {
            rename: function(item) {
                Jsw.getComponent('fm-content').showPopup(Smb.FileManager.Rename, {
                    id: 'fm-rename',
                    renderTo: 'fm-modal-dialog-box-rename',
                    cls: 'popup-panel',
                    handlerUrl: Jsw.prepareUrl('/file-manager/rename'),
                    locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.rename')) ?>,
                    itemId: item.id,
                    fileName: item.name
                });
            },

            extractArchive: function(item) {
                Jsw.getComponent('fm-content').showPopup(Smb.FileManager.ExtractArchive, {
                    id: 'fm-extract-archive',
                    renderTo: 'fm-modal-dialog-box-extract-archive',
                    cls: 'popup-panel',
                    handlerUrl: Jsw.prepareUrl('/file-manager/extract-archive'),
                    locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.extract-archive')) ?>,
                    titleCls: 'pp-archive-extract',
                    fileName: item.id
                });
            },

            logBrowser: function (item) {
                var url = <?=$this->jsEscape($this->baseUrl("/smb/log-file/browser/id/{$this->domainId}/"))?>;
                var currentDir = Jsw.getComponent('fm-content').getCurrentDir();
                var logPath = encodeURIComponent(currentDir.replace(/\/?$/, '/')) + item.id;
                url += "?file=" + logPath;
                url += "&custom=true";
                var returnUrl = <?=$this->jsEscape(urlencode($this->url(['action' => 'list'])))?>;
                url += "&returnUrl=" + returnUrl;
                Jsw.redirect(url);
            },

            view: redirectAction.bind(null, 'show'),
            textEdit: redirectAction.bind(null, 'edit'),
            visualEdit: redirectAction.bind(null, 'wysiwyg'),
            textCodeEditor: redirectAction.bind(null, 'code-editor'),
            download: redirectAction.bind(null, 'download'),
            permissions: redirectAction.bind(null, 'permissions')
        },
        operations: [{
                id: 'upload-file-button',
                componentType: 'Smb.FileManager.UploadButton',
                title: <?php echo $this->filesList->jsLmsg('buttonUploadFiles');?>,
                description: <?php echo $this->filesList->jsLmsg('hintUploadFiles'); ?>,
                addCls: 'sb-upload-file',
                href: '#',
                uploadPopupParams: {
                    id: 'fm-upload',
                    renderTo: 'fm-modal-dialog-box-upload',
                    cls: 'popup-panel',
                    handlerUrl: Jsw.prepareUrl('/file-manager/upload'),
                    locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.upload')) ?>,
                    uploadMaxFileSize: <?php echo $this->uploadMaxFileSize; ?>,
                    titleCls: 'pp-upload'
                }
            }, {
                componentType: 'Jsw.list.AdditionalActions',
                title: <?php echo $this->filesList->jsLmsg('buttonNewActions'); ?>,
                titleAddCls: 'sb-add-new',
                href: '#',
                operations: [{
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->filesList->jsLmsg('buttonNewFile');?>,
                        description: <?php echo $this->filesList->jsLmsg('hintNewFile'); ?>,
                        addCls: 'sb-add-file',
                        cls: 'sb-btn',
                        href: '#',
                        handler: function() {
                            Jsw.getComponent('fm-content').showPopup(Smb.FileManager.CreateFile, {
                                id: 'fm-create-file',
                                renderTo: 'fm-modal-dialog-box-create-file',
                                cls: 'popup-panel',
                                handlerUrl: Jsw.prepareUrl('/file-manager/create-file'),
                                locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.create-file')) ?>,
                                titleCls: 'pp-add-file'
                            });
                        }
                    }, {
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->filesList->jsLmsg('buttonNewDirectory');?>,
                        description: <?php echo $this->filesList->jsLmsg('hintNewDirectory'); ?>,
                        addCls: 'sb-add-folder',
                        cls: 'sb-btn',
                        href: '#',
                        handler: function() {
                            Jsw.getComponent('fm-content').showPopup(Smb.FileManager.CreateFolder, {
                                id: 'fm-create-directory',
                                renderTo: 'fm-modal-dialog-box-create-directory',
                                cls: 'popup-panel',
                                handlerUrl: Jsw.prepareUrl('/file-manager/create-directory'),
                                locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.create-directory')) ?>,
                                titleCls: 'pp-add-folder'
                            });
                        }
                    }
                ]
            }, {
                componentType: 'Jsw.bar.Separator'
            }, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->filesList->jsLmsg('buttonCopyFiles');?>,
                description: <?php echo $this->filesList->jsLmsg('hintCopyFiles'); ?>,
                addCls: 'sb-copy',
                href: '#',
                handler: function() {
                    Jsw.getComponent('fm-content').hasSelectedItems(function() {
                        this.showPopup(Smb.FileManager.CopyFiles, {
                            id: 'fm-copy-files',
                            renderTo: 'fm-modal-dialog-box-copy-files',
                            cls: 'popup-panel',
                            handlerUrl: Jsw.prepareUrl('/file-manager/copy-files'),
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.copy-files')) ?>,
                            titleCls: 'pp-copy',
                            tree: copyMoveTreeConfig
                        });
                    });
                }
            }, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->filesList->jsLmsg('buttonMoveFiles');?>,
                description: <?php echo $this->filesList->jsLmsg('hintMoveFiles'); ?>,
                addCls: 'sb-move',
                href: '#',
                handler: function() {
                    Jsw.getComponent('fm-content').hasSelectedItems(function() {
                        this.showPopup(Smb.FileManager.MoveFiles, {
                            id: 'fm-move-files',
                            renderTo: 'fm-modal-dialog-box-move-files',
                            cls: 'popup-panel',
                            handlerUrl: Jsw.prepareUrl('/file-manager/move-files'),
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.move-files')) ?>,
                            titleCls: 'pp-move',
                            tree: copyMoveTreeConfig
                        });
                    });
                }
            }, {
                componentType: 'Jsw.bar.Separator'
            }, {
                componentType: 'Jsw.SmallButton',
                id: 'buttonDeleteFile',
                title: <?php echo $this->filesList->jsLmsg('buttonDeleteFile'); ?>,
                description: <?php echo $this->filesList->jsLmsg('hintDeleteFile'); ?>,
                addCls: 'sb-remove-selected',
                href: '#',
                handler: function(event) {
                    filesList.deleteFile({
                        mouseEvent: event,
                        locale: {
                            confirmOnGroupOperation: <?php echo $this->filesList->jsLmsg('confirmOnDelete'); ?>
                        }
                    });
                }
            }, {
                componentType: 'Jsw.bar.Separator'
            }, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->filesList->jsLmsg('buttonExtractArchive');?>,
                description: <?php echo $this->filesList->jsLmsg('hintExtractArchive'); ?>,
                addCls: 'sb-archive-extract',
                href: '#',
                handler: function() {
                    Jsw.getComponent('fm-content').hasSelectedItems(function() {
                        this.showPopup(Smb.FileManager.ExtractArchive, {
                            id: 'fm-extract-archive',
                            renderTo: 'fm-modal-dialog-box-extract-archive',
                            cls: 'popup-panel',
                            handlerUrl: Jsw.prepareUrl('/file-manager/extract-archive'),
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.extract-archive')) ?>,
                            titleCls: 'pp-archive-extract'
                        });
                    });
                }
            }, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->filesList->jsLmsg('buttonCreateArchive');?>,
                description: <?php echo $this->filesList->jsLmsg('hintCreateArchive'); ?>,
                addCls: 'sb-archive-create',
                href: '#',
                handler: function() {
                    Jsw.getComponent('fm-content').hasSelectedItems(function() {
                        this.showPopup(Smb.FileManager.CreateArchive, {
                            id: 'fm-create-archive',
                            renderTo: 'fm-modal-dialog-box-create-archive',
                            cls: 'popup-panel',
                            handlerUrl: Jsw.prepareUrl('/file-manager/create-archive'),
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.create-archive')) ?>,
                            titleCls: 'pp-archive-create'
                        });
                    });
                }
            }, {
                componentType: 'Jsw.bar.Separator'
            }, {
                componentType: 'Jsw.list.AdditionalActions',
                title: <?php echo $this->filesList->jsLmsg('buttonMoreActions'); ?>,
                operations: [{
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->filesList->jsLmsg('buttonCalculateSize');?>,
                        description: <?php echo $this->filesList->jsLmsg('hintCalculateSize'); ?>,
                        addCls: 'sb-calculate',
                        cls: 'sb-btn',
                        href: '#',
                        handler: function() {
                            filesList.calculateSize();
                        }
                    }, {
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->filesList->jsLmsg('buttonChangeTimestamp');?>,
                        description: <?php echo $this->filesList->jsLmsg('hintChangeTimestamp'); ?>,
                        addCls: 'sb-change-time',
                        cls: 'sb-btn',
                        href: '#',
                        handler: function() {
                            filesList.changeTimestamp();
                        }
                    }
                ]
            }, {
                componentType: 'Jsw.SmallButton',
                id: 'buttonView',
                title: <?php echo $this->filesList->jsLmsg('buttonView'); ?>,
                description: <?php echo $this->filesList->jsLmsg('hintView'); ?>,
                addCls: 'sb-settings',
                handler: function() {
                    Jsw.getComponent('fm-content').showPopup(Smb.FileManager.ViewSettings, {
                        id: 'fm-settings',
                        renderTo: 'fm-modal-dialog-box-settings',
                        cls: 'popup-panel',
                        handlerUrl: Jsw.prepareUrl('/file-manager/view-settings'),
                        locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.view-settings')) ?>,
                        settings: {
                            folderTree: true,
                            previewPanel: false, // :TODO: not implemented
                            systemFiles: true,
                            permissions: <?php echo $this->additionalColumns ? 'true' : 'false' ?>,
                            userAndGroup: <?php echo $this->additionalColumns ? 'true' : 'false' ?>
                        }
                    });
                }
            }
        ],
        locale: <?=Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.context-menu')) ?>
    });

    function redirectAction(action, item) {
        var url = <?=$this->jsEscape($this->baseUrl("/smb/file-manager/"))?> + action;
        url += '?currentDir=' + encodeURIComponent(Jsw.getComponent('fm-content').getCurrentDir());
        url += '&file=' + item.id;
        Jsw.redirect(url);
    }

    function getIcon(item) {
        var icons = <?=Zend_Json::encode(array_map(function ($type) {
            return $this->skinUrl('/icons/16/plesk/' . Plesk_Base_Utils_File::getTypeIcon($type));
        }, [FileManager::FTYPE_PDIR, FileManager::FTYPE_DIR, FileManager::FTYPE_TEXT, FileManager::FTYPE_IMAGE,
            FileManager::FTYPE_AUDIO, FileManager::FTYPE_SECURITY, FileManager::FTYPE_WEBSCRIPT,
            FileManager::FTYPE_WEBPAGE, FileManager::FTYPE_WAPPAGE, FileManager::FTYPE_COMPRESSED,
            FileManager::FTYPE_UNKNOWN_TEXT, FileManager::FTYPE_UNKNOWN_BINARY, FileManager::FTYPE_ZIP]));?>;
        var defaultType = <?=$this->jsEscape(FileManager::FTYPE_UNKNOWN_BINARY)?>;
        if (icons[item.type]) {
            return icons[item.type];
        }
        return icons[defaultType];
    }

    Event.observe( window, 'resize', function() {
        Smb.FileManager.resizeBlocks();
    }, false);

    // black magic (fix glitches after the first rendering)
    Smb.FileManager.resizeBlocks();
    Smb.FileManager.resizeBlocks();
});
//]]>
</script>
