1
1
// add auto calculate on update
2
- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
2
+ document . addEventListener ( 'DOMContentLoaded' , async function ( ) {
3
3
let fields = document . getElementsByClassName ( 'input-field' ) ;
4
4
for ( const field of fields ) {
5
5
field . addEventListener ( 'change' , showResult ) ;
6
6
}
7
7
8
8
document . getElementById ( "input-total-cost" ) . addEventListener ( 'click' , copyToClipboard ) ;
9
+
10
+ await loadPrinterModels ( ) ;
9
11
} ) ;
10
12
11
- // calculate cost
12
- function calculateCost ( ) {
13
- const bufferFactor = 1.1 ; // add buffer on top of filament usage
14
- const filamentPrice = 20 ; // price per 1kg spool
15
- const hourlyMachineCost = 0.15 ; // the machine cost per hour (used for maintenance/repair/energy)
13
+ // load settings from json
14
+ async function loadSettings ( path = "options.json" ) {
15
+ try {
16
+ const response = await fetch ( path ) ;
17
+ if ( ! response . ok ) {
18
+ throw new Error ( 'Network response was not ok' ) ;
19
+ }
20
+ const settings = await response . json ( ) ;
21
+ return settings ;
22
+ }
23
+ catch ( error ) {
24
+ console . log ( `Got error while trying to load the configuration data: ${ error } ` ) ;
25
+ return { "message" :"error" } ;
26
+ }
27
+ }
16
28
17
- const printerModel = document . getElementById ( "input-printer-model" ) . value ;
29
+ // calculate cost
30
+ async function calculateCost ( ) {
31
+ const settings = await loadSettings ( ) ;
32
+ const bufferFactor = settings . filamentBufferFactor ; // add buffer on top of filament usage
33
+ const filamentPrice = settings . filamentPrices [ "PLA" ] . spoolPrice ; // price per 1kg spool
34
+ const printerModel = document . getElementById ( "input-printer-model" ) . value || other ; // the selected printer model
35
+ const hourlyMachineCost = settings . printerModels [ printerModel ] . hourlyMachineCost ; // the machine cost per hour (used for maintenance/repair/energy)
18
36
19
37
const filamentUsage = parseFloat ( document . getElementById ( "input-total-filament-usage" ) . value || 0 ) ; // used filament in grams
20
38
const printDuration = parseFloat ( document . getElementById ( "input-print-duration" ) . value || 0 ) ; // print duration in minutes
@@ -31,12 +49,27 @@ function calculateCost(){
31
49
return round ;
32
50
}
33
51
52
+ // load printers into select field
53
+ async function loadPrinterModels ( ) {
54
+ const settings = await loadSettings ( ) ;
55
+ const printerModels = settings . printerModels ;
56
+ const printerSelect = document . getElementById ( 'input-printer-model' ) ;
57
+
58
+ for ( const [ id , model ] of Object . entries ( printerModels ) ) {
59
+ const option = document . createElement ( 'option' ) ;
60
+ option . value = id ;
61
+ option . textContent = model . name ;
62
+ printerSelect . appendChild ( option ) ;
63
+ }
64
+ }
65
+
34
66
// show cost from calculation
35
- function showResult ( ) {
36
- const currencyIcon = "CHF" ; // the icon of your currency
67
+ async function showResult ( ) {
68
+ const settings = await loadSettings ( ) ;
69
+ const currencyIcon = settings . currencyIcon ; // the icon of your currency
37
70
38
71
let resultField = document . getElementById ( "input-total-cost" ) ;
39
- resultField . value = `${ calculateCost ( ) } ${ currencyIcon } ` ;
72
+ resultField . value = `${ await calculateCost ( ) } ${ currencyIcon } ` ;
40
73
}
41
74
42
75
// Copy to clipboard
0 commit comments