Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-atto_html-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
 
18
/*
19
 * @package    atto_html
20
 * @copyright  2013 Damyon Wiese  <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
/**
25
 * @module     moodle-atto_html-button
26
 */
27
 
28
/**
29
 * Atto text editor HTML plugin.
30
 *
31
 * @namespace M.atto_html
32
 * @class button
33
 * @extends M.editor_atto.EditorPlugin
34
 */
35
 
36
Y.namespace('M.atto_html').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
37
 
38
    _codeMirror: null,
39
 
40
    initializer: function() {
41
        this.addButton({
42
            icon: 'e/source_code',
43
            callback: this._toggleHTML
44
        });
45
 
46
        // Attach a submit listener to the form.
47
        // This ensures that the codeMirror is converted back to the original text before form submission.
48
        var form = this.get('host').textarea.ancestor('form');
49
        if (form) {
50
            form.on('submit', function() {
51
                if (this.get('isHTML')) {
52
                    this._hideCodeMirror();
53
                }
54
            }, this);
55
        }
56
    },
57
 
58
    /**
59
     * Toggle the view between the content editable div, and the textarea,
60
     * updating the content as it goes.
61
     *
62
     * @method _toggleHTML
63
     * @private
64
     */
65
    _toggleHTML: function() {
66
        // Toggle the HTML status.
67
        this.set('isHTML', !this.get('isHTML'));
68
 
69
        if (!this.get('isHTML')) {
70
            this._hideCodeMirror();
71
        } else {
72
            this._showCodeMirror();
73
        }
74
    },
75
 
76
    /**
77
     * Show the code mirror.
78
     *
79
     * @method _showCodeMirror
80
     * @private
81
     */
82
    _showCodeMirror: function() {
83
        var host = this.get('host');
84
 
85
        // Disable all plugins.
86
        host.disablePlugins();
87
 
88
        // And then re-enable this one.
89
        host.enablePlugins(this.name);
90
 
91
        // Copy the text to the contenteditable div.
92
        host.updateOriginal();
93
 
94
        // Hide the editor, and show the textarea.
95
        var dimensions = {
96
            width: this.editor.getComputedStyle('width'),
97
            height: this.editor.getComputedStyle('height')
98
        };
99
 
100
        // Tidy up the content if possible.
101
        var beautified = Y.M.atto_html.beautify.html_beautify(host.textarea.get('value'), {
102
            'indent_size': 4,
103
            'indent_inner_html': true
104
        });
105
        host.textarea.set('value', beautified);
106
 
107
        // Create the codeMirror from the textarea.
108
        this._codeMirror = Y.M.atto_html.CodeMirror.fromTextArea(host.textarea.getDOMNode(), {
109
            lineNumbers: true,
110
            mode: 'htmlmixed',
111
            tabSize: 2,
112
            lineWrapping: true
113
        });
114
        this._codeMirror.on('change', function(cm) {
115
            cm.save();
116
        });
117
 
118
        // Hide the text area and ensure that the codeMirror is displayed.
119
        this.editor.hide();
120
        this._codeMirror.setSize(dimensions.width, dimensions.height);
121
 
122
        // Focus on the codeMirror.
123
        this._codeMirror.focus();
124
    },
125
 
126
    /**
127
     * Hide the code mirror.
128
     *
129
     * @method _hideCodeMirror
130
     * @private
131
     */
132
    _hideCodeMirror: function() {
133
        var host = this.get('host');
134
 
135
        // Disable placeholder for empty content.
136
        // This will prevent the editor to copy and submit the empty placeholder.
137
        host.disablePlaceholderForEmptyContent();
138
 
139
        // Enable all plugins.
140
        host.enablePlugins();
141
 
142
        // Copy the text to the contenteditable div.
143
        host.updateFromTextArea();
144
 
145
        // Hide the textarea, and show the editor.
146
        if (this._codeMirror) {
147
            Y.one(this._codeMirror.getWrapperElement()).hide();
148
 
149
            // Calling toTextArea will remove the editor and all of its handlers.
150
            this._codeMirror.toTextArea();
151
 
152
            // Detach any remaining handlers.
153
            Y.Object.each(this._codeMirror._handlers, function(handlers, eventName) {
154
                Y.Array.each(handlers, function(handler) {
155
                    this.off(eventName, handler);
156
                }, this);
157
            }, this._codeMirror);
158
 
159
            // Cleanup.
160
            delete this._codeMirror;
161
            this._codeMirror = null;
162
        }
163
        host.textarea.hide();
164
        this.editor.show();
165
 
166
        // Focus on the editor.
167
        host.focus();
168
 
169
        // And re-mark everything as updated.
170
        this.markUpdated();
171
 
172
        // Enable placeholder for empty content.
173
        host.enablePlaceholderForEmptyContent();
174
    }
175
}, {
176
    ATTRS: {
177
        /**
178
         * The current state for the HTML view. If true, the HTML source is
179
         * shown in a textarea, otherwise the contenteditable area is
180
         * displayed.
181
         *
182
         * @attribute isHTML
183
         * @type Boolean
184
         * @default false
185
         */
186
        isHTML: {
187
            value: false
188
        }
189
    }
190
});
191
 
192
 
193
}, '@VERSION@', {
194
    "requires": [
195
        "promise",
196
        "moodle-editor_atto-plugin",
197
        "moodle-atto_html-beautify",
198
        "moodle-atto_html-codemirror",
199
        "event-valuechange"
200
    ]
201
});