Skip to content
Merged
9 changes: 8 additions & 1 deletion ui/analyse/css/_forecast.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
}

.entry {
@extend %flex-center-nowrap;
@extend %button-none, %flex-center-nowrap;
width: 100%;

cursor: pointer;
&:hover {
background: $c-primary;
color: $c-primary-over;
}

@include padding-direction(0.7em, 0.1em, 0.7em, 0.6em);

Expand Down
28 changes: 27 additions & 1 deletion ui/analyse/src/forecast/forecastCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { prop, notEmpty } from 'common';
import * as xhr from 'common/xhr';
import { ForecastCtrl, ForecastData, ForecastStep } from './interfaces';
import { AnalyseData } from '../interfaces';
import { scalachessCharPair } from 'chessops/compat';
import { parseUci } from 'chessops';
import { TreeWrapper } from 'tree';

export function make(cfg: ForecastData, data: AnalyseData, redraw: () => void): ForecastCtrl {
const saveUrl = `/${data.game.id}${data.player.id}/forecasts`;

let forecasts = cfg.steps || [];
let forecasts: ForecastStep[][] = cfg.steps || [];
const loading = prop(false);

function keyOf(fc: ForecastStep[]): string {
Expand Down Expand Up @@ -121,6 +124,28 @@ export function make(cfg: ForecastData, data: AnalyseData, redraw: () => void):
});
}

function showForecast(path: string, tree: TreeWrapper, nodes: ForecastStep[]) {
nodes.forEach(node => {
const moveId = scalachessCharPair(parseUci(node.uci)!);

// this handles the case where the move isn't in the tree yet
// if it is, it just returns
tree.addNode(
{
ply: node.ply,
fen: node.fen,
uci: node.uci,
san: node.san,
id: moveId,
children: [],
},
path // the path before this is its parent
);
path += moveId;
});
return path;
}

return {
addNodes(fc: ForecastStep[]): void {
fc = truncate(fc);
Expand All @@ -141,5 +166,6 @@ export function make(cfg: ForecastData, data: AnalyseData, redraw: () => void):
findStartingWithNode,
playAndSave,
reloadToLastPly,
showForecast,
};
}
19 changes: 14 additions & 5 deletions ui/analyse/src/forecast/forecastView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import AnalyseCtrl from '../ctrl';
import { renderNodesHtml } from '../pgnExport';
import { spinnerVdom as spinner } from 'common/spinner';
import { fixCrazySan } from 'chess';
import { findCurrentPath } from '../treeView/common';

function onMyTurn(ctrl: AnalyseCtrl, fctrl: ForecastCtrl, cNodes: ForecastStep[]): VNode | undefined {
const firstNode = cNodes[0];
Expand Down Expand Up @@ -59,11 +60,19 @@ export default function (ctrl: AnalyseCtrl, fctrl: ForecastCtrl): VNode {
h('div.top', ctrl.trans.noarg('conditionalPremoves')),
h(
'div.list',
fctrl.list().map(function (nodes, i) {
return h(
'div.entry.text',
fctrl.list().map((nodes, i) =>
h(
'button.entry.text',
{
attrs: dataIcon(licon.PlayTriangle),
hook: bind(
'click',
_ => {
const path = fctrl.showForecast(findCurrentPath(ctrl) || '', ctrl.tree, nodes);
ctrl.userJump(path);
},
ctrl.redraw
),
},
[
h('button.del', {
Expand All @@ -72,8 +81,8 @@ export default function (ctrl: AnalyseCtrl, fctrl: ForecastCtrl): VNode {
}),
h('sans', renderNodesHtml(nodes)),
]
);
})
)
)
),
h(
'button.add.text',
Expand Down
2 changes: 2 additions & 0 deletions ui/analyse/src/forecast/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Prop } from 'common';
import { TreeWrapper } from 'tree';

export interface ForecastData {
onMyTurn?: boolean;
Expand All @@ -24,4 +25,5 @@ export interface ForecastCtrl {
list(): ForecastStep[][];
loading: Prop<boolean>;
onMyTurn: boolean;
showForecast(path: string, tree: TreeWrapper, nodes: ForecastStep[]): string;
}