MDL-46240 atto_collapse: fix icon url
[moodle.git] / lib / editor / atto / plugins / collapse / yui / src / button / js / button.js
CommitLineData
fcb5b5c4
DW
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
62467795
AN
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
fcb5b5c4 22/**
62467795 23 * @module moodle-atto_collapse-button
fcb5b5c4 24 */
fcb5b5c4
DW
25
26/**
27 * Atto text editor collapse plugin.
28 *
62467795
AN
29 * @namespace M.atto_collapse
30 * @class button
31 * @extends M.editor_atto.EditorPlugin
fcb5b5c4 32 */
fcb5b5c4 33
62467795
AN
34var PLUGINNAME = 'atto_collapse',
35 ATTRSHOWGROUPS = 'showgroups',
36 COLLAPSE = 'collapse',
37 COLLAPSED = 'collapsed',
38 GROUPS = '.atto_group';
fcb5b5c4 39
62467795
AN
40Y.namespace('M.atto_collapse').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
41 initializer: function() {
9ad87e79
AN
42 var toolbarGroupCount = Y.Object.size(this.get('host').get('plugins'));
43 if (toolbarGroupCount <= 1 + parseInt(this.get(ATTRSHOWGROUPS), 10)) {
44 Y.log("There are not enough groups to require toggling - not adding the button",
45 'debug','moodle-atto_collapse');
46 return;
47 }
48
49 if (this.toolbar.all(GROUPS).size() > this.get(ATTRSHOWGROUPS)) {
50 Y.log("The collapse plugin is shown after it's cut-off - not adding the button",
51 'debug','moodle-atto_collapse');
52 return;
53 }
54
62467795 55 var button = this.addButton({
e1673b58
DP
56 icon: 'icon',
57 iconComponent: PLUGINNAME,
62467795
AN
58 callback: this._toggle
59 });
fcb5b5c4 60
62467795
AN
61 // Perform a toggle after all plugins have been loaded for the first time.
62 this.get('host').on('pluginsloaded', function(e, button) {
63 this._setVisibility(button);
fcb5b5c4 64
62467795
AN
65 // Set the toolbar to break after the initial those displayed by default.
66 var firstGroup = this.toolbar.all(GROUPS).item(this.get(ATTRSHOWGROUPS));
67 firstGroup.insert('<div class="toolbarbreak"></div>', 'before');
68 }, this, button);
fcb5b5c4
DW
69 },
70
71 /**
62467795 72 * Toggle the visibility of the extra groups in the toolbar.
fcb5b5c4 73 *
62467795
AN
74 * @method _toggle
75 * @param {EventFacade} e
76 * @private
fcb5b5c4 77 */
62467795
AN
78 _toggle: function(e) {
79 e.preventDefault();
80 var button = this.buttons[COLLAPSE];
fcb5b5c4 81
62467795 82 if (button.getData(COLLAPSED)) {
ba4e81d2 83 this.highlightButtons(COLLAPSE);
62467795 84 this._setVisibility(button, true);
fcb5b5c4 85 } else {
ba4e81d2 86 this.unHighlightButtons(COLLAPSE);
62467795 87 this._setVisibility(button);
fcb5b5c4 88 }
040e1301
AN
89
90 this.buttons[this.name].focus();
fcb5b5c4
DW
91 },
92
93 /**
62467795 94 * Set the visibility of the toolbar groups.
fcb5b5c4 95 *
62467795
AN
96 * @method _setVisibility
97 * @param {Node} button The collapse button
98 * @param {Booelan} visibility Whether the groups should be made visible
99 * @private
fcb5b5c4 100 */
62467795
AN
101 _setVisibility: function(button, visibility) {
102 var groups = this.toolbar.all(GROUPS).slice(this.get(ATTRSHOWGROUPS));
fcb5b5c4 103
62467795
AN
104 if (visibility) {
105 button.set('title', M.util.get_string('showfewer', PLUGINNAME));
106 groups.show();
107 button.setData(COLLAPSED, false);
108 } else {
109 button.set('title', M.util.get_string('showmore', PLUGINNAME));
110 groups.hide();
111 button.setData(COLLAPSED, true);
112 }
fcb5b5c4 113
62467795
AN
114 }
115}, {
116 ATTRS: {
117 /**
118 * How many groups to show when collapsed.
119 *
120 * @attribute showgroups
121 * @type Number
122 * @default 3
123 */
124 showgroups: {
125 value: 3
126 }
127 }
128});