<commit_before>$(document).on('admin#map:loaded', function(){

    /* ------- CANVAS PROPERTIES ------- */
    var floor = $('body').data().floor;
    var library = $('body').data().library;
    $('.btn-floor[data-floor='+floor+']').addClass("currentButton");

    $('#library_option').val(library);
    $('#library_option').on('change', function() {
        window.location.replace(document.location.origin + "/admin/" + this.value + "/" + floor);
    });

    var boundingBox = new fabric.Rect({
        fill: "transparent",
        width: 6000,
        height: 4000,
        hasBorders: false,
        hasControls: false,
        lockMovementX: true,
        lockMovementY: true,
        evented: false,
        selectable: false,
        stroke: "#333"
    });

    canvas.add(boundingBox);

    canvas.svgViewportTransformation = false;
    canvas.renderOnAddRemove = false;

    canvas.setBackgroundColor('#fff', function () {
        canvas.renderAll();
    });

    overlay_url = "";
    switch(floor){
        case 0: overlay_url = "/assets/overlay_ml_0.png"; break;
        case 1: overlay_url = "/assets/overlay_ml_1.png"; break;
        case 2: overlay_url = "/assets/overlay_ml_2.png"; break;
        case 3: overlay_url = "/assets/overlay_ml_3.png"; break;
        case 4: overlay_url = "/assets/overlay_ml_4.png"; break;
        case 5: overlay_url = "/assets/overlay_ml_5.png"; break;
    }

    canvas.setBackgroundImage(overlay_url, canvas.renderAll.bind(canvas), {
        width: 6000,
        height: 4000,
        opacity: 0.3
    });

    wallCircles = [];

    var minZoom = canvas.width / boundingBox.width;
    canvas.viewport.zoom = minZoom;
    $("#zoomSlider").attr("min", minZoom);
    $("#zoomSlider").val(minZoom);
    $(".progress-bar").css("width", $("#zoomSlider").val());



    var gridSize = 20;

    var gridEnabled = true;
    var lines = [];
    for (var i = 0; i < (boundingBox.width / gridSize); i++) {
        lines.push(new fabric.Line([ i * gridSize, 0, i * gridSize, boundingBox.height], { stroke: '#ccc'})); // vertical lines
    }

    for (var i = 0; i < (boundingBox.height / gridSize); i++) {
        lines.push(new fabric.Line([ 0, i * gridSize, boundingBox.width, i * gridSize], { stroke: '#ccc'})); // horizontal lines
    }

    var grid = new fabric.Group(lines, {selectable: false, evented: false});

    canvas.add(grid);

    var printQueue = [];

    /* ------- CANVAS METHODS ------- */

    saveCanvas = function() {
        canvas.deactivateAll();
        removeWallCircles();

        var obj = canvas.getObjects().filter(function(o) {
            return o.modified == true
        } );

        console.log(JSON.stringify(obj));
        restoreWallCircles();

        return $.ajax({
            url : "/admin/" + library + "/"+floor,
            type : "post",
            data : { elements: JSON.stringify(obj) },
            success: function(data, textStatus, xhr) {
                var idCount = data.next_id;
                obj.forEach(function(o) {
                    o.modified = false;
                    if (!o.id) {
                        o.id = idCount;
                        idCount++;
                    }
                });
                $.notify({
                    message: 'The canvas has been saved successfully!',
                },{
                    type: 'success',
                    offset: 10
                });
            }
        });
    };

    saveElementData = function () {
        var obj = canvas.getActiveObject();

        obj.range_end_opt =  $(".range-form:visible .range_end_opt").val();
        obj.range_end_digits =  $(".range-form:visible .range_end_digits").val();
        obj.range_end_letters = $(".range-form:visible .range_end_letters").val();

        obj.range_start_opt =  $(".range-form:visible .range_start_opt").val();
        obj.range_start_digits =  $(".range-form:visible .range_start_digits").val();
        obj.range_start_letters = $(".range-form:visible .range_start_letters").val();

        obj.identifier = $("#identifier").val();

        console.log(obj)

        $.ajax({
            url : "/admin/save_element/" + library + "/" + floor,
            type : "post",
            data : { element: JSON.stringify(obj) },
            success: function(data) {

                if(!obj.id) {
                    obj.id = data.next_id;
                }

                $.notify({
                    message: 'Shelf attributes saved successfully!'
                },{
                    type: 'success',
                    offset: 10
                });
            },
            error: function(xhr) {

                var errors = $.parseJSON(xhr.responseText).errors;
                $.notify({
                    message: errors
                },{
                    type: 'danger',
                    offset: 10
                });
            }
        });
    };


    removeSelected = function() {
        var activeObject = canvas.getActiveObject(),
            activeGroup = canvas.getActiveGroup();

        if ((activeObject || activeGroup) && confirm('Are you sure?')) {
            var toRemove = [];

            if (activeGroup) {
                toRemove = activeGroup.getObjects();
                canvas.discardActiveGroup();
            } else if (activeObject) {
                toRemove.push(activeObject);
            }

            toRemove.forEach(function(object) {
                if (object.type != "circle") {
                    canvas.remove(object);
                } else {
                    // If wall
                    canvas.remove(object.line1);
                    canvas.remove(object.line2);
                    for (var i=0; i < wallCircles.length; i++) {
                        if (object.line1) {
                            if (wallCircles[i].line1 == object.line1 || wallCircles[i].line2 == object.line1) {
                                canvas.remove(wallCircles[i]);
                            }
                        } else if (object.line2) {
                            if (wallCircles[i].line1 == object.line2 || wallCircles[i].line2 == object.line2) {
                                canvas.remove(wallCircles[i]);
                            }
                        }
                    }
                }
            });

        }
    };

    rasterizeSVG = function() {
        var progress = $.notify({
            message: 'Publication in progress...'
        },{
            type: 'warning',
            allow_dismiss: false,
            delay: 20000,
            offset: 10
        });
        var w = canvas.width;
        var h = canvas.height;
        var z = canvas.viewport.zoom;
        var x = canvas.viewport.position.x;
        var y = canvas.viewport.position.y;


        canvas.setZoom(1);

        canvas.setWidth(6003);
        canvas.setHeight(4003);

        canvas.viewport.position.x = 0;
        canvas.viewport.position.y = 0;

        canvas.renderAll();

        canvas.remove(grid);
        removeWallCircles();
        canvas.setBackgroundImage(null, canvas.renderAll.bind(canvas));
        canvas.renderAll();


        var data = canvas.toSVG();
        var data2 = canvas.toDataURL().replace(/^data:image\/(png|jpg);base64,/, "");

        $.ajax({
            url : "/admin/save_svg/" + library + "/"+floor,
            type : "post",
            data : {
                svg_data: data,
                png_data: data2
            },
            success: function() {
                progress.update('type', 'success');
                progress.update('message', 'The canvas has been published successfully! <br> Check the live version by clicking <a href="/QA1234">here</a>.');

                setTimeout(function() {
                    progress.close();
                }, 5000);<commit_msg>Update progress bar text on publication<commit_after>$(document).on('admin#map:loaded', function(){

    /* ------- CANVAS PROPERTIES ------- */
    var floor = $('body').data().floor;
    var library = $('body').data().library;
    $('.btn-floor[data-floor='+floor+']').addClass("currentButton");

    $('#library_option').val(library);
    $('#library_option').on('change', function() {
        window.location.replace(document.location.origin + "/admin/" + this.value + "/" + floor);
    });

    var boundingBox = new fabric.Rect({
        fill: "transparent",
        width: 6000,
        height: 4000,
        hasBorders: false,
        hasControls: false,
        lockMovementX: true,
        lockMovementY: true,
        evented: false,
        selectable: false,
        stroke: "#333"
    });

    canvas.add(boundingBox);

    canvas.svgViewportTransformation = false;
    canvas.renderOnAddRemove = false;

    canvas.setBackgroundColor('#fff', function () {
        canvas.renderAll();
    });

    overlay_url = "";
    switch(floor){
        case 0: overlay_url = "/assets/overlay_ml_0.png"; break;
        case 1: overlay_url = "/assets/overlay_ml_1.png"; break;
        case 2: overlay_url = "/assets/overlay_ml_2.png"; break;
        case 3: overlay_url = "/assets/overlay_ml_3.png"; break;
        case 4: overlay_url = "/assets/overlay_ml_4.png"; break;
        case 5: overlay_url = "/assets/overlay_ml_5.png"; break;
    }

    canvas.setBackgroundImage(overlay_url, canvas.renderAll.bind(canvas), {
        width: 6000,
        height: 4000,
        opacity: 0.3
    });

    wallCircles = [];

    var minZoom = canvas.width / boundingBox.width;
    canvas.viewport.zoom = minZoom;
    $("#zoomSlider").attr("min", minZoom);
    $("#zoomSlider").val(minZoom);
    $(".progress-bar").css("width", $("#zoomSlider").val());



    var gridSize = 20;

    var gridEnabled = true;
    var lines = [];
    for (var i = 0; i < (boundingBox.width / gridSize); i++) {
        lines.push(new fabric.Line([ i * gridSize, 0, i * gridSize, boundingBox.height], { stroke: '#ccc'})); // vertical lines
    }

    for (var i = 0; i < (boundingBox.height / gridSize); i++) {
        lines.push(new fabric.Line([ 0, i * gridSize, boundingBox.width, i * gridSize], { stroke: '#ccc'})); // horizontal lines
    }

    var grid = new fabric.Group(lines, {selectable: false, evented: false});

    canvas.add(grid);

    var printQueue = [];

    /* ------- CANVAS METHODS ------- */

    saveCanvas = function() {
        canvas.deactivateAll();
        removeWallCircles();

        var obj = canvas.getObjects().filter(function(o) {
            return o.modified == true
        } );

        console.log(JSON.stringify(obj));
        restoreWallCircles();

        return $.ajax({
            url : "/admin/" + library + "/"+floor,
            type : "post",
            data : { elements: JSON.stringify(obj) },
            success: function(data, textStatus, xhr) {
                var idCount = data.next_id;
                obj.forEach(function(o) {
                    o.modified = false;
                    if (!o.id) {
                        o.id = idCount;
                        idCount++;
                    }
                });
                $.notify({
                    message: 'The canvas has been saved successfully!',
                },{
                    type: 'success',
                    offset: 10
                });
            }
        });
    };

    saveElementData = function () {
        var obj = canvas.getActiveObject();

        obj.range_end_opt =  $(".range-form:visible .range_end_opt").val();
        obj.range_end_digits =  $(".range-form:visible .range_end_digits").val();
        obj.range_end_letters = $(".range-form:visible .range_end_letters").val();

        obj.range_start_opt =  $(".range-form:visible .range_start_opt").val();
        obj.range_start_digits =  $(".range-form:visible .range_start_digits").val();
        obj.range_start_letters = $(".range-form:visible .range_start_letters").val();

        obj.identifier = $("#identifier").val();

        console.log(obj)

        $.ajax({
            url : "/admin/save_element/" + library + "/" + floor,
            type : "post",
            data : { element: JSON.stringify(obj) },
            success: function(data) {

                if(!obj.id) {
                    obj.id = data.next_id;
                }

                $.notify({
                    message: 'Shelf attributes saved successfully!'
                },{
                    type: 'success',
                    offset: 10
                });
            },
            error: function(xhr) {

                var errors = $.parseJSON(xhr.responseText).errors;
                $.notify({
                    message: errors
                },{
                    type: 'danger',
                    offset: 10
                });
            }
        });
    };


    removeSelected = function() {
        var activeObject = canvas.getActiveObject(),
            activeGroup = canvas.getActiveGroup();

        if ((activeObject || activeGroup) && confirm('Are you sure?')) {
            var toRemove = [];

            if (activeGroup) {
                toRemove = activeGroup.getObjects();
                canvas.discardActiveGroup();
            } else if (activeObject) {
                toRemove.push(activeObject);
            }

            toRemove.forEach(function(object) {
                if (object.type != "circle") {
                    canvas.remove(object);
                } else {
                    // If wall
                    canvas.remove(object.line1);
                    canvas.remove(object.line2);
                    for (var i=0; i < wallCircles.length; i++) {
                        if (object.line1) {
                            if (wallCircles[i].line1 == object.line1 || wallCircles[i].line2 == object.line1) {
                                canvas.remove(wallCircles[i]);
                            }
                        } else if (object.line2) {
                            if (wallCircles[i].line1 == object.line2 || wallCircles[i].line2 == object.line2) {
                                canvas.remove(wallCircles[i]);
                            }
                        }
                    }
                }
            });

        }
    };

    rasterizeSVG = function() {
        var progress = $.notify({
            message: 'Publication in progress...'
        },{
            type: 'warning',
            allow_dismiss: false,
            delay: 20000,
            offset: 10
        });
        var w = canvas.width;
        var h = canvas.height;
        var z = canvas.viewport.zoom;
        var x = canvas.viewport.position.x;
        var y = canvas.viewport.position.y;


        canvas.setZoom(1);

        canvas.setWidth(6003);
        canvas.setHeight(4003);

        canvas.viewport.position.x = 0;
        canvas.viewport.position.y = 0;

        canvas.renderAll();

        canvas.remove(grid);
        removeWallCircles();
        canvas.setBackgroundImage(null, canvas.renderAll.bind(canvas));
        canvas.renderAll();


        var data = canvas.toSVG();
        var data2 = canvas.toDataURL().replace(/^data:image\/(png|jpg);base64,/, "");

        $.ajax({
            url : "/admin/save_svg/" + library + "/"+floor,
            type : "post",
            data : {
                svg_data: data,
                png_data: data2
            },
            success: function() {
                progress.update('type', 'success');
                progress.update('message', 'The canvas has been published successfully! <br> Check the live version by clicking <a href="/?floor='+floor+'&library='+library+'" target="_blank">here</a>.');

                setTimeout(function() {
                    progress.close();
                }, 5000);
{'fraction_non_alphanumeric': 0.14447825535691644, 'fraction_numerical': 0.010904019272220108, 'mean_word_length': 1.5447652847233424, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3493694', 'n_tokens_mistral': 4576, 'n_tokens_neox': 4216, 'n_words': 843}