define(['i18n!mathquill', 'jquery', 'str/htmlEscape'] , function (I18n, $, htmlEscape){
var undefined, _, jQueryDataKey = '[[mathquill internal data]]', min = Math.min, max = Math.max;
function MathElement(){
}
_ = MathElement.prototype;
_.prev = 0;
_.next = 0;
_.parent = 0;
_.firstChild = 0;
_.lastChild = 0;
_.eachChild = function (fn){
for (var child = this.firstChild;
child; child = child.next)if (fn.call(this, child) === false ) break ;
return this;
}
;
_.foldChildren = function (fold, fn){
this.eachChild(function (child){
fold = fn.call(this, fold, child);
}
);
return fold;
}
;
_.keydown = function (e){
return this.parent.keydown(e);
}
;
_.textInput = function (ch){
return this.parent.textInput(ch);
}
;
function MathCommand(){
}
_ = MathCommand.prototype = new MathElement();
_.init = function (cmd, html_template, text_template, replacedFragment){
var self = this;
if (cmd) self.cmd = cmd;
if (html_template) self.html_template = html_template;
if (text_template) self.text_template = text_template;
self.jQ = $(self.html_template[0]).data(jQueryDataKey, {
cmd: self}
);
self.initBlocks(replacedFragment);
}
;
_.initBlocks = function (replacedFragment){
var self = this;
if (_AN_Read_length('length', self.html_template) === 1) {
self.firstChild = self.lastChild = self.jQ.data(jQueryDataKey).block = (replacedFragment && replacedFragment.blockify()) || new MathBlock();
self.firstChild.parent = self;
self.firstChild.jQ = self.jQ.append(self.firstChild.jQ);
return ;
}
var newBlock, prev, num_blocks = _AN_Read_length('length', self.html_template);
this.firstChild = newBlock = prev = (replacedFragment && replacedFragment.blockify()) || new MathBlock();
newBlock.parent = self;
newBlock.jQ = $(self.html_template[1]).data(jQueryDataKey, {
block: newBlock}
).append(newBlock.jQ).appendTo(self.jQ);
newBlock.blur();
for (var i = 2;
i < num_blocks; i += 1){
newBlock = new MathBlock();
newBlock.parent = self;
newBlock.prev = prev;
prev.next = newBlock;
prev = newBlock;
newBlock.jQ = $(self.html_template[i]).data(jQueryDataKey, {
block: newBlock}
).appendTo(self.jQ);
newBlock.blur();
}
self.lastChild = newBlock;
}
;
_.latex = function (){
return this.foldChildren(this.cmd, function (latex, child){
return latex + '{' + (child.latex() || ' ') + '}';
}
);
}
;
_.text_template = [''] ;
_.text = function (){
var i = 0;
return this.foldChildren(this.text_template[i], function (text, child){
i += 1;
var child_text = child.text();
if (text && this.text_template[i] === '(' && child_text[0] === '(' && child_text.slice(-1) === ')') return text + child_text.slice(1, -1) + this.text_template[i];
return text + child.text() + (this.text_template[i] || '');
}
);
}
;
_.insertAt = function (cursor){
var cmd = this;
cmd.parent = cursor.parent;
cmd.next = cursor.next;
cmd.prev = cursor.prev;
if (cursor.prev) cursor.prev.next = cmd;
else cursor.parent.firstChild = cmd;
if (cursor.next) cursor.next.prev = cmd;
else cursor.parent.lastChild = cmd;
cursor.prev = cmd;
cmd.jQ.insertBefore(cursor.jQ);
cmd.respace();
if (cmd.next) cmd.next.respace();
if (cmd.prev) cmd.prev.respace();
cmd.placeCursor(cursor);
cursor.redraw();
}
;
_.respace = $.noop;
_.placeCursor = function (cursor){
cursor.appendTo(this.foldChildren(this.firstChild, function (prev, child){
return prev.isEmpty()? prev: child;
}
));
}
;
_.isEmpty = function (){
return this.foldChildren(true , function (isEmpty, child){
return isEmpty && child.isEmpty();
}
);
}
;
_.remove = function (){
var self = this, prev = self.prev, next = self.next, parent = self.parent;
if (prev) prev.next = next;
else parent.firstChild = next;
if (next) next.prev = prev;
else parent.lastChild = prev;
self.jQ.remove();
return self;
}
;
function Symbol(cmd, html, text){
_AN_Call_init('init', this, cmd, [html] , [text || (cmd && _AN_Read_length('length', cmd) > 1? cmd.slice(1): cmd)] );
}
_ = Symbol.prototype = new MathCommand();
_.initBlocks = $.noop;
_.latex = function (){
return this.cmd;
}
;
_.text = function (){
return this.text_template;
}
;
_.placeCursor = $.noop;
_.isEmpty = function (){
return true ;
}
;
function MathBlock(){
}
_ = MathBlock.prototype = new MathElement();
_.latex = function (){
return this.foldChildren('', function (latex, child){
return latex + child.latex();
}
);
}
;
_.text = function (){
return this.firstChild === this.lastChild? this.firstChild.text(): this.foldChildren('(', function (text, child){
return text + child.text();
}
) + ')';
}
;
_.isEmpty = function (){
return this.firstChild === 0 && this.lastChild === 0;
}
;
_.focus = function (){
this.jQ.addClass('hasCursor');
if (this.isEmpty()) this.jQ.removeClass('empty');
return this;
}
;
_.blur = function (){
this.jQ.removeClass('hasCursor');
if (this.isEmpty()) this.jQ.addClass('empty');
return this;
}
;
function MathFragment(parent, prev, next){
if (!_AN_Read_length('length', arguments)) return ;
var self = this;
self.parent = parent;
self.prev = prev || 0;
self.next = next || 0;
self.jQinit(self.fold($(), function (jQ, child){
return child.jQ.add(jQ);
}
));
}
_ = MathFragment.prototype;
_.remove = MathCommand.prototype.remove;
_.jQinit = function (children){
this.jQ = children;
}
;
_.each = function (fn){
for (var el = this.prev.next || this.parent.firstChild;
el !== this.next; el = el.next)if (fn.call(this, el) === false ) break ;
return this;
}
;
_.fold = function (fold, fn){
this.each(function (el){
fold = fn.call(this, fold, el);
}
);
return fold;
}
;
_.latex = function (){
return this.fold('', function (latex, el){
return latex + el.latex();
}
);
}
;
_.blockify = function (){
var self = this, prev = self.prev, next = self.next, parent = self.parent, newBlock = new MathBlock(), newFirstChild = newBlock.firstChild = prev.next || parent.firstChild, newLastChild = newBlock.lastChild = next.prev || parent.lastChild;
if (prev) prev.next = next;
else parent.firstChild = next;
if (next) next.prev = prev;
else parent.lastChild = prev;
newFirstChild.prev = self.prev = 0;
newLastChild.next = self.next = 0;
self.parent = newBlock;
self.each(function (el){
el.parent = newBlock;
}
);
newBlock.jQ = self.jQ;
return newBlock;
}
;
function createRoot(jQ, root, textbox, editable, include_toolbar){
var contents = jQ.contents().detach();
if (!textbox) jQ.addClass('mathquill-rendered-math');
root.jQ = jQ.data(jQueryDataKey, {
block: root,
revert: function (){
jQ.empty().unbind('.mathquill').removeClass('mathquill-rendered-math mathquill-editable mathquill-textbox mathquill-editor').append(contents);
}
}
);
var cursor = _AN_Write_cursor('cursor', root, false , new Cursor(root));
root.renderLatex(contents.text());
var textareaSpan = root.textarea = $(''), textarea = textareaSpan.children();
if (include_toolbar) addToolbar(root, jQ);
var textareaSelectionTimeout;
root.selectionChanged = function (){
if (textareaSelectionTimeout === undefined) textareaSelectionTimeout = _AN_Call_settimeout('setTimeout', window, setTextareaSelection);
forceIERedraw(jQ[0]);
}
;
function setTextareaSelection(){
textareaSelectionTimeout = undefined;
var latex = cursor.selection? '$' + cursor.selection.latex() + '$': '';
textarea.val(latex);
if (latex) {
if (textarea[0].select) textarea[0].select();
else if (document.selection) {
var range = textarea[0].createTextRange();
range.expand('textedit');
range.select();
}
}
}
;
jQ.bind('selectstart.mathquill', function (e){
if (_AN_Read_target('target', e) !== textarea[0]) e.preventDefault();
e.stopPropagation();
}
);
var anticursor, blink = cursor.blink;
jQ.bind('mousedown.mathquill', function (e){
cursor.blink = $.noop;
cursor.seek($(_AN_Read_target('target', e)), e.pageX, e.pageY);
anticursor = new MathFragment(cursor.parent, cursor.prev, cursor.next);
if (!editable) jQ.prepend(textareaSpan);
jQ.mousemove(mousemove);
$(document).mousemove(docmousemove).mouseup(mouseup);
e.stopPropagation();
}
);
function mousemove(e){
cursor.seek($(_AN_Read_target('target', e)), e.pageX, e.pageY);
if (cursor.prev !== anticursor.prev || cursor.parent !== anticursor.parent) cursor.selectFrom(anticursor);
return false ;
}
function docmousemove(e){
delete e.target;
return mousemove(e);
}
function mouseup(e){
anticursor = undefined;
cursor.blink = blink;
if (!cursor.selection) {
if (editable) _AN_Call_show('show', cursor);
else textareaSpan.detach();
}
jQ.unbind('mousemove', mousemove);
$(document).unbind('mousemove', docmousemove).unbind('mouseup', mouseup);
}
if (!editable) {
jQ.bind('cut paste', false ).bind('copy', setTextareaSelection).prepend('$' + htmlEscape(root.latex()) + '$');
textarea.blur(function (){
cursor.clearSelection();
_AN_Call_settimeout('setTimeout', window, function detach(){
textareaSpan.detach();
}
);
}
);
return ;
}
jQ.prepend(textareaSpan);
jQ.addClass('mathquill-editable');
if (textbox) jQ.addClass('mathquill-textbox');
textarea.focus(function (e){
if (!cursor.parent) cursor.appendTo(root);
cursor.parent.jQ.addClass('hasCursor');
if (cursor.selection) {
cursor.selection.jQ.removeClass('blur');
_AN_Call_settimeout('setTimeout', window, root.selectionChanged);
}
else _AN_Call_show('show', cursor);
e.stopPropagation();
}
).blur(function (e){
cursor.hide().parent.blur();
if (cursor.selection) cursor.selection.jQ.addClass('blur');
e.stopPropagation();
}
);
jQ.bind('focus.mathquill blur.mathquill', function (e){
textarea.trigger(e);
}
).bind('mousedown.mathquill', function (){
_AN_Call_settimeout('setTimeout', window, focus);
}
).bind('click.mathquill', focus).blur();
function focus(){
textarea.focus();
}
jQ.bind('cut', function (e){
setTextareaSelection();
if (cursor.selection) _AN_Call_settimeout('setTimeout', window, function (){
cursor.deleteSelection();
cursor.redraw();
}
);
e.stopPropagation();
}
).bind('copy', function (e){
setTextareaSelection();
skipTextInput = true ;
e.stopPropagation();
}
).bind('paste', function (e){
skipTextInput = true ;
_AN_Call_settimeout('setTimeout', window, paste);
e.stopPropagation();
}
);
function paste(){
var latex = textarea.val();
if (latex.slice(0, 1) === '$' && latex.slice(-1) === '$') latex = latex.slice(1, -1);
else if (!latex.match(/^\\/)) latex = '\\text{' + latex + '}';
_AN_Call_show('show', cursor.writeLatex(latex));
textarea.val('');
}
var lastKeydn, lastKeydnHappened, lastKeypressWhich, skipTextInput = false ;
jQ.bind('keydown.mathquill', function (e){
lastKeydn = e;
lastKeydnHappened = true ;
if (cursor.parent.keydown(e) === false ) e.preventDefault();
}
).bind('keypress.mathquill', function (e){
if (lastKeydnHappened) lastKeydnHappened = false ;
else {
if (lastKeypressWhich !== e.which) return ;
else cursor.parent.keydown(lastKeydn);
}
lastKeypressWhich = e.which;
if (textareaSelectionTimeout !== undefined) clearTimeout(textareaSelectionTimeout);
skipTextInput = false ;
_AN_Call_settimeout('setTimeout', window, textInput);
}
);
function textInput(){
if (skipTextInput) return ;
var text = textarea.val();
if (text) {
textarea.val('');
for (var i = 0;
i < _AN_Read_length('length', text); i += 1){
cursor.parent.textInput(text.charAt(i));
}
}
else {
if (cursor.selection || textareaSelectionTimeout !== undefined) setTextareaSelection();
}
}
}
function addToolbar(root, jQ){
var button_tabs = [{
name: I18n.t('tabs.basic', 'Basic'),
example: '+',
button_groups: [["subscript", "supscript", "frac", "sqrt", "nthroot", "langle", "binomial", "vector", "f", "prime"] , ["+", "-", "pm", "mp", "cdot", "=", "times", "div", "ast"] , ["therefore", "because"] , ["sum", "prod", "coprod", "int"] , ["N", "P", "Z", "Q", "R", "C", "H"] ] }
, {
name: I18n.t('tabs.greek', 'Greek'),
example: 'π',
button_groups: [["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega"] , ["digamma", "varepsilon", "vartheta", "varkappa", "varpi", "varrho", "varsigma", "varphi"] , ["Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma", "Upsilon", "Phi", "Psi", "Omega"] ] }
, {
name: I18n.t('tabs.operators', 'Operators'),
example: '⊕',
button_groups: [["wedge", "vee", "cup", "cap", "diamond", "bigtriangleup", "ominus", "uplus", "otimes", "oplus", "bigtriangledown", "sqcap", "triangleleft", "sqcup", "triangleright", "odot", "bigcirc", "dagger", "ddagger", "wr", "amalg"] ] }
, {
name: I18n.t('tabs.relationships', 'Relationships'),
example: '≤',
button_groups: [["<", ">", "equiv", "cong", "sim", "notin", "ne", "propto", "approx", "le", "ge", "in", "ni", "notni", "subset", "supset", "notsubset", "notsupset", "subseteq", "supseteq", "notsubseteq", "notsupseteq", "models", "prec", "succ", "preceq", "succeq", "simeq", "mid", "ll", "gg", "parallel", "bowtie", "sqsubset", "sqsupset", "smile", "sqsubseteq", "sqsupseteq", "doteq", "frown", "vdash", "dashv", "exists", "varnothing"] ] }
, {
name: I18n.t('tabs.arrows', 'Arrows'),
example: '⇔',
button_groups: [["longleftarrow", "longrightarrow", "Longleftarrow", "Longrightarrow", "longleftrightarrow", "updownarrow", "Longleftrightarrow", "Updownarrow", "mapsto", "nearrow", "hookleftarrow", "hookrightarrow", "searrow", "leftharpoonup", "rightharpoonup", "swarrow", "leftharpoondown", "rightharpoondown", "nwarrow", "downarrow", "Downarrow", "uparrow", "Uparrow", "rightarrow", "Rightarrow", "leftarrow", "lArr", "leftrightarrow", "Leftrightarrow"] ] }
, {
name: I18n.t('tabs.delimiters', 'Delimiters'),
example: '{',
button_groups: [["lfloor", "rfloor", "lceil", "rceil", "slash", "lbrace", "rbrace"] ] }
, {
name: I18n.t('tabs.miscellaneous', 'Misc'),
example: '∞',
button_groups: [["forall", "ldots", "cdots", "vdots", "ddots", "surd", "triangle", "ell", "top", "flat", "natural", "sharp", "wp", "bot", "clubsuit", "diamondsuit", "heartsuit", "spadesuit", "caret", "underscore", "backslash", "vert", "perp", "nabla", "hbar", "AA", "circ", "bullet", "setminus", "neg", "dots", "Re", "Im", "partial", "infty", "aleph", "deg", "angle"] ] }
] ;
var html_template_overrides = {
binomial: '(nm)',
frac: 'nm',
sqrt: '√ ',
nthroot: 'n√ ',
supscript: 'sup',
subscript: 'sub',
vector: '123'}
;
var tabs = [] ;
var panes = [] ;
$.each(button_tabs, function (index, tab){
tabs.push('
' + ' ' + htmlEscape(tab.example) + '' + htmlEscape(tab.name) + '');
var buttons = [] ;
$.each(tab.button_groups, function (index, group){
$.each(group, function (index, cmd){
var obj = new LatexCmds[cmd](undefined, cmd);
buttons.push('' + $.raw(html_template_overrides[cmd]? html_template_overrides[cmd]: '' + $.raw(obj.html_template.join('')) + '') + '');
}
);
buttons.push('');
}
);
panes.push('' + $.raw(buttons.join('')) + '
');
}
);
root.toolbar = $('#mathquill-view .mathquill-toolbar').html('' + $.raw(tabs.join('')) + '
' + $.raw(panes.join('')) + '
');
$('#mathquill-view .mathquill-tab-bar li a').click(function (e){
e.preventDefault();
$('#mathquill-view .mathquill-tab-bar li').removeClass('mathquill-tab-selected').find('a').attr('tabindex', '-1').attr('aria-selected', 'false');
$('#mathquill-view .mathquill-tab-pane').removeClass('mathquill-tab-pane-selected');
$(this).attr('tabindex', '0').attr('aria-selected', 'true').focus().parent().addClass('mathquill-tab-selected');
$(_AN_Call_replace('replace', _AN_Read_href('href', this), /.*#/, '#')).addClass('mathquill-tab-pane-selected');
}
).keydown(function (e){
var direction, listIndex, $tabLinks;
switch (e.keyCode){
case 37: direction = 'l';
break ;
case 39: direction = 'r';
break ;
default : {
return true ;
}
}
e.preventDefault();
$tabLinks = $('#mathquill-view .mathquill-tab-bar li a');
listIndex = $tabLinks.index(this);
if (listIndex === _AN_Read_length('length', $tabLinks) - 1 && direction === 'r') {
listIndex = -1;
}
(direction === 'r')? listIndex++ : listIndex-- ;
$($tabLinks.get(listIndex)).focus().click();
}
);
$('#mathquill-view .mathquill-tab-bar li:first-child a').click();
$('#mathquill-view a.mathquill-rendered-math').mousedown(function (e){
e.stopPropagation();
}
).click(function (){
root.cursor.writeLatex(this.title, true );
jQ.focus();
}
);
}
function RootMathBlock(){
}
_ = RootMathBlock.prototype = new MathBlock();
_.latex = function (){
return _AN_Call_replace('replace', MathBlock.prototype.latex.call(this), /(\\[a-z]+) (?![a-z])/ig, '$1');
}
;
_.text = function (){
return this.foldChildren('', function (text, child){
return text + child.text();
}
);
}
;
_.renderLatex = function (latex){
this.jQ.children().slice(1).remove();
this.firstChild = this.lastChild = 0;
this.cursor.appendTo(this).writeLatex(latex);
this.blur();
}
;
_.keydown = function (e){
e.ctrlKey = e.ctrlKey || e.metaKey;
switch ((e.originalEvent && e.originalEvent.keyIdentifier) || e.which){
case 8: case 'Backspace': case 'U+0008': if (e.ctrlKey) while (this.cursor.prev || this.cursor.selection)this.cursor.backspace();
else this.cursor.backspace();
break ;
case 27: case 'Esc': case 'U+001B': case 9: case 'Tab': case 'U+0009': if (e.ctrlKey) break ;
var parent = this.cursor.parent;
if (e.shiftKey) {
if (parent === this.cursor.root) return this.skipTextInput = true ;
else if (parent.prev) this.cursor.appendTo(parent.prev);
else this.cursor.insertBefore(parent.parent);
}
else {
if (parent === this.cursor.root) return this.skipTextInput = true ;
else if (parent.next) this.cursor.prependTo(parent.next);
else this.cursor.insertAfter(parent.parent);
}
this.cursor.clearSelection();
break ;
case 13: case 'Enter': break ;
case 35: case 'End': if (e.shiftKey) while (this.cursor.next || (e.ctrlKey && this.cursor.parent !== this))this.cursor.selectRight();
else this.cursor.clearSelection().appendTo(e.ctrlKey? this: this.cursor.parent);
break ;
case 36: case 'Home': if (e.shiftKey) while (this.cursor.prev || (e.ctrlKey && this.cursor.parent !== this))this.cursor.selectLeft();
else this.cursor.clearSelection().prependTo(e.ctrlKey? this: this.cursor.parent);
break ;
case 37: case 'Left': if (e.ctrlKey) break ;
if (e.shiftKey) this.cursor.selectLeft();
else this.cursor.moveLeft();
break ;
case 38: case 'Up': if (e.ctrlKey) break ;
if (e.shiftKey) {
if (this.cursor.prev) while (this.cursor.prev)this.cursor.selectLeft();
else this.cursor.selectLeft();
}
else if (this.cursor.parent.prev) this.cursor.clearSelection().appendTo(this.cursor.parent.prev);
else if (this.cursor.prev) this.cursor.clearSelection().prependTo(this.cursor.parent);
else if (this.cursor.parent !== this) this.cursor.clearSelection().insertBefore(this.cursor.parent.parent);
break ;
case 39: case 'Right': if (e.ctrlKey) break ;
if (e.shiftKey) this.cursor.selectRight();
else this.cursor.moveRight();
break ;
case 40: case 'Down': if (e.ctrlKey) break ;
if (e.shiftKey) {
if (this.cursor.next) while (this.cursor.next)this.cursor.selectRight();
else this.cursor.selectRight();
}
else if (this.cursor.parent.next) this.cursor.clearSelection().prependTo(this.cursor.parent.next);
else if (this.cursor.next) this.cursor.clearSelection().appendTo(this.cursor.parent);
else if (this.cursor.parent !== this) this.cursor.clearSelection().insertAfter(this.cursor.parent.parent);
break ;
case 46: case 'Del': case 'U+007F': if (e.ctrlKey) while (this.cursor.next || this.cursor.selection)this.cursor.deleteForward();
else this.cursor.deleteForward();
break ;
case 65: case 'A': case 'U+0041': if (e.ctrlKey && !e.shiftKey && !e.altKey) {
if (this !== this.cursor.root) return this.parent.keydown(e);
this.cursor.clearSelection().appendTo(this);
while (this.cursor.prev)this.cursor.selectLeft();
break ;
}
default : {
this.skipTextInput = false ;
return true ;
}
}
this.skipTextInput = true ;
return false ;
}
;
_.textInput = function (ch){
if (!this.skipTextInput) _AN_Call_write('write', this.cursor, ch);
}
;
function RootMathCommand(cursor){
_AN_Call_init('init', this, '$');
_AN_Write_cursor('cursor', this.firstChild, false , cursor);
this.firstChild.textInput = function (ch){
if (this.skipTextInput) return ;
if (ch !== '$' || cursor.parent !== this) _AN_Call_write('write', cursor, ch);
else if (this.isEmpty()) {
_AN_Call_show('show', cursor.insertAfter(this.parent).backspace().insertNew(new VanillaSymbol('\\$', '$')));
}
else if (!cursor.next) cursor.insertAfter(this.parent);
else if (!cursor.prev) cursor.insertBefore(this.parent);
else _AN_Call_write('write', cursor, ch);
}
;
}
_ = RootMathCommand.prototype = new MathCommand();
_.html_template = [''] ;
_.initBlocks = function (){
this.firstChild = this.lastChild = this.jQ.data(jQueryDataKey).block = new RootMathBlock();
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ;
}
;
_.latex = function (){
return '$' + this.firstChild.latex() + '$';
}
;
function RootTextBlock(){
}
_ = RootTextBlock.prototype = new MathBlock();
_.renderLatex = function (latex){
var self = this, cursor = self.cursor;
self.jQ.children().slice(1).remove();
self.firstChild = self.lastChild = 0;
_AN_Call_show('show', cursor).appendTo(self);
latex = latex.match(/(?:\\\$|[^$])+|\$(?:\\\$|[^$])*\$|\$(?:\\\$|[^$])*$/g) || '';
for (var i = 0;
i < _AN_Read_length('length', latex); i += 1){
var chunk = latex[i];
if (chunk[0] === '$') {
if (chunk[-1 + _AN_Read_length('length', chunk)] === '$' && chunk[-2 + _AN_Read_length('length', chunk)] !== '\\') chunk = chunk.slice(1, -1);
else chunk = chunk.slice(1);
var root = new RootMathCommand(cursor);
cursor.insertNew(root);
root.firstChild.renderLatex(chunk);
_AN_Call_show('show', cursor).insertAfter(root);
}
else {
for (var j = 0;
j < _AN_Read_length('length', chunk); j += 1)this.cursor.insertNew(new VanillaSymbol(chunk[j]));
}
}
}
;
_.keydown = RootMathBlock.prototype.keydown;
_.textInput = function (ch){
if (this.skipTextInput) return ;
this.cursor.deleteSelection();
if (ch === '$') this.cursor.insertNew(new RootMathCommand(this.cursor));
else this.cursor.insertNew(new VanillaSymbol(ch));
}
;
var CharCmds = {
}
, LatexCmds = {
}
;
var scale, forceIERedraw = $.noop, div = _AN_Call_createelement('createElement', document, 'div'), div_style = div.style, transformPropNames = {
transform: 1,
WebkitTransform: 1,
MozTransform: 1,
OTransform: 1,
msTransform: 1}
, transformPropName;
for (var prop in transformPropNames){
if (prop in div_style) {
transformPropName = prop;
break ;
}
}
if (transformPropName) {
scale = function (jQ, x, y){
jQ.css(transformPropName, 'scale(' + x + ',' + y + ')');
}
;
}
else if ('filter' in div_style) {
forceIERedraw = function (el){
el.className = el.className;
}
;
scale = function (jQ, x, y){
x /= (1 + (y - 1) / 2);
jQ.addClass('matrixed').css({
fontSize: y + 'em',
marginTop: '-.1em',
filter: 'progid:DXImageTransform.Microsoft' + '.Matrix(M11=' + x + ",SizingMethod='auto expand')"}
);
function calculateMarginRight(){
jQ.css('marginRight', (1 + jQ.width()) * (x - 1) / x + 'px');
}
calculateMarginRight();
var intervalId = _AN_Call_setinterval('setInterval', window, calculateMarginRight);
_AN_Call_load('load', $(window), function (){
clearTimeout(intervalId);
calculateMarginRight();
}
);
}
;
}
else {
scale = function (jQ, x, y){
jQ.css('fontSize', y + 'em');
}
;
}
function proto(parent, child){
child.prototype = parent.prototype;
return child;
}
function bind(cons){
var args = Array.prototype.slice.call(arguments, 1);
return proto(cons, function (){
cons.apply(this, Array.prototype.concat.apply(args, arguments));
}
);
}
function Style(cmd, html_template, replacedFragment){
_AN_Call_init('init', this, cmd, [html_template] , undefined, replacedFragment);
}
proto(MathCommand, Style);
LatexCmds.mathrm = bind(Style, '\\mathrm', '');
LatexCmds.mathit = bind(Style, '\\mathit', '');
LatexCmds.mathbf = bind(Style, '\\mathbf', '');
LatexCmds.mathsf = bind(Style, '\\mathsf', '');
LatexCmds.mathtt = bind(Style, '\\mathtt', '');
LatexCmds.underline = bind(Style, '\\underline', '');
LatexCmds.overline = LatexCmds.bar = bind(Style, '\\overline', '');
function SupSub(cmd, html, text, replacedFragment){
_AN_Call_init('init', this, cmd, [html] , [text] , replacedFragment);
}
_ = SupSub.prototype = new MathCommand();
_.latex = function (){
var latex = this.firstChild.latex();
if (_AN_Read_length('length', latex) === 1) return this.cmd + latex;
else return this.cmd + '{' + (latex || ' ') + '}';
}
;
_.redraw = function (){
if (this.prev) this.prev.respace();
if (!(this.prev instanceof SupSub)) {
this.respace();
if (this.next && !(this.next instanceof SupSub)) this.next.respace();
}
}
;
_.respace = function (){
if (this.prev.cmd === '\\int ' || (this.prev instanceof SupSub && this.prev.cmd != this.cmd && this.prev.prev && this.prev.prev.cmd === '\\int ')) {
if (!this.limit) {
this.limit = true ;
this.jQ.addClass('limit');
}
}
else {
if (this.limit) {
this.limit = false ;
this.jQ.removeClass('limit');
}
}
this.respaced = this.prev instanceof SupSub && this.prev.cmd != this.cmd && !this.prev.respaced;
if (this.respaced) {
var fontSize = + this.jQ.css('fontSize').slice(0, -2), prevWidth = this.prev.jQ.outerWidth(), thisWidth = this.jQ.outerWidth();
this.jQ.css({
left: (this.limit && this.cmd === '_'? -0.25: 0) - prevWidth / fontSize + 'em',
marginRight: 0.1 - min(thisWidth, prevWidth) / fontSize + 'em'}
);
}
else if (this.limit && this.cmd === '_') {
this.jQ.css({
left: '-.25em',
marginRight: ''}
);
}
else {
this.jQ.css({
left: '',
marginRight: ''}
);
}
if (this.next instanceof SupSub) this.next.respace();
return this;
}
;
LatexCmds.subscript = LatexCmds._ = proto(SupSub, function (replacedFragment){
SupSub.call(this, '_', '', '_', replacedFragment);
}
);
LatexCmds.superscript = LatexCmds.supscript = LatexCmds["^"] = proto(SupSub, function (replacedFragment){
SupSub.call(this, '^', '', '**', replacedFragment);
}
);
function Fraction(replacedFragment){
_AN_Call_init('init', this, '\\frac', undefined, undefined, replacedFragment);
this.jQ.append(' ');
}
_ = Fraction.prototype = new MathCommand();
_.html_template = ['', '', ''] ;
_.text_template = ['(', '/', ')'] ;
LatexCmds.frac = LatexCmds.dfrac = LatexCmds.cfrac = LatexCmds.fraction = Fraction;
function LiveFraction(){
Fraction.apply(this, arguments);
}
_ = LiveFraction.prototype = new Fraction();
_.placeCursor = function (cursor){
if (this.firstChild.isEmpty()) {
var prev = this.prev;
while (prev && !(prev instanceof BinaryOperator || prev instanceof TextBlock || prev instanceof BigSymbol))prev = prev.prev;
if (prev instanceof BigSymbol && prev.next instanceof SupSub) {
prev = prev.next;
if (prev.next instanceof SupSub && prev.next.cmd != prev.cmd) prev = prev.next;
}
if (prev !== this.prev) {
var newBlock = new MathFragment(this.parent, prev, this).blockify();
newBlock.jQ = this.firstChild.jQ.empty().removeClass('empty').append(newBlock.jQ).data(jQueryDataKey, {
block: newBlock}
);
newBlock.next = this.lastChild;
newBlock.parent = this;
this.firstChild = this.lastChild.prev = newBlock;
}
}
cursor.appendTo(this.lastChild);
}
;
LatexCmds.over = CharCmds["/"] = LiveFraction;
function SquareRoot(replacedFragment){
_AN_Call_init('init', this, '\\sqrt', undefined, undefined, replacedFragment);
}
_ = SquareRoot.prototype = new MathCommand();
_.html_template = ['√', ''] ;
_.text_template = ['sqrt(', ')'] ;
_.redraw = function (){
var block = this.lastChild.jQ;
scale(block.prev(), 1, block.innerHeight() / + block.css('fontSize').slice(0, -2) - 0.1);
}
;
_.optional_arg_command = 'nthroot';
LatexCmds.sqrt = LatexCmds["\xE2\x88\x9A"] = SquareRoot;
function NthRoot(replacedFragment){
SquareRoot.call(this, replacedFragment);
this.jQ = this.firstChild.jQ.detach().add(this.jQ);
}
_ = NthRoot.prototype = new SquareRoot();
_.html_template = ['√', '', ''] ;
_.text_template = ['sqrt[', '](', ')'] ;
_.latex = function (){
return '\\sqrt[' + this.firstChild.latex() + ']{' + this.lastChild.latex() + '}';
}
;
LatexCmds.nthroot = NthRoot;
function Bracket(open, close, cmd, end, replacedFragment){
_AN_Call_init('init', this, '\\left' + cmd, ['' + htmlEscape(open) + '' + htmlEscape(close) + ''] , [open, close] , replacedFragment);
this.end = '\\right' + end;
}
_ = Bracket.prototype = new MathCommand();
_.initBlocks = function (replacedFragment){
this.firstChild = this.lastChild = (replacedFragment && replacedFragment.blockify()) || new MathBlock();
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ.children(':eq(1)').data(jQueryDataKey, {
block: this.firstChild}
).append(this.firstChild.jQ);
var block = this.blockjQ = this.firstChild.jQ;
this.bracketjQs = block.prev().add(block.next());
}
;
_.latex = function (){
return this.cmd + this.firstChild.latex() + this.end;
}
;
_.redraw = function (){
var height = this.blockjQ.outerHeight() / + this.blockjQ.css('fontSize').slice(0, -2);
scale(this.bracketjQs, min(1 + 0.2 * (height - 1), 1.2), 1.05 * height);
}
;
CharCmds["{"] = proto(Bracket, function (replacedFragment){
Bracket.call(this, '{', '}', '\\{', '\\}', replacedFragment);
}
);
LatexCmds.langle = LatexCmds.lang = proto(Bracket, function (replacedFragment){
Bracket.call(this, '〈', '〉', '\\langle ', '\\rangle ', replacedFragment);
}
);
function CloseBracket(open, close, cmd, end, replacedFragment){
Bracket.apply(this, arguments);
}
_ = CloseBracket.prototype = new Bracket();
_.placeCursor = function (cursor){
if (!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty()) cursor.backspace().insertAfter(this.parent.parent);
else {
this.firstChild.blur();
this.redraw();
}
}
;
CharCmds["}"] = proto(CloseBracket, function (replacedFragment){
CloseBracket.call(this, '{', '}', '\\{', '\\}', replacedFragment);
}
);
LatexCmds.rangle = LatexCmds.rang = proto(CloseBracket, function (replacedFragment){
CloseBracket.call(this, '〈', '〉', '\\langle ', '\\rangle ', replacedFragment);
}
);
function Paren(open, close, replacedFragment){
Bracket.call(this, open, close, open, close, replacedFragment);
}
Paren.prototype = Bracket.prototype;
LatexCmds.lparen = CharCmds["("] = proto(Paren, function (replacedFragment){
Paren.call(this, '(', ')', replacedFragment);
}
);
LatexCmds.lbrack = LatexCmds.lbracket = CharCmds["["] = proto(Paren, function (replacedFragment){
Paren.call(this, '[', ']', replacedFragment);
}
);
function CloseParen(open, close, replacedFragment){
CloseBracket.call(this, open, close, open, close, replacedFragment);
}
CloseParen.prototype = CloseBracket.prototype;
LatexCmds.rparen = CharCmds[")"] = proto(CloseParen, function (replacedFragment){
CloseParen.call(this, '(', ')', replacedFragment);
}
);
LatexCmds.rbrack = LatexCmds.rbracket = CharCmds["]"] = proto(CloseParen, function (replacedFragment){
CloseParen.call(this, '[', ']', replacedFragment);
}
);
function Pipes(replacedFragment){
Paren.call(this, '|', '|', replacedFragment);
}
_ = Pipes.prototype = new Paren();
_.placeCursor = function (cursor){
if (!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty()) cursor.backspace().insertAfter(this.parent.parent);
else cursor.appendTo(this.firstChild);
}
;
LatexCmds.lpipe = LatexCmds.rpipe = CharCmds["|"] = Pipes;
function TextBlock(replacedText){
if (replacedText instanceof MathFragment) this.replacedText = replacedText.remove().jQ.text();
else if (typeof replacedText === 'string') this.replacedText = replacedText;
_AN_Call_init('init', this);
}
_ = TextBlock.prototype = new MathCommand();
_.cmd = '\\text';
_.html_template = [''] ;
_.text_template = ['"', '"'] ;
_.initBlocks = function (){
this.firstChild = this.lastChild = this.jQ.data(jQueryDataKey).block = new InnerTextBlock();
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ.append(this.firstChild.jQ);
}
;
_.placeCursor = function (cursor){
(_AN_Write_cursor('cursor', this, false , cursor)).appendTo(this.firstChild);
if (this.replacedText) for (var i = 0;
i < _AN_Read_length('length', this.replacedText); i += 1)_AN_Call_write('write', this, this.replacedText.charAt(i));
}
;
_.write = function (ch){
this.cursor.insertNew(new VanillaSymbol(ch));
}
;
_.keydown = function (e){
if (!this.cursor.selection && ((e.which === 8 && !this.cursor.prev) || (e.which === 46 && !this.cursor.next))) {
if (this.isEmpty()) this.cursor.insertAfter(this);
return false ;
}
return this.parent.keydown(e);
}
;
_.textInput = function (ch){
this.cursor.deleteSelection();
if (ch !== '$') _AN_Call_write('write', this, ch);
else if (this.isEmpty()) this.cursor.insertAfter(this).backspace().insertNew(new VanillaSymbol('\\$', '$'));
else if (!this.cursor.next) this.cursor.insertAfter(this);
else if (!this.cursor.prev) this.cursor.insertBefore(this);
else {
var next = new TextBlock(new MathFragment(this.firstChild, this.cursor.prev));
next.placeCursor = function (cursor){
this.prev = 0;
delete this.placeCursor;
this.placeCursor(cursor);
}
;
next.firstChild.focus = function (){
return this;
}
;
this.cursor.insertAfter(this).insertNew(next);
next.prev = this;
this.cursor.insertBefore(next);
delete next.firstChild.focus;
}
}
;
function InnerTextBlock(){
}
_ = InnerTextBlock.prototype = new MathBlock();
_.blur = function (){
this.jQ.removeClass('hasCursor');
if (this.isEmpty()) {
var textblock = this.parent, cursor = textblock.cursor;
if (cursor.parent === this) this.jQ.addClass('empty');
else {
cursor.hide();
textblock.remove();
if (cursor.next === textblock) cursor.next = textblock.next;
else if (cursor.prev === textblock) cursor.prev = textblock.prev;
_AN_Call_show('show', cursor).redraw();
}
}
return this;
}
;
_.focus = function (){
MathBlock.prototype.focus.call(this);
var textblock = this.parent;
if (textblock.next.cmd === textblock.cmd) {
var innerblock = this, cursor = textblock.cursor, next = textblock.next.firstChild;
next.eachChild(function (child){
child.parent = innerblock;
child.jQ.appendTo(innerblock.jQ);
}
);
if (this.lastChild) this.lastChild.next = next.firstChild;
else this.firstChild = next.firstChild;
next.firstChild.prev = this.lastChild;
this.lastChild = next.lastChild;
next.parent.remove();
if (cursor.prev) cursor.insertAfter(cursor.prev);
else cursor.prependTo(this);
cursor.redraw();
}
else if (textblock.prev.cmd === textblock.cmd) {
var cursor = textblock.cursor;
if (cursor.prev) textblock.prev.firstChild.focus();
else cursor.appendTo(textblock.prev.firstChild);
}
return this;
}
;
CharCmds.$ = _AN_Write_text('text', LatexCmds, false , LatexCmds.textnormal = LatexCmds.textrm = LatexCmds.textup = LatexCmds.textmd = TextBlock);
function makeTextBlock(latex, html){
function SomeTextBlock(){
TextBlock.apply(this, arguments);
}
_ = SomeTextBlock.prototype = new TextBlock();
_.cmd = latex;
_.html_template = [html] ;
return SomeTextBlock;
}
LatexCmds.em = LatexCmds.italic = LatexCmds.italics = LatexCmds.emph = LatexCmds.textit = LatexCmds.textsl = makeTextBlock('\\textit', '');
LatexCmds.strong = LatexCmds.bold = LatexCmds.textbf = makeTextBlock('\\textbf', '');
LatexCmds.sf = LatexCmds.textsf = makeTextBlock('\\textsf', '');
LatexCmds.tt = LatexCmds.texttt = makeTextBlock('\\texttt', '');
LatexCmds.textsc = makeTextBlock('\\textsc', '');
LatexCmds.uppercase = makeTextBlock('\\uppercase', '');
LatexCmds.lowercase = makeTextBlock('\\lowercase', '');
function LatexCommandInput(replacedFragment){
_AN_Call_init('init', this, '\\');
if (replacedFragment) {
this.replacedFragment = replacedFragment.detach();
this.isEmpty = function (){
return false ;
}
;
}
}
_ = LatexCommandInput.prototype = new MathCommand();
_.html_template = ['\\'] ;
_.text_template = ['\\'] ;
_.placeCursor = function (cursor){
_AN_Write_cursor('cursor', this, false , cursor.appendTo(this.firstChild));
if (this.replacedFragment) this.jQ = this.jQ.add(this.replacedFragment.jQ.addClass('blur').bind('mousedown mousemove', function (e){
$(_AN_Write_target('target', e, false , this.nextSibling)).trigger(e);
return false ;
}
).insertBefore(this.jQ));
}
;
_.latex = function (){
return '\\' + this.firstChild.latex() + ' ';
}
;
_.keydown = function (e){
if (e.which === 9 || e.which === 13) {
this.renderCommand();
return false ;
}
return this.parent.keydown(e);
}
;
_.textInput = function (ch){
if (ch.match(/[a-z:]/i)) {
this.cursor.deleteSelection();
this.cursor.insertNew(new VanillaSymbol(ch));
return ;
}
this.renderCommand();
if (ch === ' ' || (ch === '\\' && this.firstChild.isEmpty())) return ;
this.cursor.parent.textInput(ch);
}
;
_.renderCommand = function (){
this.jQ = this.jQ.last();
this.remove();
if (this.next) this.cursor.insertBefore(this.next);
else this.cursor.appendTo(this.parent);
var latex = this.firstChild.latex();
if (latex) this.cursor.insertCmd(latex, this.replacedFragment);
else {
var cmd = new VanillaSymbol('\\backslash ', '\\');
this.cursor.insertNew(cmd);
if (this.replacedFragment) this.replacedFragment.remove();
}
}
;
CharCmds["\\\\"] = LatexCommandInput;
function Binomial(replacedFragment){
_AN_Call_init('init', this, '\\binom', undefined, undefined, replacedFragment);
this.jQ.wrapInner('');
this.blockjQ = this.jQ.children();
this.bracketjQs = $('(').prependTo(this.jQ).add($(')').appendTo(this.jQ));
}
_ = Binomial.prototype = new MathCommand();
_.html_template = ['', '', ''] ;
_.text_template = ['choose(', ',', ')'] ;
_.redraw = Bracket.prototype.redraw;
LatexCmds.binom = LatexCmds.binomial = Binomial;
function Choose(){
Binomial.apply(this, arguments);
}
_ = Choose.prototype = new Binomial();
_.placeCursor = LiveFraction.prototype.placeCursor;
LatexCmds.choose = Choose;
function Vector(replacedFragment){
_AN_Call_init('init', this, '\\vector', undefined, undefined, replacedFragment);
}
_ = Vector.prototype = new MathCommand();
_.html_template = ['', ''] ;
_.latex = function (){
return '\\begin{matrix}' + this.foldChildren([] , function (latex, child){
latex.push(child.latex());
return latex;
}
).join('\\\\') + '\\end{matrix}';
}
;
_.text = function (){
return '[' + this.foldChildren([] , function (text, child){
text.push(child.text());
return text;
}
).join() + ']';
}
;
_.placeCursor = function (cursor){
_AN_Write_cursor('cursor', this, false , cursor.appendTo(this.firstChild));
}
;
_.keydown = function (e){
var currentBlock = this.cursor.parent;
if (currentBlock.parent === this) {
if (e.which === 13) {
var newBlock = new MathBlock();
newBlock.parent = this;
newBlock.jQ = $('').data(jQueryDataKey, {
block: newBlock}
).insertAfter(currentBlock.jQ);
if (currentBlock.next) currentBlock.next.prev = newBlock;
else this.lastChild = newBlock;
newBlock.next = currentBlock.next;
currentBlock.next = newBlock;
newBlock.prev = currentBlock;
this.cursor.appendTo(newBlock).redraw();
return false ;
}
else if (e.which === 9 && !e.shiftKey && !currentBlock.next) {
if (currentBlock.isEmpty()) {
if (currentBlock.prev) {
this.cursor.insertAfter(this);
delete currentBlock.prev.next;
this.lastChild = currentBlock.prev;
currentBlock.jQ.remove();
this.cursor.redraw();
return false ;
}
else return this.parent.keydown(e);
}
var newBlock = new MathBlock();
newBlock.parent = this;
newBlock.jQ = $('').data(jQueryDataKey, {
block: newBlock}
).appendTo(this.jQ);
this.lastChild = newBlock;
currentBlock.next = newBlock;
newBlock.prev = currentBlock;
this.cursor.appendTo(newBlock).redraw();
return false ;
}
else if (e.which === 8) {
if (currentBlock.isEmpty()) {
if (currentBlock.prev) {
this.cursor.appendTo(currentBlock.prev);
currentBlock.prev.next = currentBlock.next;
}
else {
this.cursor.insertBefore(this);
this.firstChild = currentBlock.next;
}
if (currentBlock.next) currentBlock.next.prev = currentBlock.prev;
else this.lastChild = currentBlock.prev;
currentBlock.jQ.remove();
if (this.isEmpty()) this.cursor.deleteForward();
else this.cursor.redraw();
return false ;
}
else if (!this.cursor.prev) return false ;
}
}
return this.parent.keydown(e);
}
;
LatexCmds.vector = Vector;
LatexCmds.editable = proto(RootMathCommand, function (){
_AN_Call_init('init', this, '\\editable');
createRoot(this.jQ, this.firstChild, false , true );
var cursor;
this.placeCursor = function (c){
cursor = c.appendTo(this.firstChild);
}
;
this.firstChild.blur = function (){
if (cursor.prev !== this.parent) return ;
delete this.blur;
this.cursor.appendTo(this);
MathBlock.prototype.blur.call(this);
}
;
this.latex = function (){
return this.firstChild.latex();
}
;
this.text = function (){
return this.firstChild.text();
}
;
}
);
LatexCmds.f = bind(Symbol, 'f', 'ƒ ');
function Variable(ch, html){
Symbol.call(this, ch, '' + $.raw(html || htmlEscape(ch)) + '');
}
_ = Variable.prototype = new Symbol();
_.text = function (){
var text = this.cmd;
if (this.prev && !(this.prev instanceof Variable) && !(this.prev instanceof BinaryOperator)) text = '*' + text;
if (this.next && !(this.next instanceof BinaryOperator) && !(this.next.cmd === '^')) text += '*';
return text;
}
;
function VanillaSymbol(ch, html){
Symbol.call(this, ch, '' + $.raw(html || htmlEscape(ch)) + '');
}
VanillaSymbol.prototype = Symbol.prototype;
LatexCmds[":"] = CharCmds[" "] = bind(VanillaSymbol, '\\:', ' ');
LatexCmds.prime = CharCmds["'"] = bind(VanillaSymbol, "'", '′');
function NonSymbolaSymbol(ch, html){
Symbol.call(this, ch, '' + $.raw(html || htmlEscape(ch)) + '');
}
NonSymbolaSymbol.prototype = Symbol.prototype;
LatexCmds["@"] = NonSymbolaSymbol;
LatexCmds["&"] = bind(NonSymbolaSymbol, '\\&', '&');
LatexCmds["%"] = bind(NonSymbolaSymbol, '\\%', '%');
LatexCmds.alpha = LatexCmds.beta = LatexCmds.gamma = LatexCmds.delta = LatexCmds.zeta = LatexCmds.eta = LatexCmds.theta = LatexCmds.iota = LatexCmds.kappa = LatexCmds.mu = LatexCmds.nu = LatexCmds.xi = LatexCmds.rho = LatexCmds.sigma = LatexCmds.tau = LatexCmds.chi = LatexCmds.psi = LatexCmds.omega = proto(Symbol, function (replacedFragment, latex){
Variable.call(this, '\\' + latex + ' ', '&' + latex + ';');
}
);
LatexCmds.phi = bind(Variable, '\\phi ', 'ϕ');
LatexCmds.phiv = LatexCmds.varphi = bind(Variable, '\\varphi ', 'φ');
LatexCmds.epsilon = bind(Variable, '\\epsilon ', 'ϵ');
LatexCmds.epsiv = LatexCmds.varepsilon = bind(Variable, '\\varepsilon ', 'ε');
LatexCmds.piv = LatexCmds.varpi = bind(Variable, '\\varpi ', 'ϖ');
LatexCmds.sigmaf = LatexCmds.sigmav = LatexCmds.varsigma = bind(Variable, '\\varsigma ', 'ς');
LatexCmds.thetav = LatexCmds.vartheta = LatexCmds.thetasym = bind(Variable, '\\vartheta ', 'ϑ');
LatexCmds.upsilon = LatexCmds.upsi = bind(Variable, '\\upsilon ', 'υ');
LatexCmds.gammad = LatexCmds.Gammad = LatexCmds.digamma = bind(Variable, '\\digamma ', 'ϝ');
LatexCmds.kappav = LatexCmds.varkappa = bind(Variable, '\\varkappa ', 'ϰ');
LatexCmds.rhov = LatexCmds.varrho = bind(Variable, '\\varrho ', 'ϱ');
LatexCmds.pi = LatexCmds.π = bind(NonSymbolaSymbol, '\\pi ', 'π');
LatexCmds.lambda = bind(NonSymbolaSymbol, '\\lambda ', 'λ');
LatexCmds.Upsilon = LatexCmds.Upsi = LatexCmds.upsih = LatexCmds.Upsih = bind(Symbol, '\\Upsilon ', 'ϒ');
LatexCmds.Gamma = LatexCmds.Delta = LatexCmds.Theta = LatexCmds.Lambda = LatexCmds.Xi = LatexCmds.Pi = LatexCmds.Sigma = LatexCmds.Phi = LatexCmds.Psi = LatexCmds.Omega = LatexCmds.forall = proto(Symbol, function (replacedFragment, latex){
VanillaSymbol.call(this, '\\' + latex + ' ', '&' + latex + ';');
}
);
function BinaryOperator(cmd, html, text){
Symbol.call(this, cmd, '' + html + '', text);
}
BinaryOperator.prototype = new Symbol();
function PlusMinus(cmd, html){
VanillaSymbol.apply(this, arguments);
}
_ = PlusMinus.prototype = new BinaryOperator();
_.respace = function (){
if (!this.prev) {
this.jQ[0].className = '';
}
else if (this.prev instanceof BinaryOperator && this.next && !(this.next instanceof BinaryOperator)) {
this.jQ[0].className = 'unary-operator';
}
else {
this.jQ[0].className = 'binary-operator';
}
return this;
}
;
LatexCmds["+"] = bind(PlusMinus, '+', '+');
LatexCmds["\xE2\x80\x93"] = LatexCmds["-"] = bind(PlusMinus, '-', '−');
LatexCmds.± = LatexCmds.pm = LatexCmds.plusmn = LatexCmds.plusminus = bind(PlusMinus, '\\pm ', '±');
LatexCmds.mp = LatexCmds.mnplus = LatexCmds.minusplus = bind(PlusMinus, '\\mp ', '∓');
CharCmds["*"] = LatexCmds.sdot = LatexCmds.cdot = bind(BinaryOperator, '\\cdot ', '·');
LatexCmds["="] = bind(BinaryOperator, '=', '=');
LatexCmds["<"] = bind(BinaryOperator, '<', '<');
LatexCmds[">"] = bind(BinaryOperator, '>', '>');
LatexCmds.notin = LatexCmds.sim = LatexCmds.cong = LatexCmds.equiv = LatexCmds.oplus = LatexCmds.otimes = proto(BinaryOperator, function (replacedFragment, latex){
BinaryOperator.call(this, '\\' + latex + ' ', '&' + latex + ';');
}
);
LatexCmds.times = proto(BinaryOperator, function (replacedFragment, latex){
BinaryOperator.call(this, '\\times ', '×', '[x]');
}
);
LatexCmds.÷ = LatexCmds.div = LatexCmds.divide = LatexCmds.divides = bind(BinaryOperator, '\\div ', '÷', '[/]');
LatexCmds["\xE2\x89\xA0"] = LatexCmds.ne = LatexCmds.neq = bind(BinaryOperator, '\\ne ', '≠');
LatexCmds.ast = LatexCmds.star = LatexCmds.loast = LatexCmds.lowast = bind(BinaryOperator, '\\ast ', '∗');
LatexCmds.therefor = LatexCmds.therefore = bind(BinaryOperator, '\\therefore ', '∴');
LatexCmds.cuz = LatexCmds.because = bind(BinaryOperator, '\\because ', '∵');
LatexCmds.prop = LatexCmds.propto = bind(BinaryOperator, '\\propto ', '∝');
LatexCmds["\xE2\x89\x88"] = LatexCmds.asymp = LatexCmds.approx = bind(BinaryOperator, '\\approx ', '≈');
LatexCmds.lt = bind(BinaryOperator, '<', '<');
LatexCmds.gt = bind(BinaryOperator, '>', '>');
LatexCmds["\xE2\x89\xA4"] = LatexCmds.le = LatexCmds.leq = bind(BinaryOperator, '\\le ', '≤');
LatexCmds["\xE2\x89\xA5"] = LatexCmds.ge = LatexCmds.geq = bind(BinaryOperator, '\\ge ', '≥');
LatexCmds.isin = LatexCmds["in"] = bind(BinaryOperator, '\\in ', '∈');
LatexCmds.ni = LatexCmds.contains = bind(BinaryOperator, '\\ni ', '∋');
LatexCmds.notni = LatexCmds.niton = LatexCmds.notcontains = LatexCmds.doesnotcontain = bind(BinaryOperator, '\\not\\ni ', '∌');
LatexCmds.sub = LatexCmds.subset = bind(BinaryOperator, '\\subset ', '⊂');
LatexCmds.sup = LatexCmds.supset = LatexCmds.superset = bind(BinaryOperator, '\\supset ', '⊃');
LatexCmds.nsub = LatexCmds.notsub = LatexCmds.nsubset = LatexCmds.notsubset = bind(BinaryOperator, '\\not\\subset ', '⊄');
LatexCmds.nsup = LatexCmds.notsup = LatexCmds.nsupset = LatexCmds.notsupset = LatexCmds.nsuperset = LatexCmds.notsuperset = bind(BinaryOperator, '\\not\\supset ', '⊅');
LatexCmds.sube = LatexCmds.subeq = LatexCmds.subsete = LatexCmds.subseteq = bind(BinaryOperator, '\\subseteq ', '⊆');
LatexCmds.supe = LatexCmds.supeq = LatexCmds.supsete = LatexCmds.supseteq = LatexCmds.supersete = LatexCmds.superseteq = bind(BinaryOperator, '\\supseteq ', '⊇');
LatexCmds.nsube = LatexCmds.nsubeq = LatexCmds.notsube = LatexCmds.notsubeq = LatexCmds.nsubsete = LatexCmds.nsubseteq = LatexCmds.notsubsete = LatexCmds.notsubseteq = bind(BinaryOperator, '\\not\\subseteq ', '⊈');
LatexCmds.nsupe = LatexCmds.nsupeq = LatexCmds.notsupe = LatexCmds.notsupeq = LatexCmds.nsupsete = LatexCmds.nsupseteq = LatexCmds.notsupsete = LatexCmds.notsupseteq = LatexCmds.nsupersete = LatexCmds.nsuperseteq = LatexCmds.notsupersete = LatexCmds.notsuperseteq = bind(BinaryOperator, '\\not\\supseteq ', '⊉');
function BigSymbol(ch, html){
Symbol.call(this, ch, '' + html + '');
}
BigSymbol.prototype = new Symbol();
LatexCmds["\xE2\x88\x91"] = LatexCmds.sum = LatexCmds.summation = bind(BigSymbol, '\\sum ', '∑');
LatexCmds["\xE2\x88\x8F"] = LatexCmds.prod = LatexCmds.product = bind(BigSymbol, '\\prod ', '∏');
LatexCmds.coprod = LatexCmds.coproduct = bind(BigSymbol, '\\coprod ', '∐');
LatexCmds["\xE2\x88\xAB"] = LatexCmds["int"] = LatexCmds.integral = bind(BigSymbol, '\\int ', '∫');
LatexCmds.N = LatexCmds.naturals = LatexCmds.Naturals = bind(VanillaSymbol, '\\mathbb{N}', 'ℕ');
LatexCmds.P = LatexCmds.primes = LatexCmds.Primes = LatexCmds.projective = LatexCmds.Projective = LatexCmds.probability = LatexCmds.Probability = bind(VanillaSymbol, '\\mathbb{P}', 'ℙ');
LatexCmds.Z = LatexCmds.integers = LatexCmds.Integers = bind(VanillaSymbol, '\\mathbb{Z}', 'ℤ');
LatexCmds.Q = LatexCmds.rationals = LatexCmds.Rationals = bind(VanillaSymbol, '\\mathbb{Q}', 'ℚ');
LatexCmds.R = LatexCmds.reals = LatexCmds.Reals = bind(VanillaSymbol, '\\mathbb{R}', 'ℝ');
LatexCmds.C = LatexCmds.complex = LatexCmds.Complex = LatexCmds.complexes = LatexCmds.Complexes = LatexCmds.complexplane = LatexCmds.Complexplane = LatexCmds.ComplexPlane = bind(VanillaSymbol, '\\mathbb{C}', 'ℂ');
LatexCmds.H = LatexCmds.Hamiltonian = LatexCmds.quaternions = LatexCmds.Quaternions = bind(VanillaSymbol, '\\mathbb{H}', 'ℍ');
LatexCmds.quad = LatexCmds.emsp = bind(VanillaSymbol, '\\quad ', ' ');
LatexCmds.qquad = bind(VanillaSymbol, '\\qquad ', ' ');
LatexCmds.diamond = bind(VanillaSymbol, '\\diamond ', '◇');
LatexCmds.bigtriangleup = bind(VanillaSymbol, '\\bigtriangleup ', '△');
LatexCmds.ominus = bind(VanillaSymbol, '\\ominus ', '⊖');
LatexCmds.uplus = bind(VanillaSymbol, '\\uplus ', '⊎');
LatexCmds.bigtriangledown = bind(VanillaSymbol, '\\bigtriangledown ', '▽');
LatexCmds.sqcap = bind(VanillaSymbol, '\\sqcap ', '⊓');
LatexCmds.triangleleft = bind(VanillaSymbol, '\\triangleleft ', '⊲');
LatexCmds.sqcup = bind(VanillaSymbol, '\\sqcup ', '⊔');
LatexCmds.triangleright = bind(VanillaSymbol, '\\triangleright ', '⊳');
LatexCmds.odot = bind(VanillaSymbol, '\\odot ', '⊙');
LatexCmds.bigcirc = bind(VanillaSymbol, '\\bigcirc ', '◯');
LatexCmds.dagger = bind(VanillaSymbol, '\\dagger ', '');
LatexCmds.ddagger = bind(VanillaSymbol, '\\ddagger ', '');
LatexCmds.wr = bind(VanillaSymbol, '\\wr ', '≀');
LatexCmds.amalg = bind(VanillaSymbol, '\\amalg ', '∐');
LatexCmds.models = bind(VanillaSymbol, '\\models ', '⊨');
LatexCmds.prec = bind(VanillaSymbol, '\\prec ', '≺');
LatexCmds.succ = bind(VanillaSymbol, '\\succ ', '≻');
LatexCmds.preceq = bind(VanillaSymbol, '\\preceq ', '≼');
LatexCmds.succeq = bind(VanillaSymbol, '\\succeq ', '≽');
LatexCmds.simeq = bind(VanillaSymbol, '\\simeq ', '≃');
LatexCmds.mid = bind(VanillaSymbol, '\\mid ', '∣');
LatexCmds.ll = bind(VanillaSymbol, '\\ll ', '≪');
LatexCmds.gg = bind(VanillaSymbol, '\\gg ', '≫');
LatexCmds.parallel = bind(VanillaSymbol, '\\parallel ', '∥');
LatexCmds.bowtie = bind(VanillaSymbol, '\\bowtie ', '⋈');
LatexCmds.sqsubset = bind(VanillaSymbol, '\\sqsubset ', '⊏');
LatexCmds.sqsupset = bind(VanillaSymbol, '\\sqsupset ', '⊐');
LatexCmds.smile = bind(VanillaSymbol, '\\smile ', '⌣');
LatexCmds.sqsubseteq = bind(VanillaSymbol, '\\sqsubseteq ', '⊑');
LatexCmds.sqsupseteq = bind(VanillaSymbol, '\\sqsupseteq ', '⊒');
LatexCmds.doteq = bind(VanillaSymbol, '\\doteq ', '≐');
LatexCmds.frown = bind(VanillaSymbol, '\\frown ', '⌢');
LatexCmds.vdash = bind(VanillaSymbol, '\\vdash ', '⊦');
LatexCmds.dashv = bind(VanillaSymbol, '\\dashv ', '⊣');
LatexCmds.longleftarrow = bind(VanillaSymbol, '\\longleftarrow ', '←');
LatexCmds.longrightarrow = bind(VanillaSymbol, '\\longrightarrow ', '→');
LatexCmds.Longleftarrow = bind(VanillaSymbol, '\\Longleftarrow ', '⇐');
LatexCmds.Longrightarrow = bind(VanillaSymbol, '\\Longrightarrow ', '⇒');
LatexCmds.longleftrightarrow = bind(VanillaSymbol, '\\longleftrightarrow ', '↔');
LatexCmds.updownarrow = bind(VanillaSymbol, '\\updownarrow ', '↕');
LatexCmds.Longleftrightarrow = bind(VanillaSymbol, '\\Longleftrightarrow ', '⇔');
LatexCmds.Updownarrow = bind(VanillaSymbol, '\\Updownarrow ', '⇕');
LatexCmds.mapsto = bind(VanillaSymbol, '\\mapsto ', '↦');
LatexCmds.nearrow = bind(VanillaSymbol, '\\nearrow ', '↗');
LatexCmds.hookleftarrow = bind(VanillaSymbol, '\\hookleftarrow ', '↩');
LatexCmds.hookrightarrow = bind(VanillaSymbol, '\\hookrightarrow ', '↪');
LatexCmds.searrow = bind(VanillaSymbol, '\\searrow ', '↘');
LatexCmds.leftharpoonup = bind(VanillaSymbol, '\\leftharpoonup ', '↼');
LatexCmds.rightharpoonup = bind(VanillaSymbol, '\\rightharpoonup ', '⇀');
LatexCmds.swarrow = bind(VanillaSymbol, '\\swarrow ', '↙');
LatexCmds.leftharpoondown = bind(VanillaSymbol, '\\leftharpoondown ', '↽');
LatexCmds.rightharpoondown = bind(VanillaSymbol, '\\rightharpoondown ', '⇁');
LatexCmds.nwarrow = bind(VanillaSymbol, '\\nwarrow ', '↖');
LatexCmds.ldots = bind(VanillaSymbol, '\\ldots ', '…');
LatexCmds.cdots = bind(VanillaSymbol, '\\cdots ', '⋯');
LatexCmds.vdots = bind(VanillaSymbol, '\\vdots ', '⋮');
LatexCmds.ddots = bind(VanillaSymbol, '\\ddots ', '⋰');
LatexCmds.surd = bind(VanillaSymbol, '\\surd ', '√');
LatexCmds.triangle = bind(VanillaSymbol, '\\triangle ', '▵');
LatexCmds.ell = bind(VanillaSymbol, '\\ell ', 'ℓ');
LatexCmds.top = bind(VanillaSymbol, '\\top ', '⊤');
LatexCmds.flat = bind(VanillaSymbol, '\\flat ', '♭');
LatexCmds.natural = bind(VanillaSymbol, '\\natural ', '♮');
LatexCmds.sharp = bind(VanillaSymbol, '\\sharp ', '♯');
LatexCmds.wp = bind(VanillaSymbol, '\\wp ', '℘');
LatexCmds.bot = bind(VanillaSymbol, '\\bot ', '⊥');
LatexCmds.clubsuit = bind(VanillaSymbol, '\\clubsuit ', '♣');
LatexCmds.diamondsuit = bind(VanillaSymbol, '\\diamondsuit ', '♢');
LatexCmds.heartsuit = bind(VanillaSymbol, '\\heartsuit ', '♡');
LatexCmds.spadesuit = bind(VanillaSymbol, '\\spadesuit ', '♠');
LatexCmds.oint = bind(VanillaSymbol, '\\oint ', '∮');
LatexCmds.bigcap = bind(VanillaSymbol, '\\bigcap ', '∩');
LatexCmds.bigcup = bind(VanillaSymbol, '\\bigcup ', '∪');
LatexCmds.bigsqcup = bind(VanillaSymbol, '\\bigsqcup ', '⊔');
LatexCmds.bigvee = bind(VanillaSymbol, '\\bigvee ', '∨');
LatexCmds.bigwedge = bind(VanillaSymbol, '\\bigwedge ', '∧');
LatexCmds.bigodot = bind(VanillaSymbol, '\\bigodot ', '⊙');
LatexCmds.bigotimes = bind(VanillaSymbol, '\\bigotimes ', '⊗');
LatexCmds.bigoplus = bind(VanillaSymbol, '\\bigoplus ', '⊕');
LatexCmds.biguplus = bind(VanillaSymbol, '\\biguplus ', '⊎');
LatexCmds.lfloor = bind(VanillaSymbol, '\\lfloor ', '⌊');
LatexCmds.rfloor = bind(VanillaSymbol, '\\rfloor ', '⌋');
LatexCmds.lceil = bind(VanillaSymbol, '\\lceil ', '⌈');
LatexCmds.rceil = bind(VanillaSymbol, '\\rceil ', '⌉');
LatexCmds.slash = bind(VanillaSymbol, '/');
LatexCmds.lbrace = bind(VanillaSymbol, '\\lbrace ', '{');
LatexCmds.rbrace = bind(VanillaSymbol, '\\rbrace ', '}');
LatexCmds.caret = bind(VanillaSymbol, '\\caret ', '^');
LatexCmds.underscore = bind(VanillaSymbol, '\\underscore ', '_');
LatexCmds.backslash = bind(VanillaSymbol, '\\backslash ', '\\');
LatexCmds.vert = bind(VanillaSymbol, '|');
LatexCmds.perp = LatexCmds.perpendicular = bind(VanillaSymbol, '\\perp ', '⊥');
LatexCmds.nabla = LatexCmds.del = bind(VanillaSymbol, '\\nabla ', '∇');
LatexCmds.hbar = bind(VanillaSymbol, '\\hbar ', 'ℏ');
LatexCmds.AA = LatexCmds.Angstrom = LatexCmds.angstrom = bind(VanillaSymbol, '\\text\\AA ', 'Å');
LatexCmds.ring = LatexCmds.circ = LatexCmds.circle = bind(VanillaSymbol, '\\circ ', '∘');
LatexCmds.bull = LatexCmds.bullet = bind(VanillaSymbol, '\\bullet ', '•');
LatexCmds.setminus = LatexCmds.smallsetminus = bind(VanillaSymbol, '\\setminus ', '∖');
LatexCmds.not = LatexCmds.¬ = LatexCmds.neg = bind(VanillaSymbol, '\\neg ', '¬');
LatexCmds["\xE2\x80\xA6"] = LatexCmds.dots = LatexCmds.ellip = LatexCmds.hellip = LatexCmds.ellipsis = LatexCmds.hellipsis = bind(VanillaSymbol, '\\dots ', '…');
LatexCmds.converges = LatexCmds.darr = LatexCmds.dnarr = LatexCmds.dnarrow = LatexCmds.downarrow = bind(VanillaSymbol, '\\downarrow ', '↓');
LatexCmds.dArr = LatexCmds.dnArr = LatexCmds.dnArrow = LatexCmds.Downarrow = bind(VanillaSymbol, '\\Downarrow ', '⇓');
LatexCmds.diverges = LatexCmds.uarr = LatexCmds.uparrow = bind(VanillaSymbol, '\\uparrow ', '↑');
LatexCmds.uArr = LatexCmds.Uparrow = bind(VanillaSymbol, '\\Uparrow ', '⇑');
LatexCmds.to = bind(BinaryOperator, '\\to ', '→');
LatexCmds.rarr = LatexCmds.rightarrow = bind(VanillaSymbol, '\\rightarrow ', '→');
LatexCmds.implies = bind(BinaryOperator, '\\Rightarrow ', '⇒');
LatexCmds.rArr = LatexCmds.Rightarrow = bind(VanillaSymbol, '\\Rightarrow ', '⇒');
LatexCmds.gets = bind(BinaryOperator, '\\gets ', '←');
LatexCmds.larr = LatexCmds.leftarrow = bind(VanillaSymbol, '\\leftarrow ', '←');
LatexCmds.impliedby = bind(BinaryOperator, '\\Leftarrow ', '⇐');
LatexCmds.lArr = LatexCmds.Leftarrow = bind(VanillaSymbol, '\\Leftarrow ', '⇐');
LatexCmds.harr = LatexCmds.lrarr = LatexCmds.leftrightarrow = bind(VanillaSymbol, '\\leftrightarrow ', '↔');
LatexCmds.iff = bind(BinaryOperator, '\\Leftrightarrow ', '⇔');
LatexCmds.hArr = LatexCmds.lrArr = LatexCmds.Leftrightarrow = bind(VanillaSymbol, '\\Leftrightarrow ', '⇔');
LatexCmds.Re = LatexCmds.Real = LatexCmds.real = bind(VanillaSymbol, '\\Re ', 'ℜ');
LatexCmds.Im = LatexCmds.imag = LatexCmds.image = LatexCmds.imagin = LatexCmds.imaginary = LatexCmds.Imaginary = bind(VanillaSymbol, '\\Im ', 'ℑ');
LatexCmds.part = LatexCmds.partial = bind(VanillaSymbol, '\\partial ', '∂');
LatexCmds.inf = LatexCmds.infin = LatexCmds.infty = LatexCmds.infinity = bind(VanillaSymbol, '\\infty ', '∞');
LatexCmds.alef = LatexCmds.alefsym = LatexCmds.aleph = LatexCmds.alephsym = bind(VanillaSymbol, '\\aleph ', 'ℵ');
LatexCmds.xist = LatexCmds.xists = LatexCmds.exist = LatexCmds.exists = bind(VanillaSymbol, '\\exists ', '∃');
LatexCmds.and = LatexCmds.land = LatexCmds.wedge = bind(VanillaSymbol, '\\wedge ', '∧');
LatexCmds.or = LatexCmds.lor = LatexCmds.vee = bind(VanillaSymbol, '\\vee ', '∨');
LatexCmds.o = LatexCmds.O = LatexCmds.empty = LatexCmds.emptyset = LatexCmds.oslash = LatexCmds.Oslash = LatexCmds.nothing = LatexCmds.varnothing = bind(BinaryOperator, '\\varnothing ', '∅');
LatexCmds.cup = LatexCmds.union = bind(VanillaSymbol, '\\cup ', '∪');
LatexCmds.cap = LatexCmds.intersect = LatexCmds.intersection = bind(VanillaSymbol, '\\cap ', '∩');
LatexCmds.deg = LatexCmds.degree = bind(VanillaSymbol, '^\\circ ', '°');
LatexCmds.ang = LatexCmds.angle = bind(VanillaSymbol, '\\angle ', '∠');
function NonItalicizedFunction(replacedFragment, fn){
Symbol.call(this, '\\' + fn + ' ', '' + htmlEscape(fn) + '');
}
_ = NonItalicizedFunction.prototype = new Symbol();
_.respace = function (){
this.jQ[0].className = (this.next instanceof SupSub || this.next instanceof Bracket)? '': 'non-italicized-function';
}
;
LatexCmds.ln = LatexCmds.lg = LatexCmds.log = LatexCmds.span = LatexCmds.proj = LatexCmds.det = LatexCmds.dim = LatexCmds.min = LatexCmds.max = LatexCmds.mod = LatexCmds.lcm = LatexCmds.gcd = LatexCmds.gcf = LatexCmds.hcf = LatexCmds.lim = NonItalicizedFunction;
(function (){
var trig = ['sin', 'cos', 'tan', 'sec', 'cosec', 'csc', 'cotan', 'cot'] ;
for (var i in trig){
LatexCmds[trig[i]] = LatexCmds[trig[i] + 'h'] = LatexCmds['a' + trig[i]] = LatexCmds['arc' + trig[i]] = LatexCmds['a' + trig[i] + 'h'] = LatexCmds['arc' + trig[i] + 'h'] = NonItalicizedFunction;
}
}
());
function Cursor(root){
this.parent = this.root = root;
var jQ = this.jQ = this._jQ = $('');
this.blink = function (){
jQ.toggleClass('blink');
}
;
}
_ = Cursor.prototype;
_.prev = 0;
_.next = 0;
_.parent = 0;
_.show = function (){
this.jQ = this._jQ.removeClass('blink');
if ('intervalId' in this) clearInterval(this.intervalId);
else {
if (this.next) {
if (this.selection && this.selection.prev === this.prev) this.jQ.insertBefore(this.selection.jQ);
else this.jQ.insertBefore(this.next.jQ.first());
}
else this.jQ.appendTo(this.parent.jQ);
this.parent.focus();
}
this.intervalId = _AN_Call_setinterval('setInterval', window, this.blink, 500);
return this;
}
;
_.hide = function (){
if ('intervalId' in this) clearInterval(this.intervalId);
delete this.intervalId;
this.jQ.detach();
this.jQ = $();
return this;
}
;
_.redraw = function (){
for (var ancestor = this.parent;
ancestor; ancestor = ancestor.parent)if (ancestor.redraw) ancestor.redraw();
}
;
_.insertAt = function (parent, prev, next){
var old_parent = this.parent;
this.parent = parent;
this.prev = prev;
this.next = next;
old_parent.blur();
}
;
_.insertBefore = function (el){
this.insertAt(el.parent, el.prev, el);
this.parent.jQ.addClass('hasCursor');
this.jQ.insertBefore(el.jQ.first());
return this;
}
;
_.insertAfter = function (el){
this.insertAt(el.parent, el, el.next);
this.parent.jQ.addClass('hasCursor');
this.jQ.insertAfter(el.jQ.last());
return this;
}
;
_.prependTo = function (el){
this.insertAt(el, 0, el.firstChild);
if (el.textarea) this.jQ.insertAfter(el.textarea);
else this.jQ.prependTo(el.jQ);
el.focus();
return this;
}
;
_.appendTo = function (el){
this.insertAt(el, el.lastChild, 0);
this.jQ.appendTo(el.jQ);
el.focus();
return this;
}
;
_.hopLeft = function (){
this.jQ.insertBefore(this.prev.jQ.first());
this.next = this.prev;
this.prev = this.prev.prev;
return this;
}
;
_.hopRight = function (){
this.jQ.insertAfter(this.next.jQ.last());
this.prev = this.next;
this.next = this.next.next;
return this;
}
;
_.moveLeft = function (){
if (this.selection) this.insertBefore(this.selection.prev.next || this.parent.firstChild).clearSelection();
else {
if (this.prev) {
if (this.prev.lastChild) this.appendTo(this.prev.lastChild);
else this.hopLeft();
}
else {
if (this.parent.prev) this.appendTo(this.parent.prev);
else if (this.parent !== this.root) this.insertBefore(this.parent.parent);
}
}
return _AN_Call_show('show', this);
}
;
_.moveRight = function (){
if (this.selection) this.insertAfter(this.selection.next.prev || this.parent.lastChild).clearSelection();
else {
if (this.next) {
if (this.next.firstChild) this.prependTo(this.next.firstChild);
else this.hopRight();
}
else {
if (this.parent.next) this.prependTo(this.parent.next);
else if (this.parent !== this.root) this.insertAfter(this.parent.parent);
}
}
return _AN_Call_show('show', this);
}
;
_.seek = function (target, pageX, pageY){
var cursor = this.clearSelection();
if (target.hasClass('empty')) {
cursor.prependTo(target.data(jQueryDataKey).block);
return cursor;
}
var data = target.data(jQueryDataKey);
if (data) {
if (data.cmd && !data.block) {
if (target.outerWidth() > 2 * (pageX - target.offset().left)) cursor.insertBefore(data.cmd);
else cursor.insertAfter(data.cmd);
return cursor;
}
}
else {
target = target.parent();
data = target.data(jQueryDataKey);
if (!data) data = {
block: cursor.root}
;
}
if (data.cmd) cursor.insertAfter(data.cmd);
else cursor.appendTo(data.block);
var dist = cursor.jQ.offset().left - pageX, prevDist;
do {
cursor.moveLeft();
prevDist = dist;
dist = cursor.jQ.offset().left - pageX;
}
while(dist > 0 && (cursor.prev || cursor.parent !== cursor.root))if (- dist > prevDist) cursor.moveRight();
return cursor;
}
;
_.resolveNonItalicizedFunctions = function (){
var node = this.prev;
var raw = node? _AN_Call_replace('replace', node.latex(), / $/, ''): null ;
var count = 0;
var functions = {
ln: 1,
lg: 1,
log: 1,
span: 1,
proj: 1,
det: 1,
dim: 1,
min: 1,
max: 1,
mod: 1,
lcm: 1,
gcd: 1,
gcf: 1,
hcf: 1,
lim: 1,
sin: 1,
sinh: 1,
asin: 1,
arcsin: 1,
asinh: 1,
arcsinh: 1,
cos: 1,
cosh: 1,
acos: 1,
arccos: 1,
acosh: 1,
arccosh: 1,
tan: 1,
tanh: 1,
atan: 1,
arctan: 1,
atanh: 1,
arctanh: 1,
sec: 1,
sech: 1,
asec: 1,
arcsec: 1,
asech: 1,
arcsech: 1,
cosec: 1,
cosech: 1,
acosec: 1,
arccosec: 1,
acosech: 1,
arccosech: 1,
csc: 1,
csch: 1,
acsc: 1,
arccsc: 1,
acsch: 1,
arccsch: 1,
cotan: 1,
cotanh: 1,
acotan: 1,
arccotan: 1,
acotanh: 1,
arccotanh: 1,
cot: 1,
coth: 1,
acot: 1,
arccot: 1,
acoth: 1,
arccoth: 1}
;
var latex = '';
while (node && raw){
var single_char = raw.match(/^[a-z]$/);
if (single_char || latex && raw[0] == '\\' && functions[raw.substring(1)] && functions[(raw + latex).substring(1)]) {
count++ ;
latex = _AN_Call_replace('replace', raw, /\\/, '') + latex;
node = node.prev;
raw = node? _AN_Call_replace('replace', node.latex(), / $/, ''): null ;
if (!single_char || (!node || !raw.match(/^[a-z]$/)) && functions[latex]) {
for (var i = 0;
i < count; i++ ){
this.selectLeft();
}
this.writeLatex("\\" + latex);
return ;
}
}
else {
return ;
}
}
}
;
_.writeLatex = function (latex, noMoveCursor){
this.deleteSelection();
latex = (latex && latex.match(/\\text\{([^}]|\\\})*\}|\\:|\\[a-z]*|[^\s]/ig)) || 0;
(function writeLatexBlock(cursor){
while (latex.length){
var token = latex.shift();
if (!token || token === '}' || token === ']') return ;
var cmd;
if (token.slice(0, 6) === '\\text{') {
cmd = new TextBlock(token.slice(6, -1));
cursor.insertNew(cmd).insertAfter(cmd);
continue ;
}
else if (token === '\\left' || token === '\\right') {
token = latex.shift();
if (token === '\\') token = latex.shift();
cursor.insertCh(token);
cmd = cursor.prev || cursor.parent.parent;
if (cursor.prev) return ;
else latex.unshift('{');
}
else if (/^\\[a-z:]+$/i.test(token)) {
token = token.slice(1);
var cmd = LatexCmds[token];
if (cmd) {
cmd = new cmd(undefined, token);
if (latex[0] === '[' && cmd.optional_arg_command) {
token = cmd.optional_arg_command;
cmd = new LatexCmds[token](undefined, token);
}
cursor.insertNew(cmd);
}
else {
cmd = new TextBlock(token);
cursor.insertNew(cmd).insertAfter(cmd);
continue ;
}
}
else {
if (token.match(/[a-eg-zA-Z]/)) cmd = new Variable(token);
else if (cmd = LatexCmds[token]) cmd = new cmd();
else cmd = new VanillaSymbol(token);
cursor.insertNew(cmd);
}
cmd.eachChild(function (child){
cursor.appendTo(child);
var token = latex.shift();
if (!token) return false ;
if (token === '{' || token === '[') writeLatexBlock(cursor);
else cursor.insertCh(token);
}
);
if (!noMoveCursor) cursor.insertAfter(cmd);
}
}
(this));
return this.hide();
}
;
_.write = function (ch){
var ret = _AN_Call_show('show', this).insertCh(ch);
if (this.root.toolbar) this.resolveNonItalicizedFunctions();
return ret;
}
;
_.insertCh = function (ch){
if (this.selection) {
this.prev = this.selection.prev;
this.next = this.selection.next;
}
var cmd;
if (ch.match(/^[a-eg-zA-Z]$/)) cmd = new Variable(ch);
else if (cmd = CharCmds[ch] || LatexCmds[ch]) cmd = new cmd(this.selection, ch);
else cmd = new VanillaSymbol(ch);
if (this.selection) {
if (cmd instanceof Symbol) this.selection.remove();
delete this.selection;
}
return this.insertNew(cmd);
}
;
_.insertNew = function (cmd){
cmd.insertAt(this);
return this;
}
;
_.insertCmd = function (latexCmd, replacedFragment){
var cmd = LatexCmds[latexCmd];
if (cmd) {
cmd = new cmd(replacedFragment, latexCmd);
this.insertNew(cmd);
if (cmd instanceof Symbol && replacedFragment) replacedFragment.remove();
}
else {
cmd = new TextBlock(latexCmd);
cmd.firstChild.focus = function (){
delete this.focus;
return this;
}
;
this.insertNew(cmd).insertAfter(cmd);
if (replacedFragment) replacedFragment.remove();
}
return this;
}
;
_.unwrapGramp = function (){
var gramp = this.parent.parent, greatgramp = gramp.parent, prev = gramp.prev, cursor = this;
gramp.eachChild(function (uncle){
if (uncle.isEmpty()) return ;
uncle.eachChild(function (cousin){
cousin.parent = greatgramp;
cousin.jQ.insertBefore(gramp.jQ.first());
}
);
uncle.firstChild.prev = prev;
if (prev) prev.next = uncle.firstChild;
else greatgramp.firstChild = uncle.firstChild;
prev = uncle.lastChild;
}
);
prev.next = gramp.next;
if (gramp.next) gramp.next.prev = prev;
else greatgramp.lastChild = prev;
if (!this.next) {
if (this.prev) this.next = this.prev.next;
else {
while (!this.next){
this.parent = this.parent.next;
if (this.parent) this.next = this.parent.firstChild;
else {
this.next = gramp.next;
this.parent = greatgramp;
break ;
}
}
}
}
if (this.next) this.insertBefore(this.next);
else this.appendTo(greatgramp);
gramp.jQ.remove();
if (gramp.prev) gramp.prev.respace();
if (gramp.next) gramp.next.respace();
}
;
_.backspace = function (){
if (this.deleteSelection()) ;
else if (this.prev) {
if (this.prev.isEmpty()) this.prev = this.prev.remove().prev;
else this.selectLeft();
}
else if (this.parent !== this.root) {
if (this.parent.parent.isEmpty()) return this.insertAfter(this.parent.parent).backspace();
else this.unwrapGramp();
}
if (this.prev) this.prev.respace();
if (this.next) this.next.respace();
this.redraw();
return this;
}
;
_.deleteForward = function (){
if (this.deleteSelection()) ;
else if (this.next) {
if (this.next.isEmpty()) this.next = this.next.remove().next;
else this.selectRight();
}
else if (this.parent !== this.root) {
if (this.parent.parent.isEmpty()) return this.insertBefore(this.parent.parent).deleteForward();
else this.unwrapGramp();
}
if (this.prev) this.prev.respace();
if (this.next) this.next.respace();
this.redraw();
return this;
}
;
_.selectFrom = function (anticursor){
var oneA = this, otherA = anticursor;
loopThroughAncestors: while (true ){
for (var oneI = this;
oneI !== oneA.parent.parent; oneI = oneI.parent.parent)if (oneI.parent === otherA.parent) {
left = oneI;
right = otherA;
break loopThroughAncestors;
}
for (var otherI = anticursor;
otherI !== otherA.parent.parent; otherI = otherI.parent.parent)if (oneA.parent === otherI.parent) {
left = oneA;
right = otherI;
break loopThroughAncestors;
}
if (oneA.parent.parent) oneA = oneA.parent.parent;
if (otherA.parent.parent) otherA = otherA.parent.parent;
}
var left, right, leftRight;
if (left.next !== right) {
for (var next = left;
next; next = next.next){
if (next === right.prev) {
leftRight = true ;
break ;
}
}
if (!leftRight) {
leftRight = right;
right = left;
left = leftRight;
}
}
this.hide().selection = new Selection(left.parent, left.prev, right.next);
this.insertAfter(right.next.prev || right.parent.lastChild);
this.root.selectionChanged();
}
;
_.selectLeft = function (){
if (this.selection) {
if (this.selection.prev === this.prev) {
if (this.prev) {
this.hopLeft().next.jQ.prependTo(this.selection.jQ);
this.selection.prev = this.prev;
}
else if (this.parent !== this.root) this.insertBefore(this.parent.parent).selection.levelUp();
}
else {
this.prev.jQ.insertAfter(this.selection.jQ);
this.hopLeft().selection.next = this.next;
if (this.selection.prev === this.prev) {
this.deleteSelection();
return ;
}
}
}
else {
if (this.prev) this.hopLeft();
else if (this.parent !== this.root) this.insertBefore(this.parent.parent);
else return ;
this.hide().selection = new Selection(this.parent, this.prev, this.next.next);
}
this.root.selectionChanged();
}
;
_.selectRight = function (){
if (this.selection) {
if (this.selection.next === this.next) {
if (this.next) {
this.hopRight().prev.jQ.appendTo(this.selection.jQ);
this.selection.next = this.next;
}
else if (this.parent !== this.root) this.insertAfter(this.parent.parent).selection.levelUp();
}
else {
this.next.jQ.insertBefore(this.selection.jQ);
this.hopRight().selection.prev = this.prev;
if (this.selection.next === this.next) {
this.deleteSelection();
return ;
}
}
}
else {
if (this.next) this.hopRight();
else if (this.parent !== this.root) this.insertAfter(this.parent.parent);
else return ;
this.hide().selection = new Selection(this.parent, this.prev.prev, this.next);
}
this.root.selectionChanged();
}
;
_.clearSelection = function (){
if (_AN_Call_show('show', this).selection) {
_AN_Call_clear('clear', this.selection);
delete this.selection;
this.root.selectionChanged();
}
return this;
}
;
_.deleteSelection = function (){
if (!_AN_Call_show('show', this).selection) return false ;
this.prev = this.selection.prev;
this.next = this.selection.next;
this.selection.remove();
delete this.selection;
this.root.selectionChanged();
return true ;
}
;
function Selection(parent, prev, next){
MathFragment.apply(this, arguments);
}
_ = Selection.prototype = new MathFragment();
_.jQinit = function (children){
this.jQ = children.wrapAll('').parent();
}
;
_.levelUp = function (){
_AN_Call_clear('clear', this).jQinit(this.parent.parent.jQ);
this.prev = this.parent.parent.prev;
this.next = this.parent.parent.next;
this.parent = this.parent.parent.parent;
return this;
}
;
_.clear = function (){
this.jQ.replaceWith(this.jQ.children());
return this;
}
;
_.blockify = function (){
this.jQ.replaceWith(this.jQ = this.jQ.children());
return MathFragment.prototype.blockify.call(this);
}
;
_.detach = function (){
var block = MathFragment.prototype.blockify.call(this);
this.blockify = function (){
this.jQ.replaceWith(block.jQ = this.jQ = this.jQ.children());
return block;
}
;
return this;
}
;
$.fn.mathquill = function (cmd, latex){
switch (cmd){
case 'redraw': this.find(':not(:has(:first))').each(function (){
var data = $(this).data(jQueryDataKey);
if (data && (data.cmd || data.block)) Cursor.prototype.redraw.call(data.cmd || data.block);
}
);
return this;
case 'revert': return this.each(function (){
var data = $(this).data(jQueryDataKey);
if (data && data.revert) data.revert();
}
);
case 'latex': if (_AN_Read_length('length', arguments) > 1) {
return this.each(function (){
var data = $(this).data(jQueryDataKey);
if (data && data.block && data.block.renderLatex) data.block.renderLatex(latex);
}
);
}
var data = this.data(jQueryDataKey);
return data && data.block && data.block.latex();
case 'text': var data = this.data(jQueryDataKey);
return data && data.block && data.block.text();
case 'html': return _AN_Call_replace('replace', _AN_Call_replace('replace', _AN_Call_replace('replace', _AN_Call_replace('replace', this.html(), / ?hasCursor|hasCursor /, ''), / class=(""|(?= |>))/g, ''), /<\/span>/i, ''), /