<commit_before>Template.WidgetShow.helpers({
  widgetTemplate: function() {
    return this.templateFor('Widget');
  },
  settingsTemplate: function() {
    return this.templateFor('Settings');
  },
  infoTemplate: function() {
    return this.templateFor('Info');
  },
  providesInfo: function() {
    return this.providesTemplate('Info');
  },
  providesSettings: function() {
    return this.providesTemplate('Settings');
  },
  widgetId: function(aspect) {
    aspect = aspect || 'widget';
    return this.packageName + '-' + this._id + '-' + aspect;
  },
  widgetClass: function() {
    return this.packageName;
  },
  widgetMetadata: function() {
    return this.metadata();
  },
  titleBar: function() {
    return Widget.Settings.titleBar;
  },
  gridAttrs: function() {
    var resizeConstraints = this.metadata().widget.resize.constraints;
    var attrs = {
      'data-sizex': this.width,
      'data-sizey': this.height,
      'data-min-sizex': resizeConstraints.width.min,
      'data-max-sizex': resizeConstraints.width.max,
      'data-min-sizey': resizeConstraints.height.min,
      'data-max-sizey': resizeConstraints.height.max
    };
    if (this.position) {
      attrs['data-row'] = this.position.x;
      attrs['data-col'] = this.position.y;
    }
    return attrs;
  }
});

Template.DefaultWidgetInfo.helpers({
  widgetMetadata: function() {
    return WidgetTypes.findOne({ packageName: this.packageName });
  }
});

Template.DefaultWidgetInfo.events({
  'click .close-info': function(ev, template) {
    this.data.closeInfo(template);
  }
});

Template.WidgetShow.onCreated(function() {
  var self = this;
  self.gridUnitsToPixels = function(dims) {
    var dashboard = self.data.dashboard();
    var currentDims = {
      width: self.$('.widget').attr('data-sizex'),
      height: self.$('.widget').attr('data-sizey')
    };
    dims = dims || currentDims;
    return {
      width: dims.width * dashboard.columnWidth +
        dashboard.gutter * (dims.width - 1),
      height: dims.height * dashboard.rowHeight +
        dashboard.gutter * (dims.height - 1)
    };
  };
  self.scaleBody = function() {
    var widget = self.data;
    var $widgetBody = self.$('.widget-body');

    var currentPixelDims = self.gridUnitsToPixels();
    var originalGridDims = widget.metadata().widget.dimensions;
    var originalPixelDims = self.gridUnitsToPixels(originalGridDims);

    // We're just scaling the body, so don't count the title bar.
    currentPixelDims.height -= Widget.Settings.titleBar.height;
    originalPixelDims.height -= Widget.Settings.titleBar.height;

    $widgetBody.css({
      transform: 'scaleX(' + currentPixelDims.width / originalPixelDims.width + ') ' +
                 'scaleY(' + currentPixelDims.height / originalPixelDims.height + ')'
    });
  };
});

Template.WidgetShow.onRendered(function() {
  var dashboardTemplate = Dashboards.templateFromChild(this);
  var widgetNode = this.firstNode;
  var self = this;

  if (dashboardTemplate.gridster) {
    dashboardTemplate.gridster.add_widget(
      widgetNode, $(widgetNode).data('sizex'), $(widgetNode).data('sizey'));
    Widgets.updatePositions(dashboardTemplate.gridster.serialize());
  } else {
    dashboardTemplate.widgetNodes.push(widgetNode);
  }

  if (this.data.metadata().widget.resize.mode === 'scale') {
    var originalGridDims = self.data.metadata().widget.dimensions;
    var originalPixelDims = self.gridUnitsToPixels(originalGridDims);
    originalPixelDims.height -= Widget.Settings.titleBar.height;

    // Pin the width and height so we don't get both reflows and transforms.
    self.$('.widget-body').css(originalPixelDims);
    self.scaleBody();
  }
});

Template.WidgetShow.events({
  'click .remove-widget': function(ev, template) {
    var dashboardTemplate = Dashboards.templateFromChild(template);
    var dashboard = dashboardTemplate.data;

    dashboardTemplate.gridster.remove_widget(template.firstNode);
    Widgets.updatePositions(dashboardTemplate.gridster.serialize());
    dashboard.removeWidget(this);
  },
  'gridster:resizestart': function(ev, template) {
    if (this.metadata().widget.resize.mode === 'scale') {
      template.$('.widget-body').append('<div class="resizing-cover" />');
    }
    ev.stopPropagation();
  },
  'gridster:resizestop': function(ev, template) {
    if (this.metadata().widget.resize.mode === 'scale') {
      template.scaleBody();
      template.$('.resizing-cover').remove();
    }
    ev.stopPropagation();
  }
});

Template.registerHelper('widgetLoading', function() {
  return 'Loading...';
});<commit_msg>scale when other client resizes - #12435<commit_after>Template.WidgetShow.helpers({
  widgetTemplate: function() {
    return this.templateFor('Widget');
  },
  settingsTemplate: function() {
    return this.templateFor('Settings');
  },
  infoTemplate: function() {
    return this.templateFor('Info');
  },
  providesInfo: function() {
    return this.providesTemplate('Info');
  },
  providesSettings: function() {
    return this.providesTemplate('Settings');
  },
  widgetId: function(aspect) {
    aspect = aspect || 'widget';
    return this.packageName + '-' + this._id + '-' + aspect;
  },
  widgetClass: function() {
    return this.packageName;
  },
  widgetMetadata: function() {
    return this.metadata();
  },
  titleBar: function() {
    return Widget.Settings.titleBar;
  },
  gridAttrs: function() {
    var resizeConstraints = this.metadata().widget.resize.constraints;
    var attrs = {
      'data-sizex': this.width,
      'data-sizey': this.height,
      'data-min-sizex': resizeConstraints.width.min,
      'data-max-sizex': resizeConstraints.width.max,
      'data-min-sizey': resizeConstraints.height.min,
      'data-max-sizey': resizeConstraints.height.max
    };
    if (this.position) {
      attrs['data-row'] = this.position.x;
      attrs['data-col'] = this.position.y;
    }
    return attrs;
  }
});

Template.DefaultWidgetInfo.helpers({
  widgetMetadata: function() {
    return WidgetTypes.findOne({ packageName: this.packageName });
  }
});

Template.DefaultWidgetInfo.events({
  'click .close-info': function(ev, template) {
    this.data.closeInfo(template);
  }
});

Template.WidgetShow.onCreated(function() {
  var self = this;

  self.gridUnitsToPixels = function(dims) {
    var dashboard = self.data.dashboard();
    var currentDims = {
      width: self.$('.widget').attr('data-sizex'),
      height: self.$('.widget').attr('data-sizey')
    };
    dims = dims || currentDims;
    _.defaults(dims, currentDims);
    return {
      width: dims.width * dashboard.columnWidth +
        dashboard.gutter * (dims.width - 1),
      height: dims.height * dashboard.rowHeight +
        dashboard.gutter * (dims.height - 1)
    };
  };

  self.scaleBody = function(newDims) {
    var widget = self.data;
    var $widgetBody = self.$('.widget-body');

    var newPixelDims = self.gridUnitsToPixels(newDims);
    var originalGridDims = widget.metadata().widget.dimensions;
    var originalPixelDims = self.gridUnitsToPixels(originalGridDims);

    // We're just scaling the body, so don't count the title bar.
    newPixelDims.height -= Widget.Settings.titleBar.height;
    originalPixelDims.height -= Widget.Settings.titleBar.height;

    $widgetBody.css({
      transform: 'scaleX(' + newPixelDims.width / originalPixelDims.width + ') ' +
                 'scaleY(' + newPixelDims.height / originalPixelDims.height + ')'
    });
  };
});

Template.WidgetShow.onRendered(function() {
  var dashboardTemplate = Dashboards.templateFromChild(this);
  var widgetNode = this.firstNode;
  var self = this;

  if (dashboardTemplate.gridster) {
    dashboardTemplate.gridster.add_widget(
      widgetNode, $(widgetNode).data('sizex'), $(widgetNode).data('sizey'));
    Widgets.updatePositions(dashboardTemplate.gridster.serialize());
  } else {
    dashboardTemplate.widgetNodes.push(widgetNode);
  }

  if (this.data.metadata().widget.resize.mode === 'scale') {
    var originalGridDims = self.data.metadata().widget.dimensions;
    var originalPixelDims = self.gridUnitsToPixels(originalGridDims);
    originalPixelDims.height -= Widget.Settings.titleBar.height;

    // Pin the width and height so we don't get both reflows and transforms.
    self.$('.widget-body').css(originalPixelDims);
    self.scaleBody();
  }

  // When other clients resize widgets
  self.autorun(function() {
    Widgets.find(self.data._id).observeChanges({
      changed: function(id, fields) {
        if (_(fields).has('width') || _(fields).has('height')) {
          self.scaleBody(fields);
        }
      }
    });
  });
});

Template.WidgetShow.events({
  'click .remove-widget': function(ev, template) {
    var dashboardTemplate = Dashboards.templateFromChild(template);
    var dashboard = dashboardTemplate.data;

    dashboardTemplate.gridster.remove_widget(template.firstNode);
    Widgets.updatePositions(dashboardTemplate.gridster.serialize());
    dashboard.removeWidget(this);
  },
  'gridster:resizestart': function(ev, template) {
    if (this.metadata().widget.resize.mode === 'scale') {
      template.$('.widget-body').append('<div class="resizing-cover" />');
    }
    // This was passed down from the dashboard - don't bubble it back up.
    ev.stopPropagation();
  },
  'gridster:resizestop': function(ev, template) {
    if (this.metadata().widget.resize.mode === 'scale') {
      template.scaleBody();
      template.$('.resizing-cover').remove();
    }
    ev.stopPropagation();
  }
});

Template.registerHelper('widgetLoading', function() {
  return 'Loading...';
});
{'fraction_non_alphanumeric': 0.15199248904652618, 'fraction_numerical': 0.0009388691842269978, 'mean_word_length': 3.5500711912672047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5915442', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2944, 'n_words': 501}