Annotation of kupuMPIWG/plone/kupu_plone_layer/kupuploneui.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: kupuploneui.js 9879 2005-03-18 12:04:00Z yuppie $
12:
13: function PloneKupuUI(textstyleselectid) {
14: this.tsselect = getFromSelector(textstyleselectid);
15: this.otherstyle = null;
16: this.styles = {};
17: var styles = this.styles; // use an object here so we can use the 'in' operator later on
18:
19: function cleanStyles(options) {
20: for (var i=0; i < options.length; i++) {
21: var style = options[i].value;
22: if (style.indexOf('|') > -1) {
23: var split = style.split('|');
24: style = split[0].toLowerCase() + "|" + split[1];
25: };
26: styles[style] = i;
27: };
28: }
29: cleanStyles(this.tsselect.options);
30:
31: this.nodeStyle = function(node) {
32: var currnode = node;
33: var index = -1;
34: var styles = this.styles;
35: var options = this.tsselect.options;
36: this.styletag = undefined;
37: this.classname = '';
38:
39: while (currnode) {
40: if (currnode.nodeType==1) {
41: var tag = currnode.tagName;
42: if (tag=='BODY') {
43: if (!this.styletag) {
44: // Force style setting
45: this.setTextStyle(options[0].value, true);
46: return 0;
47: }
48: break;
49: }
50: tag = tag.toLowerCase();
51: if (/p|div|h.|t.|ul|ol|dl|menu|dir|pre|blockquote|address|center/.test(tag)) {
52: var className = currnode.className;
53: this.styletag = tag;
54: this.classname = className;
55: var style = tag+'|'+className;
56: if (style in styles) {
57: index = styles[style];
58: } else if (!className && tag in styles) {
59: index = styles[tag];
60: }
61: }
62: }
63: currnode = currnode.parentNode;
64: }
65: return index;
66: }
67:
68: this.updateState = function(selNode) {
69: /* set the text-style pulldown */
70:
71: // first get the nearest style
72: // search the list of nodes like in the original one, break if we encounter a match,
73: // this method does some more than the original one since it can handle commands in
74: // the form of '<style>|<classname>' next to the plain
75: // '<style>' commands
76: var index = undefined;
77: var mixed = false;
78: var styletag, classname;
79:
80: var selection = this.editor.getSelection();
81:
82: for (var el=selNode.firstChild; el; el=el.nextSibling) {
83: if (el.nodeType==1 && selection.containsNode(el)) {
84: var i = this.nodeStyle(el);
85: if (index===undefined) {
86: index = i;
87: styletag = this.styletag;
88: classname = this.classname;
89: }
90: if (index != i || styletag!=this.styletag || classname != this.classname) {
91: mixed = true;
92: break;
93: }
94: }
95: };
96:
97: if (index===undefined) {
98: index = this.nodeStyle(selNode);
99: }
100:
101: if (this.otherstyle) {
102: this.tsselect.removeChild(this.otherstyle);
103: this.otherstyle = null;
104: }
105:
106: if (index < 0 || mixed) {
107: var caption = mixed ? 'Mixed styles' :
108: 'Other: ' + this.styletag + ' '+ this.classname;
109:
110: if (!this.otherstyle) {
111: var opt = document.createElement('option');
112: this.tsselect.appendChild(opt);
113: this.otherstyle = opt;
114: this.otherstyle.text = caption;
115: }
116:
117: index = this.tsselect.length-1;
118: }
119: this.tsselect.selectedIndex = Math.max(index,0);
120: };
121:
122: this._cleanNode = function(node) {
123: /* Clean up a block style node (e.g. P, DIV, Hn)
124: * Remove trailing whitespace, then also remove up to one
125: * trailing <br>
126: * If the node is now empty, remove the node itself.
127: */
128: var len = node.childNodes.length;
129: function stripspace() {
130: var c;
131: while ((c = node.lastChild) && c.nodeType==3 && /^\s*$/.test(c.data)) {
132: node.removeChild(c);
133: }
134: }
135: stripspace();
136: var c = node.lastChild;
137: if (c && c.nodeType==1 && c.tagName=='BR') {
138: node.removeChild(c);
139: }
140: stripspace();
141: if (node.childNodes.length==0) {
142: node.parentNode.removeChild(node);
143: };
144: }
145:
146: this._setClass = function(el, classname) {
147: var parent = el.parentNode;
148: if (parent.tagName=='DIV') {
149: // fixup buggy formatting
150: var gp = parent.parentNode;
151: if (el != parent.firstChild) {
152: var previous = parent.cloneNode(false);
153: while (el != parent.firstChild) {
154: previous.appendChild(parent.firstChild);
155: }
156: gp.insertBefore(previous, parent);
157: this._cleanNode(previous);
158: }
159: gp.insertBefore(el, parent);
160: this._cleanNode(el);
161: this._cleanNode(parent);
162: }
163: // now set the classname
164: if (classname) {
165: el.className = classname;
166: } else {
167: el.removeAttribute(el.className ?"className":"class");
168: }
169: }
170: this.setTextStyle = function(style, noupdate) {
171: /* parse the argument into a type and classname part
172: generate a block element accordingly
173: */
174: var classname = '';
175: var eltype = style.toUpperCase();
176: if (style.indexOf('|') > -1) {
177: style = style.split('|');
178: eltype = style[0].toUpperCase();
179: classname = style[1];
180: };
181:
182: var command = eltype;
183: // first create the element, then find it and set the classname
184: if (this.editor.getBrowserName() == 'IE') {
185: command = '<' + eltype + '>';
186: };
187: this.editor.getDocument().execCommand('formatblock', command);
188:
189: // now get a reference to the element just added
190: var selNode = this.editor.getSelectedNode();
191: var el = this.editor.getNearestParentOfType(selNode, eltype);
192: if (el) {
193: this._setClass(el, classname);
194: } else {
195: var selection = this.editor.getSelection();
196: var elements = selNode.getElementsByTagName(eltype);
197: for (var i = 0; i < elements.length; i++) {
198: el = elements[i];
199: if (selection.containsNode(el)) {
200: this._setClass(el, classname);
201: }
202: }
203: }
204: if (el) {
205: this.editor.getSelection().selectNodeContents(el);
206: }
207: if (!noupdate) {
208: this.editor.updateState();
209: }
210: };
211: };
212:
213: PloneKupuUI.prototype = new KupuUI;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>