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