MDL-51719 atto_rtl: RTL/LTR buttons now indicate state
[moodle.git] / lib / editor / atto / plugins / rtl / yui / src / button / js / button.js
CommitLineData
fe0d2477
JM
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_rtl
fe0d2477
JM
18 * @copyright 2014 Jerome Mouneyrac
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20 */
fe0d2477 21
62467795
AN
22/**
23 * @module moodle-atto_rtl-button
24 */
fe0d2477 25
62467795
AN
26/**
27 * Atto text editor rtl plugin.
28 *
29 * @namespace M.atto_rtl
30 * @class button
31 * @extends M.editor_atto.EditorPlugin
32 */
fe0d2477 33
62467795
AN
34Y.namespace('M.atto_rtl').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
35 initializer: function() {
36 var direction;
fe0d2477 37
62467795
AN
38 direction = 'ltr';
39 this.addButton({
40 icon: 'e/left_to_right',
41 title: direction,
42 buttonName: direction,
43 callback: this._toggleRTL,
e746afac
DP
44 callbackArgs: direction,
45 tags: '[dir=ltr]'
62467795 46 });
fe0d2477 47
62467795
AN
48 direction = 'rtl';
49 this.addButton({
50 icon: 'e/right_to_left',
51 title: direction,
52 buttonName: direction,
53 callback: this._toggleRTL,
e746afac
DP
54 callbackArgs: direction,
55 tags: '[dir=rtl]'
62467795
AN
56 });
57 },
fe0d2477 58
62467795
AN
59 /**
60 * Toggle the RTL/LTR values based on the supplied direction.
61 *
62 * @method _toggleRTL
63 * @param {EventFacade} e
64 * @param {String} direction
65 */
66 _toggleRTL: function(e, direction) {
67 var host = this.get('host'),
2209309e
LG
68 sourceSelection = rangy.saveSelection(),
69 selection = host.getSelection(),
99690e0f
AN
70 newDirection = {
71 rtl: 'ltr',
72 ltr: 'rtl'
73 };
62467795
AN
74 if (selection) {
75 // Format the selection to be sure it has a tag parent (not the contenteditable).
76 var parentNode = host.formatSelectionBlock(),
77 parentDOMNode = parentNode.getDOMNode();
fe0d2477 78
62467795
AN
79 var currentDirection = parentDOMNode.getAttribute('dir');
80 if (currentDirection === direction) {
2209309e 81 parentDOMNode.setAttribute("dir", newDirection[direction]);
62467795
AN
82 } else {
83 parentDOMNode.setAttribute("dir", direction);
84 }
fe0d2477 85
2209309e
LG
86 // Change selection from the containing paragraph to the original one.
87 rangy.restoreSelection(sourceSelection);
62467795
AN
88 // Mark the text as having been updated.
89 this.markUpdated();
90 }
fe0d2477 91 }
62467795 92});