Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
berhalak committed Sep 27, 2024
1 parent d5f1b53 commit 8ff8db5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 31 deletions.
63 changes: 48 additions & 15 deletions timeline/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Timeline, DataSet, TimelineOptions } from 'vis-timeline/standalone';
import moment from 'moment';


declare global {
var grist: any;
Expand Down Expand Up @@ -56,15 +58,26 @@ const options: TimelineOptions = {

orientation: 'top',

zoomMin: 1000 * 60 * 60 * 24, // one day in milliseconds
zoomMax: 1000 * 60 * 60 * 24 * 31 * 3, // about three months in milliseconds
timeAxis: {
scale: 'week',
step: 1
},
format: {
minorLabels: {week: 'w'}
},

locale: 'pl',

zoomMin: 1000 * 60 * 60 * 24 * 31 * 3, // one day in milliseconds
zoomMax: 1000 * 60 * 60 * 24 * 31 * 12, // about three months in milliseconds
};

// Create a Timeline
var timeline = new Timeline(container, items, options);

function onSelect(data) {
grist.setCursorPos({ rowId: data.items[0] });
async function onSelect(data) {
await grist.setCursorPos({ rowId: data.items[0] });
await grist.commandApi.run('viewAsCard');
}

// add event listener
Expand All @@ -77,18 +90,29 @@ const records = observable([]);
let show = () => {};

grist.onRecords(recs => {
console.log('records', recs);
console.error('records', recs);
const keys = Object.keys(recs[0] || {});
console.error('keys', keys);
console.log(recs);
records(recs);
show();
});

function getFrom(r: any) {
return r.Valid_From;
}

function getTo(r: any) {
return r.Valid_To;
}

function recToItem(r) {
return {
id: r.id,
content: r.Subject || 'no title',
start: trimTime(r.From),
end: trimTime(r.To),
type: same(r.To, r.From) ? 'point' : 'range',
start: trimTime(getFrom(r)),
end: trimTime(getTo(r)),
type: same(getTo(r), getFrom(r)) ? 'point' : 'range',
group: undefined,
};
}
Expand Down Expand Up @@ -162,7 +186,7 @@ function renderItems(group?: string, show?: (r: any) => string) {
const removed = existing.filter(x => !newIds.has(x));
items.remove(removed);
const newItems: any = recs
.filter(r => r.From || r.To)
.filter(r => getFrom(r) || getTo(r))
.map(r => {
const result = recToItem(r);
if (group) {
Expand All @@ -176,9 +200,14 @@ function renderItems(group?: string, show?: (r: any) => string) {

items.update(newItems);
}
const formatCurrency = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});

function showCampaings() {
renderItems('Campaign');
const formated = x => formatCurrency.format(x);
renderItems('Campaign', x => `${x.Subject} (${formated(x.Campaign_MSRP)})`);
renderGroups('Campaign');
}

Expand All @@ -195,13 +224,17 @@ function renderGroups(group: string) {

function showModel() {
// Same thing as above, but by Part.
renderItems('Part', (x: any) => x.Reseller);
const formated = x => formatCurrency.format(x);
renderItems('Part', x => `${x.Reseller} (${formated(x.Campaign_MSRP)})`);

renderGroups('Part');
}

function showReseller() {
// Same thing as above, but by Reseller.
renderItems('Reseller', (x: any) => x.Part);

const formated = x => formatCurrency.format(x);
renderItems('Reseller', x => `${x.Part} (${formated(x.Campaign_MSRP)})`);

renderGroups('Reseller');
}

Expand All @@ -212,10 +245,10 @@ function showAll() {
const removed = existing.filter(x => !newIds.has(x));
items.remove(removed);
const newItems: any = recs
.filter(r => r.From || r.To)
.filter(r => getFrom(r) || getTo(r))
.map(recToItem);
items.update(newItems);
timeline.setGroups();
}

show = showCampaings;
show = showCampaings;
51 changes: 38 additions & 13 deletions timeline/out/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37776,31 +37776,49 @@ input.vis-configuration.vis-config-range:focus::-ms-fill-upper {
zoomKey: "ctrlKey",
height: "100%",
orientation: "top",
zoomMin: 1e3 * 60 * 60 * 24,
timeAxis: {
scale: "week",
step: 1
},
format: {
minorLabels: { week: "w" }
},
locale: "pl",
zoomMin: 1e3 * 60 * 60 * 24 * 31 * 3,
// one day in milliseconds
zoomMax: 1e3 * 60 * 60 * 24 * 31 * 3
zoomMax: 1e3 * 60 * 60 * 24 * 31 * 12
// about three months in milliseconds
};
var timeline = new Timeline(container, items, options);
function onSelect(data2) {
grist.setCursorPos({ rowId: data2.items[0] });
async function onSelect(data2) {
await grist.setCursorPos({ rowId: data2.items[0] });
await grist.commandApi.run("viewAsCard");
}
timeline.on("select", onSelect);
var records = observable([]);
var show = () => {
};
grist.onRecords((recs) => {
console.log("records", recs);
console.error("records", recs);
const keys4 = Object.keys(recs[0] || {});
console.error("keys", keys4);
console.log(recs);
records(recs);
show();
});
function getFrom(r) {
return r.Valid_From;
}
function getTo(r) {
return r.Valid_To;
}
function recToItem(r) {
return {
id: r.id,
content: r.Subject || "no title",
start: trimTime(r.From),
end: trimTime(r.To),
type: same(r.To, r.From) ? "point" : "range",
start: trimTime(getFrom(r)),
end: trimTime(getTo(r)),
type: same(getTo(r), getFrom(r)) ? "point" : "range",
group: void 0
};
}
Expand Down Expand Up @@ -37862,7 +37880,7 @@ input.vis-configuration.vis-config-range:focus::-ms-fill-upper {
const existing = items.getIds();
const removed = existing.filter((x) => !newIds.has(x));
items.remove(removed);
const newItems = recs.filter((r) => r.From || r.To).map((r) => {
const newItems = recs.filter((r) => getFrom(r) || getTo(r)).map((r) => {
const result = recToItem(r);
if (group) {
result.group = r[group];
Expand All @@ -37874,8 +37892,13 @@ input.vis-configuration.vis-config-range:focus::-ms-fill-upper {
});
items.update(newItems);
}
var formatCurrency = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
function showCampaings() {
renderItems("Campaign");
const formated = (x) => formatCurrency.format(x);
renderItems("Campaign", (x) => `${x.Subject} (${formated(x.Campaign_MSRP)})`);
renderGroups("Campaign");
}
function renderGroups(group) {
Expand All @@ -37888,11 +37911,13 @@ input.vis-configuration.vis-config-range:focus::-ms-fill-upper {
timeline.setGroups(groups);
}
function showModel() {
renderItems("Part", (x) => x.Reseller);
const formated = (x) => formatCurrency.format(x);
renderItems("Part", (x) => `${x.Reseller} (${formated(x.Campaign_MSRP)})`);
renderGroups("Part");
}
function showReseller() {
renderItems("Reseller", (x) => x.Part);
const formated = (x) => formatCurrency.format(x);
renderItems("Reseller", (x) => `${x.Part} (${formated(x.Campaign_MSRP)})`);
renderGroups("Reseller");
}
function showAll() {
Expand All @@ -37901,7 +37926,7 @@ input.vis-configuration.vis-config-range:focus::-ms-fill-upper {
const existing = items.getIds();
const removed = existing.filter((x) => !newIds.has(x));
items.remove(removed);
const newItems = recs.filter((r) => r.From || r.To).map(recToItem);
const newItems = recs.filter((r) => getFrom(r) || getTo(r)).map(recToItem);
items.update(newItems);
timeline.setGroups();
}
Expand Down
6 changes: 3 additions & 3 deletions timeline/out/index.js.map

Large diffs are not rendered by default.

0 comments on commit 8ff8db5

Please sign in to comment.