<?php
// Copyright 1999-2012. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<div id="fm" class="objects-management-box 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="objects-details" class="objects-details">
        <div id="objects-warnings" class="warning-block" style="display: none; "></div>
        <div id="fm-content"></div>
    </div>
    <div class="fm-footer">
    </div>
</div>

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

    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);
            },
            onRootNodeClick: function() {
                Jsw.getComponent('fm-content').changeDirectory('/', true);
            }
        },
        isDisabledItem: function(item) {
            return ('..' == item.name);
        },
        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 = '<img src="' + item.icon + '"> ';

                    if (item.isDirectory) {
                        var linkStartTag = '<a class="fm-file-name-link" href="#" onclick="Jsw.getComponent(\'fm-content\').changeDirectory(\'' + item.name.replace(/"/g, '\\&quot;').replace(/'/g, '\\&apos;') + '\')">';
                    } else {
                        var linkStartTag = '<a class="fm-file-name-link" href="' + item.defaultAction + '">';
                    }

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

                    var actions = '';
                    if (item.actions) {
                        actions = '<a class="s-btn sb-expand js-operations-button" href="#"><i><i><i><span>File Operations</span></i></i></i></a>';
                    }

                    var cellHtml = '' +
                        actions +
                        '<table class="fm-file-name-wrap">' +
                            '<tbody>' +
                                '<tr class="" id="">' +
                                    '<td class="fm-file-name-container">' +
                                        itemTitle +
                                    '</td>' +
                                '</tr>' +
                            '</tbody>' +
                        '</table>';

                    return cellHtml;
                }
            }, {
                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',
                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; ?>
        ],
        itemActions: {
            browse: function(item, action) {
                window.open(action.href);
            },

            rename: function(item, action) {
                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')) ?>,
                    titleCls: 'pp-rename',
                    fileName: item.id
                });
            },

            extractArchive: function(item, action) {
                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
                });
            }
        },
        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')) ?>,
                    titleCls: 'pp-upload'
                }
            }, {
                componentType: 'Jsw.list.AdditionalActions',
                title: <?php echo $this->filesList->jsLmsg('buttonNewActions'); ?>,
                titleAddCls: 'sb-add-new',
                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',
                handler: function(event) {
                    Jsw.getComponent('fm-content').execGroupOperation({
                        url: '/file-manager/delete',
                        subtype: 'delete',
                        mouseEvent: event,
                        locale: {
                            confirmOnGroupOperation: <?php echo $this->filesList->jsLmsg('confirmOnDelete'); ?>
                        },
                        onSuccess: function() {
                            Jsw.getComponent('fm-content').enable();
                            Jsw.getComponent('fm-tree').reloadPath(Jsw.getComponent('fm-content').getCurrentDir());
                        }
                    });
                }
            }, {
                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('buttonExtractArchive');?>,
                        description: <?php echo $this->filesList->jsLmsg('hintExtractArchive'); ?>,
                        addCls: 'sb-archive-extract',
                        cls: 'sb-btn',
                        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',
                        cls: 'sb-btn',
                        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.HorizontalSeparator'
                    }, {
                        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() {
                            Jsw.getComponent('fm-content').execGroupOperation({
                                url: '/file-manager/touch',
                                skipConfirmation: true
                            });
                        }
                    }
                ]
            }, {
                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')) ?>,
                        titleCls: 'pp-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' ?>
                        }
                    });
                }
            }
        ]
    });

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

    new Smb.FileManager.DragAndDrop({
        applyTo: 'fm-content-container',
        dropAreaId: 'drop-area',
        locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.file-manager.upload')) ?>,
        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')) ?>,
            titleCls: 'pp-upload'
    }});

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