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 |
* @module moodle-editor_atto-editor
|
|
|
18 |
* @submodule textarea
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Textarea functions for the Atto editor.
|
|
|
23 |
*
|
|
|
24 |
* See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
|
|
|
25 |
*
|
|
|
26 |
* @namespace M.editor_atto
|
|
|
27 |
* @class EditorTextArea
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
function EditorTextArea() {}
|
|
|
31 |
|
|
|
32 |
EditorTextArea.ATTRS = {
|
|
|
33 |
};
|
|
|
34 |
|
|
|
35 |
EditorTextArea.prototype = {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Return the appropriate empty content value for the current browser.
|
|
|
39 |
*
|
|
|
40 |
* Different browsers use a different content when they are empty and
|
|
|
41 |
* we must set this reliable across the board.
|
|
|
42 |
*
|
|
|
43 |
* @method _getEmptyContent
|
|
|
44 |
* @return String The content to use representing no user-provided content
|
|
|
45 |
* @private
|
|
|
46 |
*/
|
|
|
47 |
_getEmptyContent: function() {
|
|
|
48 |
if (!this.enableAppropriateEmptyContent) {
|
|
|
49 |
// Return the empty string if we do not enable the empty placeholder. Ex: HTML mode.
|
|
|
50 |
return '';
|
|
|
51 |
}
|
|
|
52 |
var alignment;
|
|
|
53 |
if (this.coreDirection === 'rtl') {
|
|
|
54 |
alignment = 'style="text-align: right;"';
|
|
|
55 |
} else {
|
|
|
56 |
alignment = 'style="text-align: left;"';
|
|
|
57 |
}
|
|
|
58 |
if (Y.UA.ie && Y.UA.ie < 10) {
|
|
|
59 |
return '<p dir="' + this.coreDirection + '" ' + alignment + '></p>';
|
|
|
60 |
} else {
|
|
|
61 |
return '<p dir="' + this.coreDirection + '" ' + alignment + '><br></p>';
|
|
|
62 |
}
|
|
|
63 |
},
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Copy and clean the text from the textarea into the contenteditable div.
|
|
|
67 |
*
|
|
|
68 |
* If the text is empty, provide a default paragraph tag to hold the content.
|
|
|
69 |
*
|
|
|
70 |
* @method updateFromTextArea
|
|
|
71 |
* @chainable
|
|
|
72 |
*/
|
|
|
73 |
updateFromTextArea: function() {
|
|
|
74 |
// Clear it first.
|
|
|
75 |
this.editor.setHTML('');
|
|
|
76 |
|
|
|
77 |
// Copy cleaned HTML to editable div.
|
|
|
78 |
this.editor.append(this._cleanHTML(this.textarea.get('value'), true));
|
|
|
79 |
|
|
|
80 |
// Insert a paragraph in the empty contenteditable div.
|
|
|
81 |
if (this.editor.getHTML() === '') {
|
|
|
82 |
this.editor.setHTML(this._getEmptyContent());
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return this;
|
|
|
86 |
},
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Copy the text from the contenteditable to the textarea which it replaced.
|
|
|
90 |
*
|
|
|
91 |
* @method updateOriginal
|
|
|
92 |
* @chainable
|
|
|
93 |
*/
|
|
|
94 |
updateOriginal: function() {
|
|
|
95 |
// Get the previous and current value to compare them.
|
|
|
96 |
var oldValue = this.textarea.get('value'),
|
|
|
97 |
newValue = this.getCleanHTML();
|
|
|
98 |
|
|
|
99 |
if (newValue === "" && this.isActive()) {
|
|
|
100 |
// The content was entirely empty so get the empty content placeholder.
|
|
|
101 |
newValue = this._getEmptyContent();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Only call this when there has been an actual change to reduce processing.
|
|
|
105 |
if (oldValue !== newValue) {
|
|
|
106 |
// Insert the cleaned content.
|
|
|
107 |
this.textarea.set('value', newValue);
|
|
|
108 |
|
|
|
109 |
// Trigger the onchange callback on the textarea, essentially to notify the formchangechecker module.
|
|
|
110 |
this.textarea.simulate('change');
|
|
|
111 |
|
|
|
112 |
// Trigger handlers for this action.
|
|
|
113 |
this.fire('change');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return this;
|
|
|
117 |
}
|
|
|
118 |
};
|
|
|
119 |
|
|
|
120 |
Y.Base.mix(Y.M.editor_atto.Editor, [EditorTextArea]);
|