JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / stylescombo / plugin.js
index 2b4716a..086f109 100644 (file)
@@ -1,12 +1,10 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
 (function()\r
 {\r
-       var stylesManager;\r
-\r
        CKEDITOR.plugins.add( 'stylescombo',\r
        {\r
                requires : [ 'richcombo', 'styles' ],\r
@@ -15,20 +13,40 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        var config = editor.config,\r
                                lang = editor.lang.stylesCombo,\r
-                               pluginPath = this.path,\r
-                               styles;\r
+                               styles = {},\r
+                               stylesList = [],\r
+                               combo;\r
+\r
+                       function loadStylesSet( callback )\r
+                       {\r
+                               editor.getStylesSet( function( stylesDefinitions )\r
+                               {\r
+                                       if ( !stylesList.length )\r
+                                       {\r
+                                               var style,\r
+                                                       styleName;\r
+\r
+                                               // Put all styles into an Array.\r
+                                               for ( var i = 0, count = stylesDefinitions.length ; i < count ; i++ )\r
+                                               {\r
+                                                       var styleDefinition = stylesDefinitions[ i ];\r
 \r
-                       if ( !stylesManager )\r
-                               stylesManager = CKEDITOR.stylesSet;\r
+                                                       styleName = styleDefinition.name;\r
 \r
-                       var comboStylesSet = config.stylesCombo_stylesSet.split( ':' ),\r
-                               styleSetName = comboStylesSet[ 0 ],\r
-                               externalPath = comboStylesSet[ 1 ];\r
+                                                       style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );\r
+                                                       style._name = styleName;\r
+                                                       style._.enterMode = config.enterMode;\r
 \r
-                       stylesManager.addExternal( styleSetName,\r
-                                       externalPath ?\r
-                                               comboStylesSet.slice( 1 ).join( ':' ) :\r
-                                               pluginPath + 'styles/' + styleSetName + '.js', '' );\r
+                                                       stylesList.push( style );\r
+                                               }\r
+\r
+                                               // Sorts the Array, so the styles get grouped by type.\r
+                                               stylesList.sort( sortStyles );\r
+                                       }\r
+\r
+                                       callback && callback();\r
+                               });\r
+                       }\r
 \r
                        editor.ui.addRichCombo( 'Styles',\r
                                {\r
@@ -45,43 +63,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        init : function()\r
                                        {\r
-                                               var combo = this;\r
+                                               combo = this;\r
 \r
-                                               CKEDITOR.stylesSet.load( styleSetName, function( stylesSet )\r
+                                               loadStylesSet( function()\r
                                                        {\r
-                                                               var stylesDefinitions = stylesSet[ styleSetName ],\r
-                                                                       style,\r
+                                                               var style,\r
                                                                        styleName,\r
-                                                                       stylesList = [];\r
-\r
-                                                               styles = {};\r
-\r
-                                                               // Put all styles into an Array.\r
-                                                               for ( var i = 0 ; i < stylesDefinitions.length ; i++ )\r
-                                                               {\r
-                                                                       var styleDefinition = stylesDefinitions[ i ];\r
-\r
-                                                                       styleName = styleDefinition.name;\r
-\r
-                                                                       style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );\r
-                                                                       style._name = styleName;\r
-\r
-                                                                       stylesList.push( style );\r
-                                                               }\r
-\r
-                                                               // Sorts the Array, so the styles get grouped\r
-                                                               // by type.\r
-                                                               stylesList.sort( sortStyles );\r
+                                                                       lastType,\r
+                                                                       type,\r
+                                                                       i,\r
+                                                                       count;\r
 \r
                                                                // Loop over the Array, adding all items to the\r
                                                                // combo.\r
-                                                               var lastType;\r
-                                                               for ( i = 0 ; i < stylesList.length ; i++ )\r
+                                                               for ( i = 0, count = stylesList.length ; i < count ; i++ )\r
                                                                {\r
                                                                        style = stylesList[ i ];\r
                                                                        styleName = style._name;\r
-\r
-                                                                       var type = style.type;\r
+                                                                       type = style.type;\r
 \r
                                                                        if ( type != lastType )\r
                                                                        {\r
@@ -91,13 +90,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                        combo.add(\r
                                                                                styleName,\r
-                                                                               style.type == CKEDITOR.STYLE_OBJECT ? styleName : buildPreview( style._.definition ),\r
+                                                                               style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(),\r
                                                                                styleName );\r
                                                                }\r
 \r
                                                                combo.commit();\r
 \r
-                                                               combo.onOpen();\r
                                                        });\r
                                        },\r
 \r
@@ -107,23 +105,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                editor.fire( 'saveSnapshot' );\r
 \r
                                                var style = styles[ value ],\r
-                                                       selection = editor.getSelection();\r
-\r
-                                               if ( style.type == CKEDITOR.STYLE_OBJECT )\r
-                                               {\r
-                                                       var element = selection.getSelectedElement();\r
-                                                       if ( element )\r
-                                                               style.applyToObject( element );\r
+                                                       selection = editor.getSelection(),\r
+                                                       elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );\r
 \r
-                                                       return;\r
-                                               }\r
-\r
-                                               var elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );\r
-\r
-                                               if ( style.type == CKEDITOR.STYLE_INLINE && style.checkActive( elementPath ) )\r
-                                                       style.remove( editor.document );\r
-                                               else\r
-                                                       style.apply( editor.document );\r
+                                               style[ style.checkActive( elementPath ) ? 'remove' : 'apply' ]( editor.document );\r
 \r
                                                editor.fire( 'saveSnapshot' );\r
                                        },\r
@@ -132,13 +117,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                editor.on( 'selectionChange', function( ev )\r
                                                        {\r
-                                                               var currentValue = this.getValue();\r
-\r
-                                                               var elementPath = ev.data.path,\r
+                                                               var currentValue = this.getValue(),\r
+                                                                       elementPath = ev.data.path,\r
                                                                        elements = elementPath.elements;\r
 \r
                                                                // For each element into the elements path.\r
-                                                               for ( var i = 0, element ; i < elements.length ; i++ )\r
+                                                               for ( var i = 0, count = elements.length, element ; i < count ; i++ )\r
                                                                {\r
                                                                        element = elements[i];\r
 \r
@@ -163,16 +147,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        onOpen : function()\r
                                        {\r
-                                               if ( CKEDITOR.env.ie )\r
+                                               if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )\r
                                                        editor.focus();\r
 \r
-                                               var selection = editor.getSelection();\r
+                                               var selection = editor.getSelection(),\r
+                                                       element = selection.getSelectedElement(),\r
+                                                       elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() ),\r
+                                                       counter = [ 0, 0, 0, 0 ];\r
 \r
-                                               var element = selection.getSelectedElement(),\r
-                                                       elementName = element && element.getName(),\r
-                                                       elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() );\r
-\r
-                                               var counter = [ 0, 0, 0, 0 ];\r
                                                this.showAll();\r
                                                this.unmarkAll();\r
                                                for ( var name in styles )\r
@@ -180,25 +162,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        var style = styles[ name ],\r
                                                                type = style.type;\r
 \r
-                                                       if ( type == CKEDITOR.STYLE_OBJECT )\r
+                                                       if ( style.checkActive( elementPath ) )\r
+                                                               this.mark( name );\r
+                                                       else if ( type == CKEDITOR.STYLE_OBJECT && !style.checkApplicable( elementPath ) )\r
                                                        {\r
-                                                               if ( element && style.element == elementName )\r
-                                                               {\r
-                                                                       if ( style.checkElementRemovable( element, true ) )\r
-                                                                               this.mark( name );\r
-\r
-                                                                       counter[ type ]++;\r
-                                                               }\r
-                                                               else\r
-                                                                       this.hideItem( name );\r
+                                                               this.hideItem( name );\r
+                                                               counter[ type ]--;\r
                                                        }\r
-                                                       else\r
-                                                       {\r
-                                                               if ( style.checkActive( elementPath ) )\r
-                                                                       this.mark( name );\r
 \r
-                                                               counter[ type ]++;\r
-                                                       }\r
+                                                       counter[ type ]++;\r
                                                }\r
 \r
                                                if ( !counter[ CKEDITOR.STYLE_BLOCK ] )\r
@@ -209,42 +181,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                if ( !counter[ CKEDITOR.STYLE_OBJECT ] )\r
                                                        this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );\r
+                                       },\r
+\r
+                                       // Force a reload of the data\r
+                                       reset: function()\r
+                                       {\r
+                                               if ( combo )\r
+                                               {\r
+                                                       delete combo._.panel;\r
+                                                       delete combo._.list;\r
+                                                       combo._.committed = 0;\r
+                                                       combo._.items = {};\r
+                                                       combo._.state = CKEDITOR.TRISTATE_OFF;\r
+                                               }\r
+                                               styles = {};\r
+                                               stylesList = [];\r
+                                               loadStylesSet();\r
                                        }\r
                                });\r
-               }\r
-       });\r
 \r
-       function buildPreview( styleDefinition )\r
-       {\r
-               var html = [];\r
-\r
-               var elementName = styleDefinition.element;\r
-\r
-               // Avoid <bdo> in the preview.\r
-               if ( elementName == 'bdo' )\r
-                       elementName = 'span';\r
-\r
-               html = [ '<', elementName ];\r
-\r
-               // Assign all defined attributes.\r
-               var attribs     = styleDefinition.attributes;\r
-               if ( attribs )\r
-               {\r
-                       for ( var att in attribs )\r
-                       {\r
-                               html.push( ' ', att, '="', attribs[ att ], '"' );\r
-                       }\r
+                       editor.on( 'instanceReady', function() { loadStylesSet(); } );\r
                }\r
-\r
-               // Assign the style attribute.\r
-               var cssStyle = CKEDITOR.style.getStyleText( styleDefinition );\r
-               if ( cssStyle )\r
-                       html.push( ' style="', cssStyle, '"' );\r
-\r
-               html.push( '>', styleDefinition.name, '</', elementName, '>' );\r
-\r
-               return html.join( '' );\r
-       }\r
+       });\r
 \r
        function sortStyles( styleA, styleB )\r
        {\r
@@ -258,25 +216,3 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        -1;\r
        }\r
 })();\r
-\r
-/**\r
- * The "styles definition set" to load into the styles combo. The styles may\r
- * be defined in the page containing the editor, or can be loaded on demand\r
- * from an external file when opening the styles combo for the fist time. In\r
- * the second case, if this setting contains only a name, the styles definition\r
- * file will be loaded from the "styles" folder inside the stylescombo plugin\r
- * folder. Otherwise, this setting has the "name:url" syntax, making it\r
- * possible to set the URL from which loading the styles file.\r
- * @type string\r
- * @default 'default'\r
- * @example\r
- * // Load from the stylescombo styles folder (mystyles.js file).\r
- * config.stylesCombo_stylesSet = 'mystyles';\r
- * @example\r
- * // Load from a relative URL.\r
- * config.stylesCombo_stylesSet = 'mystyles:/editorstyles/styles.js';\r
- * @example\r
- * // Load from a full URL.\r
- * config.stylesCombo_stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';\r
- */\r
-CKEDITOR.config.stylesCombo_stylesSet = 'default';\r