@ -65,9 +65,10 @@ const fillFields = function (prices, first_buy, previous_pattern) {
const initialize = function ( ) {
try {
const prices = getPrices ( )
const first _buy = getFirstBuyState ( ) ;
const previous _pattern = getPreviousPatternState ( ) ;
const previous = getPrevious ( ) ;
const first _buy = previous [ 0 ] ;
const previous _pattern = previous [ 1 ] ;
const prices = previous [ 2 ] ;
if ( prices === null ) {
fillFields ( [ ] , first _buy , previous _pattern )
} else {
@ -101,14 +102,65 @@ const isEmpty = function (arr) {
return filtered . length == 0
}
const getFirstBuyState = function ( ) {
const getFirstBuyStateFromQuery = function ( param ) {
try {
const params = new URLSearchParams ( window . location . search . substr ( 1 ) ) ;
const firstbuy _str = params . get ( param ) ;
if ( firstbuy _str == null ) {
return null ;
}
firstbuy = null ;
if ( firstbuy _str == "1" || firstbuy _str == "yes" || firstbuy _str == "true" ) {
firstbuy = true ;
} else if ( firstbuy _str == "0" || firstbuy _str == "no" || firstbuy _str == "false" ) {
firstbuy = false ;
}
return firstbuy ;
} catch ( e ) {
return null ;
}
}
const getFirstBuyStateFromLocalstorage = function ( ) {
return JSON . parse ( localStorage . getItem ( 'first_buy' ) )
}
const getPreviousPatternState = function ( ) {
const getPreviousPatternState FromLocalstorage = function ( ) {
return JSON . parse ( localStorage . getItem ( 'previous_pattern' ) )
}
const getPreviousPatternStateFromQuery = function ( param ) {
try {
const params = new URLSearchParams ( window . location . search . substr ( 1 ) ) ;
const pattern _str = params . get ( param ) ;
if ( pattern _str == null ) {
return null ;
}
if ( pattern _str == "0" || pattern _str == "fluctuating" ) {
pattern = 0 ;
} else if ( pattern _str == "1" || pattern _str == "large-spike" ) {
pattern = 1 ;
} else if ( pattern _str == "2" || pattern _str == "decreasing" ) {
pattern = 2 ;
} else if ( pattern _str == "3" || pattern _str == "small-spike" ) {
pattern = 3 ;
} else {
pattern = - 1 ;
}
return pattern ;
} catch ( e ) {
return null ;
}
}
const getPricesFromLocalstorage = function ( ) {
try {
const sell _prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) ) ;
@ -123,10 +175,10 @@ const getPricesFromLocalstorage = function () {
}
} ;
const getPricesFromQuery = function ( ) {
const getPricesFromQuery = function ( param ) {
try {
const params = new URLSearchParams ( window . location . search . substr ( 1 ) ) ;
const sell _prices = params . get ( "prices" ) . split ( "." ) . map ( ( x ) => parseInt ( x , 10 ) ) ;
const sell _prices = params . get ( param ) . split ( "." ) . map ( ( x ) => parseInt ( x , 10 ) ) ;
if ( ! Array . isArray ( sell _prices ) ) {
return null ;
@ -141,15 +193,52 @@ const getPricesFromQuery = function () {
sell _prices . push ( 0 ) ;
}
window . price _from _query = true ;
return sell _prices ;
} catch ( e ) {
return null ;
}
} ;
const getPrices = function ( ) {
return getPricesFromQuery ( ) || getPricesFromLocalstorage ( ) ;
const getPreviousFromQuery = function ( ) {
const queries = [
[ "first" , getFirstBuyStateFromQuery ] ,
[ "pattern" , getPreviousPatternStateFromQuery ] ,
[ "prices" , getPricesFromQuery ]
] ;
found = null ; /* value to save if we found any parameters */
ret = [ ] ;
for ( q of queries ) {
val = q [ 1 ] ( q [ 0 ] ) ; /* run the function with the parameter */
found = found || val ;
ret . push ( val ) ;
}
if ( found != null ) { /* if we found any parameter */
window . from _query = true ;
document . getElementById ( "from_query" ) . style . visibility = "visible" ;
return ret ;
}
return null ;
} ;
const getPreviousFromLocalstorage = function ( ) {
return [
getFirstBuyStateFromLocalstorage ( ) ,
getPreviousPatternStateFromQuery ( ) ,
getPricesFromLocalstorage ( )
] ;
} ;
/ * *
* Gets previous values . First tries to parse parameters ,
* if none of them match then it looks in local storage .
* @ return { [ first time , previous pattern , prices ] }
* /
const getPrevious = function ( ) {
return getPreviousFromQuery ( ) || getPreviousFromLocalstorage ( ) ;
} ;
const getSellPrices = function ( ) {
@ -187,13 +276,13 @@ const update = function () {
const buy _price = parseInt ( buy _input . val ( ) ) ;
const first _buy = getCheckedRadio ( first _buy _radios ) == 'true' ;
const previous _pattern = parseInt ( getCheckedRadio ( previous _pattern _radios ) ) ;
buy _input [ 0 ] . disabled = first _buy ;
buy _input [ 0 ] . placeholder = first _buy ? '—' : '...'
const prices = [ buy _price , buy _price , ... sell _prices ] ;
if ( ! window . price_ from_query ) {
if ( ! window . from_query ) {
updateLocalStorage ( prices , first _buy , previous _pattern ) ;
}