Merge branch 'MDL-46497_master' of https://github.com/nadavkav/moodle
[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,
46 callbackArgs: direction
47 });
fe0d2477 48
62467795
AN
49 direction = 'rtl';
50 this.addButton({
51 icon: 'e/right_to_left',
52 title: direction,
53 buttonName: direction,
54 callback: this._toggleRTL,
55 callbackArgs: direction
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(),
70 newDirection = { rtl: 'ltr', ltr: 'rtl' };
62467795
AN
71 if (selection) {
72 // Format the selection to be sure it has a tag parent (not the contenteditable).
73 var parentNode = host.formatSelectionBlock(),
74 parentDOMNode = parentNode.getDOMNode();
fe0d2477 75
62467795
AN
76 var currentDirection = parentDOMNode.getAttribute('dir');
77 if (currentDirection === direction) {
2209309e 78 parentDOMNode.setAttribute("dir", newDirection[direction]);
62467795
AN
79 } else {
80 parentDOMNode.setAttribute("dir", direction);
81 }
fe0d2477 82
2209309e
LG
83 // Change selection from the containing paragraph to the original one.
84 rangy.restoreSelection(sourceSelection);
62467795
AN
85 // Mark the text as having been updated.
86 this.markUpdated();
87 }
fe0d2477 88 }
62467795 89});
fe0d2477
JM
90
91
62467795 92}, '@VERSION@', {"requires": ["moodle-editor_atto-plugin"]});