X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fimage%2Fdialogs%2Fimage.js;h=496827bc190d4646e14620450d789e60c6d47be5;hb=fb481ba0a7d298e3e7b9034fcb9f2afdc6e8e796;hp=c65a8aa1c0c8e1e15249b661699f9365ad9d9ce6;hpb=8761695d9b70afe75905deaac88f78c1f8aeb32d;p=ckeditor.git diff --git a/_source/plugins/image/dialogs/image.js b/_source/plugins/image/dialogs/image.js index c65a8aa..496827b 100644 --- a/_source/plugins/image/dialogs/image.js +++ b/_source/plugins/image/dialogs/image.js @@ -1,150 +1,222 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ (function() { - // Load image preview. - var IMAGE = 1, - LINK = 2, - PREVIEW = 4, - CLEANUP = 8, - regexGetSize = /^\s*(\d+)((px)|\%)?\s*$/i, - regexGetSizeOrEmpty = /(^\s*(\d+)((px)|\%)?\s*$)|^$/i; - - var onSizeChange = function() + var imageDialog = function( editor, dialogType ) { - var value = this.getValue(), // This = input element. - dialog = this.getDialog(), - aMatch = value.match( regexGetSize ); // Check value - if ( aMatch ) - { - if ( aMatch[2] == '%' ) // % is allowed - > unlock ratio. - switchLockRatio( dialog, false ); // Unlock. - value = aMatch[1]; - } + // Load image preview. + var IMAGE = 1, + LINK = 2, + PREVIEW = 4, + CLEANUP = 8, + regexGetSize = /^\s*(\d+)((px)|\%)?\s*$/i, + regexGetSizeOrEmpty = /(^\s*(\d+)((px)|\%)?\s*$)|^$/i, + pxLengthRegex = /^\d+px$/; - // Only if ratio is locked - if ( dialog.lockRatio ) + var onSizeChange = function() { - var oImageOriginal = dialog.originalElement; - if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) + var value = this.getValue(), // This = input element. + dialog = this.getDialog(), + aMatch = value.match( regexGetSize ); // Check value + if ( aMatch ) { - if ( this.id == 'txtHeight' ) + if ( aMatch[2] == '%' ) // % is allowed - > unlock ratio. + switchLockRatio( dialog, false ); // Unlock. + value = aMatch[1]; + } + + // Only if ratio is locked + if ( dialog.lockRatio ) + { + var oImageOriginal = dialog.originalElement; + if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) { - if ( value && value != '0' ) - value = Math.round( oImageOriginal.$.width * ( value / oImageOriginal.$.height ) ); - if ( !isNaN( value ) ) - dialog.setValueOf( 'info', 'txtWidth', value ); + if ( this.id == 'txtHeight' ) + { + if ( value && value != '0' ) + value = Math.round( oImageOriginal.$.width * ( value / oImageOriginal.$.height ) ); + if ( !isNaN( value ) ) + dialog.setValueOf( 'info', 'txtWidth', value ); + } + else //this.id = txtWidth. + { + if ( value && value != '0' ) + value = Math.round( oImageOriginal.$.height * ( value / oImageOriginal.$.width ) ); + if ( !isNaN( value ) ) + dialog.setValueOf( 'info', 'txtHeight', value ); + } } - else //this.id = txtWidth. + } + updatePreview( dialog ); + }; + + var updatePreview = function( dialog ) + { + //Don't load before onShow. + if ( !dialog.originalElement || !dialog.preview ) + return 1; + + // Read attributes and update imagePreview; + dialog.commitContent( PREVIEW, dialog.preview ); + return 0; + }; + + // Custom commit dialog logic, where we're intended to give inline style + // field (txtdlgGenStyle) higher priority to avoid overwriting styles contribute + // by other fields. + function commitContent() + { + var args = arguments; + var inlineStyleField = this.getContentElement( 'advanced', 'txtdlgGenStyle' ); + inlineStyleField && inlineStyleField.commit.apply( inlineStyleField, args ); + + this.foreach( function( widget ) + { + if ( widget.commit && widget.id != 'txtdlgGenStyle' ) + widget.commit.apply( widget, args ); + }); + } + + // Avoid recursions. + var incommit; + + // Synchronous field values to other impacted fields is required, e.g. border + // size change should alter inline-style text as well. + function commitInternally( targetFields ) + { + if ( incommit ) + return; + + incommit = 1; + + var dialog = this.getDialog(), + element = dialog.imageElement; + if ( element ) + { + // Commit this field and broadcast to target fields. + this.commit( IMAGE, element ); + + targetFields = [].concat( targetFields ); + var length = targetFields.length, + field; + for ( var i = 0; i < length; i++ ) { - if ( value && value != '0' ) - value = Math.round( oImageOriginal.$.height * ( value / oImageOriginal.$.width ) ); - if ( !isNaN( value ) ) - dialog.setValueOf( 'info', 'txtHeight', value ); + field = dialog.getContentElement.apply( dialog, targetFields[ i ].split( ':' ) ); + // May cause recursion. + field && field.setup( IMAGE, element ); } } + + incommit = 0; } - updatePreview( dialog ); - }; - var updatePreview = function( dialog ) - { - //Don't load before onShow. - if ( !dialog.originalElement || !dialog.preview ) - return 1; + var switchLockRatio = function( dialog, value ) + { + if ( !dialog.getContentElement( 'info', 'ratioLock' ) ) + return null; - // Read attributes and update imagePreview; - dialog.commitContent( PREVIEW, dialog.preview ); - return 0; - }; + var oImageOriginal = dialog.originalElement; - var switchLockRatio = function( dialog, value ) - { - var oImageOriginal = dialog.originalElement, - ratioButton = CKEDITOR.document.getById( 'btnLockSizes' ); + // Dialog may already closed. (#5505) + if( !oImageOriginal ) + return null; - if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) - { - if ( value == 'check' ) // Check image ratio and original image ratio. + // Check image ratio and original image ratio, but respecting user's preference. + if ( value == 'check' ) { - var width = dialog.getValueOf( 'info', 'txtWidth' ), - height = dialog.getValueOf( 'info', 'txtHeight' ), - originalRatio = oImageOriginal.$.width * 1000 / oImageOriginal.$.height, - thisRatio = width * 1000 / height; - dialog.lockRatio = false; // Default: unlock ratio - - if ( !width && !height ) - dialog.lockRatio = true; - else if ( !isNaN( originalRatio ) && !isNaN( thisRatio ) ) + if ( !dialog.userlockRatio && oImageOriginal.getCustomData( 'isReady' ) == 'true' ) { - if ( Math.round( originalRatio ) == Math.round( thisRatio ) ) + var width = dialog.getValueOf( 'info', 'txtWidth' ), + height = dialog.getValueOf( 'info', 'txtHeight' ), + originalRatio = oImageOriginal.$.width * 1000 / oImageOriginal.$.height, + thisRatio = width * 1000 / height; + dialog.lockRatio = false; // Default: unlock ratio + + if ( !width && !height ) dialog.lockRatio = true; + else if ( !isNaN( originalRatio ) && !isNaN( thisRatio ) ) + { + if ( Math.round( originalRatio ) == Math.round( thisRatio ) ) + dialog.lockRatio = true; + } } } else if ( value != undefined ) dialog.lockRatio = value; else + { + dialog.userlockRatio = 1; dialog.lockRatio = !dialog.lockRatio; - } - else if ( value != 'check' ) // I can't lock ratio if ratio is unknown. - dialog.lockRatio = false; + } - if ( dialog.lockRatio ) - ratioButton.removeClass( 'cke_btn_unlocked' ); - else - ratioButton.addClass( 'cke_btn_unlocked' ); + var ratioButton = CKEDITOR.document.getById( btnLockSizesId ); + if ( dialog.lockRatio ) + ratioButton.removeClass( 'cke_btn_unlocked' ); + else + ratioButton.addClass( 'cke_btn_unlocked' ); - return dialog.lockRatio; - }; + ratioButton.setAttribute( 'aria-checked', dialog.lockRatio ); - var resetSize = function( dialog ) - { - var oImageOriginal = dialog.originalElement; - if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) - { - dialog.setValueOf( 'info', 'txtWidth', oImageOriginal.$.width ); - dialog.setValueOf( 'info', 'txtHeight', oImageOriginal.$.height ); - } - updatePreview( dialog ); - }; + // Ratio button hc presentation - WHITE SQUARE / BLACK SQUARE + if ( CKEDITOR.env.hc ) + { + var icon = ratioButton.getChild( 0 ); + icon.setHtml( dialog.lockRatio ? CKEDITOR.env.ie ? '\u25A0': '\u25A3' : CKEDITOR.env.ie ? '\u25A1' : '\u25A2' ); + } - var setupDimension = function( type, element ) - { - if ( type != IMAGE ) - return; + return dialog.lockRatio; + }; - function checkDimension( size, defaultValue ) + var resetSize = function( dialog ) { - var aMatch = size.match( regexGetSize ); - if ( aMatch ) + var oImageOriginal = dialog.originalElement; + if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) { - if ( aMatch[2] == '%' ) // % is allowed. + var widthField = dialog.getContentElement( 'info', 'txtWidth' ), + heightField = dialog.getContentElement( 'info', 'txtHeight' ); + widthField && widthField.setValue( oImageOriginal.$.width ); + heightField && heightField.setValue( oImageOriginal.$.height ); + } + updatePreview( dialog ); + }; + + var setupDimension = function( type, element ) + { + if ( type != IMAGE ) + return; + + function checkDimension( size, defaultValue ) + { + var aMatch = size.match( regexGetSize ); + if ( aMatch ) { - aMatch[1] += '%'; - switchLockRatio( dialog, false ); // Unlock ratio + if ( aMatch[2] == '%' ) // % is allowed. + { + aMatch[1] += '%'; + switchLockRatio( dialog, false ); // Unlock ratio + } + return aMatch[1]; } - return aMatch[1]; + return defaultValue; } - return defaultValue; - } - var dialog = this.getDialog(), - value = '', - dimension = (( this.id == 'txtWidth' )? 'width' : 'height' ), - size = element.getAttribute( dimension ); + var dialog = this.getDialog(), + value = '', + dimension = this.id == 'txtWidth' ? 'width' : 'height', + size = element.getAttribute( dimension ); - if ( size ) - value = checkDimension( size, value ); - value = checkDimension( element.$.style[ dimension ], value ); + if ( size ) + value = checkDimension( size, value ); + value = checkDimension( element.getStyle( dimension ), value ); - this.setValue( value ); - }; + this.setValue( value ); + }; + + var previewPreloader; - var imageDialog = function( editor, dialogType ) - { var onImgLoadEvent = function() { // Image is ready. @@ -155,14 +227,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license original.removeListener( 'abort', onImgLoadErrorEvent ); // Hide loader - CKEDITOR.document.getById( 'ImagePreviewLoader' ).setStyle( 'display', 'none' ); + CKEDITOR.document.getById( imagePreviewLoaderId ).setStyle( 'display', 'none' ); // New image -> new domensions if ( !this.dontResetSize ) resetSize( this ); if ( this.firstLoad ) - switchLockRatio( this, 'check' ); + CKEDITOR.tools.setTimeout( function(){ switchLockRatio( this, 'check' ); }, 0, this ); + this.firstLoad = false; this.dontResetSize = false; }; @@ -182,13 +255,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.preview.setAttribute( 'src', noimage ); // Hide loader - CKEDITOR.document.getById( 'ImagePreviewLoader' ).setStyle( 'display', 'none' ); + CKEDITOR.document.getById( imagePreviewLoaderId ).setStyle( 'display', 'none' ); switchLockRatio( this, false ); // Unlock. }; + + var numbering = function( id ) + { + return CKEDITOR.tools.getNextId() + '_' + id; + }, + btnLockSizesId = numbering( 'btnLockSizes' ), + btnResetSizeId = numbering( 'btnResetSize' ), + imagePreviewLoaderId = numbering( 'ImagePreviewLoader' ), + previewLinkId = numbering( 'previewLink' ), + previewImageId = numbering( 'previewImage' ); + return { - title : ( dialogType == 'image' ) ? editor.lang.image.title : editor.lang.image.titleButton, + title : editor.lang.image[ dialogType == 'image' ? 'title' : 'titleButton' ], minWidth : 420, - minHeight : 310, + minHeight : 360, onShow : function() { this.imageElement = false; @@ -199,20 +283,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.linkEditMode = false; this.lockRatio = true; + this.userlockRatio = 0; this.dontResetSize = false; this.firstLoad = true; this.addLink = false; - //Hide loader. - CKEDITOR.document.getById( 'ImagePreviewLoader' ).setStyle( 'display', 'none' ); - // Preview - this.preview = CKEDITOR.document.getById( 'previewImage' ); - var editor = this.getParentEditor(), - sel = this.getParentEditor().getSelection(), - element = sel.getSelectedElement(), + sel = editor.getSelection(), + element = sel && sel.getSelectedElement(), link = element && element.getAscendant( 'a' ); + //Hide loader. + CKEDITOR.document.getById( imagePreviewLoaderId ).setStyle( 'display', 'none' ); + // Create the preview before setup the dialog contents. + previewPreloader = new CKEDITOR.dom.element( 'img', editor.document ); + this.preview = CKEDITOR.document.getById( previewImageId ); + // Copy of the image this.originalElement = editor.document.createElement( 'img' ); this.originalElement.setAttribute( 'alt', '' ); @@ -242,22 +328,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.setupContent( LINK, link ); } - if ( element && element.getName() == 'img' && !element.getAttribute( '_cke_protected_html' ) ) - this.imageEditMode = 'img'; - else if ( element && element.getName() == 'input' && element.getAttribute( 'type' ) && element.getAttribute( 'type' ) == 'image' ) - this.imageEditMode = 'input'; + if ( element && element.getName() == 'img' && !element.data( 'cke-realelement' ) + || element && element.getName() == 'input' && element.getAttribute( 'type' ) == 'image' ) + { + this.imageEditMode = element.getName(); + this.imageElement = element; + } - if ( this.imageEditMode || this.imageElement ) + if ( this.imageEditMode ) { - if ( !this.imageElement ) - this.imageElement = element; + // Use the original element as a buffer from since we don't want + // temporary changes to be committed, e.g. if the dialog is canceled. + this.cleanImageElement = this.imageElement; + this.imageElement = this.cleanImageElement.clone( true, true ); // Fill out all fields. this.setupContent( IMAGE, this.imageElement ); - - // Refresh LockRatio button - switchLockRatio ( this, true ); } + else + this.imageElement = editor.document.createElement( 'img' ); + + // Refresh LockRatio button + switchLockRatio ( this, true ); // Dont show preview if no URL given. if ( !CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtUrl' ) ) ) @@ -296,6 +388,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license ); editor.insertElement( this.imageElement ); } + else + { + // Restore the original element before all commits. + this.imageElement = this.cleanImageElement; + delete this.cleanImageElement; + } } else // Create a new image. { @@ -318,6 +416,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.commitContent( IMAGE, this.imageElement ); this.commitContent( LINK, this.linkElement ); + // Remove empty style attribute. + if ( !this.imageElement.getAttribute( 'style' ) ) + this.imageElement.removeAttribute( 'style' ); + // Insert a new Image. if ( !this.imageEditMode ) { @@ -326,11 +428,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license //Insert a new Link. if ( !this.linkEditMode ) { - editor.insertElement(this.linkElement); - this.linkElement.append(this.imageElement, false); + editor.insertElement( this.linkElement ); + this.linkElement.append( this.imageElement, false ); } else //Link already exists, image not. - editor.insertElement(this.imageElement ); + editor.insertElement( this.imageElement ); } else editor.insertElement( this.imageElement ); @@ -356,8 +458,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( dialogType != 'image' ) this.hidePage( 'Link' ); //Hide Link tab. var doc = this._.element.getDocument(); - this.addFocusable( doc.getById( 'btnResetSize' ), 5 ); - this.addFocusable( doc.getById( 'btnLockSizes' ), 5 ); + + if ( this.getContentElement( 'info', 'ratioLock' ) ) + { + this.addFocusable( doc.getById( btnResetSizeId ), 5 ); + this.addFocusable( doc.getById( btnLockSizesId ), 5 ); + } + + this.commitContent = commitContent; }, onHide : function() { @@ -372,6 +480,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.originalElement.remove(); this.originalElement = false; // Dialog is closed. } + + delete this.imageElement; }, contents : [ { @@ -386,10 +496,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license children : [ { - type : 'html', - html : '' + CKEDITOR.tools.htmlEncode( editor.lang.image.url ) + '' - }, - { type : 'hbox', widths : [ '280px', '110px' ], align : 'right', @@ -398,7 +504,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { id : 'txtUrl', type : 'text', - label : '', + label : editor.lang.common.url, + required: true, onChange : function() { var dialog = this.getDialog(), @@ -414,7 +521,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license original.setCustomData( 'isReady', 'false' ); // Show loader - var loader = CKEDITOR.document.getById( 'ImagePreviewLoader' ); + var loader = CKEDITOR.document.getById( imagePreviewLoaderId ); if ( loader ) loader.setStyle( 'display', '' ); @@ -422,8 +529,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license original.on( 'error', onImgLoadErrorEvent, dialog ); original.on( 'abort', onImgLoadErrorEvent, dialog ); original.setAttribute( 'src', newUrl ); - dialog.preview.setAttribute( 'src', newUrl ); + // Query the preloader to figure out the url impacted by based href. + previewPreloader.setAttribute( 'src', newUrl ); + dialog.preview.setAttribute( 'src', previewPreloader.$.src ); updatePreview( dialog ); } // Dont show preview if no URL given. @@ -437,28 +546,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { if ( type == IMAGE ) { - var url = element.getAttribute( '_cke_saved_src' ) || element.getAttribute( 'src' ); + var url = element.data( 'cke-saved-src' ) || element.getAttribute( 'src' ); var field = this; this.getDialog().dontResetSize = true; - // In IE7 the dialog is being rendered improperly when loading - // an image with a long URL. So we need to delay it a bit. (#4122) - setTimeout( function() - { - field.setValue( url ); // And call this.onChange() - // Manually set the initial value.(#4191) - field.setInitValue(); - field.focus(); - }, 0 ); + field.setValue( url ); // And call this.onChange() + // Manually set the initial value.(#4191) + field.setInitValue(); } }, commit : function( type, element ) { if ( type == IMAGE && ( this.getValue() || this.isChanged() ) ) { - element.setAttribute( '_cke_saved_src', decodeURI( this.getValue() ) ); - element.setAttribute( 'src', decodeURI( this.getValue() ) ); + element.data( 'cke-saved-src', this.getValue() ); + element.setAttribute( 'src', this.getValue() ); } else if ( type == CLEANUP ) { @@ -471,6 +574,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'button', id : 'browse', + // v-align with the 'txtUrl' field. + // TODO: We need something better than a fixed size here. + style : 'display:inline-block;margin-top:10px;', align : 'center', label : editor.lang.common.browseServer, hidden : true, @@ -484,7 +590,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license id : 'txtAlt', type : 'text', label : editor.lang.image.alt, - accessKey : 'A', + accessKey : 'T', 'default' : '', onChange : function() { @@ -514,17 +620,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, { type : 'hbox', - widths : [ '140px', '240px' ], children : [ { + id : 'basic', type : 'vbox', - padding : 10, children : [ { type : 'hbox', - widths : [ '70%', '30%' ], + widths : [ '50%', '50%' ], children : [ { @@ -536,30 +641,35 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', width: '40px', id : 'txtWidth', - labelLayout : 'horizontal', - label : editor.lang.image.width, + label : editor.lang.common.width, onKeyUp : onSizeChange, - validate: function() + onChange : function() + { + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); + }, + validate : function() { - var aMatch = this.getValue().match( regexGetSizeOrEmpty ); - if ( !aMatch ) - alert( editor.lang.common.validateNumberFailed ); - return !!aMatch; + var aMatch = this.getValue().match( regexGetSizeOrEmpty ), + isValid = !!( aMatch && parseInt( aMatch[1], 10 ) !== 0 ); + if ( !isValid ) + alert( editor.lang.common.invalidWidth ); + return isValid; }, setup : setupDimension, - commit : function( type, element ) + commit : function( type, element, internalCommit ) { + var value = this.getValue(); if ( type == IMAGE ) { - var value = this.getValue(); if ( value ) - element.setAttribute( 'width', value ); - else if ( !value && this.isChanged() ) - element.removeAttribute( 'width' ); + element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) ); + else + element.removeStyle( 'width' ); + + !internalCommit && element.removeAttribute( 'width' ); } else if ( type == PREVIEW ) { - value = this.getValue(); var aMatch = value.match( regexGetSize ); if ( !aMatch ) { @@ -568,11 +678,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license element.setStyle( 'width', oImageOriginal.$.width + 'px'); } else - element.setStyle( 'width', value + 'px'); + element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) ); } else if ( type == CLEANUP ) { - element.setStyle( 'width', '0px' ); // If removeAttribute doesn't work. element.removeAttribute( 'width' ); element.removeStyle( 'width' ); } @@ -582,43 +691,47 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', id : 'txtHeight', width: '40px', - labelLayout : 'horizontal', - label : editor.lang.image.height, + label : editor.lang.common.height, onKeyUp : onSizeChange, - validate: function() + onChange : function() { - var aMatch = this.getValue().match( regexGetSizeOrEmpty ); - if ( !aMatch ) - alert( editor.lang.common.validateNumberFailed ); - return !!aMatch; + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); + }, + validate : function() + { + var aMatch = this.getValue().match( regexGetSizeOrEmpty ), + isValid = !!( aMatch && parseInt( aMatch[1], 10 ) !== 0 ); + if ( !isValid ) + alert( editor.lang.common.invalidHeight ); + return isValid; }, setup : setupDimension, - commit : function( type, element ) + commit : function( type, element, internalCommit ) { + var value = this.getValue(); if ( type == IMAGE ) { - var value = this.getValue(); if ( value ) - element.setAttribute( 'height', value ); - else if ( !value && this.isChanged() ) - element.removeAttribute( 'height' ); + element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) ); + else + element.removeStyle( 'height' ); + + !internalCommit && element.removeAttribute( 'height' ); } else if ( type == PREVIEW ) { - value = this.getValue(); var aMatch = value.match( regexGetSize ); if ( !aMatch ) { var oImageOriginal = this.getDialog().originalElement; if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' ) - element.setStyle( 'height', oImageOriginal.$.height + 'px'); + element.setStyle( 'height', oImageOriginal.$.height + 'px' ); } else - element.setStyle( 'height', value + 'px'); + element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) ); } else if ( type == CLEANUP ) { - element.setStyle( 'height', '0px' ); // If removeAttribute doesn't work. element.removeAttribute( 'height' ); element.removeStyle( 'height' ); } @@ -627,18 +740,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license ] }, { + id : 'ratioLock', type : 'html', - style : 'margin-top:10px;width:40px;height:40px;', + style : 'margin-top:30px;width:40px;height:40px;', onLoad : function() { // Activate Reset button - var resetButton = CKEDITOR.document.getById( 'btnResetSize' ), - ratioButton = CKEDITOR.document.getById( 'btnLockSizes' ); + var resetButton = CKEDITOR.document.getById( btnResetSizeId ), + ratioButton = CKEDITOR.document.getById( btnLockSizesId ); if ( resetButton ) { - resetButton.on( 'click', function() + resetButton.on( 'click', function( evt ) { resetSize( this ); + evt.data && evt.data.preventDefault(); }, this.getDialog() ); resetButton.on( 'mouseover', function() { @@ -652,7 +767,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Activate (Un)LockRatio button if ( ratioButton ) { - ratioButton.on( 'click', function() + ratioButton.on( 'click', function(evt) { var locked = switchLockRatio( this ), oImageOriginal = this.originalElement, @@ -667,6 +782,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license updatePreview( this ); } } + evt.data && evt.data.preventDefault(); }, this.getDialog() ); ratioButton.on( 'mouseover', function() { @@ -680,9 +796,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, html : '
'+ '' + + '" class="cke_btn_locked" id="' + btnLockSizesId + '" role="checkbox">' + editor.lang.image.lockRatio + '' + ''+ + '" class="cke_btn_reset" id="' + btnResetSizeId + '" role="button">' + editor.lang.image.resetSize + ''+ '
' } ] @@ -696,41 +812,51 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', id : 'txtBorder', width: '60px', - labelLayout : 'horizontal', label : editor.lang.image.border, 'default' : '', onKeyUp : function() { updatePreview( this.getDialog() ); }, - validate: function() + onChange : function() { - var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed ); - return func.apply( this ); + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); }, + validate : CKEDITOR.dialog.validate.integer( editor.lang.image.validateBorder ), setup : function( type, element ) { if ( type == IMAGE ) - this.setValue( element.getAttribute( 'border' ) ); - }, - commit : function( type, element ) - { - if ( type == IMAGE ) { - if ( this.getValue() || this.isChanged() ) - element.setAttribute( 'border', this.getValue() ); + var value, + borderStyle = element.getStyle( 'border-width' ); + borderStyle = borderStyle && borderStyle.match( /^(\d+px)(?: \1 \1 \1)?$/ ); + value = borderStyle && parseInt( borderStyle[ 1 ], 10 ); + isNaN ( parseInt( value, 10 ) ) && ( value = element.getAttribute( 'border' ) ); + this.setValue( value ); } - else if ( type == PREVIEW ) + }, + commit : function( type, element, internalCommit ) + { + var value = parseInt( this.getValue(), 10 ); + if ( type == IMAGE || type == PREVIEW ) { - var value = parseInt( this.getValue(), 10 ); - value = isNaN( value ) ? 0 : value; - element.setAttribute( 'border', value ); - element.setStyle( 'border', value + 'px solid black' ); + if ( !isNaN( value ) ) + { + element.setStyle( 'border-width', CKEDITOR.tools.cssLength( value ) ); + element.setStyle( 'border-style', 'solid' ); + } + else if ( !value && this.isChanged() ) + element.removeStyle( 'border' ); + + if ( !internalCommit && type == IMAGE ) + element.removeAttribute( 'border' ); } else if ( type == CLEANUP ) { element.removeAttribute( 'border' ); - element.removeStyle( 'border' ); + element.removeStyle( 'border-width' ); + element.removeStyle( 'border-style' ); + element.removeStyle( 'border-color' ); } } }, @@ -738,41 +864,56 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', id : 'txtHSpace', width: '60px', - labelLayout : 'horizontal', label : editor.lang.image.hSpace, 'default' : '', onKeyUp : function() { updatePreview( this.getDialog() ); }, - validate: function() + onChange : function() { - var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed ); - return func.apply( this ); + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); }, + validate : CKEDITOR.dialog.validate.integer( editor.lang.image.validateHSpace ), setup : function( type, element ) { if ( type == IMAGE ) { - var value = element.getAttribute( 'hspace' ); - if ( value != -1 ) // In IE empty = -1. - this.setValue( value ); + var value, + marginLeftPx, + marginRightPx, + marginLeftStyle = element.getStyle( 'margin-left' ), + marginRightStyle = element.getStyle( 'margin-right' ); + + marginLeftStyle = marginLeftStyle && marginLeftStyle.match( pxLengthRegex ); + marginRightStyle = marginRightStyle && marginRightStyle.match( pxLengthRegex ); + marginLeftPx = parseInt( marginLeftStyle, 10 ); + marginRightPx = parseInt( marginRightStyle, 10 ); + + value = ( marginLeftPx == marginRightPx ) && marginLeftPx; + isNaN( parseInt( value, 10 ) ) && ( value = element.getAttribute( 'hspace' ) ); + + this.setValue( value ); } }, - commit : function( type, element ) + commit : function( type, element, internalCommit ) { - if ( type == IMAGE ) - { - if ( this.getValue() || this.isChanged() ) - element.setAttribute( 'hspace', this.getValue() ); - } - else if ( type == PREVIEW ) + var value = parseInt( this.getValue(), 10 ); + if ( type == IMAGE || type == PREVIEW ) { - var value = parseInt( this.getValue(), 10 ); - value = isNaN( value ) ? 0 : value; - element.setAttribute( 'hspace', value ); - element.setStyle( 'margin-left', value + 'px' ); - element.setStyle( 'margin-right', value + 'px' ); + if ( !isNaN( value ) ) + { + element.setStyle( 'margin-left', CKEDITOR.tools.cssLength( value ) ); + element.setStyle( 'margin-right', CKEDITOR.tools.cssLength( value ) ); + } + else if ( !value && this.isChanged( ) ) + { + element.removeStyle( 'margin-left' ); + element.removeStyle( 'margin-right' ); + } + + if ( !internalCommit && type == IMAGE ) + element.removeAttribute( 'hspace' ); } else if ( type == CLEANUP ) { @@ -786,37 +927,55 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', id : 'txtVSpace', width : '60px', - labelLayout : 'horizontal', label : editor.lang.image.vSpace, 'default' : '', onKeyUp : function() { updatePreview( this.getDialog() ); }, - validate: function() + onChange : function() { - var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed ); - return func.apply( this ); + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); }, + validate : CKEDITOR.dialog.validate.integer( editor.lang.image.validateVSpace ), setup : function( type, element ) { if ( type == IMAGE ) - this.setValue( element.getAttribute( 'vspace' ) ); - }, - commit : function( type, element ) - { - if ( type == IMAGE ) { - if ( this.getValue() || this.isChanged() ) - element.setAttribute( 'vspace', this.getValue() ); + var value, + marginTopPx, + marginBottomPx, + marginTopStyle = element.getStyle( 'margin-top' ), + marginBottomStyle = element.getStyle( 'margin-bottom' ); + + marginTopStyle = marginTopStyle && marginTopStyle.match( pxLengthRegex ); + marginBottomStyle = marginBottomStyle && marginBottomStyle.match( pxLengthRegex ); + marginTopPx = parseInt( marginTopStyle, 10 ); + marginBottomPx = parseInt( marginBottomStyle, 10 ); + + value = ( marginTopPx == marginBottomPx ) && marginTopPx; + isNaN ( parseInt( value, 10 ) ) && ( value = element.getAttribute( 'vspace' ) ); + this.setValue( value ); } - else if ( type == PREVIEW ) + }, + commit : function( type, element, internalCommit ) + { + var value = parseInt( this.getValue(), 10 ); + if ( type == IMAGE || type == PREVIEW ) { - var value = parseInt( this.getValue(), 10 ); - value = isNaN( value ) ? 0 : value; - element.setAttribute( 'vspace', this.getValue() ); - element.setStyle( 'margin-top', value + 'px' ); - element.setStyle( 'margin-bottom', value + 'px' ); + if ( !isNaN( value ) ) + { + element.setStyle( 'margin-top', CKEDITOR.tools.cssLength( value ) ); + element.setStyle( 'margin-bottom', CKEDITOR.tools.cssLength( value ) ); + } + else if ( !value && this.isChanged( ) ) + { + element.removeStyle( 'margin-top' ); + element.removeStyle( 'margin-bottom' ); + } + + if ( !internalCommit && type == IMAGE ) + element.removeAttribute( 'vspace' ); } else if ( type == CLEANUP ) { @@ -829,62 +988,73 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { id : 'cmbAlign', type : 'select', - labelLayout : 'horizontal', widths : [ '35%','65%' ], style : 'width:90px', - label : editor.lang.image.align, + label : editor.lang.common.align, 'default' : '', items : [ [ editor.lang.common.notSet , ''], - [ editor.lang.image.alignLeft , 'left'], - [ editor.lang.image.alignAbsBottom , 'absBottom'], - [ editor.lang.image.alignAbsMiddle , 'absMiddle'], - [ editor.lang.image.alignBaseline , 'baseline'], - [ editor.lang.image.alignBottom , 'bottom'], - [ editor.lang.image.alignMiddle , 'middle'], - [ editor.lang.image.alignRight , 'right'], - [ editor.lang.image.alignTextTop , 'textTop'], - [ editor.lang.image.alignTop , 'top'] + [ editor.lang.common.alignLeft , 'left'], + [ editor.lang.common.alignRight , 'right'] + // Backward compatible with v2 on setup when specified as attribute value, + // while these values are no more available as select options. + // [ editor.lang.image.alignAbsBottom , 'absBottom'], + // [ editor.lang.image.alignAbsMiddle , 'absMiddle'], + // [ editor.lang.image.alignBaseline , 'baseline'], + // [ editor.lang.image.alignTextTop , 'text-top'], + // [ editor.lang.image.alignBottom , 'bottom'], + // [ editor.lang.image.alignMiddle , 'middle'], + // [ editor.lang.image.alignTop , 'top'] ], onChange : function() { updatePreview( this.getDialog() ); + commitInternally.call( this, 'advanced:txtdlgGenStyle' ); }, setup : function( type, element ) { if ( type == IMAGE ) - this.setValue( element.getAttribute( 'align' ) ); + { + var value = element.getStyle( 'float' ); + switch( value ) + { + // Ignore those unrelated values. + case 'inherit': + case 'none': + value = ''; + } + + !value && ( value = ( element.getAttribute( 'align' ) || '' ).toLowerCase() ); + this.setValue( value ); + } }, - commit : function( type, element ) + commit : function( type, element, internalCommit ) { var value = this.getValue(); - if ( type == IMAGE ) + if ( type == IMAGE || type == PREVIEW ) { - if ( value || this.isChanged() ) - element.setAttribute( 'align', value ); - } - else if ( type == PREVIEW ) - { - element.setAttribute( 'align', this.getValue() ); - - if ( value == 'absMiddle' || value == 'middle' ) - element.setStyle( 'vertical-align', 'middle' ); - else if ( value == 'top' || value == 'textTop' ) - element.setStyle( 'vertical-align', 'top' ); - else - element.removeStyle( 'vertical-align' ); - - if ( value == 'right' || value == 'left' ) - element.setStyle( 'styleFloat', value ); + if ( value ) + element.setStyle( 'float', value ); else - element.removeStyle( 'styleFloat' ); + element.removeStyle( 'float' ); + if ( !internalCommit && type == IMAGE ) + { + value = ( element.getAttribute( 'align' ) || '' ).toLowerCase(); + switch( value ) + { + // we should remove it only if it matches "left" or "right", + // otherwise leave it intact. + case 'left': + case 'right': + element.removeAttribute( 'align' ); + } + } } else if ( type == CLEANUP ) - { - element.removeAttribute( 'align' ); - } + element.removeStyle( 'float' ); + } } ] @@ -898,15 +1068,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license [ { type : 'html', + id : 'htmlPreview', style : 'width:95%;', - html : '
' + CKEDITOR.tools.htmlEncode( editor.lang.image.preview ) +'
'+ - ''+ - '
'+ - ''+ - 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. '+ + html : '
' + CKEDITOR.tools.htmlEncode( editor.lang.common.preview ) +'
'+ + ''+ + '
'+ + ''+ + '' + + ( editor.config.image_previewText || + 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. '+ 'Maecenas feugiat consequat diam. Maecenas metus. Vivamus diam purus, cursus a, commodo non, facilisis vitae, '+ - 'nulla. Aenean dictum lacinia tortor. Nunc iaculis, nibh non iaculis aliquam, orci felis euismod neque, sed ornare massa mauris sed velit. Nulla pretium mi et risus. Fusce mi pede, tempor id, cursus ac, ullamcorper nec, enim. Sed tortor. Curabitur molestie. Duis velit augue, condimentum at, ultrices a, luctus ut, orci. Donec pellentesque egestas eros. Integer cursus, augue in cursus faucibus, eros pede bibendum sem, in tempus tellus justo quis ligula. Etiam eget tortor. Vestibulum rutrum, est ut placerat elementum, lectus nisl aliquam velit, tempor aliquam eros nunc nonummy metus. In eros metus, gravida a, gravida sed, lobortis id, turpis. Ut ultrices, ipsum at venenatis fringilla, sem nulla lacinia tellus, eget aliquet turpis mauris non enim. Nam turpis. Suspendisse lacinia. Curabitur ac tortor ut ipsum egestas elementum. Nunc imperdiet gravida mauris.' + - ''+'' + 'nulla. Aenean dictum lacinia tortor. Nunc iaculis, nibh non iaculis aliquam, orci felis euismod neque, sed ornare massa mauris sed velit. Nulla pretium mi et risus. Fusce mi pede, tempor id, cursus ac, ullamcorper nec, enim. Sed tortor. Curabitur molestie. Duis velit augue, condimentum at, ultrices a, luctus ut, orci. Donec pellentesque egestas eros. Integer cursus, augue in cursus faucibus, eros pede bibendum sem, in tempus tellus justo quis ligula. Etiam eget tortor. Vestibulum rutrum, est ut placerat elementum, lectus nisl aliquam velit, tempor aliquam eros nunc nonummy metus. In eros metus, gravida a, gravida sed, lobortis id, turpis. Ut ultrices, ipsum at venenatis fringilla, sem nulla lacinia tellus, eget aliquet turpis mauris non enim. Nam turpis. Suspendisse lacinia. Curabitur ac tortor ut ipsum egestas elementum. Nunc imperdiet gravida mauris.' ) + + '
' } ] } @@ -923,14 +1096,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { id : 'txtUrl', type : 'text', - label : editor.lang.image.url, + label : editor.lang.common.url, style : 'width: 100%', 'default' : '', setup : function( type, element ) { if ( type == LINK ) { - var href = element.getAttribute( '_cke_saved_href' ); + var href = element.data( 'cke-saved-href' ); if ( !href ) href = element.getAttribute( 'href' ); this.setValue( href ); @@ -942,9 +1115,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { if ( this.getValue() || this.isChanged() ) { - element.setAttribute( '_cke_saved_href', decodeURI( this.getValue() ) ); - element.setAttribute( 'href', 'javascript:void(0)/*' + - CKEDITOR.tools.getNextNumber() + '*/' ); + var url = decodeURI( this.getValue() ); + element.data( 'cke-saved-href', url ); + element.setAttribute( 'href', url ); if ( this.getValue() || !editor.config.image_removeLinkByEmptyURL ) this.getDialog().addLink = true; @@ -955,7 +1128,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'button', id : 'browse', - filebrowser : 'Link:txtUrl', + filebrowser : + { + action : 'Browse', + target: 'Link:txtUrl', + url: editor.config.filebrowserImageBrowseLinkUrl + }, style : 'float:right', hidden : true, label : editor.lang.common.browseServer @@ -963,20 +1141,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { id : 'cmbTarget', type : 'select', - label : editor.lang.link.target, + label : editor.lang.common.target, 'default' : '', items : [ - [ editor.lang.link.targetNotSet , ''], - [ editor.lang.link.targetNew , '_blank'], - [ editor.lang.link.targetTop , '_top'], - [ editor.lang.link.targetSelf , '_self'], - [ editor.lang.link.targetParent , '_parent'] + [ editor.lang.common.notSet , ''], + [ editor.lang.common.targetNew , '_blank'], + [ editor.lang.common.targetTop , '_top'], + [ editor.lang.common.targetSelf , '_self'], + [ editor.lang.common.targetParent , '_parent'] ], setup : function( type, element ) { if ( type == LINK ) - this.setValue( element.getAttribute( 'target' ) ); + this.setValue( element.getAttribute( 'target' ) || '' ); }, commit : function( type, element ) { @@ -1166,6 +1344,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license type : 'text', id : 'txtdlgGenStyle', label : editor.lang.common.cssStyle, + validate : CKEDITOR.dialog.validate.inlineStyle( editor.lang.common.invalidInlineStyle ), 'default' : '', setup : function( type, element ) { @@ -1188,40 +1367,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }; } }, + onChange : function () + { + commitInternally.call( this, + [ 'info:cmbFloat', 'info:cmbAlign', + 'info:txtVSpace', 'info:txtHSpace', + 'info:txtBorder', + 'info:txtWidth', 'info:txtHeight' ] ); + updatePreview( this ); + }, commit : function( type, element ) { if ( type == IMAGE && ( this.getValue() || this.isChanged() ) ) { element.setAttribute( 'style', this.getValue() ); - - // Set STYLE dimensions. - var height = element.getAttribute( 'height' ), - width = element.getAttribute( 'width' ); - - if ( this.attributesInStyle && this.attributesInStyle.height ) - { - if ( height ) - { - if ( height.match( regexGetSize )[2] == '%' ) // % is allowed - element.setStyle( 'height', height + '%' ); - else - element.setStyle( 'height', height + 'px' ); - } - else - element.removeStyle( 'height' ); - } - if ( this.attributesInStyle && this.attributesInStyle.width ) - { - if ( width ) - { - if ( width.match( regexGetSize )[2] == '%' ) // % is allowed - element.setStyle( 'width', width + '%' ); - else - element.setStyle( 'width', width + 'px' ); - } - else - element.removeStyle( 'width' ); - } } } }