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_rtl
|
|
|
18 |
* @copyright 2014 Jerome Mouneyrac
|
|
|
19 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @module moodle-atto_rtl-button
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Atto text editor rtl plugin.
|
|
|
28 |
*
|
|
|
29 |
* @namespace M.atto_rtl
|
|
|
30 |
* @class button
|
|
|
31 |
* @extends M.editor_atto.EditorPlugin
|
|
|
32 |
*/
|
|
|
33 |
|
|
|
34 |
Y.namespace('M.atto_rtl').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
|
|
|
35 |
initializer: function() {
|
|
|
36 |
var direction;
|
|
|
37 |
|
|
|
38 |
direction = 'ltr';
|
|
|
39 |
this.addButton({
|
|
|
40 |
icon: 'e/left_to_right',
|
|
|
41 |
title: direction,
|
|
|
42 |
buttonName: direction,
|
|
|
43 |
callback: this._toggleRTL,
|
|
|
44 |
callbackArgs: direction,
|
|
|
45 |
tags: '[dir=ltr]'
|
|
|
46 |
});
|
|
|
47 |
|
|
|
48 |
direction = 'rtl';
|
|
|
49 |
this.addButton({
|
|
|
50 |
icon: 'e/right_to_left',
|
|
|
51 |
title: direction,
|
|
|
52 |
buttonName: direction,
|
|
|
53 |
callback: this._toggleRTL,
|
|
|
54 |
callbackArgs: direction,
|
|
|
55 |
tags: '[dir=rtl]'
|
|
|
56 |
});
|
|
|
57 |
},
|
|
|
58 |
|
|
|
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'),
|
|
|
68 |
sourceSelection = window.rangy.saveSelection(),
|
|
|
69 |
selection = host.getSelection(),
|
|
|
70 |
newDirection = {
|
|
|
71 |
rtl: 'ltr',
|
|
|
72 |
ltr: 'rtl'
|
|
|
73 |
},
|
|
|
74 |
directionAlignment = {
|
|
|
75 |
rtl: 'right',
|
|
|
76 |
ltr: 'left'
|
|
|
77 |
};
|
|
|
78 |
if (selection) {
|
|
|
79 |
// Format the selection to be sure it has a tag parent (not the contenteditable).
|
|
|
80 |
var parentNode = host.formatSelectionBlock(),
|
|
|
81 |
parentDOMNode = parentNode.getDOMNode();
|
|
|
82 |
|
|
|
83 |
var currentDirection = parentDOMNode.getAttribute('dir');
|
|
|
84 |
if (currentDirection === direction) {
|
|
|
85 |
parentDOMNode.setAttribute("dir", newDirection[direction]);
|
|
|
86 |
parentDOMNode.style.textAlign = directionAlignment[newDirection[direction]];
|
|
|
87 |
} else {
|
|
|
88 |
parentDOMNode.setAttribute("dir", direction);
|
|
|
89 |
parentDOMNode.style.textAlign = directionAlignment[direction];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Change selection from the containing paragraph to the original one.
|
|
|
93 |
window.rangy.restoreSelection(sourceSelection);
|
|
|
94 |
// Mark the text as having been updated.
|
|
|
95 |
this.markUpdated();
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
});
|