<commit_before>'use strict';

/**
 * @ngdoc directive
 * @name otaniemi3dApp.directive:tooltip
 * @description
 * # tooltip
 */
angular.module('otaniemi3dApp')
  .directive('tooltip', function () {
    return {
      restrict: 'E',
      template: [
        '<table id="infocontent" class="tooltip-table">',
          '<tr>',
            '<th colspan="2" style="text-align:center">{{tooltip.room}}</th>',
          '</tr>',
          '<tr ng-repeat="sensor in tooltip.sensors"',
              'ng-style="{\'background-color\': sensor.color}">',
            '<th>{{sensor.name}}</th>',
            '<td>{{sensor.values[0].value}} {{sensor.suffix}}</td>',
          '</tr>',
          '<tr>',
            '<td colspan="2">',
              '<i>Click to lock the tooltip</i>',
            '</td>',
          '</tr>',
        '</table>'
      ].join(''),
      scope: {
        sensorData: '='
      },
      controller: function () {
        this.sensors = [];
        this.room = '';
      },
      controllerAs: 'tooltip',
      bindToController: true,
      link: function postLink(scope, element, attrs, tooltipCtrl) {

        function showTooltip(d) {
          d3.select(element[0]).style('display', null);

          if (!d) {
            return;
          }

          scope.$apply(function () {
            tooltipCtrl.sensors = [];
            tooltipCtrl.room = '';

            if (d.sensors.length) {
              for (var i = 0; i < d.sensors.length; i++) {
                tooltipCtrl.sensors.push(d.sensors[i]);
              }
            }

            if (d.room) {
              tooltipCtrl.room = d.room;
            }

          });
        }

        function moveTooltip() {
          if (d3.event.pageY > window.innerHeight /2) {
            d3.select(element[0]).style('bottom',
                (window.innerHeight - d3.event.pageY) + 'px')
              .style('top', 'auto');
          }
          else {
            d3.select(element[0]).style('top',
                (d3.event.pageY - 10) + 'px')
              .style('bottom', 'auto');
          }

          d3.select(element[0]).style('left',
              (d3.event.pageX) + 'px')
            .style('right', 'auto');
        }

        function hideTooltip() {
          d3.select(element[0]).style('display', 'none');
        }

        function lockTooltip() {
          // body...
        }

        d3.select(element[0]).style('display', 'none');

        d3.select(element.parent()[0])
          .selectAll('[data-room-id]')
            .on('mouseover', showTooltip)
            .on('mousemove', moveTooltip)
            .on('mouseout', hideTooltip);

        scope.$on('$destroy', function () {
          d3.select(element.parent()[0])
            .selectAll('[data-room-id]')
              .on('.mouseover', null)
              .on('.mousemove', null)
              .on('.mouseout', null);
        });

      }
    };
  });<commit_msg>Tooltip can now be locked in place<commit_after>'use strict';

/**
 * @ngdoc directive
 * @name otaniemi3dApp.directive:tooltip
 * @description
 * # tooltip
 */
angular.module('otaniemi3dApp')
  .directive('tooltip', function () {
    return {
      restrict: 'E',
      template: [
        '<table id="infocontent" class="tooltip-table">',
          '<tr>',
            '<th colspan="2" style="text-align:center">{{tooltip.room}}</th>',
          '</tr>',
          '<tr ng-repeat="sensor in tooltip.sensors"',
              'ng-style="{\'background-color\': sensor.color}">',
            '<th>{{sensor.name}}</th>',
            '<td>{{sensor.values[0].value}} {{sensor.suffix}}</td>',
          '</tr>',
          '<tr>',
            '<td colspan="2">',
              '<i>{{tooltip.caption}}</i>',
            '</td>',
          '</tr>',
        '</table>'
      ].join(''),
      scope: {
        sensorData: '='
      },
      controller: function () {
        this.sensors = [];
        this.room = '';
        this.caption = 'Downloading sensor data...';
        this.isLocked = false;
      },
      controllerAs: 'tooltip',
      bindToController: true,
      link: function postLink(scope, element, attrs, tooltipCtrl) {

        function showTooltip() {
          d3.select(element[0]).style('display', null);
        }

        function moveTooltip(d) {

          if (tooltipCtrl.isLocked) {
            return;
          }

          showTooltip();

          if (d) {
            tooltipCtrl.caption = 'Click to lock the tooltip';

            scope.$apply(function () {
              tooltipCtrl.sensors = [];
              tooltipCtrl.room = '';

              if (d.sensors.length) {
                for (var i = 0; i < d.sensors.length; i++) {
                  tooltipCtrl.sensors.push(d.sensors[i]);
                }
              }

              if (d.room) {
                tooltipCtrl.room = d.room;
              }

            });
          }

          if (d3.event.pageY > window.innerHeight /2) {
            d3.select(element[0]).style('bottom',
                (window.innerHeight - d3.event.pageY) + 'px')
              .style('top', 'auto');
          }
          else {
            d3.select(element[0]).style('top',
                (d3.event.pageY - 10) + 'px')
              .style('bottom', 'auto');
          }

          d3.select(element[0]).style('left',
              (d3.event.pageX) + 'px')
            .style('right', 'auto');
        }

        function hideTooltip() {
          if (tooltipCtrl.isLocked) {
            return;
          }
          d3.select(element[0]).style('display', 'none');
        }

        function lockTooltip() {
          tooltipCtrl.isLocked = false;
          moveTooltip();
          tooltipCtrl.isLocked = true;
          d3.event.preventDefault();
        }

        function unlockTooltip() {
          if (!d3.event.defaultPrevented) {
            tooltipCtrl.isLocked = false;
            hideTooltip();
          }
        }

        d3.select(element[0]).style('display', 'none');

        d3.select(element.parent()[0])
          .selectAll('[data-room-id]')
            .on('mouseover', showTooltip)
            .on('mousemove', moveTooltip)
            .on('mouseout', hideTooltip)
            .on('mouseup', lockTooltip);

        d3.select(element.parent()[0])
          .on('mousedown', unlockTooltip)
          .on('dragstart', unlockTooltip);

        scope.$on('$destroy', function () {
          d3.select(element.parent()[0])
            .selectAll('[data-room-id]')
              .on('mouseover', null)
              .on('mousemove', null)
              .on('mouseout', null)
              .on('click', null);
        });

      }
    };
  });
{'fraction_non_alphanumeric': 0.17639118044097796, 'fraction_numerical': 0.009299535023248838, 'mean_word_length': 1.5124340617935192, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4881414', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1978, 'n_words': 313}