--- loncom/html/adm/LC_math_editor/src/ui.js 2014/09/24 18:14:39 1.1 +++ loncom/html/adm/LC_math_editor/src/ui.js 2015/02/24 15:20:44 1.2 @@ -43,8 +43,15 @@ var handleChange = function(math_object) test2 = test1.replace(/\([^\(\)]*\)/g, ''); } if (test2.split("(").length == test2.split(")").length) { - if (test2.indexOf(Definitions.ARG_SEPARATOR) != -1) { - txt = '['+txt+']'; + test1 = ''; + while (test2 != test1) { + test1 = test2; + test2 = test1.replace(/\{[^\{\}]*\}/g, ''); + } + if (test2.split("{").length == test2.split("}").length) { + if (test2.indexOf(Definitions.ARG_SEPARATOR) != -1) { + txt = '['+txt+']'; + } } } } @@ -86,64 +93,217 @@ var handleChange = function(math_object) } } -var init_done = false; +var math_objects = []; /* Looks for elements with the "math" class, and adds a preview div afterward which is updated automatically. + Can be called again after math fields have been added, removed, or when options have changed. */ var initEditors = function() { - if (init_done) - return; - init_done = true; - var math_objects = []; + // to hide the MathJax messages (note: this could be done elsewhere) + MathJax.Hub.Config({ + messageStyle: "none" + }); var math_inputs = document.getElementsByClassName('math'); + // first remove the nodes and objects for the inputs that are gone + for (var i=0; i ta_rect.bottom + output_node.offsetHeight) + output_node.style.top = (ta_pos.top + ta.offsetHeight) + "px"; + else + output_node.style.top = (ta_pos.top - output_node.offsetHeight) + "px"; + } + if (ta.nextSibling) + ta.parentNode.insertBefore(output_node, ta.nextSibling); + else + ta.parentNode.appendChild(output_node); + var hide_node = function(an_output_node) { + // returns a function that will hide the node on any event + // (we can't use the node directly because it changes in the loop) + return function(e) { + an_output_node.style.display = "none"; + }; + } + var focus = function(a_ta, an_output_node) { + return function(e) { + if (a_ta.value != '') { + an_output_node.style.display = "block"; + place(a_ta, an_output_node); + } + }; }; - }; - var startChange = changeObjectN(i); - if (ta.value != oldtxt) - startChange(); // process non-empty fields even though they are not visible yet - ta.addEventListener('change', startChange, false); - ta.addEventListener('keyup', startChange, false); + ta.addEventListener("blur", hide_node(output_node), false); + ta.addEventListener("focus", focus(ta, output_node), false); + output_node.addEventListener("mouseenter", hide_node(output_node), false); + ind_math = math_objects.length; + var oldtxt = ""; + math_objects[ind_math] = { + "ta": ta, + "output_node": output_node, + "oldtxt": oldtxt, + "parser": new Parser(implicit_operators, unit_mode, constants) + }; + var changeObjectN = function(n) { + return function(e) { + var obj = math_objects[n]; + handleChange(obj); + if (document.activeElement == obj.ta) { + if (obj.ta.value != '') { + obj.output_node.style.display = "block"; + MathJax.Hub.Queue(function () { + // position the element only when MathJax is done, because the output_node height might change + place(obj.ta, obj.output_node); + }); + } else { + obj.output_node.style.display = "none"; + } + } + }; + }; + var startChange = changeObjectN(ind_math); + if (ta.value != oldtxt) + startChange(); // process non-empty fields even though they are not visible yet + ta.addEventListener('change', startChange, false); + ta.addEventListener('keyup', startChange, false); + } else { + // only create a new parser and update the result if the options have changed + var same_constants; + var parser = math_objects[ind_math].parser; + if (!constants && parser.constants.length == 0) { + same_constants = true; + } else { + if (constants) { + same_constants = parser.constants.length == constants.length; + if (same_constants) { + for (var j=0; j and
(LON-CAPA ln and dlm tags) + */ +var updateMathSpanAndDiv = function() { + var nl = document.getElementsByClassName('math'); + // convert to an array because the nodelist would change as we are removing nodes from the document + var math_nodes = []; + for (var i = 0, ref = math_nodes.length = nl.length; i < ref; i++) { + math_nodes[i] = nl[i]; + } + for (var i=0; i