X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fimage%2Fdialogs%2Fimage.js;h=cc1893693a7a81444cc33f1ff75f1f9d87831d04;hb=059b4c2fef02528bf1af189f7996e80652faddfb;hp=233a291915f90ff84cbd334e61643130e217e7ef;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/plugins/image/dialogs/image.js b/_source/plugins/image/dialogs/image.js index 233a291..cc18936 100644 --- a/_source/plugins/image/dialogs/image.js +++ b/_source/plugins/image/dialogs/image.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -11,7 +11,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license PREVIEW = 4, CLEANUP = 8, regexGetSize = /^\s*(\d+)((px)|\%)?\s*$/i, - regexGetSizeOrEmpty = /(^\s*(\d+)((px)|\%)?\s*$)|^$/i; + regexGetSizeOrEmpty = /(^\s*(\d+)((px)|\%)?\s*$)|^$/i, + pxLengthRegex = /^\d+px$/; var onSizeChange = function() { @@ -61,6 +62,55 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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++ ) + { + field = dialog.getContentElement.apply( dialog, targetFields[ i ].split( ':' ) ); + // May cause recursion. + field && field.setup( IMAGE, element ); + } + } + + incommit = 0; + } + var switchLockRatio = function( dialog, value ) { var oImageOriginal = dialog.originalElement, @@ -97,6 +147,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license else ratioButton.addClass( 'cke_btn_unlocked' ); + var lang = dialog._.editor.lang.image, + label = lang[ dialog.lockRatio ? 'unlockRatio' : 'lockRatio' ]; + + ratioButton.setAttribute( 'title', label ); + ratioButton.getFirst().setText( label ); + return dialog.lockRatio; }; @@ -138,13 +194,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( size ) value = checkDimension( size, value ); - value = checkDimension( element.$.style[ dimension ], value ); + value = checkDimension( element.getStyle( dimension ), value ); this.setValue( value ); }; var imageDialog = function( editor, dialogType ) { + var previewPreloader; + var onImgLoadEvent = function() { // Image is ready. @@ -162,7 +220,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license resetSize( this ); if ( this.firstLoad ) - switchLockRatio( this, 'check' ); + CKEDITOR.tools.setTimeout( function(){ switchLockRatio( this, 'check' ); }, 0, this ); + this.firstLoad = false; this.dontResetSize = false; }; @@ -203,16 +262,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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(), link = element && element.getAscendant( 'a' ); + //Hide loader. + CKEDITOR.document.getById( 'ImagePreviewLoader' ).setStyle( 'display', 'none' ); + // Create the preview before setup the dialog contents. + previewPreloader = new CKEDITOR.dom.element( 'img', editor.document ); + this.preview = CKEDITOR.document.getById( 'previewImage' ); + // Copy of the image this.originalElement = editor.document.createElement( 'img' ); this.originalElement.setAttribute( 'alt', '' ); @@ -242,15 +302,19 @@ 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.getAttribute( '_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 ); @@ -258,6 +322,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Refresh LockRatio button switchLockRatio ( this, true ); } + else + this.imageElement = editor.document.createElement( 'img' ); + + // Dont show preview if no URL given. + if ( !CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtUrl' ) ) ) + { + this.preview.removeAttribute( 'src' ); + this.preview.setStyle( 'display', 'none' ); + } }, onOk : function() { @@ -289,6 +362,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. { @@ -311,6 +390,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 ) { @@ -351,6 +434,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var doc = this._.element.getDocument(); this.addFocusable( doc.getById( 'btnResetSize' ), 5 ); this.addFocusable( doc.getById( 'btnLockSizes' ), 5 ); + + this.commitContent = commitContent; }, onHide : function() { @@ -365,6 +450,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.originalElement.remove(); this.originalElement = false; // Dialog is closed. } + + delete this.imageElement; }, contents : [ { @@ -379,10 +466,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', @@ -391,7 +474,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(), @@ -403,6 +487,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license dialog = this.getDialog(); var original = dialog.originalElement; + dialog.preview.removeStyle( 'display' ); + original.setCustomData( 'isReady', 'false' ); // Show loader var loader = CKEDITOR.document.getById( 'ImagePreviewLoader' ); @@ -413,10 +499,18 @@ 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. + else if ( dialog.preview ) + { + dialog.preview.removeAttribute( 'src' ); + dialog.preview.setStyle( 'display', 'none' ); + } }, setup : function( type, element ) { @@ -427,13 +521,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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() - field.focus(); - }, 0 ); + field.setValue( url ); // And call this.onChange() + // Manually set the initial value.(#4191) + field.setInitValue(); + field.focus(); } }, commit : function( type, element ) @@ -448,11 +539,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license element.setAttribute( 'src', '' ); // If removeAttribute doesn't work. element.removeAttribute( 'src' ); } - } + }, + validate : CKEDITOR.dialog.validate.notEmpty( editor.lang.image.urlMissing ) }, { 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, @@ -521,27 +616,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license labelLayout : 'horizontal', label : editor.lang.image.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 ); + alert( editor.lang.image.validateWidth ); return !!aMatch; }, 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 if ( !value && this.isChanged( ) ) + element.removeStyle( 'width' ); + + !internalCommit && element.removeAttribute( 'width' ); } else if ( type == PREVIEW ) { - value = this.getValue(); var aMatch = value.match( regexGetSize ); if ( !aMatch ) { @@ -554,7 +654,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } else if ( type == CLEANUP ) { - element.setStyle( 'width', '0px' ); // If removeAttribute doesn't work. element.removeAttribute( 'width' ); element.removeStyle( 'width' ); } @@ -567,40 +666,45 @@ For licensing, see LICENSE.html or http://ckeditor.com/license labelLayout : 'horizontal', label : editor.lang.image.height, 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 ); + alert( editor.lang.image.validateHeight ); return !!aMatch; }, 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.setStyle( 'height', CKEDITOR.tools.cssLength( value ) ); + else if ( !value && this.isChanged( ) ) + element.removeStyle( 'height' ); + + if ( !internalCommit && type == IMAGE ) 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', value + 'px' ); } else if ( type == CLEANUP ) { - element.setStyle( 'height', '0px' ); // If removeAttribute doesn't work. element.removeAttribute( 'height' ); element.removeStyle( 'height' ); } @@ -618,9 +722,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license ratioButton = CKEDITOR.document.getById( 'btnLockSizes' ); if ( resetButton ) { - resetButton.on( 'click', function() + resetButton.on( 'click', function(evt) { resetSize( this ); + evt.data.preventDefault(); }, this.getDialog() ); resetButton.on( 'mouseover', function() { @@ -634,7 +739,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, @@ -649,6 +754,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license updatePreview( this ); } } + evt.data.preventDefault(); }, this.getDialog() ); ratioButton.on( 'mouseover', function() { @@ -661,10 +767,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }, html : '