Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * @package    atto_managefiles
18
 * @copyright  2014 Frédéric Massart
19
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20
 */
21
 
22
/**
23
 * @module moodle-atto-managefiles-button
24
 */
25
 
26
/**
27
 * Atto text editor managefiles plugin.
28
 *
29
 * @namespace M.atto_link
30
 * @class button
31
 * @extends M.editor_atto.EditorPlugin
32
 */
33
 
34
var LOGNAME = 'atto_managefiles',
35
    CAN_RECEIVE_FOCUS_SELECTOR = '.fp-navbar a:not([disabled])',
36
    FILE_MANAGER_SELECTOR = '#fitem_id_files_filemanager';
37
 
38
Y.namespace('M.atto_managefiles').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
39
 
40
    /**
41
     * A reference to the current selection at the time that the dialogue
42
     * was opened.
43
     *
44
     * @property _currentSelection
45
     * @type Range
46
     * @private
47
     */
48
    _currentSelection: null,
49
 
50
    initializer: function() {
51
        if (this.get('disabled')) {
52
            return;
53
        }
54
 
55
        var host = this.get('host'),
56
            area = this.get('area'),
57
            options = host.get('filepickeroptions');
58
 
59
        if (options.image && options.image.itemid) {
60
            area.itemid = options.image.itemid;
61
            this.set('area', area);
62
        } else {
63
            Y.log('Plugin managefiles not available because itemid is missing.',
64
                    'warn', LOGNAME);
65
            return;
66
        }
67
 
68
        this.addButton({
69
            icon: 'e/manage_files',
70
            callback: this._displayDialogue
71
        });
72
    },
73
 
74
    /**
75
     * Display the manage files.
76
     *
77
     * @method _displayDialogue
78
     * @private
79
     */
80
    _displayDialogue: function(e) {
81
        e.preventDefault();
82
 
83
        var dialogue = this.getDialogue({
84
            headerContent: M.util.get_string('managefiles', LOGNAME),
85
            width: '800px',
86
            focusAfterHide: true
87
        });
88
 
89
        var iframe = Y.Node.create('<iframe></iframe>');
90
        // We set the height here because otherwise it is really small. That might not look
91
        // very nice on mobile devices, but we considered that enough for now.
92
        iframe.setStyles({
93
            height: '700px',
94
            border: 'none',
95
            width: '100%'
96
        });
97
        iframe.setAttribute('src', this._getIframeURL());
98
 
99
        // Focus on the first focusable element of the file manager after it is fully loaded.
100
        iframe.on('load', function(e, frame) {
101
            var fileManager = frame.getDOMNode().contentDocument.querySelector(FILE_MANAGER_SELECTOR);
102
            // The file manager component is loaded asynchronously after the page is loaded.
103
            // We check for the presence of .fm-loaded every 200 ms to determine if the file manager is loaded yet.
104
            var intervalId = setInterval(function() {
105
                if (fileManager.querySelector('.fm-loaded')) {
106
                    var firstFocusableElement = fileManager.querySelector(CAN_RECEIVE_FOCUS_SELECTOR);
107
                    if (firstFocusableElement) {
108
                        firstFocusableElement.focus();
109
                    }
110
                    clearInterval(intervalId);
111
                }
112
            }, 200);
113
        }, this, iframe);
114
 
115
        dialogue.set('bodyContent', iframe)
116
                .show();
117
 
118
        this.markUpdated();
119
    },
120
 
121
    /**
122
     * Returns the URL to the file manager.
123
     *
124
     * @param _getIframeURL
125
     * @return {String} URL
126
     * @private
127
     */
128
    _getIframeURL: function() {
129
        var args = Y.mix({
130
                    elementid: this.get('host').get('elementid')
131
                },
132
                this.get('area'));
133
        return M.cfg.wwwroot + '/lib/editor/atto/plugins/managefiles/manage.php?' +
134
                Y.QueryString.stringify(args);
135
    }
136
}, {
137
    ATTRS: {
138
        disabled: {
139
            value: true
140
        },
141
        area: {
142
            value: {}
143
        }
144
    }
145
});