Source: widgets/SpwAccessFile.js

Retour à la documentation
/**
 * @class spw.widgets.SpwAccessFile
 */
define([
    'dojo/_base/declare', 'dojo/_base/lang', 'dojo/_base/array',
    'spw/api/SpwBaseTemplatedWidget',
    'dojo/text!./templates/SpwAccessFile.html',
    'esri/request', 'dojo/dom-construct', 'dojo/on',
    'dojo/request/iframe', 'spw/api/MessageManager',
    'dijit/layout/BorderContainer', 'dijit/layout/ContentPane',
    'dijit/form/Form', 'dijit/form/ValidationTextBox', 'dijit/form/Button'
], function(declare, lang, array,
            _Templated,
            tmpl,
            request, domConstruct, on,
            iframe, MessageManager) {

    return declare('spw.widgets.SpwAccessFile', _Templated, {
        templateString: tmpl,

        accessFileUrl: '/geoviewer/AccessFileServlet?url={path}&protocol={protocol}',

        postCreate: function() {
            this.inherited(arguments);

            this._formNode.on('submit', lang.hitch(this, this.onFormSubmit));
        },

        onFormSubmit: function() {
            if (!this._formNode.validate()) {
                return false;
            }

            var path = this._formNode.get('value').path;

            this.currentRootPath = path;

            this.openFolder(path, true);

            return false;
        },

        openFolder: function(path) {
            this.showLoading();

            var protocol = 'file';

            if (path.indexOf('sftp') > -1) {
                path = path.replace('sftp://', '');
                protocol = 'sftp';
            }
            else if (path.indexOf('ftp') > -1) {
                path = path.replace('ftp://', '');
                protocol = 'ftp';
            }

            request({
                url: lang.replace(this.accessFileUrl, {
                    path: path,
                    protocol: protocol
                }),
                handleAs: 'json'
            }).then(lang.hitch(this, function(files) {

                this.hideLoading();

                if (files == null || !lang.isArray(files)) {
                    MessageManager.getInstance().notifyError('Impossible de récupérer la liste des fichiers. Vous devez entrer un répertoire valide...');
                    return;
                }

                domConstruct.empty(this._listNode);

                files.sort(function(f1, f2) {
                    if (f1.isDirectory && !f2.isDirectory) {
                        return -1;
                    }
                    else if (!f1.isDirectory && f2.isDirectory) {
                        return 1;
                    }

                    return f1.path.localeCompare(f2.path);
                });

                if (path !== this.currentRootPath) {
                    files.splice(0, 0, {
                        path: this.extractParent(path),
                        name: '..',
                        isDirectory: true
                    });
                }

                array.forEach(files, lang.hitch(this, function(file) {

                    var li = domConstruct.create('li', null, this._listNode);

                    var img = domConstruct.create('img', {
                        'src': this.imagesPath + (file.isDirectory ? 'folder.png' : 'file.png'),
                        'class': 'file-icon'
                    }, li);

                    var span = domConstruct.create('span', {
                        'class': 'file',
                        innerHTML: file.name
                    }, li);



                    if (file.isDirectory) {
                        on(span, 'click', lang.hitch(this, this.openFolder, file.path));
                        on(img, 'click', lang.hitch(this, this.openFolder, file.path));
                    }
                    else {
                        on(span, 'click', lang.hitch(this, this.openFile, file.path));
                        on(img, 'click', lang.hitch(this, this.openFile, file.path));
                    }

                }));

            }), lang.hitch(this, function(err) {
                this.hideLoading();
                console.error(err);
                MessageManager.getInstance().notifyError('Impossible de récupérer la liste des fichiers (erreur serveur)');
            }));
        },

        openFile: function(path) {
            iframe._currentDfd = null;

            var protocol = 'file';

            if (path.indexOf('sftp') > -1) {
                path = path.replace('sftp://', '');
                protocol = 'sftp';
            }
            else if (path.indexOf('ftp') > -1) {
                path = path.replace('ftp://', '');
                protocol = 'ftp';
            }

            iframe(lang.replace(this.accessFileUrl, {
                path: path,
                protocol: protocol
            }));
        },

        extractParent: function(path) {
            if (path.indexOf('//') === 0) {
                if (path.charAt(path.length - 1) === '/') {
                    path = path.substring(0, path.length - 1);
                }
                return path.substring(0, path.lastIndexOf('/'));
            }
            return path.substring(0, path.lastIndexOf('\\'));
        },

        resize: function() {
            this.inherited(arguments);
            this._borderContainer && this._borderContainer.resize();
        }
    });

});