Extension:FCKeditor (by Mafs)/fck image js

From MediaWiki.org

Jump to: navigation, search

Copy-How-To

The source code contains special html entities which are not correctly displayed in normal page view therefore you MUST open the page in edit mode for copying the code.

After opening the page in edit mode copy from "==>" to "<==" at the end of the page.



// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = false;
var sajax_request_type = "GET";
 
 
var started;
var typing;
var memory=null;
var body=null;
var oldbody=null;
 
 
function sajax_debug(text) {
        if (sajax_debug_mode)
                alert("RSD: " + text)
}
 
 
function sajax_init_object() {
        sajax_debug("sajax_init_object() called..")
        var A;
        try {
                A=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        A=new ActiveXObject("Microsoft.XMLHTTP");
                } catch (oc) {
                        A=null;
                }
        }
        if(!A && typeof XMLHttpRequest != "undefined")
                A = new XMLHttpRequest();
        if (!A)
                sajax_debug("Could not create connection object.");
        return A;
}
 
 
function sajax_do_call(func_name, args) {
        var i, x, n;
        var uri;
        var post_data;
        uri = wgServer + "/" + wgScriptPath + "/index.php?action=ajax";
        if (sajax_request_type == "GET") {
                if (uri.indexOf("?") == -1)
                        uri = uri + "?rs=" + escape(func_name);
                else
                        uri = uri + "&rs=" + escape(func_name);
                for (i = 0; i < args.length-1; i++)
                        uri = uri + "&rsargs[]=" + escape(args[i]);
                //uri = uri + "&rsrnd=" + new Date().getTime();
                post_data = null;
        } else {
                post_data = "rs=" + escape(func_name);
                for (i = 0; i < args.length-1; i++)
                        post_data = post_data + "&rsargs[]=" + escape(args[i]);
        }
        //alert(uri);
        x = sajax_init_object();
        x.open(sajax_request_type, uri, true);
        if (sajax_request_type == "POST") {
                x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
                x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }
        x.setRequestHeader("Pragma", "cache=yes");
        x.setRequestHeader("Cache-Control", "no-transform");
        x.onreadystatechange = function() {
                if (x.readyState != 4)
                        return;
                sajax_debug("received " + x.responseText);
                var status;
                var data;
                status = x.responseText.charAt(0);
                data = x.responseText.substring(2);
                if (status == "-")
                        alert("Error: " + data);
                else
                        args[args.length-1](data);
        }
        x.send(post_data);
        sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
        sajax_debug(func_name + " waiting..");
        delete x;
}
 
 
 
 
 
 
// Remove the typing barrier to allow call() to complete
function Search_doneTyping()
{
        typing=false;
}
 
// Wait 500ms to run call()
function Searching_Go()
{
        setTimeout("Searching_Call()", 500);
}
 
// If the user is typing wait until they are done.
function Search_Typing() {
        started=true;
        typing=true;
        window.status = "Waiting until you're done typing...";
        setTimeout("Search_doneTyping()", 500);
 
        // I believe these are needed by IE for when the users press return?
        if (window.event)
        {
                if (event.keyCode == 13)
                {
                        event.cancelBubble = true;
                        event.returnValue = false;
                }
        }
}
 
// Set the body div to the results
function Searching_SetResult(result)
{
        //patch : remove first byte
        if (result.substr(0,1)==":") result = result.substr(1, result.length-1);
 
        pat = /<input[^>]*hidden[^>]*wfSajaxSearchImageFCKeditor[^>]*>/
        if (!result.match(pat)) result = "ajax request error.";
 
 
        //body.innerHTML = result;
        t = document.getElementById("searchTarget");
        if ( t == null ) {
                oldbody=body.innerHTML;
                body.innerHTML= '<div id="searchTargetContainer"><div id="searchTarget" ></div></div>' ;
                t = document.getElementById("searchTarget");
        }
        t.innerHTML = result;
        t.style.display='block';
}
 
function Searching_Hide_Results()
{
        t = document.getElementById("searchTarget");
        t.style.display='none';
        body.innerHTML = oldbody;
}
 
 
// This will call the php function that will eventually
// return a results table
function Searching_Call()
{
        var x;
        Searching_Go();
 
        //Don't proceed if user is typing
        if (typing)
                return;
 
        x = document.getElementById("txtImage").value;
 
        // Don't search again if the query is the same
        if (x==memory)
                return;
 
        memory=x;
        if (started) {
                // Don't search for blank or < 3 chars.
                if ((x=="") || (x.length < 3))
                {
                        return;
                }
                x_wfSajaxSearchImage(x, Searching_SetResult);
        }
}
 
function x_wfSajaxSearchImage() {
        sajax_do_call( "wfSajaxSearchImageFCKeditor", x_wfSajaxSearchImage.arguments );
}
 
 
 
 
 
/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 *              http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 *              http://www.fckeditor.net/
 * 
 * "Support Open Source software. What about a donation today?"
 * 
 * File Name: fck_image.js
 *      Scripts related to the Image dialog window (see fck_image.html).
 * 
 * File Authors:
 *              Frederico Caldeira Knabben (fredck@fckeditor.net)
 *
 * Modified version : image dialog for mediawiki
 *                   see http://meta.wikimedia.org/wiki/FCKeditor
 */
 
var oEditor             = window.parent.InnerDialogLoaded() ;
var FCK                 = oEditor.FCK ;
var FCKLang             = oEditor.FCKLang ;
var FCKConfig   = oEditor.FCKConfig ;
var FCKDebug    = oEditor.FCKDebug ;
 
 
 
 
 
 
// patch 
 
if (FCKConfig.mwScriptPath == "") alert("You must specify \"FCKConfig.mwScriptPath\" in fckconfig.js.");
if (FCKConfig.mwScriptPath.substr(0, 1) != "/") FCKConfig.mwScriptPath = "/"+FCKConfig.mwScriptPath
 
var wgServer = ""; 
var wgScriptPath = FCKConfig.mwScriptPath.substr(1, FCKConfig.mwScriptPath.length-1);
 
var mwImageName;
var mwImageFullName; //with prefix
var mwURLImage;
var mwURLImagePage;
 
function clickOnList(ImageName, URLImage, URLImagePage, ImageFullName) {
 
        GetE('txtUrl').value = URLImage;
        mwImageName = ImageName;
        mwURLImagePage = URLImagePage;
        mwImageFullName = ImageFullName;
 
        UpdatePreview();
}
 
 
 
var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
 
//#### Dialog Tabs
 
// Set the dialog tabs.
window.parent.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
 
if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
        window.parent.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
 
if ( FCKConfig.ImageUpload )
        window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
 
if ( !FCKConfig.ImageDlgHideAdvanced )
        window.parent.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
 
// Function called when a dialog tag is selected.
function OnDialogTabChange( tabCode )
{
        ShowE('divInfo'         , ( tabCode == 'Info' ) ) ;
        ShowE('divLink'         , ( tabCode == 'Link' ) ) ;
        ShowE('divUpload'       , ( tabCode == 'Upload' ) ) ;
        ShowE('divAdvanced'     , ( tabCode == 'Advanced' ) ) ;
}
 
// Get the selected image (if available).
var oImage = FCK.Selection.GetSelectedElement() ;
 
if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
        oImage = null ;
 
// Get the active link.
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
 
var oImageOriginal ;
 
function UpdateOriginal( resetSize )
{
        if ( !eImgPreview )
                return ;
 
        oImageOriginal = document.createElement( 'IMG' ) ; // new Image() ;
 
        if ( resetSize )
        {
                oImageOriginal.onload = function()
                {
                        this.onload = null ;
                        ResetSizes() ;
                }
        }
 
        oImageOriginal.src = eImgPreview.src ;
}
 
var bPreviewInitialized ;
 
window.onload = function()
{
        // Translate the dialog box texts.
        oEditor.FCKLanguageManager.TranslatePage(document) ;
 
        GetE('btnLockSizes').title = FCKLang.DlgImgLockRatio ;
        GetE('btnResetSize').title = FCKLang.DlgBtnResetSize ;
 
        // Load the selected element information (if any).
        LoadSelection() ;
 
        // Show/Hide the "Browse Server" button.
        GetE('tdBrowse').style.display                  = FCKConfig.ImageBrowser        ? '' : 'none' ;
        GetE('divLnkBrowseServer').style.display        = FCKConfig.LinkBrowser         ? '' : 'none' ;
 
        UpdateOriginal() ;
 
        // Set the actual uploader URL.
        if ( FCKConfig.ImageUpload )
                GetE('frmUpload').action = FCKConfig.ImageUploadURL ;
 
        window.parent.SetAutoSize( true ) ;
 
        // Activate the "OK" button.
        window.parent.SetOkButton( true ) ;
 
 
        //patch: suggest list
        x = document.getElementById( 'txtImage' );
        x.onkeypress= function() { Search_Typing(); };
        Searching_Go();
        body = document.getElementById("txtImageRes");
        body.innerHTML = "";
        GetE('txtImage').focus() ;
}
 
function LoadSelection()
{
        if ( ! oImage ) return ;
 
        var sUrl = GetAttribute( oImage, '_fcksavedurl', '' ) ;
        if ( sUrl.length == 0 )
                sUrl = GetAttribute( oImage, 'src', '' ) ;
 
        // TODO: Wait stable version and remove the following commented lines.
//      if ( sUrl.startsWith( FCK.BaseUrl ) )
//              sUrl = sUrl.remove( 0, FCK.BaseUrl.length ) ;
 
        GetE('txtUrl').value    = sUrl ;
        GetE('txtAlt').value    = GetAttribute( oImage, 'alt', '' ) ;
        GetE('txtVSpace').value = GetAttribute( oImage, 'vspace', '' ) ;
        GetE('txtHSpace').value = GetAttribute( oImage, 'hspace', '' ) ;
        GetE('txtBorder').value = GetAttribute( oImage, 'border', '' ) ;
        GetE('cmbAlign').value  = GetAttribute( oImage, 'align', '' ) ;
 
        //patch
        mwImageFullName = GetE('txtAlt').value;
 
        var iWidth, iHeight ;
 
        var regexSize = /^\s*(\d+)px\s*$/i ;
 
        if ( oImage.style.width )
        {
                var aMatch  = oImage.style.width.match( regexSize ) ;
                if ( aMatch )
                {
                        iWidth = aMatch[1] ;
                        oImage.style.width = '' ;
                }
        }
 
        if ( oImage.style.height )
        {
                var aMatch  = oImage.style.height.match( regexSize ) ;
                if ( aMatch )
                {
                        iHeight = aMatch[1] ;
                        oImage.style.height = '' ;
                }
        }
 
        GetE('txtWidth').value  = iWidth ? iWidth : GetAttribute( oImage, "width", '' ) ;
        GetE('txtHeight').value = iHeight ? iHeight : GetAttribute( oImage, "height", '' ) ;
 
        // Get Advances Attributes
        GetE('txtAttId').value                  = oImage.id ;
        GetE('cmbAttLangDir').value             = oImage.dir ;
        GetE('txtAttLangCode').value            = oImage.lang ;
        GetE('txtAttTitle').value               = oImage.title ;
        GetE('txtAttClasses').value             = oImage.getAttribute('class',2) || '' ;
        GetE('txtLongDesc').value               = oImage.longDesc ;
 
        if ( oEditor.FCKBrowserInfo.IsIE )
                GetE('txtAttStyle').value       = oImage.style.cssText ;
        else
                GetE('txtAttStyle').value       = oImage.getAttribute('style',2) ;
 
        if ( oLink )
        {
                var sUrl = GetAttribute( oLink, '_fcksavedurl', '' ) ;
                if ( sUrl.length == 0 )
                        sUrl = oLink.getAttribute('href',2) ;
 
                GetE('txtLnkUrl').value         = sUrl ;
                GetE('cmbLnkTarget').value      = oLink.target ;
        }
 
        UpdatePreview() ;
}
 
//#### The OK button was hit.
function Ok()
{ 
        if ( GetE('txtUrl').value.length == 0 )
        {
                window.parent.SetSelectedTab( 'Info' ) ;
                GetE('txtUrl').focus() ;
 
                //alert( FCKLang.DlgImgAlertUrl ) ;
 
                return false ;
        }
 
        var bHasImage = ( oImage != null ) ;
 
        if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
        {
                if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
                        oImage = null ;
        }
        else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
        {
                if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
                        oImage = null ;
        }
 
        if ( !bHasImage )
        {
                if ( bImageButton )
                {
                        oImage = FCK.EditorDocument.createElement( 'INPUT' ) ;
                        oImage.type = 'image' ;
                        oImage = FCK.InsertElementAndGetIt( oImage ) ;
                }
                else
                        oImage = FCK.CreateElement( 'IMG' ) ;
        }
        else
                oEditor.FCKUndo.SaveUndoStep() ;
 
        UpdateImage( oImage ) ;
 
        //patch
        //var sLnkUrl = GetE('txtLnkUrl').value.trim() ;
        var sLnkUrl = mwURLImagePage ; 
 
        if ( sLnkUrl.length == 0 )
        {
                if ( oLink )
                        FCK.ExecuteNamedCommand( 'Unlink' ) ;
        }
        else
        {
                if ( oLink )    // Modifying an existent link.
                        oLink.href = sLnkUrl ;
                else                    // Creating a new link.
                {
                        if ( !bHasImage )
                                oEditor.FCKSelection.SelectNode( oImage ) ;
 
                        oLink = oEditor.FCK.CreateLink( sLnkUrl ) ;
 
                        if ( !bHasImage )
                        {
                                oEditor.FCKSelection.SelectNode( oLink ) ;
                                oEditor.FCKSelection.Collapse( false ) ;
                        }
                }
 
                SetAttribute( oLink, '_fcksavedurl', sLnkUrl ) ;
                SetAttribute( oLink, 'target', GetE('cmbLnkTarget').value ) ;
        }
 
        return true ;
}
 
function UpdateImage( e, skipId )
{
        //patch
        GetE('txtAlt').value    = mwImageFullName;
 
        e.src = GetE('txtUrl').value ;
        SetAttribute( e, "_fcksavedurl", GetE('txtUrl').value ) ;
        //patch
        //SetAttribute( e, "alt"   , GetE('txtAlt').value ) ;
        SetAttribute( e, "alt"   , mwImageFullName ) ;
 
        SetAttribute( e, "width" , GetE('txtWidth').value ) ;
        SetAttribute( e, "height", GetE('txtHeight').value ) ;
        SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
        SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
        SetAttribute( e, "border", GetE('txtBorder').value ) ;
        SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
 
        // Advances Attributes
 
        if ( ! skipId )
                SetAttribute( e, 'id', GetE('txtAttId').value ) ;
 
        SetAttribute( e, 'dir'          , GetE('cmbAttLangDir').value ) ;
        SetAttribute( e, 'lang'         , GetE('txtAttLangCode').value ) ;
        //patch
        //SetAttribute( e, 'title'      , GetE('txtAttTitle').value ) ;
        SetAttribute( e, 'title'        , mwImageFullName ) ;
        //patch
        //SetAttribute( e, 'class'      , GetE('txtAttClasses').value ) ;
        SetAttribute( e, 'class'        , "image" ) ;
        //patch
        //SetAttribute( e, 'longDesc'   , GetE('txtLongDesc').value ) ;
        SetAttribute( e, 'longDesc'     , GetE('txtUrl').value ) ;
 
        if ( oEditor.FCKBrowserInfo.IsIE )
                e.style.cssText = GetE('txtAttStyle').value ;
        else
                SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
}
 
var eImgPreview ;
var eImgPreviewLink ;
 
function SetPreviewElements( imageElement, linkElement )
{
        eImgPreview = imageElement ;
        eImgPreviewLink = linkElement ;
 
        UpdatePreview() ;
        UpdateOriginal() ;
 
        bPreviewInitialized = true ;
}
 
 
function UpdatePreview()
{
        //patch
        document.getElementById( 'txtImageName' ).innerHTML = mwImageFullName;
 
        if ( !eImgPreview || !eImgPreviewLink )
                return ;
 
        if ( GetE('txtUrl').value.length == 0 )
                eImgPreviewLink.style.display = 'none' ;
        else
        {
                UpdateImage( eImgPreview, true ) ;
 
                if ( GetE('txtLnkUrl').value.trim().length > 0 )
                        eImgPreviewLink.href = 'javascript:void(null);' ;
                else
                        SetAttribute( eImgPreviewLink, 'href', '' ) ;
 
                eImgPreviewLink.style.display = '' ;
        }
 
}
 
var bLockRatio = true ;
 
function SwitchLock( lockButton )
{
        bLockRatio = !bLockRatio ;
        lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
        lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
 
        if ( bLockRatio )
        {
                if ( GetE('txtWidth').value.length > 0 )
                        OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
                else
                        OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
        }
}
 
// Fired when the width or height input texts change
function OnSizeChanged( dimension, value )
{
        // Verifies if the aspect ration has to be mantained
        if ( oImageOriginal && bLockRatio )
        {
                var e = dimension == 'Width' ? GetE('txtHeight') : GetE('txtWidth') ;
 
                if ( value.length == 0 || isNaN( value ) )
                {
                        e.value = '' ;
                        return ;
                }
 
                if ( dimension == 'Width' )
                        value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value  / oImageOriginal.width ) ) ;
                else
                        value = value == 0 ? 0 : Math.round( oImageOriginal.width  * ( value / oImageOriginal.height ) ) ;
 
                if ( !isNaN( value ) )
                        e.value = value ;
        }
 
        UpdatePreview() ;
}
 
// Fired when the Reset Size button is clicked
function ResetSizes()
{
        if ( ! oImageOriginal ) return ;
 
        GetE('txtWidth').value  = oImageOriginal.width ;
        GetE('txtHeight').value = oImageOriginal.height ;
 
        UpdatePreview() ;
}
 
function BrowseServer()
{
        OpenServerBrowser(
                'Image',
                FCKConfig.ImageBrowserURL,
                FCKConfig.ImageBrowserWindowWidth,
                FCKConfig.ImageBrowserWindowHeight ) ;
}
 
function LnkBrowseServer()
{
        OpenServerBrowser(
                'Link',
                FCKConfig.LinkBrowserURL,
                FCKConfig.LinkBrowserWindowWidth,
                FCKConfig.LinkBrowserWindowHeight ) ;
}
 
function OpenServerBrowser( type, url, width, height )
{
        sActualBrowser = type ;
        OpenFileBrowser( url, width, height ) ;
}
 
var sActualBrowser ;
 
function SetUrl( url, width, height, alt )
{
 
        if ( sActualBrowser == 'Link' )
        {
                GetE('txtLnkUrl').value = url ;
                UpdatePreview() ;
        }
        else
        {
                GetE('txtUrl').value = url ;
                GetE('txtWidth').value = width ? width : '' ;
                GetE('txtHeight').value = height ? height : '' ;
 
                if ( alt )
                        GetE('txtAlt').value = alt;
 
                UpdatePreview() ;
                UpdateOriginal( true ) ;
        }
 
        window.parent.SetSelectedTab( 'Info' ) ;
}
 
function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
{
        switch ( errorNumber )
        {
                case 0 :   // No errors
                        alert( 'Your file has been successfully uploaded' ) ;
                        break ;
                case 1 :   // Custom error
                        alert( customMsg ) ;
                        return ;
                case 101 : // Custom warning
                        alert( customMsg ) ;
                        break ;
                case 201 :
                        alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
                        break ;
                case 202 :
                        alert( 'Invalid file type' ) ;
                        return ;
                case 203 :
                        alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
                        return ;
                default :
                        alert( 'Error on file upload. Error number: ' + errorNumber ) ;
                        return ;
        }
 
        sActualBrowser = ''
        SetUrl( fileUrl ) ;
        GetE('frmUpload').reset() ;
}
 
var oUploadAllowedExtRegex      = new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
var oUploadDeniedExtRegex       = new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
 
function CheckUpload()
{
        var sFile = GetE('txtUploadFile').value ;
 
        if ( sFile.length == 0 )
        {
                alert( 'Please select a file to upload' ) ;
                return false ;
        }
 
        if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
                ( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
        {
                OnUploadCompleted( 202 ) ;
                return false ;
        }
 
        return true ;
}
 
// ******************************************************************************************
 
// section modified image dialog
 
FCKConfig.LinkDlgHideTarget     = true ;
FCKConfig.LinkDlgHideAdvanced   = true ;
FCKConfig.ImageDlgHideLink      = true ;
FCKConfig.ImageDlgHideAdvanced  = true ;
FCKConfig.FlashDlgHideAdvanced  = true ;
 
FCKConfig.LinkBrowser   = false ;
FCKConfig.ImageBrowser  = false ;
FCKConfig.FlashBrowser  = false ;
FCKConfig.LinkUpload    = false ;
FCKConfig.ImageUpload   = false ;
FCKConfig.FlashUpload   = false ;
 
FCKConfig.mwScriptPath = "/wiki";     // When using the modified image dialog
                                 // you must set this variable. It must correspond to 
                                 // $wgScriptPath in LocalSettings.php.
 
// end section modified image dialog
 
FCKConfig.ToolbarSets["Wiki"] = [
        ['Source','-','Save','NewPage','-'],
        ['Cut','Copy','Paste','PasteText','PasteWord','-','Print'],
        ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
        ['Bold','Italic','Underline','StrikeThrough','-','Subscript','Superscript'],
        ['OrderedList','UnorderedList','-','Outdent','Indent'],
        ['JustifyLeft','JustifyCenter','JustifyRight','JustifyFull'],
        ['Link','Unlink','Anchor'],
        ['Image','Table','Rule','Smiley','SpecialChar','PageBreak','UniversalKey'],
        ['Style','FontFormat','FontName','FontSize'],
        ['TextColor','BGColor'],
        ['FitWindow','-','About']
] ;
Personal tools