MDL-51719 atto_rtl: RTL/LTR buttons now indicate state
[moodle.git] / lib / editor / atto / plugins / rtl / yui / build / moodle-atto_rtl-button / moodle-atto_rtl-button.js
CommitLineData
fe0d2477
JM
1YUI.add('moodle-atto_rtl-button', function (Y, NAME) {
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
62467795
AN
18/*
19 * @package atto_rtl
fe0d2477
JM
20 * @copyright 2014 Jerome Mouneyrac
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
fe0d2477 23
62467795
AN
24/**
25 * @module moodle-atto_rtl-button
26 */
fe0d2477 27
62467795
AN
28/**
29 * Atto text editor rtl plugin.
30 *
31 * @namespace M.atto_rtl
32 * @class button
33 * @extends M.editor_atto.EditorPlugin
34 */
fe0d2477 35
62467795
AN
36Y.namespace('M.atto_rtl').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
37 initializer: function() {
38 var direction;
fe0d2477 39
62467795
AN
40 direction = 'ltr';
41 this.addButton({
42 icon: 'e/left_to_right',
43 title: direction,
44 buttonName: direction,
45 callback: this._toggleRTL,
e746afac
DP
46 callbackArgs: direction,
47 tags: '[dir=ltr]'
62467795 48 });
fe0d2477 49
62467795
AN
50 direction = 'rtl';
51 this.addButton({
52 icon: 'e/right_to_left',
53 title: direction,
54 buttonName: direction,
55 callback: this._toggleRTL,
e746afac
DP
56 callbackArgs: direction,
57 tags: '[dir=rtl]'
62467795
AN
58 });
59 },
fe0d2477 60
62467795
AN
61 /**
62 * Toggle the RTL/LTR values based on the supplied direction.
63 *
64 * @method _toggleRTL
65 * @param {EventFacade} e
66 * @param {String} direction
67 */
68 _toggleRTL: function(e, direction) {
69 var host = this.get('host'),
2209309e
LG
70 sourceSelection = rangy.saveSelection(),
71 selection = host.getSelection(),
99690e0f
AN
72 newDirection = {
73 rtl: 'ltr',
74 ltr: 'rtl'
75 };
62467795
AN
76 if (selection) {
77 // Format the selection to be sure it has a tag parent (not the contenteditable).
78 var parentNode = host.formatSelectionBlock(),
79 parentDOMNode = parentNode.getDOMNode();
fe0d2477 80
62467795
AN
81 var currentDirection = parentDOMNode.getAttribute('dir');
82 if (currentDirection === direction) {
2209309e 83 parentDOMNode.setAttribute("dir", newDirection[direction]);
62467795
AN
84 } else {
85 parentDOMNode.setAttribute("dir", direction);
86 }
fe0d2477 87
2209309e
LG
88 // Change selection from the containing paragraph to the original one.
89 rangy.restoreSelection(sourceSelection);
62467795
AN
90 // Mark the text as having been updated.
91 this.markUpdated();
92 }
fe0d2477 93 }
62467795 94});
fe0d2477
JM
95
96
62467795 97}, '@VERSION@', {"requires": ["moodle-editor_atto-plugin"]});