JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / image / dialogs / image.js
index 268185f..992d700 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -115,17 +115,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                var switchLockRatio = function( dialog, value )\r
                {\r
+                       if ( !dialog.getContentElement( 'info', 'ratioLock' ) )\r
+                               return null;\r
+\r
                        var oImageOriginal = dialog.originalElement;\r
 \r
                        // Dialog may already closed. (#5505)\r
                        if( !oImageOriginal )\r
                                return null;\r
 \r
-                       var ratioButton = CKEDITOR.document.getById( btnLockSizesId );\r
-\r
-                       if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' )\r
+                       // Check image ratio and original image ratio, but respecting user's preference.\r
+                       if ( value == 'check' )\r
                        {\r
-                               if ( value == 'check' )                 // Check image ratio and original image ratio.\r
+                               if ( !dialog.userlockRatio && oImageOriginal.getCustomData( 'isReady' ) == 'true'  )\r
                                {\r
                                        var width = dialog.getValueOf( 'info', 'txtWidth' ),\r
                                                height = dialog.getValueOf( 'info', 'txtHeight' ),\r
@@ -141,24 +143,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        dialog.lockRatio = true;\r
                                        }\r
                                }\r
-                               else if ( value != undefined )\r
-                                       dialog.lockRatio = value;\r
-                               else\r
-                                       dialog.lockRatio = !dialog.lockRatio;\r
                        }\r
-                       else if ( value != 'check' )            // I can't lock ratio if ratio is unknown.\r
-                               dialog.lockRatio = false;\r
+                       else if ( value != undefined )\r
+                               dialog.lockRatio = value;\r
+                       else\r
+                       {\r
+                               dialog.userlockRatio = 1;\r
+                               dialog.lockRatio = !dialog.lockRatio;\r
+                       }\r
 \r
+                       var ratioButton = CKEDITOR.document.getById( btnLockSizesId );\r
                        if ( dialog.lockRatio )\r
                                ratioButton.removeClass( 'cke_btn_unlocked' );\r
                        else\r
                                ratioButton.addClass( 'cke_btn_unlocked' );\r
 \r
-                       var lang = dialog._.editor.lang.image,\r
-                               label =  lang[  dialog.lockRatio ? 'unlockRatio' : 'lockRatio' ];\r
+                       ratioButton.setAttribute( 'aria-checked', dialog.lockRatio );\r
 \r
-                       ratioButton.setAttribute( 'title', label );\r
-                       ratioButton.getFirst().setText( label );\r
+                       // Ratio button hc presentation - WHITE SQUARE / BLACK SQUARE\r
+                       if ( CKEDITOR.env.hc )\r
+                       {\r
+                               var icon = ratioButton.getChild( 0 );\r
+                               icon.setHtml(  dialog.lockRatio ? CKEDITOR.env.ie ? '\u25A0': '\u25A3' : CKEDITOR.env.ie ? '\u25A1' : '\u25A2' );\r
+                       }\r
 \r
                        return dialog.lockRatio;\r
                };\r
@@ -168,8 +175,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        var oImageOriginal = dialog.originalElement;\r
                        if ( oImageOriginal.getCustomData( 'isReady' ) == 'true' )\r
                        {\r
-                               dialog.setValueOf( 'info', 'txtWidth', oImageOriginal.$.width );\r
-                               dialog.setValueOf( 'info', 'txtHeight', oImageOriginal.$.height );\r
+                               var widthField = dialog.getContentElement( 'info', 'txtWidth' ),\r
+                                       heightField = dialog.getContentElement( 'info', 'txtHeight' );\r
+                               widthField && widthField.setValue( oImageOriginal.$.width );\r
+                               heightField && heightField.setValue( oImageOriginal.$.height );\r
                        }\r
                        updatePreview( dialog );\r
                };\r
@@ -196,7 +205,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        var dialog = this.getDialog(),\r
                                value = '',\r
-                               dimension = (( this.id == 'txtWidth' )? 'width' : 'height' ),\r
+                               dimension = this.id == 'txtWidth' ? 'width' : 'height',\r
                                size = element.getAttribute( dimension );\r
 \r
                        if ( size )\r
@@ -257,14 +266,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        btnLockSizesId = numbering( 'btnLockSizes' ),\r
                        btnResetSizeId = numbering( 'btnResetSize' ),\r
                        imagePreviewLoaderId = numbering( 'ImagePreviewLoader' ),\r
-                       imagePreviewBoxId = numbering( 'ImagePreviewBox' ),\r
                        previewLinkId = numbering( 'previewLink' ),\r
                        previewImageId = numbering( 'previewImage' );\r
 \r
                return {\r
-                       title : ( dialogType == 'image' ) ? editor.lang.image.title : editor.lang.image.titleButton,\r
+                       title : editor.lang.image[ dialogType == 'image' ? 'title' : 'titleButton' ],\r
                        minWidth : 420,\r
-                       minHeight : 310,\r
+                       minHeight : 360,\r
                        onShow : function()\r
                        {\r
                                this.imageElement = false;\r
@@ -275,13 +283,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                this.linkEditMode = false;\r
 \r
                                this.lockRatio = true;\r
+                               this.userlockRatio = 0;\r
                                this.dontResetSize = false;\r
                                this.firstLoad = true;\r
                                this.addLink = false;\r
 \r
                                var editor = this.getParentEditor(),\r
-                                       sel = this.getParentEditor().getSelection(),\r
-                                       element = sel.getSelectedElement(),\r
+                                       sel = editor.getSelection(),\r
+                                       element = sel && sel.getSelectedElement(),\r
                                        link = element && element.getAscendant( 'a' );\r
 \r
                                //Hide loader.\r
@@ -319,7 +328,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                this.setupContent( LINK, link );\r
                                }\r
 \r
-                               if ( element && element.getName() == 'img' && !element.getAttribute( '_cke_realelement' )\r
+                               if ( element && element.getName() == 'img' && !element.data( 'cke-realelement' )\r
                                        || element && element.getName() == 'input' && element.getAttribute( 'type' ) == 'image' )\r
                                {\r
                                        this.imageEditMode = element.getName();\r
@@ -335,13 +344,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        // Fill out all fields.\r
                                        this.setupContent( IMAGE, this.imageElement );\r
-\r
-                                       // Refresh LockRatio button\r
-                                       switchLockRatio ( this, true );\r
                                }\r
                                else\r
                                        this.imageElement =  editor.document.createElement( 'img' );\r
 \r
+                               // Refresh LockRatio button\r
+                               switchLockRatio ( this, true );\r
+\r
                                // Dont show preview if no URL given.\r
                                if ( !CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtUrl' ) ) )\r
                                {\r
@@ -419,11 +428,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                //Insert a new Link.\r
                                                if ( !this.linkEditMode )\r
                                                {\r
-                                                       editor.insertElement(this.linkElement);\r
-                                                       this.linkElement.append(this.imageElement, false);\r
+                                                       editor.insertElement( this.linkElement );\r
+                                                       this.linkElement.append( this.imageElement, false );\r
                                                }\r
                                                else     //Link already exists, image not.\r
-                                                       editor.insertElement(this.imageElement );\r
+                                                       editor.insertElement( this.imageElement );\r
                                        }\r
                                        else\r
                                                editor.insertElement( this.imageElement );\r
@@ -449,8 +458,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                if ( dialogType != 'image' )\r
                                        this.hidePage( 'Link' );                //Hide Link tab.\r
                                var doc = this._.element.getDocument();\r
-                               this.addFocusable( doc.getById( btnResetSizeId ), 5 );\r
-                               this.addFocusable( doc.getById( btnLockSizesId ), 5 );\r
+\r
+                               if ( this.getContentElement( 'info', 'ratioLock' ) )\r
+                               {\r
+                                       this.addFocusable( doc.getById( btnResetSizeId ), 5 );\r
+                                       this.addFocusable( doc.getById( btnLockSizesId ), 5 );\r
+                               }\r
 \r
                                this.commitContent = commitContent;\r
                        },\r
@@ -533,7 +546,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                        {\r
                                                                                                if ( type == IMAGE )\r
                                                                                                {\r
-                                                                                                       var url = element.getAttribute( '_cke_saved_src' ) || element.getAttribute( 'src' );\r
+                                                                                                       var url = element.data( 'cke-saved-src' ) || element.getAttribute( 'src' );\r
                                                                                                        var field = this;\r
 \r
                                                                                                        this.getDialog().dontResetSize = true;\r
@@ -547,8 +560,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                        {\r
                                                                                                if ( type == IMAGE && ( this.getValue() || this.isChanged() ) )\r
                                                                                                {\r
-                                                                                                       element.setAttribute( '_cke_saved_src', decodeURI( this.getValue() ) );\r
-                                                                                                       element.setAttribute( 'src', decodeURI( this.getValue() ) );\r
+                                                                                                       element.data( 'cke-saved-src', this.getValue() );\r
+                                                                                                       element.setAttribute( 'src', this.getValue() );\r
                                                                                                }\r
                                                                                                else if ( type == CLEANUP )\r
                                                                                                {\r
@@ -607,17 +620,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                },\r
                                                {\r
                                                        type : 'hbox',\r
-                                                       widths : [ '140px', '240px' ],\r
                                                        children :\r
                                                        [\r
                                                                {\r
+                                                                       id : 'basic',\r
                                                                        type : 'vbox',\r
-                                                                       padding : 10,\r
                                                                        children :\r
                                                                        [\r
                                                                                {\r
                                                                                        type : 'hbox',\r
-                                                                                       widths : [ '70%', '30%' ],\r
+                                                                                       widths : [ '50%', '50%' ],\r
                                                                                        children :\r
                                                                                        [\r
                                                                                                {\r
@@ -629,8 +641,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                        type : 'text',\r
                                                                                                                        width: '40px',\r
                                                                                                                        id : 'txtWidth',\r
-                                                                                                                       labelLayout : 'horizontal',\r
-                                                                                                                       label : editor.lang.image.width,\r
+                                                                                                                       label : editor.lang.common.width,\r
                                                                                                                        onKeyUp : onSizeChange,\r
                                                                                                                        onChange : function()\r
                                                                                                                        {\r
@@ -638,10 +649,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                        },\r
                                                                                                                        validate : function()\r
                                                                                                                        {\r
-                                                                                                                               var aMatch  =  this.getValue().match( regexGetSizeOrEmpty );\r
-                                                                                                                               if ( !aMatch )\r
-                                                                                                                                       alert( editor.lang.image.validateWidth );\r
-                                                                                                                               return !!aMatch;\r
+                                                                                                                               var aMatch  =  this.getValue().match( regexGetSizeOrEmpty ),\r
+                                                                                                                                       isValid = !!( aMatch && parseInt( aMatch[1], 10 ) !== 0 );\r
+                                                                                                                               if ( !isValid )\r
+                                                                                                                                       alert( editor.lang.common.invalidWidth );\r
+                                                                                                                               return isValid;\r
                                                                                                                        },\r
                                                                                                                        setup : setupDimension,\r
                                                                                                                        commit : function( type, element, internalCommit )\r
@@ -651,7 +663,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                                {\r
                                                                                                                                        if ( value )\r
                                                                                                                                                element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) );\r
-                                                                                                                                       else if ( !value && this.isChanged( ) )\r
+                                                                                                                                       else\r
                                                                                                                                                element.removeStyle( 'width' );\r
 \r
                                                                                                                                        !internalCommit && element.removeAttribute( 'width' );\r
@@ -679,8 +691,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                        type : 'text',\r
                                                                                                                        id : 'txtHeight',\r
                                                                                                                        width: '40px',\r
-                                                                                                                       labelLayout : 'horizontal',\r
-                                                                                                                       label : editor.lang.image.height,\r
+                                                                                                                       label : editor.lang.common.height,\r
                                                                                                                        onKeyUp : onSizeChange,\r
                                                                                                                        onChange : function()\r
                                                                                                                        {\r
@@ -688,10 +699,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                        },\r
                                                                                                                        validate : function()\r
                                                                                                                        {\r
-                                                                                                                               var aMatch = this.getValue().match( regexGetSizeOrEmpty );\r
-                                                                                                                               if ( !aMatch )\r
-                                                                                                                                       alert( editor.lang.image.validateHeight );\r
-                                                                                                                               return !!aMatch;\r
+                                                                                                                               var aMatch = this.getValue().match( regexGetSizeOrEmpty ),\r
+                                                                                                                                       isValid = !!( aMatch && parseInt( aMatch[1], 10 ) !== 0 );\r
+                                                                                                                               if ( !isValid )\r
+                                                                                                                                       alert( editor.lang.common.invalidHeight );\r
+                                                                                                                               return isValid;\r
                                                                                                                        },\r
                                                                                                                        setup : setupDimension,\r
                                                                                                                        commit : function( type, element, internalCommit )\r
@@ -701,11 +713,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                                {\r
                                                                                                                                        if ( value )\r
                                                                                                                                                element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );\r
-                                                                                                                                       else if ( !value && this.isChanged( ) )\r
+                                                                                                                                       else\r
                                                                                                                                                element.removeStyle( 'height' );\r
 \r
-                                                                                                                                       if ( !internalCommit && type == IMAGE )\r
-                                                                                                                                               element.removeAttribute( 'height' );\r
+                                                                                                                                       !internalCommit && element.removeAttribute( 'height' );\r
                                                                                                                                }\r
                                                                                                                                else if ( type == PREVIEW )\r
                                                                                                                                {\r
@@ -729,8 +740,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        ]\r
                                                                                                },\r
                                                                                                {\r
+                                                                                                       id : 'ratioLock',\r
                                                                                                        type : 'html',\r
-                                                                                                       style : 'margin-top:10px;width:40px;height:40px;',\r
+                                                                                                       style : 'margin-top:30px;width:40px;height:40px;',\r
                                                                                                        onLoad : function()\r
                                                                                                        {\r
                                                                                                                // Activate Reset button\r
@@ -738,10 +750,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                        ratioButton = CKEDITOR.document.getById( btnLockSizesId );\r
                                                                                                                if ( resetButton )\r
                                                                                                                {\r
-                                                                                                                       resetButton.on( 'click', function(evt)\r
+                                                                                                                       resetButton.on( 'click', function( evt )\r
                                                                                                                                {\r
                                                                                                                                        resetSize( this );\r
-                                                                                                                                       evt.data.preventDefault();\r
+                                                                                                                                       evt.data && evt.data.preventDefault();\r
                                                                                                                                }, this.getDialog() );\r
                                                                                                                        resetButton.on( 'mouseover', function()\r
                                                                                                                                {\r
@@ -770,7 +782,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                                                        updatePreview( this );\r
                                                                                                                                                }\r
                                                                                                                                        }\r
-                                                                                                                                       evt.data.preventDefault();\r
+                                                                                                                                       evt.data && evt.data.preventDefault();\r
                                                                                                                                }, this.getDialog() );\r
                                                                                                                        ratioButton.on( 'mouseover', function()\r
                                                                                                                                {\r
@@ -783,8 +795,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                                }\r
                                                                                                        },\r
                                                                                                        html : '<div>'+\r
-                                                                                                               '<a href="javascript:void(0)" tabindex="-1" title="' + editor.lang.image.unlockRatio +\r
-                                                                                                               '" class="cke_btn_locked" id="' + btnLockSizesId + '" role="button"><span class="cke_label">' + editor.lang.image.unlockRatio + '</span></a>' +\r
+                                                                                                               '<a href="javascript:void(0)" tabindex="-1" title="' + editor.lang.image.lockRatio +\r
+                                                                                                               '" class="cke_btn_locked" id="' + btnLockSizesId + '" role="checkbox"><span class="cke_icon"></span><span class="cke_label">' + editor.lang.image.lockRatio + '</span></a>' +\r
                                                                                                                '<a href="javascript:void(0)" tabindex="-1" title="' + editor.lang.image.resetSize +\r
                                                                                                                '" class="cke_btn_reset" id="' + btnResetSizeId + '" role="button"><span class="cke_label">' + editor.lang.image.resetSize + '</span></a>'+\r
                                                                                                                '</div>'\r
@@ -800,7 +812,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        type : 'text',\r
                                                                                                        id : 'txtBorder',\r
                                                                                                        width: '60px',\r
-                                                                                                       labelLayout : 'horizontal',\r
                                                                                                        label : editor.lang.image.border,\r
                                                                                                        'default' : '',\r
                                                                                                        onKeyUp : function()\r
@@ -857,7 +868,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        type : 'text',\r
                                                                                                        id : 'txtHSpace',\r
                                                                                                        width: '60px',\r
-                                                                                                       labelLayout : 'horizontal',\r
                                                                                                        label : editor.lang.image.hSpace,\r
                                                                                                        'default' : '',\r
                                                                                                        onKeyUp : function()\r
@@ -921,7 +931,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                        type : 'text',\r
                                                                                                        id : 'txtVSpace',\r
                                                                                                        width : '60px',\r
-                                                                                                       labelLayout : 'horizontal',\r
                                                                                                        label : editor.lang.image.vSpace,\r
                                                                                                        'default' : '',\r
                                                                                                        onKeyUp : function()\r
@@ -983,16 +992,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                                {\r
                                                                                                        id : 'cmbAlign',\r
                                                                                                        type : 'select',\r
-                                                                                                       labelLayout : 'horizontal',\r
                                                                                                        widths : [ '35%','65%' ],\r
                                                                                                        style : 'width:90px',\r
-                                                                                                       label : editor.lang.image.align,\r
+                                                                                                       label : editor.lang.common.align,\r
                                                                                                        'default' : '',\r
                                                                                                        items :\r
                                                                                                        [\r
                                                                                                                [ editor.lang.common.notSet , ''],\r
-                                                                                                               [ editor.lang.image.alignLeft , 'left'],\r
-                                                                                                               [ editor.lang.image.alignRight , 'right']\r
+                                                                                                               [ editor.lang.common.alignLeft , 'left'],\r
+                                                                                                               [ editor.lang.common.alignRight , 'right']\r
                                                                                                                // Backward compatible with v2 on setup when specified as attribute value,\r
                                                                                                                // while these values are no more available as select options.\r
                                                                                                                //      [ editor.lang.image.alignAbsBottom , 'absBottom'],\r
@@ -1064,10 +1072,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        [\r
                                                                                {\r
                                                                                        type : 'html',\r
+                                                                                       id : 'htmlPreview',\r
                                                                                        style : 'width:95%;',\r
                                                                                        html : '<div>' + CKEDITOR.tools.htmlEncode( editor.lang.common.preview ) +'<br>'+\r
                                                                                        '<div id="' + imagePreviewLoaderId + '" class="ImagePreviewLoader" style="display:none"><div class="loading">&nbsp;</div></div>'+\r
-                                                                                       '<div id="' + imagePreviewBoxId + '" class="ImagePreviewBox"><table><tr><td>'+\r
+                                                                                       '<div class="ImagePreviewBox"><table><tr><td>'+\r
                                                                                        '<a href="javascript:void(0)" target="_blank" onclick="return false;" id="' + previewLinkId + '">'+\r
                                                                                        '<img id="' + previewImageId + '" alt="" /></a>' +\r
                                                                                        ( editor.config.image_previewText ||\r
@@ -1098,7 +1107,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                if ( type == LINK )\r
                                                                {\r
-                                                                       var href = element.getAttribute( '_cke_saved_href' );\r
+                                                                       var href = element.data( 'cke-saved-href' );\r
                                                                        if ( !href )\r
                                                                                href = element.getAttribute( 'href' );\r
                                                                        this.setValue( href );\r
@@ -1110,9 +1119,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                {\r
                                                                        if ( this.getValue() || this.isChanged() )\r
                                                                        {\r
-                                                                               element.setAttribute( '_cke_saved_href', decodeURI( this.getValue() ) );\r
-                                                                               element.setAttribute( 'href', 'javascript:void(0)/*' +\r
-                                                                                       CKEDITOR.tools.getNextNumber() + '*/' );\r
+                                                                               var url = decodeURI( this.getValue() );\r
+                                                                               element.data( 'cke-saved-href', url );\r
+                                                                               element.setAttribute( 'href', url );\r
 \r
                                                                                if ( this.getValue() || !editor.config.image_removeLinkByEmptyURL )\r
                                                                                        this.getDialog().addLink = true;\r
@@ -1339,6 +1348,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        type : 'text',\r
                                                        id : 'txtdlgGenStyle',\r
                                                        label : editor.lang.common.cssStyle,\r
+                                                       validate : CKEDITOR.dialog.validate.inlineStyle( editor.lang.common.invalidInlineStyle ),\r
                                                        'default' : '',\r
                                                        setup : function( type, element )\r
                                                        {\r