User:Cm~mediawikiwiki/ImagePlusjs

From mediawiki.org
/**
 * # Copyright (C) 2007 Florian Mayrhuber <f_mayrhuber@gmx.at> # # This program
 * is free software; you can redistribute it and/or modify # it under the terms
 * of the GNU General Public License as published by # the Free Software
 * Foundation; either version 2 of the License, or # (at your option) any later
 * version. # # This program is distributed in the hope that it will be useful, #
 * but WITHOUT ANY WARRANTY; without even the implied warranty of #
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General
 * Public License for more details. # # You should have received a copy of the
 * GNU General Public License along # with this program; if not, write to the
 * Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA. # http://www.gnu.org/copyleft/gpl.html # # # #
 * 
 */

// global variables
var startPos; // stores startposition of selection (or cursor position)
var endPos; // stores endposition of selection
var imgName; // stores imagename
var mode; // edit markup or new image

/**
 * shows / hides image plus fields retrieves current selection and runs
 * corresponding functions
 */
function showHide () 
{
	
	// reset globals
	startPos = "";
	endPos = "";
	mode = "";
	imgName = "";
	
	// get text of current selection
	if (document.getElementById('imageplus').style.display=="none") {
		// IE
		if (document.selection && !is_gecko) {
			document.editform.wpTextbox1.focus(document.editform.wpTextbox1.caretPos);
			document.editform.wpTextbox1.caretPos = document.selection.createRange().duplicate();
			var selection = document.selection.createRange().text;
		} else { // Mozilla
			startPos =  document.editform.wpTextbox1.selectionStart;
			endPos =  document.editform.wpTextbox1.selectionEnd;
			var selection  =  document.editform.wpTextbox1.value.substring(startPos, endPos);
		}
		// get markup data from selection
		editMarkup(selection);
		// show imageplus fields
		document.getElementById('imageplus').style.display = 'block';
		createMarkup(selection);
	} else {
		// hide imageplus fields
		document.getElementById('imageplus').style.display = 'none';
	}
}

/**
 * function for editing selected markup from article retrieve all markup
 * functions from selection and set fields accordingly param selection: string
 * of current selection
 */		
function editMarkup(selection) 
{	
	// reset all fields
	document.getElementById('nonea').checked = true;
	document.getElementById('nonef').checked = true;
	document.getElementById('caption').value = "";
	document.getElementById('files').value = "";
	document.getElementById('markup').value = "";
	document.editform.size.value = ""; 			
			
	// remove blanks of selection
	selection = selection.replace(/\s+$/,"");
	selection = selection.replace(/^\s+/,"");

	// check if selection is valid markup
	options = splitMarkup(selection);
						
	// evaluate options and adapt fields acc.
	if (options != null ) {
		
		// set mode to edit and change label of insert button
		mode = "edit";
		// document.getElementById('insert').value = "Insert Markup & Close";
		
		// locate filename in files array
		var file_name = "";
		var sel_name = options[0];
		
		for (var i=0;i<aImages.length;i++) {
			file_name = aImages[i].substr(0,sel_name.length);	
			if (file_name == sel_name) {
				imgName = file_name;
				break; // image name found, exit for
			}
		}

		if ( imgName.length > 0 ) {
			var dot = imgName.lastIndexOf("."); 
			if( dot == -1 ) return ""; 
			var extension = imgName.substr(dot+1,imgName.length).toLowerCase(); 
			if (extension == 'jpg'
				|| extension == 'jpeg'
				|| extension == 'png'
				|| extension == 'gif') {
				document.getElementById('imageedit').style.display = 'block';
			} else {
				document.getElementById('imageedit').style.display = 'none';
			}
		}

		document.getElementById('files').value = imgName;
		
		// retrieve format and alignment options and set image plus fields
		// accordingly
		for (var i=1;i<options.length;i++) {
			
			var o = options[i].replace(/\s+$/,"").replace(/^\s+/,"");
			o = o.toLowerCase();
			
			if (o == "border" ) document.getElementById('border').checked = true;
			if (o == "thumb") document.getElementById('thumb').checked = true;
			if (o == "frame") document.getElementById('frame').checked = true;
			if (o == "center") document.getElementById('center').checked = true;
			if (o == "left") document.getElementById('left').checked = true;
			if (o == "right") document.getElementById('right').checked = true;
			if (o == "none") document.getElementById('nonea').checked = true;
			
			// retrieve caption
			if (o.substr(0,7) == "caption") {
				var caption = o.substring(8,o.length);
				// remove blanks
				caption = caption.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
				document.getElementById('caption').value = caption;
			}
								 
			// retrieve image size
			if (o.substr(options[i].length-2,2) == "px") {
				var size = o.substring(0,options[i].length-2);	
				var s = size.split("x");
				var width = "";
				var height = "";
				
				// remove blanks
				width = s[0].replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
				if (s.length != 1) {
					height = "x" + s[1].replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
				}
				
				// no values or zero values, do nothing
				if ((width != "") && (width != "0" && height != "0")) {
					document.getElementById('size').value = width + height; 			
				}
			}
		}
	}
	
}

/**
 * determine if current selection is valid image markup param sel: string of
 * current selection return options: array with markup options
 */
function splitMarkup( sel ) 
{
	
	if ( (sel.substr(0,8) == "[[Datei:") && (sel.substr(sel.length-2,2) == "]]") ) {
		sel = sel.substring(8,sel.length-2);
		// split selection to get options
		var options = sel.split("|");
		return options;
	} else return null;

}

/**
 * function for creating new image markup read all imageplus fields, create
 * valid wiki markup and insert it at last cursor position param obj: obj,
 * object from wich function was called to determine imagename
 */	
function createMarkup( object )
{			

	// textfield
	if ( object.id == "files" && object.value != "") {
		imgName = document.getElementById('files').value;
		// imgName = imgName.replace(/\s+$/,"").replace(/^\s+/,"");
		var pos = imgName.indexOf("(");
		imgName = imgName.substring(0,pos-1);
		document.getElementById('latestFiles').selectedIndex = 0;
	}
	
	// overwrite if called from selectbox
	if ( object.id == "latestFiles" && object.value != "none") {
		imgName = document.getElementById('latestFiles').value;
		var pos = imgName.indexOf("(");
		imgName = imgName.substring (0,pos-1);
		document.getElementById('files').value = "";
	} 

	if ( imgName.length > 0 ) {
		var dot = imgName.lastIndexOf("."); 
		if( dot == -1 ) return ""; 
		var extension = imgName.substr(dot+1,imgName.length).toLowerCase(); 
		if (extension == 'jpg'
			|| extension == 'jpeg'
			|| extension == 'png'
			|| extension == 'gif') {
			document.getElementById('imageedit').style.display = 'block';
		} else {
			document.getElementById('imageedit').style.display = 'none';
		}
	}
	
	// initialize markup with opening tag
	var markup = "[[Datei:";
	
	// assign filename
	markup += imgName; 
	
	// create markup for alignment option
	for (var i=0;i<4;i++) {
		if (document.editform.position[i].checked) {
			markup += "|" + document.editform.position[i].value;	
		}
	}
	
	// create markup for image limits
	var size = document.getElementById('size').value;
	var pos = size.indexOf("x");
	var size = size.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");		

	if (pos != "-1") { // x found => width AND height
		// remove blanks
		var width = size.substring(0,pos).replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
		var height = size.substring(pos+1,size.length).replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
		
		// no values or zero values, do nothing
		if ((width != "" || height != "") && (width != "0" || height != "0") ) 
			markup += "|" + width + "x" + height + "px";
	} else { // no x, just width
		if (!isNaN(size) && size != "" && size != "0") markup += "|" + size + "px";
	}
	
	// create markup for format options
	for (var i=0;i<3;i++) {
		if (document.editform.format[i].checked) {
			markup += "|" + document.editform.format[i].value;
		} 
	}
		
	// create caption markup
	var cap = document.getElementById('caption').value;
	if (cap != "Caption") {
		cap = cap.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,"");
		if (cap != "") markup +=  "|" + cap;		
	}
				
	// add closing tag and put update preview field
	document.getElementById('markup').value = markup + "]]";

}

/**
 * function for inserting markup into wiki textarea at last cursor position
 */	
function insertMarkup()
{	
	
	var obj = document.editform.wpTextbox1;
	
	// existing tag being updated
	if (endPos != "") {	// Mozilla
		obj.value = obj.value.substr(0,startPos) + document.getElementById('markup').value + 
		obj.value.substr(endPos,obj.value.length);
		
		// reset cursor position
		startPos = startPos + document.getElementById('markup').value.length;
		endPos = startPos;
	} else { // IE, goto cursor pos
		obj.caretPos.text = document.getElementById('markup').value;	
	}	
	
	// if image markup was edited close image+
	document.getElementById('imageplus').style.display = 'none';
	
}

/**
 * function for formatting the autocomplete textfield result list param sText:
 * string that should be transformed return: formatted string
 */
function formatFiles( sText )
{

	return sText.substr( 0, sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ), this.sActiveValue.length ).bold().fontcolor( '#000000' ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) + this.sActiveValue.length );

}