]>
Commit | Line | Data |
---|---|---|
7adcb81e | 1 | /**\r |
3b35bd27 | 2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.\r |
7adcb81e IB |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license\r |
4 | */\r | |
5 | \r | |
6 | ( function() {\r | |
7 | 'use strict';\r | |
8 | \r | |
9 | CKEDITOR.plugins.add( 'stylescombo', {\r | |
10 | requires: 'richcombo',\r | |
11 | // jscs:disable maximumLineLength\r | |
3b35bd27 | 12 | lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r |
7adcb81e IB |
13 | // jscs:enable maximumLineLength\r |
14 | \r | |
15 | init: function( editor ) {\r | |
16 | var config = editor.config,\r | |
17 | lang = editor.lang.stylescombo,\r | |
18 | styles = {},\r | |
19 | stylesList = [],\r | |
20 | combo,\r | |
21 | allowedContent = [];\r | |
22 | \r | |
23 | editor.on( 'stylesSet', function( evt ) {\r | |
24 | var stylesDefinitions = evt.data.styles;\r | |
25 | \r | |
26 | if ( !stylesDefinitions )\r | |
27 | return;\r | |
28 | \r | |
29 | var style, styleName, styleType;\r | |
30 | \r | |
31 | // Put all styles into an Array.\r | |
32 | for ( var i = 0, count = stylesDefinitions.length; i < count; i++ ) {\r | |
33 | var styleDefinition = stylesDefinitions[ i ];\r | |
34 | \r | |
35 | if ( editor.blockless && ( styleDefinition.element in CKEDITOR.dtd.$block ) )\r | |
36 | continue;\r | |
37 | \r | |
38 | styleName = styleDefinition.name;\r | |
39 | style = new CKEDITOR.style( styleDefinition );\r | |
40 | \r | |
41 | if ( !editor.filter.customConfig || editor.filter.check( style ) ) {\r | |
42 | style._name = styleName;\r | |
43 | style._.enterMode = config.enterMode;\r | |
44 | // Get the type (which will be used to assign style to one of 3 groups) from assignedTo if it's defined.\r | |
45 | style._.type = styleType = style.assignedTo || style.type;\r | |
46 | \r | |
47 | // Weight is used to sort styles (#9029).\r | |
48 | style._.weight = i + ( styleType == CKEDITOR.STYLE_OBJECT ? 1 : styleType == CKEDITOR.STYLE_BLOCK ? 2 : 3 ) * 1000;\r | |
49 | \r | |
50 | styles[ styleName ] = style;\r | |
51 | stylesList.push( style );\r | |
52 | allowedContent.push( style );\r | |
53 | }\r | |
54 | }\r | |
55 | \r | |
56 | // Sorts the Array, so the styles get grouped by type in proper order (#9029).\r | |
57 | stylesList.sort( function( styleA, styleB ) {\r | |
58 | return styleA._.weight - styleB._.weight;\r | |
59 | } );\r | |
60 | } );\r | |
61 | \r | |
62 | editor.ui.addRichCombo( 'Styles', {\r | |
63 | label: lang.label,\r | |
64 | title: lang.panelTitle,\r | |
65 | toolbar: 'styles,10',\r | |
66 | allowedContent: allowedContent,\r | |
67 | \r | |
68 | panel: {\r | |
69 | css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),\r | |
70 | multiSelect: true,\r | |
71 | attributes: { 'aria-label': lang.panelTitle }\r | |
72 | },\r | |
73 | \r | |
74 | init: function() {\r | |
75 | var style, styleName, lastType, type, i, count;\r | |
76 | \r | |
77 | // Loop over the Array, adding all items to the\r | |
78 | // combo.\r | |
79 | for ( i = 0, count = stylesList.length; i < count; i++ ) {\r | |
80 | style = stylesList[ i ];\r | |
81 | styleName = style._name;\r | |
82 | type = style._.type;\r | |
83 | \r | |
84 | if ( type != lastType ) {\r | |
85 | this.startGroup( lang[ 'panelTitle' + String( type ) ] );\r | |
86 | lastType = type;\r | |
87 | }\r | |
88 | \r | |
89 | this.add( styleName, style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(), styleName );\r | |
90 | }\r | |
91 | \r | |
92 | this.commit();\r | |
93 | },\r | |
94 | \r | |
95 | onClick: function( value ) {\r | |
96 | editor.focus();\r | |
97 | editor.fire( 'saveSnapshot' );\r | |
98 | \r | |
99 | var style = styles[ value ],\r | |
100 | elementPath = editor.elementPath();\r | |
101 | \r | |
102 | editor[ style.checkActive( elementPath, editor ) ? 'removeStyle' : 'applyStyle' ]( style );\r | |
103 | editor.fire( 'saveSnapshot' );\r | |
104 | },\r | |
105 | \r | |
106 | onRender: function() {\r | |
107 | editor.on( 'selectionChange', function( ev ) {\r | |
108 | var currentValue = this.getValue(),\r | |
109 | elementPath = ev.data.path,\r | |
110 | elements = elementPath.elements;\r | |
111 | \r | |
112 | // For each element into the elements path.\r | |
113 | for ( var i = 0, count = elements.length, element; i < count; i++ ) {\r | |
114 | element = elements[ i ];\r | |
115 | \r | |
116 | // Check if the element is removable by any of\r | |
117 | // the styles.\r | |
118 | for ( var value in styles ) {\r | |
119 | if ( styles[ value ].checkElementRemovable( element, true, editor ) ) {\r | |
120 | if ( value != currentValue )\r | |
121 | this.setValue( value );\r | |
122 | return;\r | |
123 | }\r | |
124 | }\r | |
125 | }\r | |
126 | \r | |
127 | // If no styles match, just empty it.\r | |
128 | this.setValue( '' );\r | |
129 | }, this );\r | |
130 | },\r | |
131 | \r | |
132 | onOpen: function() {\r | |
133 | var selection = editor.getSelection(),\r | |
134 | element = selection.getSelectedElement(),\r | |
135 | elementPath = editor.elementPath( element ),\r | |
136 | counter = [ 0, 0, 0, 0 ];\r | |
137 | \r | |
138 | this.showAll();\r | |
139 | this.unmarkAll();\r | |
140 | for ( var name in styles ) {\r | |
141 | var style = styles[ name ],\r | |
142 | type = style._.type;\r | |
143 | \r | |
144 | if ( style.checkApplicable( elementPath, editor, editor.activeFilter ) )\r | |
145 | counter[ type ]++;\r | |
146 | else\r | |
147 | this.hideItem( name );\r | |
148 | \r | |
149 | if ( style.checkActive( elementPath, editor ) )\r | |
150 | this.mark( name );\r | |
151 | }\r | |
152 | \r | |
153 | if ( !counter[ CKEDITOR.STYLE_BLOCK ] )\r | |
154 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );\r | |
155 | \r | |
156 | if ( !counter[ CKEDITOR.STYLE_INLINE ] )\r | |
157 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );\r | |
158 | \r | |
159 | if ( !counter[ CKEDITOR.STYLE_OBJECT ] )\r | |
160 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );\r | |
161 | },\r | |
162 | \r | |
163 | refresh: function() {\r | |
164 | var elementPath = editor.elementPath();\r | |
165 | \r | |
166 | if ( !elementPath )\r | |
167 | return;\r | |
168 | \r | |
169 | for ( var name in styles ) {\r | |
170 | var style = styles[ name ];\r | |
171 | \r | |
172 | if ( style.checkApplicable( elementPath, editor, editor.activeFilter ) )\r | |
173 | return;\r | |
174 | }\r | |
175 | this.setState( CKEDITOR.TRISTATE_DISABLED );\r | |
176 | },\r | |
177 | \r | |
178 | // Force a reload of the data\r | |
179 | reset: function() {\r | |
180 | if ( combo ) {\r | |
181 | delete combo._.panel;\r | |
182 | delete combo._.list;\r | |
183 | combo._.committed = 0;\r | |
184 | combo._.items = {};\r | |
185 | combo._.state = CKEDITOR.TRISTATE_OFF;\r | |
186 | }\r | |
187 | styles = {};\r | |
188 | stylesList = [];\r | |
189 | }\r | |
190 | } );\r | |
191 | }\r | |
192 | } );\r | |
193 | } )();\r |