Skip to content

Commit

Permalink
resolve imports
Browse files Browse the repository at this point in the history
  • Loading branch information
katrina-cityofdetroit committed Jul 29, 2024
1 parent 79f4b23 commit d97d4e4
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 379 deletions.
2 changes: 1 addition & 1 deletion src/js/components/header/header.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './header.css';

import { CurrentFund } from "../../utils/data_utils/local_storage_handlers.js";
import CurrentFund from '../../models/current_fund';

export const Subtitle = {
update : function(subtitle){
Expand Down
3 changes: 2 additions & 1 deletion src/js/components/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import './sidebar.css'

import { formatCurrency } from "../../utils/common_utils.js";
import { TARGET } from "../../init.js";
import { Baseline, Supplemental } from "../../utils/data_utils/local_storage_handlers.js";
import Baseline from '../../models/baseline.js'
import Supplemental from '../../models/supplemental.js'

// Assuming you have a CSS variable --main-color defined on the :root
const root = document.documentElement;
Expand Down
22 changes: 12 additions & 10 deletions src/js/components/table/subcomponents/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FundLookupTable } from "../../../utils/data_utils/budget_data_handlers.js";
import { CurrentFund, CurrentPage, loadTableData, saveTableData } from "../../../utils/data_utils/local_storage_handlers.js";
import CurrentFund from '../../../models/current_fund.js'
import CurrentPage from '../../../models/current_page.js'

function fillTable(data) {
try {
Expand Down Expand Up @@ -33,24 +34,25 @@ function fillTable(data) {
} catch(error) {
console.error('No table saved in localStorage:', error);
}
saveTableData();
}

async function loadFromStorage(){
// look up table in storage and pass to table load function\
// look up table name in storage
if (CurrentFund.number()){
var key = `${CurrentPage.load()}_${CurrentFund.number()}`;
} else {
var key = CurrentPage.load();
}
const data = await loadTableData(key);
if (!data){
// if no table in storage, return 0
// load from local storage
const data = localStorage.getItem(key);

// if nothing in storage, return a zero
if ( data == '' || data == '[]' ) {
return 0;
} else {
fillTable(data);
return 1;
}
};
// otherwise, fill table in HTML and return success (1)
fillTable(await JSON.parse(data));
return 1;
}


Expand Down
31 changes: 23 additions & 8 deletions src/js/components/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import Columns from './subcomponents/columns.js'
import Header from './subcomponents/headers.js'
import Rows from './subcomponents/rows.js'
import Data from './subcomponents/data.js'
import { saveTableData } from '../../utils/data_utils/local_storage_handlers.js'
import Tooltip from '../tooltip/tooltip.js';
import { convertToJSON } from "../../utils/data_utils/JSON_data_handlers.js";
import Sidebar from '../sidebar/sidebar.js';
import CurrentFund from '../../models/current_fund.js';
import CurrentPage from '../../models/current_page.js';

function adjustTableWidth(width_pct){
const table = document.getElementById('main-table');
Expand All @@ -31,6 +34,24 @@ function hideTable(){
Buttons.AddRow.hide();
}

function saveTableData() {
// remove the detail text
Tooltip.unlink();
// get table
var table = document.getElementById('main-table');
// determine save_as name
if (CurrentFund.number()) {
var save_as = `${CurrentPage.load()}_${CurrentFund.number()}`;
} else {
var save_as = CurrentPage.load();
}
localStorage.setItem(save_as, convertToJSON(table, ['Edit']));
// update sidebar with new data
Sidebar.updateTotals();
// relink, depending on page
Tooltip.linkAll();
}

const Table = {
Buttons : Buttons,
Cell : Cell,
Expand All @@ -45,13 +66,7 @@ const Table = {
clear : clearTable,
hide : hideTable,
show : showTable,
save : async function() {
// remove the detail text
Tooltip.unlink();
saveTableData();
// relink, depending on page
Tooltip.linkAll();
}
save : saveTableData
}

export default Table;
2 changes: 1 addition & 1 deletion src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export let TARGET = 10000000;
export var FISCAL_YEAR = '26';

// import functions
import { CurrentPage } from './utils/data_utils/local_storage_handlers.js';
import CurrentPage from './models/current_page.js';

// sheets to expect on detail sheet
export const SHEETS = {
Expand Down
2 changes: 2 additions & 0 deletions src/js/models/baseline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Fund from "./fund.js";
import FundLookupTable from "./fund_lookup_table.js";


export class Baseline {
// baseline will just contain a list of funds, each with
Expand Down
2 changes: 1 addition & 1 deletion src/js/models/current_fund.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { FundLookupTable } from "../utils/data_utils/budget_data_handlers";
import FundLookupTable from "./fund_lookup_table";

export const CurrentFund = {
update : function(fund){
Expand Down
113 changes: 113 additions & 0 deletions src/js/models/fund_lookup_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import CurrentFund from "./current_fund.js";
import { getUniqueValues } from "../utils/common_utils.js";

export const FundLookupTable = {
retrieve : function() {
return JSON.parse(localStorage.getItem('fund-lookup-table')) || {};
},
save : function(fundDict){
localStorage.setItem('fund-lookup-table', JSON.stringify(fundDict));
},

update : function(fundData){
const table = this.retrieve();

for (let fund of Object.keys(fundData)){

// add to lookup table if not in there already
if (!table[fund]){
// get fund name
const fundName = fundData[fund][0]['Fund Name'];
// add fund to dictionary
table[fund] = {};
table[fund]['name'] = fundName;
table[fund]['viewed'] = false;
// build lists of unique cost centers and appropriations
table[fund]['approp'] = getUniqueValues(fundData[fund], 'Appropriation Name');
table[fund]['cc'] = getUniqueValues(fundData[fund], 'Cost Center Name');
}
}
// save any updates
this.save(table);
},

getAll: function(key) {
// function to aggregate all approps or CCs for every fund in one array
const funds = this.retrieve();
const ret = [];
for (const fund in funds) {
if (funds.hasOwnProperty(fund)) {
for (let i in funds[fund][key]){
ret.push(funds[fund][key][i]);
}
}
}
return ret;
},

getCostCenters : function() {
// get current fund
const fund = CurrentFund.number()
if (this.retrieve()[fund]){
return this.retrieve()[fund]['cc'];
}
// if no fund (ie. we're on the new initiative page), return all options
return this.getAll('cc');
},

getApprops : function() {
// get current fund
const fund = CurrentFund.number()
if (this.retrieve()[fund]){
return this.retrieve()[fund]['approp'];
}
// if no fund (ie. we're on the new initiative page), return all options
return this.getAll('approp');
},

reset : function() {
this.save({});
},
getName : function(number){
if(!number || !this.retrieve()) { return '' };
return this.retrieve()[number]['name'];
},
listFunds : function(){
return Object.keys(this.retrieve());
},
listFundNames : function(){
const funds = this.retrieve();
// initialize array
var ret = [];
Object.keys(funds).forEach( (fund_number) => {
var fund_name = funds[fund_number]['name'];
ret.push(fund_name);
});
return ret;
},
editFund : function(fund){
const table = this.retrieve();
if (table[fund]){
table[fund]['viewed'] = true;
this.save(table);
} else {
console.error('No fund selected.');
}

},
listUneditedFunds : function(){
const table = this.retrieve();
const ret = [];
this.listFunds().forEach(key => {
if (!table[key]['viewed']){
ret.push(key);
}
});
return ret;
},
fundsLeft : function(){
return (this.listUneditedFunds().length > 0);
}
}

export default FundLookupTable
File renamed without changes.
28 changes: 27 additions & 1 deletion src/js/utils/common_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,30 @@ export function removeNewLines(str){
str = str.replaceAll(' ', ' ');
str = str.replace(/^\s+|\s+$/g, '');
return str;
}
}

export function colSum(table, colName) {
// fill with zero until there is something saved in storage
if(!table || table == ''){
return 0;
}
const headers = Object.keys(table[0]);
if (headers.includes(colName)) {
let sum = 0;
for (let i = 0; i < table.length; i++){
var value = Math.round(parseFloat(table[i][colName]));
// treat NaN (non-numerics) as zeroes
if (value) { sum += value; }
}
return sum;
} else {
// console.error(`Could not find expected total column in saved data for ${name}. Returning 0. See StoredTable.totalCol() switch.`);
return 0;
}

}

export function getUniqueValues(data, key) {
const values = data.map(obj => obj[key]);
return Array.from(new Set(values));
}
Loading

0 comments on commit d97d4e4

Please sign in to comment.