@ -9,10 +9,12 @@ const getSellFields = function () {
const sell _inputs = getSellFields ( )
const sell _inputs = getSellFields ( )
const buy _input = $ ( "#buy" )
const buy _input = $ ( "#buy" )
const first _buy _field = $ ( "#first_buy" ) ;
//Functions
//Functions
const fillFields = function ( prices ) {
const fillFields = function ( prices , first _buy ) {
first _buy _field . prop ( "checked" , first _buy ) ;
buy _input . focus ( ) ;
buy _input . focus ( ) ;
buy _input . val ( prices [ 0 ] || '' )
buy _input . val ( prices [ 0 ] || '' )
buy _input . blur ( ) ;
buy _input . blur ( ) ;
@ -33,11 +35,11 @@ const fillFields = function (prices) {
const initialize = function ( ) {
const initialize = function ( ) {
try {
try {
const prices = getPrices ( )
const prices = getPrices ( )
console . log ( "Prices from Storage" , prices )
const first _buy = getFirstBuyState ( ) ;
if ( prices === null ) {
if ( prices === null ) {
return
fillFields ( [ ] , first _buy )
} else {
} else {
fillFields ( prices )
fillFields ( prices , first _buy )
}
}
$ ( document ) . trigger ( "input" ) ;
$ ( document ) . trigger ( "input" ) ;
} catch ( e ) {
} catch ( e ) {
@ -45,14 +47,16 @@ const initialize = function () {
}
}
$ ( "#reset" ) . on ( "click" , function ( ) {
$ ( "#reset" ) . on ( "click" , function ( ) {
first _buy _field . prop ( 'checked' , false ) ;
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
} )
} )
}
}
const updateLocalStorage = function ( data ) {
const updateLocalStorage = function ( prices, first _buy ) {
try {
try {
if ( data . length !== 14 ) throw "The data array needs exactly 14 elements to be valid"
if ( prices . length !== 14 ) throw "The data array needs exactly 14 elements to be valid"
localStorage . setItem ( "sell_prices" , JSON . stringify ( data ) )
localStorage . setItem ( "sell_prices" , JSON . stringify ( prices ) )
localStorage . setItem ( "first_buy" , JSON . stringify ( first _buy ) ) ;
} catch ( e ) {
} catch ( e ) {
console . error ( e )
console . error ( e )
}
}
@ -63,6 +67,10 @@ const isEmpty = function (arr) {
return filtered . length == 0
return filtered . length == 0
}
}
const getFirstBuyState = function ( ) {
return JSON . parse ( localStorage . getItem ( 'first_buy' ) )
}
const getPrices = function ( ) {
const getPrices = function ( ) {
let prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) )
let prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) )
if ( ! prices || isEmpty ( prices ) || prices . length !== 14 ) {
if ( ! prices || isEmpty ( prices ) || prices . length !== 14 ) {
@ -79,13 +87,13 @@ const getSellPrices = function () {
} )
} )
}
}
const calculateOutput = function ( data ) {
const calculateOutput = function ( data , first _buy ) {
if ( isEmpty ( data ) ) {
if ( isEmpty ( data ) ) {
$ ( "#output" ) . html ( "" ) ;
$ ( "#output" ) . html ( "" ) ;
return ;
return ;
}
}
let output _possibilities = "" ;
let output _possibilities = "" ;
for ( let poss of analyze _possibilities ( data )) {
for ( let poss of analyze _possibilities ( data , first _buy )) {
var out _line = "<tr><td>" + poss . pattern _description + "</td>"
var out _line = "<tr><td>" + poss . pattern _description + "</td>"
for ( let day of poss . prices . slice ( 1 ) ) {
for ( let day of poss . prices . slice ( 1 ) ) {
if ( day . min !== day . max ) {
if ( day . min !== day . max ) {
@ -104,9 +112,13 @@ const calculateOutput = function (data) {
const update = function ( ) {
const update = function ( ) {
const sell _prices = getSellPrices ( ) ;
const sell _prices = getSellPrices ( ) ;
const buy _price = parseInt ( buy _input . val ( ) ) ;
const buy _price = parseInt ( buy _input . val ( ) ) ;
const data = [ buy _price , buy _price , ... sell _prices ] ;
const first _buy = first _buy _field . is ( ":checked" ) ;
updateLocalStorage ( data ) ;
calculateOutput ( data ) ;
buy _input . prop ( 'disabled' , first _buy ) ;
const prices = [ buy _price , buy _price , ... sell _prices ] ;
updateLocalStorage ( prices , first _buy ) ;
calculateOutput ( prices , first _buy ) ;
}
}
$ ( document ) . ready ( initialize ) ;
$ ( document ) . ready ( initialize ) ;