Skip to content

Commit

Permalink
life cycle hooks for laps chart
Browse files Browse the repository at this point in the history
  • Loading branch information
mayfield committed Dec 30, 2024
1 parent d2abb88 commit 79b28c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/site/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,21 @@ sauce.ns('analysis', ns => {
}


function renderEffortsChart(efforts) {
function initLapsAddon() {
document.addEventListener('lap-efforts-table-view-render', ev => {
renderEffortsChart();
});
renderEffortsChart();
}


function renderEffortsChart() {
const $anchor = jQuery('#efforts-table');
if (!$anchor.length) {
console.warn("no laps view");
return;
}
$anchor.siblings('.sauce-efforts-chart').remove();
let types = [{
attr: 'avg_watts',
label: 'Power', // XXX locale
Expand All @@ -666,14 +680,12 @@ sauce.ns('analysis', ns => {
format: H.hr,
color: 'red',
}];
const efforts = pageView.lapEfforts();
types = types.filter(x => efforts.models.some(xx => xx.get(x.attr) != null));
let type = types[0].attr;
const $anchor = jQuery('#efforts-table');
if (!$anchor.length) {
console.warn("no laps view");
if (!types.length) {
return;
}
$anchor.siblings('.sauce-efforts-chart').remove();
let type = types[0].attr;
$anchor.before(`
<div class="sauce-efforts-chart">
<header>
Expand Down Expand Up @@ -786,15 +798,7 @@ sauce.ns('analysis', ns => {
isSyncAthlete: !!ns.syncAthlete,
}).catch(console.error);
if (sauce.options['analysis-lap-efforts-chart'] || 'XXX') {
const lapEfforts = pageView.lapEfforts();
if (lapEfforts.length) {
renderEffortsChart(lapEfforts);
}
let to;
lapEfforts.on('change', () => {
clearTimeout(to);
to = setTimeout(() => renderEffortsChart(lapEfforts), 1);
});
initLapsAddon();
}
if (sauce.options['analysis-cp-chart']) {
const menu = [/*locale keys*/];
Expand Down Expand Up @@ -3077,7 +3081,7 @@ sauce.ns('analysis', ns => {
} finally {
if (v) {
pf.page = setget;
resolve();
queueMicrotask(resolve);
}
}
};
Expand Down
15 changes: 15 additions & 0 deletions src/site/preloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ self.saucePreloaderInit = function saucePreloaderInit() {
const NewKlass = (Strava.Labs.Activities.BasicAnalysisView = function() {
sauce.basicAnalysisView = this;
Klass.apply(this, arguments);
const event = new Event('basic-analysis-view-ready');
event.view = this;
document.dispatchEvent(event);
});
NewKlass.prototype = Klass.prototype;

Expand All @@ -361,6 +364,18 @@ self.saucePreloaderInit = function saucePreloaderInit() {
});


sauce.propDefined('Strava.Labs.Activities.LapEffortsTableView', View => {
const saveRender = View.prototype.render;
View.prototype.render = function() {
const r = saveRender.apply(this, arguments);
const event = new Event('lap-efforts-table-view-render');
event.view = this;
document.dispatchEvent(event);
return r;
};
});


/* Patch dragging bug when scrolled in this old jquery ui code.
* NOTE: We must use Promise.then instead of a callback because the
* draggable widget isn't fully baked when it's first defined. The
Expand Down

0 comments on commit 79b28c0

Please sign in to comment.