1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * FCKToolbarPanelButton Class: Handles the Fonts combo selector.
22 */
23
24var FCKToolbarStyleCombo = function( tooltip, style )
25{
26	if ( tooltip === false )
27		return ;
28
29	this.CommandName = 'Style' ;
30	this.Label		= this.GetLabel() ;
31	this.Tooltip	= tooltip ? tooltip : this.Label ;
32	this.Style		= style ? style : FCK_TOOLBARITEM_ICONTEXT ;
33
34	this.DefaultLabel = FCKConfig.DefaultStyleLabel || '' ;
35}
36
37// Inherit from FCKToolbarSpecialCombo.
38FCKToolbarStyleCombo.prototype = new FCKToolbarSpecialCombo ;
39
40FCKToolbarStyleCombo.prototype.GetLabel = function()
41{
42	return FCKLang.Style ;
43}
44
45FCKToolbarStyleCombo.prototype.GetStyles = function()
46{
47	var styles = {} ;
48	var allStyles = FCK.ToolbarSet.CurrentInstance.Styles.GetStyles() ;
49
50	for ( var styleName in allStyles )
51	{
52		var style = allStyles[ styleName ] ;
53		if ( !style.IsCore )
54			styles[ styleName ] = style ;
55	}
56	return styles ;
57}
58
59FCKToolbarStyleCombo.prototype.CreateItems = function( targetSpecialCombo )
60{
61	var targetDoc = targetSpecialCombo._Panel.Document ;
62
63	// Add the Editor Area CSS to the panel so the style classes are previewed correctly.
64	FCKTools.AppendStyleSheet( targetDoc, FCKConfig.ToolbarComboPreviewCSS ) ;
65	FCKTools.AppendStyleString( targetDoc, FCKConfig.EditorAreaStyles ) ;
66	targetDoc.body.className += ' ForceBaseFont' ;
67
68	// Add ID and Class to the body.
69	FCKConfig.ApplyBodyAttributes( targetDoc.body ) ;
70
71	// Get the styles list.
72	var styles = this.GetStyles() ;
73
74	for ( var styleName in styles )
75	{
76		var style = styles[ styleName ] ;
77
78		// Object type styles have no preview.
79		var caption = style.GetType() == FCK_STYLE_OBJECT ?
80			styleName :
81			FCKToolbarStyleCombo_BuildPreview( style, style.Label || styleName ) ;
82
83		var item = targetSpecialCombo.AddItem( styleName, caption ) ;
84
85		item.Style = style ;
86	}
87
88	// We must prepare the list before showing it.
89	targetSpecialCombo.OnBeforeClick = this.StyleCombo_OnBeforeClick ;
90}
91
92FCKToolbarStyleCombo.prototype.RefreshActiveItems = function( targetSpecialCombo )
93{
94	var startElement = FCK.ToolbarSet.CurrentInstance.Selection.GetBoundaryParentElement( true ) ;
95
96	if ( startElement )
97	{
98		var path = new FCKElementPath( startElement ) ;
99		var elements = path.Elements ;
100
101		for ( var e = 0 ; e < elements.length ; e++ )
102		{
103			for ( var i in targetSpecialCombo.Items )
104			{
105				var item = targetSpecialCombo.Items[i] ;
106				var style = item.Style ;
107
108				if ( style.CheckElementRemovable( elements[ e ], true ) )
109				{
110					targetSpecialCombo.SetLabel( style.Label || style.Name ) ;
111					return ;
112				}
113			}
114		}
115	}
116
117	targetSpecialCombo.SetLabel( this.DefaultLabel ) ;
118}
119
120FCKToolbarStyleCombo.prototype.StyleCombo_OnBeforeClick = function( targetSpecialCombo )
121{
122	// Two things are done here:
123	//	- In a control selection, get the element name, so we'll display styles
124	//	  for that element only.
125	//	- Select the styles that are active for the current selection.
126
127	// Clear the current selection.
128	targetSpecialCombo.DeselectAll() ;
129
130	var startElement ;
131	var path ;
132	var tagName ;
133
134	var selection = FCK.ToolbarSet.CurrentInstance.Selection ;
135
136	if ( selection.GetType() == 'Control' )
137	{
138		startElement = selection.GetSelectedElement() ;
139		tagName = startElement.nodeName.toLowerCase() ;
140	}
141	else
142	{
143		startElement = selection.GetBoundaryParentElement( true ) ;
144		path = new FCKElementPath( startElement ) ;
145	}
146
147	for ( var i in targetSpecialCombo.Items )
148	{
149		var item = targetSpecialCombo.Items[i] ;
150		var style = item.Style ;
151
152		if ( ( tagName && style.Element == tagName ) || ( !tagName && style.GetType() != FCK_STYLE_OBJECT ) )
153		{
154			item.style.display = '' ;
155
156			if ( ( path && style.CheckActive( path ) ) || ( !path && style.CheckElementRemovable( startElement, true ) ) )
157				targetSpecialCombo.SelectItem( style.Name ) ;
158		}
159		else
160			item.style.display = 'none' ;
161	}
162}
163
164function FCKToolbarStyleCombo_BuildPreview( style, caption )
165{
166	var styleType = style.GetType() ;
167	var html = [] ;
168
169	if ( styleType == FCK_STYLE_BLOCK )
170		html.push( '<div class="BaseFont">' ) ;
171
172	var elementName = style.Element ;
173
174	// Avoid <bdo> in the preview.
175	if ( elementName == 'bdo' )
176		elementName = 'span' ;
177
178	html = [ '<', elementName ] ;
179
180	// Assign all defined attributes.
181	var attribs	= style._StyleDesc.Attributes ;
182	if ( attribs )
183	{
184		for ( var att in attribs )
185		{
186			html.push( ' ', att, '="', style.GetFinalAttributeValue( att ), '"' ) ;
187		}
188	}
189
190	// Assign the style attribute.
191	if ( style._GetStyleText().length > 0 )
192		html.push( ' style="', style.GetFinalStyleValue(), '"' ) ;
193
194	html.push( '>', caption, '</', elementName, '>' ) ;
195
196	if ( styleType == FCK_STYLE_BLOCK )
197		html.push( '</div>' ) ;
198
199	return html.join( '' ) ;
200}