Skip to content

Commit

Permalink
Add method for admins to override our provided echart behavior (#89)
Browse files Browse the repository at this point in the history
* Add method for admins to override our provided echart behavior
  • Loading branch information
ColdHeat authored Sep 3, 2024
1 parent fea8f53 commit 0eff226
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 43 deletions.
4 changes: 3 additions & 1 deletion assets/js/scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Alpine.data("ScoreboardDetail", () => ({
async update() {
this.data = await CTFd.pages.scoreboard.getScoreboardDetail(10);

let option = getOption(CTFd.config.userMode, this.data);
let optionMerge = window.scoreboardChartOptions;
let option = getOption(CTFd.config.userMode, this.data, optionMerge);

embed(this.$refs.scoregraph, option);
this.show = Object.keys(this.data).length > 0;
},
Expand Down
3 changes: 3 additions & 0 deletions assets/js/teams/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,16 @@ Alpine.data("TeamGraphs", () => ({
this.failCount = this.fails.meta.count;
this.awardCount = this.awards.meta.count;

let optionMerge = window.teamScoreGraphChartOptions;

embed(
this.$refs.scoregraph,
getUserScoreOption(
CTFd.team.id,
CTFd.team.name,
this.solves.data,
this.awards.data,
optionMerge,
),
);
},
Expand Down
3 changes: 3 additions & 0 deletions assets/js/teams/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ Alpine.data("TeamGraphs", () => ({
this.failCount = this.fails.meta.count;
this.awardCount = this.awards.meta.count;

let optionMerge = window.teamScoreGraphChartOptions;

embed(
this.$refs.scoregraph,
getUserScoreOption(
window.TEAM.id,
window.TEAM.name,
this.solves.data,
this.awards.data,
optionMerge,
),
);
},
Expand Down
3 changes: 3 additions & 0 deletions assets/js/users/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ Alpine.data("UserGraphs", () => ({
this.failCount = this.fails.meta.count;
this.awardCount = this.awards.meta.count;

let optionMerge = window.userScoreGraphChartOptions;

embed(
this.$refs.scoregraph,
getUserScoreOption(
CTFd.user.id,
CTFd.user.name,
this.solves.data,
this.awards.data,
optionMerge,
),
);
},
Expand Down
3 changes: 3 additions & 0 deletions assets/js/users/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ Alpine.data("UserGraphs", () => ({
this.failCount = this.fails.meta.count;
this.awardCount = this.awards.meta.count;

let optionMerge = window.userScoreGraphChartOptions;

embed(
this.$refs.scoregraph,
getUserScoreOption(
window.USER.id,
window.USER.name,
this.solves.data,
this.awards.data,
optionMerge,
),
);
},
Expand Down
6 changes: 5 additions & 1 deletion assets/js/utils/graphs/echarts/categories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { colorHash } from "@ctfdio/ctfd-js/ui";
import { mergeObjects } from "../../objects";

export function getOption(solves) {
export function getOption(solves, optionMerge) {
let option = {
title: {
left: "center",
Expand Down Expand Up @@ -99,5 +100,8 @@ export function getOption(solves) {
});
});

if (optionMerge) {
option = mergeObjects(option, optionMerge);
}
return option;
}
17 changes: 6 additions & 11 deletions assets/js/utils/graphs/echarts/scoreboard.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { colorHash } from "@ctfdio/ctfd-js/ui";
import { mergeObjects } from "../../objects";
import { cumulativeSum } from "../../math";
import dayjs from "dayjs";

export function cumulativeSum(arr) {
let result = arr.concat();
for (let i = 0; i < arr.length; i++) {
result[i] = arr.slice(0, i + 1).reduce(function (p, i) {
return p + i;
});
}
return result;
}

export function getOption(mode, places) {
export function getOption(mode, places, optionMerge) {
let option = {
title: {
left: "center",
Expand Down Expand Up @@ -102,5 +94,8 @@ export function getOption(mode, places) {
option.series.push(data);
}

if (optionMerge) {
option = mergeObjects(option, optionMerge);
}
return option;
}
8 changes: 7 additions & 1 deletion assets/js/utils/graphs/echarts/solve-percentage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function getOption(solves, fails) {
import { mergeObjects } from "../../objects";

export function getOption(solves, fails, optionMerge) {
let option = {
title: {
left: "center",
Expand Down Expand Up @@ -77,5 +79,9 @@ export function getOption(solves, fails) {
},
],
};

if (optionMerge) {
option = mergeObjects(option, optionMerge);
}
return option;
}
9 changes: 7 additions & 2 deletions assets/js/utils/graphs/echarts/userscore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { colorHash } from "@ctfdio/ctfd-js/ui";
import { cumulativeSum } from "./scoreboard";
import { cumulativeSum } from "../../math";
import { mergeObjects } from "../../objects";
import dayjs from "dayjs";

export function getOption(id, name, solves, awards) {
export function getOption(id, name, solves, awards, optionMerge) {
let option = {
title: {
left: "center",
Expand Down Expand Up @@ -98,5 +99,9 @@ export function getOption(id, name, solves, awards) {
},
data: cumulativeSum(scores),
});

if (optionMerge) {
option = mergeObjects(option, optionMerge);
}
return option;
}
11 changes: 11 additions & 0 deletions assets/js/utils/objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function mergeObjects(target, source) {
// https://stackoverflow.com/a/65817907
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
for (const key of Object.keys(source)) {
if (source[key] instanceof Object)
Object.assign(source[key], mergeObjects(target[key], source[key]));
}
// Join `target` and modified `source`
Object.assign(target || {}, source);
return target;
}
1 change: 0 additions & 1 deletion static/assets/index.6c6d1611.js

This file was deleted.

1 change: 1 addition & 0 deletions static/assets/index.955e6486.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions static/assets/scoreboard.3f07d373.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion static/assets/scoreboard.4ec57e19.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion static/assets/users_private.34cf0ed1.js

This file was deleted.

Loading

0 comments on commit 0eff226

Please sign in to comment.