(function ($) { var QUEUE = MathJax.Hub.queue; // shorthand for the queue var math = null; // the element jax for the math output var trigger_update = true; var input_textarea = $('#MathInput'); input_textarea.change(function(){ trigger_update = true; }) .keyup(function(){ trigger_update = true; }); $('#input').click(function(){ input_textarea.focus(); }); // Get the element jax when MathJax has produced it. QUEUE.Push(function() { // The onchange event handler that typesets the math entered // by the user. Hide the box, then typeset, then show it again // so we don't see a flash as the math is cleared and replaced. var update_math = function(tex) { var parts = tex.split('\n'); var math_container = $('#math'), math_lines = math_container.find('div.box script'); // Select all mathjax instances which are inside a div.box element var mathjax_instances = []; var all_instances = MathJax.Hub.getAllJax('math'); for (var i = 0; i < all_instances.length; i++) { var elem = all_instances[i]; if ($('#' + elem.inputID).parent().hasClass('box')) mathjax_instances.push(elem); } var real_lines = 0, updated_line = -1; for (var p = 0; p < parts.length; p++) { if (!parts[p]) continue; // Check if we want to update an existing line or append the // line. if (real_lines < math_lines.length) { var elem = mathjax_instances[real_lines]; // Update the line when the input is modified. if (elem.originalText != parts[p]) { updated_line = real_lines; QUEUE.Push(['Text', elem, parts[p]]); } if (updated_line > -1) { // Remove the out-of-date status information. This will // be done from now on for all remaining lines, whether // they are up-to-date or not. $(math_lines[real_lines]).parent() .removeClass('wrong').removeClass('correct'); } } else { var line = '`' + parts[p] + '`', elem = $('
').text(line); math_container.append(elem); QUEUE.Push(['Typeset', MathJax.Hub, elem[0]]); } real_lines++; } QUEUE.Push(function() { // Remove out-dated mathematical lines. for (var p = real_lines; p < math_lines.length; p++) $(math_lines[p].parentNode).remove(); // Remove old hints, given that at least one line is updated. // Iterate over the DOM nodes until the updated line is found, // and remove all following hint nodes. Note that if there is // no line updated, all hints not directly following the last // line are removed. var elems = $('#math div'); if(updated_line == -1) updated_line = real_lines; for(var i = 0, lines = 0, hints = 0; i < elems.length; i++) { var elem = $(elems[i]); if (lines > updated_line || hints >= updated_line) { if (elem.hasClass('hint')) elem.remove(); } else if (elem.hasClass('hint')) hints++; else if (elem.hasClass('box')) lines++; } }); } window.update_math = function() { if (trigger_update) { trigger_update = false; update_math(input_textarea.val()); } }; setInterval(window.update_math, 100); }); var loader = $('#loader'); window.show_loader = function() { loader.show().css('display', 'inline-block'); }; window.hide_loader = function() { loader.hide(); }; window.append_hint = function(hint) { $('#math div').last().filter('.hint').remove(); var elem = $('