JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / stylescombo / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         CKEDITOR.plugins.add( 'stylescombo',\r
9         {\r
10                 requires : [ 'richcombo', 'styles' ],\r
11 \r
12                 init : function( editor )\r
13                 {\r
14                         var config = editor.config,\r
15                                 lang = editor.lang.stylesCombo,\r
16                                 styles = {},\r
17                                 stylesList = [];\r
18 \r
19                         function loadStylesSet( callback )\r
20                         {\r
21                                 editor.getStylesSet( function( stylesDefinitions )\r
22                                 {\r
23                                         if ( !stylesList.length )\r
24                                         {\r
25                                                 var style,\r
26                                                         styleName;\r
27 \r
28                                                 // Put all styles into an Array.\r
29                                                 for ( var i = 0 ; i < stylesDefinitions.length ; i++ )\r
30                                                 {\r
31                                                         var styleDefinition = stylesDefinitions[ i ];\r
32 \r
33                                                         styleName = styleDefinition.name;\r
34 \r
35                                                         style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );\r
36                                                         style._name = styleName;\r
37                                                         style._.enterMode = config.enterMode;\r
38 \r
39                                                         stylesList.push( style );\r
40                                                 }\r
41 \r
42                                                 // Sorts the Array, so the styles get grouped by type.\r
43                                                 stylesList.sort( sortStyles );\r
44                                         }\r
45 \r
46                                         callback && callback();\r
47                                 });\r
48                         }\r
49 \r
50                         editor.ui.addRichCombo( 'Styles',\r
51                                 {\r
52                                         label : lang.label,\r
53                                         title : lang.panelTitle,\r
54                                         className : 'cke_styles',\r
55 \r
56                                         panel :\r
57                                         {\r
58                                                 css : editor.skin.editor.css.concat( config.contentsCss ),\r
59                                                 multiSelect : true,\r
60                                                 attributes : { 'aria-label' : lang.panelTitle }\r
61                                         },\r
62 \r
63                                         init : function()\r
64                                         {\r
65                                                 var combo = this;\r
66 \r
67                                                 loadStylesSet( function()\r
68                                                         {\r
69                                                                 var style, styleName;\r
70 \r
71                                                                 // Loop over the Array, adding all items to the\r
72                                                                 // combo.\r
73                                                                 var lastType;\r
74                                                                 for ( var i = 0 ; i < stylesList.length ; i++ )\r
75                                                                 {\r
76                                                                         style = stylesList[ i ];\r
77                                                                         styleName = style._name;\r
78 \r
79                                                                         var type = style.type;\r
80 \r
81                                                                         if ( type != lastType )\r
82                                                                         {\r
83                                                                                 combo.startGroup( lang[ 'panelTitle' + String( type ) ] );\r
84                                                                                 lastType = type;\r
85                                                                         }\r
86 \r
87                                                                         combo.add(\r
88                                                                                 styleName,\r
89                                                                                 style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(),\r
90                                                                                 styleName );\r
91                                                                 }\r
92 \r
93                                                                 combo.commit();\r
94 \r
95                                                                 combo.onOpen();\r
96                                                         });\r
97                                         },\r
98 \r
99                                         onClick : function( value )\r
100                                         {\r
101                                                 editor.focus();\r
102                                                 editor.fire( 'saveSnapshot' );\r
103 \r
104                                                 var style = styles[ value ],\r
105                                                         selection = editor.getSelection();\r
106 \r
107                                                 var elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );\r
108 \r
109                                                 if ( style.type == CKEDITOR.STYLE_INLINE && style.checkActive( elementPath ) )\r
110                                                         style.remove( editor.document );\r
111                                                 else\r
112                                                         style.apply( editor.document );\r
113 \r
114                                                 editor.fire( 'saveSnapshot' );\r
115                                         },\r
116 \r
117                                         onRender : function()\r
118                                         {\r
119                                                 editor.on( 'selectionChange', function( ev )\r
120                                                         {\r
121                                                                 var currentValue = this.getValue();\r
122 \r
123                                                                 var elementPath = ev.data.path,\r
124                                                                         elements = elementPath.elements;\r
125 \r
126                                                                 // For each element into the elements path.\r
127                                                                 for ( var i = 0, element ; i < elements.length ; i++ )\r
128                                                                 {\r
129                                                                         element = elements[i];\r
130 \r
131                                                                         // Check if the element is removable by any of\r
132                                                                         // the styles.\r
133                                                                         for ( var value in styles )\r
134                                                                         {\r
135                                                                                 if ( styles[ value ].checkElementRemovable( element, true ) )\r
136                                                                                 {\r
137                                                                                         if ( value != currentValue )\r
138                                                                                                 this.setValue( value );\r
139                                                                                         return;\r
140                                                                                 }\r
141                                                                         }\r
142                                                                 }\r
143 \r
144                                                                 // If no styles match, just empty it.\r
145                                                                 this.setValue( '' );\r
146                                                         },\r
147                                                         this);\r
148                                         },\r
149 \r
150                                         onOpen : function()\r
151                                         {\r
152                                                 if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )\r
153                                                         editor.focus();\r
154 \r
155                                                 var selection = editor.getSelection();\r
156 \r
157                                                 var element = selection.getSelectedElement(),\r
158                                                         elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() );\r
159 \r
160                                                 var counter = [ 0, 0, 0, 0 ];\r
161                                                 this.showAll();\r
162                                                 this.unmarkAll();\r
163                                                 for ( var name in styles )\r
164                                                 {\r
165                                                         var style = styles[ name ],\r
166                                                                 type = style.type;\r
167 \r
168                                                         if ( style.checkActive( elementPath ) )\r
169                                                                 this.mark( name );\r
170                                                         else if ( type == CKEDITOR.STYLE_OBJECT && !style.checkApplicable( elementPath ) )\r
171                                                         {\r
172                                                                 this.hideItem( name );\r
173                                                                 counter[ type ]--;\r
174                                                         }\r
175 \r
176                                                         counter[ type ]++;\r
177                                                 }\r
178 \r
179                                                 if ( !counter[ CKEDITOR.STYLE_BLOCK ] )\r
180                                                         this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );\r
181 \r
182                                                 if ( !counter[ CKEDITOR.STYLE_INLINE ] )\r
183                                                         this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );\r
184 \r
185                                                 if ( !counter[ CKEDITOR.STYLE_OBJECT ] )\r
186                                                         this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );\r
187                                         }\r
188                                 });\r
189 \r
190                         editor.on( 'instanceReady', function() { loadStylesSet(); } );\r
191                 }\r
192         });\r
193 \r
194         function sortStyles( styleA, styleB )\r
195         {\r
196                 var typeA = styleA.type,\r
197                         typeB = styleB.type;\r
198 \r
199                 return typeA == typeB ? 0 :\r
200                         typeA == CKEDITOR.STYLE_OBJECT ? -1 :\r
201                         typeB == CKEDITOR.STYLE_OBJECT ? 1 :\r
202                         typeB == CKEDITOR.STYLE_BLOCK ? 1 :\r
203                         -1;\r
204         }\r
205 })();\r