<commit_before>Template.buildingsGrid.helpers({
    bldgKey: function() {
        var bldgAddr = getBldg(Session.get("currentAddress"));
        var bldg = Buildings.findOne({address: bldgAddr});
        if (bldg) {
            return bldg.key;
        }
        else {
            return Session.get("currentAddress");
        }
    }
});

function openBldgURL(externalUrl) {
    if (externalUrl) {
        var target = '_top';
        if (externalUrl.length > 4 && externalUrl.substr(0, 4) == "http")
            target = '_blank';
        window.open(externalUrl, target);
    }
}
Template.buildingsGrid.events({
    "mousedown .bldg": function(event) {
        var externalUrl = $(event.currentTarget).attr("href");
        openBldgURL(externalUrl);
    },
    "click .navigate-up": function() {
        var currentAddress = Session.get("currentAddress");
        var parts = currentAddress.split("-");
        parts.pop();
        // if it's a flr, get out to the containing bldg
        if (parts[parts.length - 1][0] == "l") {
            parts.pop();
        }
        var newAddress = parts.join("-");
        window.open("/buildings/" + newAddress, "_top");
    }
});

Template.buildingsGrid.rendered = function () {
    var WIDTH = 1200,
        HEIGHT = 600,
        BOUNDING_WIDTH = 1220,
        BOUNDING_HEIGHT = 620,
        SQUARE_WIDTH = 10,
        SQUARE_HEIGHT = 10;


    var self = this;
    var dom = {};

    var zoom = function() {
        dom.svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
    };
    var zoomBehavior = d3.behavior.zoom().scaleExtent([0.8, 60]).on("zoom", zoom);

    dom.svg = d3.select("#display").append("svg")
        .attr("width", BOUNDING_WIDTH)
        .attr("height", BOUNDING_HEIGHT)
        .append("g")
        .call(zoomBehavior)
        .append("g");

    dom.svg.append("rect")
        .attr("class", "overlay")
        .attr("width", BOUNDING_WIDTH)
        .attr("height", BOUNDING_HEIGHT)
        .attr("fill", "lightblue");


    var xScale = d3.scale.linear().domain([0, FLOOR_W * SQUARE_WIDTH]).range([0, WIDTH]);
    var yScale = d3.scale.linear().domain([0, FLOOR_H * SQUARE_HEIGHT]).range([0, HEIGHT]);

    if (!self.handle) {
        self.handle = Meteor.autorun(function () {
            dom.bldgs = dom.svg.selectAll('.bldg')
                .data(Buildings.find().fetch())
                .enter()
                .append("g")
                .attr("class", "bldg")
                .attr("xlink:href", function(d) {
                    if (d.payload.external_url) {
                        return d.payload.external_url;
                    }
                    else {
                        // if no external link, link to the 1st flr of the blsg
                        return "/buildings/" + d.address + "-l0";
                    }
                });

            dom.bldgs
                .append('rect')
                .attr({
                    x: function (d) {
                        return xScale(d.x * SQUARE_WIDTH)
                    },
                    y: function (d) {
                        return yScale(d.y * SQUARE_WIDTH)
                    },
                    width: xScale(SQUARE_WIDTH),
                    height: yScale(SQUARE_WIDTH),
                    stroke: 'grey',
                    "stroke-width": 0.05,
                    fill: function (d) {
                        if (d.contentType)
                            return "white";
                    }
                });

            dom.bldgs
                .append("foreignObject")
                .attr({
                    width: xScale(SQUARE_WIDTH),
                    height: yScale(SQUARE_WIDTH),
                    x: function (d) {
                        return xScale(d.x * SQUARE_WIDTH)
                    },
                    y: function (d) {
                        return yScale(d.y * SQUARE_WIDTH)
                    },
                    fill: 'none'
                })
                .append("xhtml:body").append("xhtml:p")
                .style({
                    "color": function (d) {
                        // different color for posts with links
                        // TODO this ain't generic
                        if (d.payload.urls && d.payload.urls.length)
                            return "blue";
                        else
                            return "navy";
                    },
                    "font-size": "0.6px"
                })
                .html(function (d) {
                    if (d.contentType == 'twitter-social-post') {
                        return d.payload.text;
                    }
                    else if (d.contentType == 'daily-feed') {
                        return d.key;
                    }
                    else if (d.contentType == 'user') {
                        return d.payload.screenName;
                    }
                });

            if (Session.get("currentAddress")) {
                var addr = Session.get("currentAddress");
                var bldgAddr = getBldg(addr);
                if (bldgAddr == addr) {
                    // given a bldg address, so zoom on it
                    var parts = bldgAddr.split("-");

                    var part = parts[parts.length - 1];
                    var coords = part.substring(2, part.length - 1);
                    var x_y = coords.split(",");
                    var x = xScale(x_y[0] * SQUARE_WIDTH),
                        y = yScale(x_y[1] * SQUARE_WIDTH);
                    var X_OFFSET = 10,
                        Y_OFFSET = 5;
                    if (x >  xScale(X_OFFSET)) x -= xScale(X_OFFSET);
                    if (y >  yScale(Y_OFFSET)) y -=  yScale(Y_OFFSET);
                    var scale = 60;
                    dom.svg.attr("transform", "translate(-" + (x*scale) + ',-' + (y*scale) + ")scale(" + scale + ")");
                }
            }
        });
    }

};<commit_msg>proper zoom on current bldg<commit_after>Template.buildingsGrid.helpers({
    bldgKey: function() {
        var bldgAddr = getBldg(Session.get("currentAddress"));
        var bldg = Buildings.findOne({address: bldgAddr});
        if (bldg) {
            return bldg.key;
        }
        else {
            return Session.get("currentAddress");
        }
    }
});

function openBldgURL(externalUrl) {
    if (externalUrl) {
        var target = '_top';
        if (externalUrl.length > 4 && externalUrl.substr(0, 4) == "http")
            target = '_blank';
        window.open(externalUrl, target);
    }
}
Template.buildingsGrid.events({
    "mousedown .bldg": function(event) {
        var externalUrl = $(event.currentTarget).attr("href");
        openBldgURL(externalUrl);
    },
    "click .navigate-up": function() {
        var currentAddress = Session.get("currentAddress");
        var parts = currentAddress.split("-");
        parts.pop();
        // if it's a flr, get out to the containing bldg
        if (parts[parts.length - 1][0] == "l") {
            parts.pop();
        }
        var newAddress = parts.join("-");
        window.open("/buildings/" + newAddress, "_top");
    }
});

Template.buildingsGrid.rendered = function () {
    var WIDTH = 1200,
        HEIGHT = 600,
        BOUNDING_WIDTH = 1220,
        BOUNDING_HEIGHT = 620,
        SQUARE_WIDTH = 10,
        SQUARE_HEIGHT = 10;


    var self = this;
    var dom = {};

    var zoom = function() {
        dom.svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
    };
    var zoomBehavior = d3.behavior.zoom().scaleExtent([0.8, 60]).on("zoom", zoom);

    dom.svg = d3.select("#display").append("svg")
        .attr("width", BOUNDING_WIDTH)
        .attr("height", BOUNDING_HEIGHT)
        .append("g")
        .call(zoomBehavior)
        .append("g");

    dom.svg.append("rect")
        .attr("class", "overlay")
        .attr("width", BOUNDING_WIDTH)
        .attr("height", BOUNDING_HEIGHT)
        .attr("fill", "lightblue");


    var xScale = d3.scale.linear().domain([0, FLOOR_W * SQUARE_WIDTH]).range([0, WIDTH]);
    var yScale = d3.scale.linear().domain([0, FLOOR_H * SQUARE_HEIGHT]).range([0, HEIGHT]);

    if (!self.handle) {
        self.handle = Meteor.autorun(function () {
            dom.bldgs = dom.svg.selectAll('.bldg')
                .data(Buildings.find().fetch())
                .enter()
                .append("g")
                .attr("class", "bldg")
                .attr("xlink:href", function(d) {
                    if (d.payload.external_url) {
                        return d.payload.external_url;
                    }
                    else {
                        // if no external link, link to the 1st flr of the blsg
                        return "/buildings/" + d.address + "-l0";
                    }
                });

            dom.bldgs
                .append('rect')
                .attr({
                    x: function (d) {
                        return xScale(d.x * SQUARE_WIDTH)
                    },
                    y: function (d) {
                        return yScale(d.y * SQUARE_WIDTH)
                    },
                    width: xScale(SQUARE_WIDTH),
                    height: yScale(SQUARE_WIDTH),
                    stroke: 'grey',
                    "stroke-width": 0.05,
                    fill: function (d) {
                        if (d.contentType)
                            return "white";
                    }
                });

            dom.bldgs
                .append("foreignObject")
                .attr({
                    width: xScale(SQUARE_WIDTH),
                    height: yScale(SQUARE_WIDTH),
                    x: function (d) {
                        return xScale(d.x * SQUARE_WIDTH)
                    },
                    y: function (d) {
                        return yScale(d.y * SQUARE_WIDTH)
                    },
                    fill: 'none'
                })
                .append("xhtml:body").append("xhtml:p")
                .style({
                    "color": function (d) {
                        // different color for posts with links
                        // TODO this ain't generic
                        if (d.payload.urls && d.payload.urls.length)
                            return "blue";
                        else
                            return "navy";
                    },
                    "font-size": "0.6px"
                })
                .html(function (d) {
                    if (d.contentType == 'twitter-social-post') {
                        return d.payload.text;
                    }
                    else if (d.contentType == 'daily-feed') {
                        return d.key;
                    }
                    else if (d.contentType == 'user') {
                        return d.payload.screenName;
                    }
                });

            // zoom on the current bldg
            if (Session.get("currentAddress")) {
                var addr = Session.get("currentAddress");
                var bldgAddr = getBldg(addr);
                if (bldgAddr == addr) {
                    // given a bldg address, so zoom on it
                    var parts = bldgAddr.split("-");

                    var part = parts[parts.length - 1];
                    var coords = part.substring(2, part.length - 1);
                    var x_y = coords.split(",");
                    var x = xScale(x_y[0] * SQUARE_WIDTH),
                        y = yScale(x_y[1] * SQUARE_WIDTH);
                    var X_OFFSET = 10,
                        Y_OFFSET = 5;
                    if (x >  xScale(X_OFFSET)) x -= xScale(X_OFFSET);
                    if (y >  yScale(Y_OFFSET)) y -=  yScale(Y_OFFSET);
                    var scale = 60;
                    zoomBehavior.scale(scale);
                    zoomBehavior.translate([-(x*scale), -(y*scale)]);
                    // set the zoomBehavior x & y
                    dom.svg.attr("transform", "translate(-" + (x*scale) + ',-' + (y*scale) + ")scale(" + scale + ")");
                }
            }
        });
    }

};
{'fraction_non_alphanumeric': 0.13142623620760113, 'fraction_numerical': 0.008827135267674703, 'mean_word_length': 1.1776116746752092, 'pattern_counts': {'":': 10, '<': 3, '<?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': '408474', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3327, 'n_words': 677}