<commit_before>// diamond.js is a file associated with the factor-trinomial 
// browser application. It role is to set up a diamond, which
// aids in the factoring of a trinomial. 
//
// copyright (c) 2013 By <NAME>
//
// ================================================================================================
// 
// DOM elements with id = ft-diamond-text-<number> 
//     these are the DOM ids that correspond to each LaTeX expression within the diamond
// expression
//     used to set up the DOM span elements inside diamond, which will hold the rendered LaTeX
// inputBox
//     used for the DOM input element when you click on part of the diamond
// coefficients
//     object containing the values a,b,c in polynomial ax^2 + bx + c
//
// ================================================================================================


Diamond = (function() {

        var parameters = [1, -17, 30]; // these are the a,b,c that go in ax^2 + bx + c -- set by initialize
        var diamondNumber = {};
        var diamondInputs = [];
        var View_MAX = 350;   // set the max pixels for the diamond's box container

        // jQuery selectors
        var diamondBox1 = ".ft-diamondBox-1"
        var diamondBox2 = ".ft-diamondBox-2"
        var diamondBox3 = ".ft-diamondBox-3"
        var diamondBox4 = ".ft-diamondBox-4"

        var diamondBoxes = [diamondBox1, diamondBox2, diamondBox3, diamondBox4];

        function clean_parameters() {  // for presentational purposes only
            var coefficients =
                   {
                    a:parameters[0],
                    b:parameters[1],
                    c:parameters[2]
                   }
            return (function() {
                
                for (var c in coefficients) {
                    switch (coefficients[c]) {
                        case 1:
                            coefficients[c] = "";
                            break;
                        case -1:
                            coefficients[c] = "- ";
                            break;
                        default:
                            if (coefficients[c] > 0) {
                                coefficients[c] = "+ "+coefficients[c];
                            }
                            else {
                                coefficients[c] = "- "+-1*coefficients[c];
                            }
                    }
                }
                return coefficients;
            }());
        };

        // TODO: these event bindings need to be implemented somewhere else... also, modularize this code... at least follow
        // a functional approach. This is ridiculous, think modular ... like oreos.
        //
        // Watch for diamond clicks, this is the hook for the mouse click event on the diamond
        // 
        function events() {
            $(diamondBox1 + ','
              + diamondBox2 + ','
              + diamondBox3 + ','
              + diamondBox4)
                .click(function(e) {
                    createInputBox(e);
                });
        };
        
        // This is the function that will create the input for LaTeX when you click on the diamond.
        // * should only be responsible for creating the input box and printing the MathJax
        function createInputBox(e) {
            $(this.inputBox).remove();
            var svgTarget = e.currentTarget;
            diamondNumber = svgTarget.className.baseVal.replace("ft-diamondBox-", "");
            var coords = [e.clientX, e.clientY];

            $("#ft-diamond-text-"+diamondNumber).remove()

            this.inputBox = document.createElement("input");
            this.expression = document.createElement("span");

            this.inputBox.setAttribute("style",
                                "position:absolute;" +
                                "top:"+coords[1] +"px;"+
                                "left:"+coords[0]+"px;"+
                                "height:50px;" +
                                "width:100px;"+
                                "font-size:22px;"
            );
            this.inputBox.setAttribute("class", "ft-d-input-box")
            $("body").append(this.inputBox);
            this.inputBox.focus();
            // binding a keyup event to the input box
            // use the event to figure out what diamond you clicked and run through a switch
            // CONTINUE ON ENTER (KEY VAL 13)
             $(".ft-d-input-box").keydown(function(e) {
                if(e.which == 13) {
                    setInputBox(e);  
                }
            });
             
        };

        function setInputBox(e) {
                var text = $(this.inputBox).val();
                // Get rid of all the spaces in the user's input text
                if (text.indexOf(" ") != -1) {
                    while (text.indexOf(" ") != -1) {
                        text = text.replace(" ", "");
                    }
                }
                // textCoords is where the text will go
                var textCoords=[null,null];

                var svg_container_offset = $(".ft-svg-container").offset();
                
                var localLeft = svg_container_offset.left;
                var localTop = svg_container_offset.top;

                switch (parseInt(diamondNumber)) {
                        // Don't ask about these ratios... they're for positioning         
                    case 1:
                        textCoords = [17.0/35.0*View_MAX, 25.0/35.0*View_MAX];
                        break;
                    case 2:
                        textCoords = [8.0/35.0*View_MAX, 17.0/35.0*View_MAX];
                        break;
                    case 3:
                        textCoords = [17.0/35.0*View_MAX, 71.0/350.0*View_MAX]
                        break;
                    case 4:
                        textCoords = [26.0/35.0*View_MAX, 17.0/35.0*View_MAX];
                        break;
                    default:
                        throw "something's wrong with the diamond number."
                }
                this.expression.setAttribute("id", "ft-diamond-text-"+diamondNumber);
                this.expression.setAttribute("style",
                                           "position:absolute;"+ 
                                           "top:"+(textCoords[1])+"px;"+
                                           "left:"+(textCoords[0])+"px;"
                );
                // now write the LaTeX string to the expression and append to body
                this.expression.textContent = "\\(\\Large "+text+"\\)"
                $(".ft-diamond").append(this.expression);
                $(this.inputBox).remove();
                // store what the user just entered in diamondInputs array
                // store text in a 1-1 fashion matching the diamondNumber
                diamondInputs[parseInt(diamondNumber)-1] = text;
                
                // ============
                // Check Values
                // ============

                diamondInputs = formatDiamondInput(diamondInputs);
                checkDiamondInputs(diamondInputs);
                // Lastly, render the LaTeX via MathJax...yes!
                MathJax.Hub.Queue(["Typeset", MathJax.Hub, String("ft-diamond-text-"+diamondNumber)]);
        };
        
        function formatDiamondInput(data) {
            for (var i=0; i<4; i++) {
                if (typeof(data[i]) == "undefined") {
                    continue;
                }
                // Handle the case if the coefficient is an implied 1 or an implied -1.
                if (isNaN(parseInt(data[i]))) {
                    switch (data[i][0]) {
                        case "x":
                            return "-1"+data[i];
                            break;
                        case "-":
                            return data[i].replace("-","-1");
                            break;
                        default:
                            Messenger.send("ft-guide", "There's something wrong with what you entered. "
                                    + "I was expecting the expression to start with x or -x");
                    }
                }
            }<commit_msg>using regex to test for exponents, rendering exponents with native html rather than MathJax<commit_after>// diamond.js is a file associated with the factor-trinomial 
// browser application. It role is to set up a diamond, which
// aids in the factoring of a trinomial. 
//
// copyright (c) 2013 By <NAME>
//
// ================================================================================================
// 
// DOM elements with id = ft-diamond-text-<number> 
//     these are the DOM ids that correspond to each LaTeX expression within the diamond
// expression
//     used to set up the DOM span elements inside diamond, which will hold the rendered LaTeX
// inputBox
//     used for the DOM input element when you click on part of the diamond
// coefficients
//     object containing the values a,b,c in polynomial ax^2 + bx + c
//
// ================================================================================================


Diamond = (function() {

        var parameters = [1, -17, 30]; // these are the a,b,c that go in ax^2 + bx + c -- set by initialize
        var diamondNumber = {};
        var diamondInputs = [];
        var View_MAX = 350;   // set the max pixels for the diamond's box container

        // jQuery selectors
        var diamondBox1 = ".ft-diamondBox-1"
        var diamondBox2 = ".ft-diamondBox-2"
        var diamondBox3 = ".ft-diamondBox-3"
        var diamondBox4 = ".ft-diamondBox-4"

        var diamondBoxes = [diamondBox1, diamondBox2, diamondBox3, diamondBox4];

        function clean_parameters() {  // for presentational purposes only
            var coefficients =
                   {
                    a:parameters[0],
                    b:parameters[1],
                    c:parameters[2]
                   }
            return (function() {
                
                for (var c in coefficients) {
                    switch (coefficients[c]) {
                        case 1:
                            coefficients[c] = "";
                            break;
                        case -1:
                            coefficients[c] = "- ";
                            break;
                        default:
                            if (coefficients[c] > 0) {
                                coefficients[c] = "+ "+coefficients[c];
                            }
                            else {
                                coefficients[c] = "- "+-1*coefficients[c];
                            }
                    }
                }
                return coefficients;
            }());
        };

        // TODO: these event bindings need to be implemented somewhere else... also, modularize this code... at least follow
        // a functional approach. This is ridiculous, think modular ... like oreos.
        //
        // Watch for diamond clicks, this is the hook for the mouse click event on the diamond
        // 
        function events() {
            $(diamondBox1 + ','
              + diamondBox2 + ','
              + diamondBox3 + ','
              + diamondBox4)
                .click(function(e) {
                    createInputBox(e);
                });
        };
        
        // This is the function that will create the input for LaTeX when you click on the diamond.
        // * should only be responsible for creating the input box and printing the MathJax
        function createInputBox(e) {
            $(this.inputBox).remove();
            var svgTarget = e.currentTarget;
            diamondNumber = svgTarget.className.baseVal.replace("ft-diamondBox-", "");
            var coords = [e.clientX, e.clientY];

            $("#ft-diamond-text-"+diamondNumber).remove()

            this.inputBox = document.createElement("input");
            this.expression = document.createElement("span");

            this.inputBox.setAttribute("style",
                                "position:absolute;" +
                                "top:"+coords[1] +"px;"+
                                "left:"+coords[0]+"px;"+
                                "height:50px;" +
                                "width:100px;"+
                                "font-size:22px;"
            );
            this.inputBox.setAttribute("class", "ft-d-input-box")
            $("body").append(this.inputBox);
            this.inputBox.focus();
            // binding a keyup event to the input box
            // use the event to figure out what diamond you clicked and run through a switch
            // CONTINUE ON ENTER (KEY VAL 13)
             $(".ft-d-input-box").keydown(function(e) {
                if(e.which == 13) {
                    setInputBox(e);  
                }
            });
             
        };

        function setInputBox(e) {
                var text = $(this.inputBox).val();
                // Get rid of all the spaces in the user's input text
                if (text.indexOf(" ") != -1) {
                    while (text.indexOf(" ") != -1) {
                        text = text.replace(" ", "");
                    }
                }
                // textCoords is where the text will go
                var textCoords=[null,null];

                var svg_container_offset = $(".ft-svg-container").offset();
                
                var localLeft = svg_container_offset.left;
                var localTop = svg_container_offset.top;

                switch (parseInt(diamondNumber)) {
                        // Don't ask about these ratios... they're for positioning         
                    case 1:
                        textCoords = [17.0/35.0*View_MAX, 25.0/35.0*View_MAX];
                        break;
                    case 2:
                        textCoords = [8.0/35.0*View_MAX, 17.0/35.0*View_MAX];
                        break;
                    case 3:
                        textCoords = [17.0/35.0*View_MAX, 71.0/350.0*View_MAX]
                        break;
                    case 4:
                        textCoords = [26.0/35.0*View_MAX, 17.0/35.0*View_MAX];
                        break;
                    default:
                        throw "something's wrong with the diamond number."
                }
                this.expression.setAttribute("id", "ft-diamond-text-"+diamondNumber);
                this.expression.setAttribute("style",
                                           "position:absolute;"+ 
                                           "top:"+(textCoords[1])+"px;"+
                                           "left:"+(textCoords[0])+"px;"
                );
                // now write the LaTeX string to the expression and append to body
 //               this.expression.textContent = "\\(\\Large "+text+"\\)";
                $(this.expression).html(renderMath(text));

                $(".ft-diamond").append(this.expression);
                $(this.inputBox).remove();
                // store what the user just entered in diamondInputs array
                // store text in a 1-1 fashion matching the diamondNumber
                diamondInputs[parseInt(diamondNumber)-1] = text;
                
                // ============
                // Check Values
                // ============

                diamondInputs = formatDiamondInput(diamondInputs);
                checkDiamondInputs(diamondInputs);
                
                
                // instead of relying on MathJax to resolve x^2... just do a regex to match
                // that style of expression and use native html x<sup> whatever </sup>
                function renderMath(diamondText) {
                    if (diamondText && diamondText.match) {
                        if (diamondText.match(/\^/)) {
                            var pre = diamondText.match(/(.*)\^/)[1];
                            var exponent = diamondText.match(/\^(.*)/)[1];
                            return pre + "<sup>" + exponent + "</sup>";
                        }
                        else {
                            return diamondText;
                        }
                    }
                    else {
                        throw new Error("The text is supposed to have a regex match method.");
                    }
                }
        };
        
        function formatDiamondInput(data) {
            for (var i=0; i<4; i++) {
                if (typeof(data[i]) == "undefined") {
                    continue;
                }
                // Handle the case if the coefficient is an implied 1 or an implied -1.
                if (isNaN(parseInt(data[i]))) {
                    switch (data[i][0]) {
                        case "x":
                            return "-1"+data[i];
                            break;
                        case "-":
                            return data[i].replace("-","-1");
                            break;
                        default:
                            Messenger.send("ft-guide", "There's something wrong with what you entered. "
                                    + "I was expecting the expression to start with x or -x");
                    }
                }
            }
{'fraction_non_alphanumeric': 0.1267186067827681, 'fraction_numerical': 0.013462419798350138, 'mean_word_length': 1.1527931927487975, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6756939', 'n_tokens_mistral': 4425, 'n_tokens_neox': 3985, 'n_words': 1126}