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