Annotation of kupuMPIWG/common/kupumultieditor.js, revision 1.1.1.1
1.1 dwinter 1: /*****************************************************************************
2: *
3: * Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
4: *
5: * This software is distributed under the terms of the Kupu
6: * License. See LICENSE.txt for license text. For a list of Kupu
7: * Contributors see CREDITS.txt.
8: *
9: *****************************************************************************/
10:
11: // $Id: kupumultieditor.js 3450 2004-03-28 11:07:30Z guido $
12:
13: function KupuMultiEditor(documents, config, logger) {
14: /* multiple kupus in one form */
15: this.documents = documents; // array of documents
16: this.config = config;
17: this.log = logger;
18: this.tools = {};
19:
20: this._designModeAttempts = 0;
21: this._initialized = false;
22:
23: this._previous_range = null;
24:
25: // here's where the current active document will be stored
26: this._current_document = documents[0];
27:
28: this.initialize = function() {
29: this._initializeEventHandlers();
30: this.getDocument().getWindow().focus();
31: if (this.getBrowserName() == 'IE') {
32: for (var i=0; i < this.documents.length; i++) {
33: var body = this.documents[i].getDocument().getElementsByTagName('body')[0];
34: body.setAttribute('contentEditable', 'true');
35: };
36: // provide an 'afterInit' method on KupuEditor.prototype
37: // for additional bootstrapping (after editor init)
38: this._initialized = true;
39: if (this.afterInit) {
40: this.afterInit();
41: };
42: this._saveSelection();
43: this.logMessage(_('Editor initialized'));
44: } else {
45: this._setDesignModeWhenReady();
46: };
47: };
48:
49: this.updateStateHandler = function(event) {
50: /* check whether the event is interesting enough to trigger the
51: updateState machinery and act accordingly */
52: var interesting_codes = new Array(8, 13, 37, 38, 39, 40, 46);
53: if (event.type == 'click' || event.type == 'dblclick' ||
54: event.type == 'select' ||
55: (event.type == 'keyup' &&
56: interesting_codes.contains(event.keyCode))) {
57: var target = event.target ? event.target : event.srcElement;
58: // find the document targeted
59: while (target.nodeType != 9) {
60: target = target.parentNode;
61: };
62: var document = null;
63: for (var i=0; i < this.documents.length; i++) {
64: document = this.documents[i];
65: if (document.getDocument() == target) {
66: break;
67: };
68: };
69: if (!document) {
70: alert('No document found!');
71: return;
72: };
73: this._current_document = document;
74: this.updateState(event);
75: };
76: // unfortunately it's not possible to do this on blur, since that's
77: // too late. also (some versions of?) IE 5.5 doesn't support the
78: // onbeforedeactivate event, which would be ideal here...
79: if (this.getBrowserName() == 'IE') {
80: this._saveSelection();
81: };
82: };
83:
84: this.saveDocument = function() {
85: throw('Not supported, use prepareForm to attach the editor to a form');
86: };
87:
88: this.getDocument = function() {
89: /* return the current active document */
90: return this._current_document;
91: };
92:
93: this._initializeEventHandlers = function() {
94: /* attache the event handlers to the iframe */
95: for (var i=0; i < this.documents.length; i++) {
96: var doc = this.documents[i].getDocument();
97: this._addEventHandler(doc, "click", this.updateStateHandler, this);
98: this._addEventHandler(doc, "keyup", this.updateStateHandler, this);
99: if (this.getBrowserName() == "IE") {
100: this._addEventHandler(doc, "dblclick", this.updateStateHandler, this);
101: this._addEventHandler(doc, "select", this.updateStateHandler, this);
102: };
103: };
104: };
105:
106: this._setDesignModeWhenReady = function() {
107: this._designModeSetAttempts++;
108: if (this._designModeSetAttempts > 25) {
109: alert(_('Couldn\'t set design mode. Kupu will not work on this browser.'));
110: return;
111: };
112: var should_retry = false;
113: for (var i=0; i < this.documents.length; i++) {
114: var document = this.documents[i];
115: if (!document._designModeSet) {
116: try {
117: this._setDesignMode(document);
118: document._designModeSet = true;
119: } catch(e) {
120: should_retry = true;
121: };
122: };
123: };
124: if (should_retry) {
125: timer_instance.registerFunction(this, this._setDesignModeWhenReady, 100);
126: } else {
127: // provide an 'afterInit' method on KupuEditor.prototype
128: // for additional bootstrapping (after editor init)
129: if (this.afterInit) {
130: this.afterInit();
131: };
132: this._initialized = true;
133: };
134: };
135:
136: this._setDesignMode = function(doc) {
137: doc.getDocument().designMode = "On";
138: doc.execCommand("undo");
139: };
140:
141: // XXX perhaps we can partially move this to a helper method to approve
142: // code reuse?
143: this.prepareForm = function(form, idprefix) {
144: /* add some fields to the form and place the contents of the iframes
145: */
146: var sourcetool = this.getTool('sourceedittool');
147: if (sourcetool) {sourcetool.cancelSourceMode();};
148:
149: // make sure people can't edit or save during saving
150: if (!this._initialized) {
151: return;
152: }
153: this._initialized = false;
154:
155: // set the window status so people can see we're actually saving
156: window.status= _("Please wait while saving document...");
157:
158: // set a default id
159: if (!idprefix) {
160: idprefix = 'kupu';
161: };
162:
163: // pass the content through the filters
164: this.logMessage(_("Starting HTML cleanup"));
165: var contents = new Array();
166: for (var i=0; i < this.documents.length; i++) {
167: var transform = this._filterContent(this.documents[i].getDocument().documentElement);
168: contents.push(this._serializeOutputToString(transform));
169: };
170:
171: this.logMessage(_("Cleanup done, sending document to server"));
172:
173: // now create the form input, since IE 5.5 doesn't support the
174: // ownerDocument property we use window.document as a fallback (which
175: // will almost by definition be correct).
176: var document = form.ownerDocument ? form.ownerDocument : window.document;
177: for (var i=0; i < contents.length; i++) {
178: var ta = document.createElement('textarea');
179: ta.style.visibility = 'hidden';
180: var text = document.createTextNode(contents[i]);
181: ta.appendChild(text);
182: ta.setAttribute('name', idprefix + '_' + i);
183:
184: // and add it to the form
185: form.appendChild(ta);
186: };
187: };
188: };
189:
190: KupuMultiEditor.prototype = new KupuEditor;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>