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_collapse
18
 * @copyright  2013 Damyon Wiese  <damyon@moodle.com>
19
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20
 */
21
 
22
/**
23
 * @module moodle-atto_collapse-button
24
 */
25
 
26
/**
27
 * Atto text editor collapse plugin.
28
 *
29
 * @namespace M.atto_collapse
30
 * @class button
31
 * @extends M.editor_atto.EditorPlugin
32
 */
33
 
34
var PLUGINNAME = 'atto_collapse',
35
    ATTRSHOWGROUPS = 'showgroups',
36
    COLLAPSE = 'collapse',
37
    COLLAPSED = 'collapsed',
38
    GROUPS = '.atto_group',
39
    ROWS = '.atto_toolbar_row';
40
 
41
Y.namespace('M.atto_collapse').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
42
    initializer: function() {
43
        var toolbarGroupCount = Y.Object.size(this.get('host').get('plugins'));
44
        if (toolbarGroupCount <= 1 + parseInt(this.get(ATTRSHOWGROUPS), 10)) {
45
            Y.log("There are not enough groups to require toggling - not adding the button",
46
                'debug', 'moodle-atto_collapse');
47
            return;
48
        }
49
 
50
        if (this.toolbar.all(GROUPS).size() > this.get(ATTRSHOWGROUPS)) {
51
            Y.log("The collapse plugin is shown after it's cut-off - not adding the button",
52
                'debug', 'moodle-atto_collapse');
53
            return;
54
        }
55
 
56
        var button = this.addButton({
57
            icon: 'icon',
58
            iconComponent: PLUGINNAME,
59
            callback: this._toggle
60
        });
61
 
62
        // Perform a toggle after all plugins have been loaded for the first time.
63
        this.get('host').on('pluginsloaded', function(e, button) {
64
            // Add 2 rows in the toolbar.
65
            var toolbarRows = [
66
                Y.Node.create('<div class="atto_toolbar_row" role="group"></div>'),
67
                Y.Node.create('<div class="atto_toolbar_row" role="group" aria-label="' +
68
                    M.util.get_string('youareonsecondrow', PLUGINNAME) +
69
                    '" tabindex="-1"></div>'),
70
            ];
71
            this.toolbar.appendChild(toolbarRows[0]).insert(toolbarRows[1], 'after');
72
 
73
            // Split toolbar buttons between the 2 rows created above.
74
            var buttonGroups = this.toolbar.all(GROUPS);
75
            buttonGroups.slice(0, this.get(ATTRSHOWGROUPS)).each(function(buttonGroup) {
76
                toolbarRows[0].appendChild(buttonGroup);
77
            });
78
            buttonGroups.slice(this.get(ATTRSHOWGROUPS)).each(function(buttonGroup) {
79
                toolbarRows[1].appendChild(buttonGroup);
80
            });
81
 
82
            this._setVisibility(button);
83
            button.setAttribute('aria-expanded', 'false');
84
        }, this, button);
85
    },
86
 
87
    /**
88
     * Toggle the visibility of the extra groups in the toolbar.
89
     *
90
     * @method _toggle
91
     * @param {EventFacade} e
92
     * @private
93
     */
94
    _toggle: function(e) {
95
        e.preventDefault();
96
        var button = this.buttons[COLLAPSE];
97
 
98
        if (button.getData(COLLAPSED)) {
99
            this.highlightButtons(COLLAPSE);
100
            this._setVisibility(button, true);
101
            this.toolbar.all(ROWS).item(1).focus();
102
        } else {
103
            this.unHighlightButtons(COLLAPSE);
104
            this._setVisibility(button);
105
            this.buttons[this.name].focus();
106
        }
107
    },
108
 
109
    /**
110
     * Set the visibility of the toolbar groups.
111
     *
112
     * @method _setVisibility
113
     * @param {Node} button The collapse button
114
     * @param {Booelan} visibility Whether the groups should be made visible
115
     * @private
116
     */
117
    _setVisibility: function(button, visibility) {
118
        var secondaryRow = this.toolbar.all(ROWS).item(1);
119
 
120
        if (visibility) {
121
            button.set('title', M.util.get_string('showfewer', PLUGINNAME));
122
            secondaryRow.show();
123
            button.setData(COLLAPSED, false);
124
            button.setAttribute('aria-expanded', 'true');
125
        } else {
126
            button.set('title', M.util.get_string('showmore', PLUGINNAME));
127
            secondaryRow.hide();
128
            button.setData(COLLAPSED, true);
129
            button.setAttribute('aria-expanded', 'false');
130
        }
131
 
132
        // We don't want to have both aria-pressed and aria-expanded set. So we remove aria-pressed here.
133
        button.removeAttribute('aria-pressed');
134
    }
135
}, {
136
    ATTRS: {
137
        /**
138
         * How many groups to show when collapsed.
139
         *
140
         * @attribute showgroups
141
         * @type Number
142
         * @default 3
143
         */
144
        showgroups: {
145
            value: 3
146
        }
147
    }
148
});