root/trunk/common/Editor.js

Revision 201, 5.1 kB (checked in by ddavis, 2 years ago)

fixed IE setting the editor text value to 'null', and use setChanged for insertHTML and execCommand

  • Property svn:keywords set to Id
Line 
1/*
2Editor Library
3$Id$
4
5Copyright (c) 2005, Six Apart, Ltd.
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11
12    * Redistributions of source code must retain the above copyright
13notice, this list of conditions and the following disclaimer.
14
15    * Redistributions in binary form must reproduce the above
16copyright notice, this list of conditions and the following disclaimer
17in the documentation and/or other materials provided with the
18distribution.
19
20    * Neither the name of "Six Apart" nor the names of its
21contributors may be used to endorse or promote products derived from
22this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*/
36
37
38Editor = new Class( Component, {
39    changed: false,
40   
41   
42    init: function( element, mode ) {
43        arguments.callee.applySuper( this, arguments );
44        this.setMode( mode || "iframe" );
45    },
46   
47   
48    initComponents: function() {
49        arguments.callee.applySuper( this, arguments );
50       
51        this.iframe = new this.constructor.Iframe( this.element.id + "-iframe", this );
52        this.addComponent( this.iframe );
53
54        this.textarea = new this.constructor.Textarea( this.element.id + "-textarea", this );
55        this.addComponent( this.textarea );
56       
57        this.toolbar = new this.constructor.Toolbar( this.element.id + "-toolbar", this );
58        this.addComponent( this.toolbar );
59    },
60   
61       
62    destroyObject: function() {
63        this.toolbar = null;
64        this.textarea = null;
65        this.iframe = null;
66        arguments.callee.applySuper( this, arguments );
67    },
68   
69   
70    toggleMode: function() {
71        return this.setMode( this.mode == "iframe" ? "textarea" : "iframe" );
72    },
73   
74   
75    setMode: function( mode ) {
76        if( mode == this.mode )
77            return;
78        log( "setting mode " + mode );
79        var html = this.actual ? this.actual.getHTML() : null;
80        this.setModeActual( mode );
81        if ( defined( html ) && html != null )
82            this.actual.setHTML( html );
83        this.reflow();
84        return html;
85    },
86   
87   
88    setModeActual: function( mode ) {
89        if( mode != "iframe" && mode != "textarea" )
90            throw "Unknown editor mode " + mode;
91        this.mode = mode;
92        this.actual = this[ this.mode ];
93        return this.actual;
94    },
95   
96   
97    reflow: function() {
98        DOM.removeClassName( this.element, /editor-mode-/ );
99        DOM.addClassName( this.element, "editor-mode-" + this.mode );
100        arguments.callee.applySuper( this, arguments );
101    },
102
103
104    updateToolbar: function() {},
105
106
107    getKeyEventCommand: function( event ) {
108        var c = ( String.fromCharCode( event.charCode || event.keyCode ) || "" ).toUpperCase();
109       
110        if( (event.metaKey || event.ctrlKey) && !event.shiftKey && !event.altKey ) {
111            switch( c ) {
112                case "B": return "bold";
113                case "I": return "italic";
114                case "U": return "underline";
115                case "Z": return "undo";
116                case "Y": return "redo";
117            }
118        } else if( (event.metaKey || event.ctrlKey) && event.shiftKey && !event.altKey ) {
119            switch( c ) {
120                case "Z": return "redo";
121            }
122        }
123    },
124
125   
126    execCommand: function( command, userInterface, argument ) {
127        this.setChanged();
128        return this.actual.execCommand( command, userInterface, argument );
129    },
130   
131
132    editLink: function( link ) {
133        this.toolbar.editLink( link );
134    },
135   
136   
137    focus: function() {
138        this.actual.focus();
139    },
140
141   
142    getHTML: function() {
143        return this.actual.getHTML();
144    },
145
146
147    setHTML: function( html ) {
148        return this.actual.setHTML( html );
149    },
150
151   
152    insertHTML: function( html, select, id, isTempId ) {
153        this.setChanged();
154        return this.actual.insertHTML( html, select, id, isTempId );
155    },
156   
157   
158    // REFACTOR
159    isTextSelected: function( selection ) {
160        return this.actual.isTextSelected();
161    },
162
163   
164    // REFACTOR
165    getSelectedLink: function() {
166        return this.actual.getSelectedLink();
167    },
168
169
170    setChanged: function() {
171        this.changed = true;
172    }
173
174
175} );
176
177
178Editor.strings = {
179    enterLinkAddress: "Enter the link address:",
180    enterTextToLinkTo: "Enter the text to link to:"
181};
Note: See TracBrowser for help on using the browser.