/*
Copyright CodeCogs 2007-2008
Written by Will Bateman.
GNU General Public License Agreement
Copyright (C) 2004-2008 CodeCogs, Zyba Ltd, Broadwood, Holford, TA5 1DU, England.
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 Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
You must retain a copy of this licence in all copies.
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.
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, see .
*/
var changed=false;
var orgtxt='';
var EQUATION_ENGINE='http://codecogs.izyba.com';
// Clears the main editor window
function cleartext() {
var id=document.getElementById('latex_formula'); id.value = ""; id.focus();
changed=false;
document.getElementById('copybutton').className='greybutton0';
document.getElementById('renderbutton').className='greybutton';
document.getElementById('clipboardbutton').className='greybutton0'; }
function textchanged() {
var txt=getEquationStr();
if(txt!=orgtxt)
{
orgtxt=txt;
document.getElementById('copybutton').className='lightbluebutton0';
document.getElementById('clipboardbutton').className='lightbluebutton0';
document.getElementById('renderbutton').className='bluebutton';
changed=true;
}
}
function formatchanged()
{
var action=false;
var format=document.getElementById('format');
if(format) { var type=format.value;
switch(type) {
case 'gif' : action=false; break;
case 'png' : action=false; break;
case 'tex' : action=false; break;
}
}
changed=true;
renderEqn();
}
// Tries to inserts text at the cursor position of text area
// wind = document <- when inserting text into the current equation editor box
// wind = window.opener.document <- when inserting text into a parent window box
function addText(wind, textbox, txt)
{
myField = wind.getElementById(textbox);
// IE
if (wind.selection) {
myField.focus();
sel = wind.selection.createRange();
sel.text = txt;
}
// MOZILLA
else
{
var scrolly=myField.scrollTop;
if (myField.selectionStart || myField.selectionStart == '0')
{
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var cursorPos = startPos + txt.length;
myField.value = myField.value.substring(0, startPos) + txt
+ myField.value.substring(endPos, myField.value.length);
pos = txt.length + endPos - startPos;
myField.selectionStart = cursorPos;
myField.selectionEnd = cursorPos;
myField.focus();
myField.setSelectionRange(startPos + pos,startPos + pos);
}
else myField.value += txt;
myField.scrollTop=scrolly;
}
}
function insertText( txt, pos, inspos )
{
var insert_pos=(inspos==null)?pos:inspos;
textchanged()
// pos = optional parameter defining where in inserted text to put the caret
// if undefined put at end of inserted text
// if pos=1000 then using style options and move to just before final }
// startPos = final position of caret in complete text
if (pos==1000) {pos=txt.length-1};
if (pos==undefined) { txt+=' '; pos=txt.length;}; // always insert a space after
// my textarea is called latex_formula
myField = document.getElementById('latex_formula');
if (document.selection) {
// IE
myField.focus();
var sel = document.selection.createRange();
// find current caret position
var i = myField.value.length+1;
theCaret = sel.duplicate();
while (theCaret.parentElement()==myField
&& theCaret.move("character",1)==1) --i;
// take account of line feeds
var startPos = i - myField.value.split('\n').length + 1 ;
if ((txt.substring(1,5) == "left" || txt.substring(insert_pos-1,insert_pos)=='{') && sel.text.length) {
// allow highlighted text to be bracketed
if(txt.substring(1,5) == "left") ins_point=7;
else ins_point=insert_pos;
if(inspos==null) pos = txt.length + sel.text.length + 1;
else if(inspos startPos) {
// allow highlighted text to be bracketed
if(txt.substring(1,5) == "left") ins_point=7;
else ins_point=insert_pos;
if(inspos==null) pos = txt.length + endPos - startPos + 1;
else if(inspos";
img.onload = processEquationChange;
img.src=EQUATION_ENGINE+'/'+format+'.latex?' + val;
changed=false;
} break;
case 0 : // site
{
img.onload ='';
img.src='/editor/images/spacer.gif';
var div = document.getElementById('equationcomment');
div.innerHTML = "
";
/* div.innerHTML = '$$' + val + '$$' ; */
div.onload = processEquationChange;
if(document.getElementById('inline').checked)
{ div.innerHTML = '\\(' + val + '\\)' ;
}
else
{ div.innerHTML = '$$' + val + '$$' ; }
img.src=EQUATION_ENGINE+'/'+'gif'+'.latex?' + val
changed=false;
} break;
}
}
else
{
var div = document.getElementById('equationcomment');
if(bracket<0) div.innerHTML = "
You have more closed '}' brackets than open '{' brackets";
else div.innerHTML = "
You have more open '{' brackets than closed '}' brackets";
}
}
/* We don't want to render with every keystroke, so we delay this part
This function will be called with each keystroke
- n is a tens of second unit
*/
var auton=0;
function renderCountdown()
{
if(auton>0)
{ auton--; setTimeout('renderCountdown()', 100); }
else renderEqn(null);
}
function autorenderEqn(n)
{
if(auton>0 && n>0) auton=n;
else { auton=n; renderCountdown(); }
}
/* The following code adds a favorite to the favorite drop down.
To achieve this, we must update the users cookies */
function addfavorite(dropdown)
{
text=document.getElementById('latex_formula').value;
var sel=document.getElementById(dropdown);
var num=sel.selectedIndex
if(num==0 || !confirm("Click Yes to replace the current equation or 'Cancel' to add a new equation"))
{
var name = prompt('Введите короткое имя для этой формулы:', '')
if(name==null || name=='') name=text.substr(0,15);
/* Add to favorite */
var j=sel.length;
sel.length=j+1;
sel.options[j].text=name;
sel.options[j].value=text;
sel.options[j].title=text;
/* Make sure cookie name is unique to website */
document.cookie = "eq_"+escape(name.replace(/\+/g,"+").replace(/\s/g,"&space;"))+"="+escape(text.replace(/\+/g,"+"))+"; path=/";
}
else
{
sel.options[num].value=text;
sel.options[num].title=text;
/* Make sure cookie name is unique to website */
document.cookie = "eq_"+escape(name.replace(/\+/g,"+").replace(/\s/g,"&space;"))+"="+escape(text.replace(/\+/g,"+"))+"; path=/";
}
}
function deletefavorite(dropdown)
{
var sel=document.getElementById(dropdown);
var num=sel.selectedIndex
if(num>0)
{
name=sel.options[num].text;
var mydate = new Date();
mydate.setTime(mydate.getTime() - 1);
document.cookie = "eq_"+escape(name.replace(/\+/g,"+").replace(/\s/g,"&space;"))+"=; expires="+mydate.toGMTString()+"; path=/";
sel.options[num]=null;
}
}
/* Help to generate a Matrix */
// generate a set of aligned equations - thornahawk
// isNumbered: switches between numbered and not numbered equations
function makeEquationsMatrix(type, isNumbered, isConditional)
{
if (isNumbered==undefined) isNumbered=false;
if (isConditional==undefined) isNumbered=false;
var eqns="\\begin{"+type+((isNumbered)?"":"*")+"}";
var eqi="\n &"+((isNumbered)?" ":"= ")+((isConditional)?"\\text{ if } x= ":"");
var eqEnd="\n\\end{"+type+((isNumbered)?"":"*")+"}";
var i=0;
var dim = prompt('Введите число строк:', '');
if (dim != '' && dim != null) {
n=parseInt(dim);
if (!isNaN(n)) {
for (i=1;i<=n-1;i++) eqns=eqns+(eqi+"\\\\ ");
eqns=(eqns+eqi)+eqEnd;
insertText(eqns,type.length+((isNumbered)?0:1)+9);
}
}
}
// generate an array of specified dimensions - thornahawk
// type: sets the type of array, e.g. pmatrix
function makeArrayMatrix( type, start, end )
{
var matr=start+'\\begin{'+type+'matrix}';
var row="\n";
var mend="\n\\end{"+type+"matrix}"+end;
var i=0;
var dim = prompt('Введите размер массива, разделяя запятой (пример "2,3" для 2 строк и 3 колонок):', '')
if (dim!='' && dim!=null)
{
dim=dim.split(',');
m=parseInt(dim[0]);
n=parseInt(dim[1]);
if (!isNaN(m) && !isNaN(n))
{
for (i=2;i<=n;i++) row=row+' & ';
for (i=1;i<=m-1;i++) matr=matr+row+'\\\\ ';
matr=matr+row+mend;
insertText(matr,type.length+start.length+15);
}
}
}
function hover(elm, e)
{
div=document.getElementById('hover');
div.innerHTML='
';
if(document.all) { div.style.top = (event.clientY-10)+'px'; div.style.left = (event.clientX+20)+'px'; }
else { if(!e) var e=window.event; div.style.top = (e.pageY-10)+'px'; div.style.left = (e.pageX+20)+'px'; }
div.style.display='block';
elm.onmouseout= function() { document.getElementById('hover').style.display='none';}
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Turnea Iulian :: http://www.eurografic.ro */
function iObject() { this.i; return this; }
var myObject=new iObject();
myObject.i=0;
var myObject2=new iObject();
myObject2.i=0;
store_text=new Array();
//store_text[0] store initial textarea value
store_text[0]="";
function countclik(tag) {
var x=tag.value;
if(myObject.i==0 || store_text[myObject.i]!=x)
{
myObject.i++;
var y=myObject.i;
store_text[y]=x;
}
myObject2.i=0;
document.getElementById('redobutton').src="/editor/images/redo-x.gif";
document.getElementById('undobutton').src="/editor/images/undo.gif";
}
function undo(box) {
tag=document.getElementById(box);
if ((myObject2.i)<(myObject.i)) {
myObject2.i++;
if(myObject2.i==myObject.i) {
document.getElementById('undobutton').src="/editor/images/undo-x.gif"; }
document.getElementById('redobutton').src="/editor/images/redo.gif";
} else {
alert("Finish Undo Action");
}
var z=store_text.length;
z=z-myObject2.i;
if (store_text[z]) { tag.value=store_text[z]; }
else { tag.value=store_text[0]; }
tag.focus();
}
function redo(box) {
tag=document.getElementById(box);
if((myObject2.i)>1) {
myObject2.i--;
if(myObject2.i==1){
document.getElementById('redobutton').src="/editor/images/redo-x.gif"; }
document.getElementById('undobutton').src="/editor/images/undo.gif";
} else {
alert("Finish Redo Action");
}
var z=store_text.length;
z=z-myObject2.i;
if (store_text[z]) { tag.value=store_text[z]; }
else { tag.value=store_text[0]; }
tag.focus();
}
function updateOpener(target, type)
{
if(target!='')
{ // *** Traditional mode with plane HTML text box ***
var text;
if(type=='phpBB')
{ // Create LaTeX string for insertion into phpBB forum
text=getEquationStr();
text = '[tex]' + text + '[/tex]\n';
}
else if(type=='html')
{ // Create
tag for insertion into html
text=getEquationStr();
text = '
';
}
else
{ // Create LaTeX string for insertion into CodeCogs of DOxygen C/C++ markup language
// Note size and
text=document.getElementById('latex_formula').value;
var size = document.getElementById('fontsize');
if(size && size.selectedIndex!=2)
text=size.options[size.selectedIndex].value+' '+text;
if(document.getElementById('inline').checked)
{
if(!document.getElementById('compressed').checked) text='\\displaystyle '+text;
text = '$' + text + '$ ';
}
else
{
if(document.getElementById('compressed').checked) text='\\inline '+text;
text = '\\[' + text + '\\]\n';
}
}
addText(window.opener.document,target,text);
}
else
{ // *** Advanced mode with FCKEditor ***
var text=document.getElementById('latex_formula').value;
var size = document.getElementById('fontsize');
if(size && size.selectedIndex!=2)
text=size.options[size.selectedIndex].value+' '+text;
if (text.length == 0) {
alert(FCKLang.EquationErrNoEqn) ;
return false ;
}
if(document.getElementById('inline').checked)
{
if(!document.getElementById('compressed').checked)
text='\\displaystyle '+text;
text = '\\f$' + text + '\\f$ ';
}
else
{
if(document.getElementById('compressed').checked)
text='\\inline '+text;
text = '\\f[' + text + '\\f]\n';
}
if ( eSelected && eSelected._fckequation == text )
return true ;
FCKEquation.Add(text);
}
window.blur();
return true ;
}