@ -1,4 +1,77 @@
function get _prices _from _localstorage ( ) {
//Reusable Fields
const getSellFields = function ( ) {
let fields = [ ] ;
for ( var i = 2 ; i < 14 ; i ++ ) {
fields . push ( $ ( "#sell_" + i ) [ 0 ] ) ;
}
return fields ;
} ;
const sell _inputs = getSellFields ( ) ;
const buy _input = $ ( "#buy" ) ;
const first _buy _field = $ ( "#first_buy" ) ;
//Functions
const fillFields = function ( prices , first _buy ) {
first _buy _field . prop ( "checked" , first _buy ) ;
buy _input . focus ( ) ;
buy _input . val ( prices [ 0 ] || '' ) ;
buy _input . blur ( ) ;
const sell _prices = prices . slice ( 2 ) ;
sell _prices . forEach ( ( price , index ) => {
if ( ! price ) {
return
} else {
const element = $ ( "#sell_" + ( index + 2 ) ) ;
element . focus ( ) ;
element . val ( price ) ;
element . blur ( ) ;
}
} )
} ;
const initialize = function ( ) {
try {
const prices = getPrices ( ) ;
const first _buy = getFirstBuyState ( ) ;
if ( prices === null ) {
fillFields ( [ ] , first _buy ) ;
} else {
fillFields ( prices , first _buy ) ;
}
$ ( document ) . trigger ( "input" ) ;
} catch ( e ) {
console . error ( e ) ;
}
$ ( "#reset" ) . on ( "click" , function ( ) {
first _buy _field . prop ( 'checked' , false ) ;
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
} )
} ;
const updateLocalStorage = function ( prices , first _buy ) {
try {
if ( prices . length !== 14 ) throw "The data array needs exactly 14 elements to be valid"
localStorage . setItem ( "sell_prices" , JSON . stringify ( prices ) ) ;
localStorage . setItem ( "first_buy" , JSON . stringify ( first _buy ) ) ;
} catch ( e ) {
console . error ( e )
}
} ;
const isEmpty = function ( arr ) {
const filtered = arr . filter ( value => value !== null && value !== '' && ! isNaN ( value ) ) ;
return filtered . length == 0 ;
} ;
const getFirstBuyState = function ( ) {
return JSON . parse ( localStorage . getItem ( 'first_buy' ) ) ;
} ;
const getPricesFromLocalstorage = function ( ) {
try {
const sell _prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) ) ;
@ -10,9 +83,9 @@ function get_prices_from_localstorage() {
} catch ( e ) {
return null ;
}
}
} ;
function get _prices _from _query ( ) {
const getPricesFromQuery = function ( ) {
try {
const params = new URLSearchParams ( window . location . search . substr ( 1 ) ) ;
const sell _prices = params . get ( "prices" ) . split ( "." ) . map ( ( x ) => parseInt ( x , 10 ) ) ;
@ -26,70 +99,26 @@ function get_prices_from_query() {
} catch ( e ) {
return null ;
}
}
$ ( document ) . ready ( function ( ) {
try {
// load sell_prices from URL hash first, then local storage
const sell _prices = get _prices _from _query ( ) || get _prices _from _localstorage ( ) ;
if ( sell _prices == null ) {
return ;
}
sell _prices . forEach ( ( sell _price , index ) => {
if ( ! sell _price ) {
return ;
}
const buyInput = $ ( "#buy" ) ;
if ( index === 0 ) {
buyInput . focus ( ) ;
buyInput . val ( sell _price ) ;
buyInput . blur ( ) ;
return ;
}
const element = $ ( "#sell_" + index ) ;
if ( element . length ) {
element . focus ( ) ;
element . val ( sell _price ) ;
element . blur ( ) ;
}
} ) ;
$ ( document ) . trigger ( "input" ) ;
} catch ( e ) {
console . error ( e ) ;
}
$ ( "#reset" ) . on ( "click" , function ( ) {
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
} )
} ) ;
} ;
$ ( document ) . on ( "input" , function ( ) {
// Update output on any input change
const getPrices = function ( ) {
return getPricesFromQuery ( ) || getPricesFromLocalstorage ( ) ;
} ;
var buy _price = parseInt ( $ ( "#buy" ) . val ( ) ) ;
const getSellPrices = function ( ) {
//Checks all sell inputs and returns an array with their values
return res = sell _inputs . map ( function ( input ) {
return parseInt ( input . value || '' ) ;
} ) ;
} ;
var sell _prices = [ buy _price , buy _price ] ;
for ( var i = 2 ; i < 14 ; i ++ ) {
sell _prices . push ( parseInt ( $ ( "#sell_" + i ) . val ( ) ) ) ;
}
if ( ! window . price _from _query ) {
localStorage . setItem ( "sell_prices" , JSON . stringify ( sell _prices ) ) ;
}
const is _empty = sell _prices . every ( sell _price => ! sell _price ) ;
if ( is _empty ) {
const calculateOutput = function ( data , first _buy ) {
if ( isEmpty ( data ) ) {
$ ( "#output" ) . html ( "" ) ;
return ;
}
let output _possibilities = "" ;
for ( let poss of analyze _possibilities ( sell_prices ) ) {
for ( let poss of analyze _possibilities ( data , first _buy ) ) {
var out _line = "<tr><td>" + poss . pattern _description + "</td>"
for ( let day of poss . prices . slice ( 1 ) ) {
if ( day . min !== day . max ) {
@ -99,8 +128,26 @@ $(document).on("input", function() {
}
}
out _line += ` <td class="one"> ${ poss . weekMin } </td><td class="one"> ${ poss . weekMax } </td></tr> ` ;
output _possibilities += out _line
output _possibilities += out _line ;
}
$ ( "#output" ) . html ( output _possibilities ) ;
} ;
const update = function ( ) {
const sell _prices = getSellPrices ( ) ;
const buy _price = parseInt ( buy _input . val ( ) ) ;
const first _buy = first _buy _field . is ( ":checked" ) ;
buy _input . prop ( 'disabled' , first _buy ) ;
const prices = [ buy _price , buy _price , ... sell _prices ] ;
if ( ! window . price _from _query ) {
updateLocalStorage ( prices , first _buy ) ;
}
calculateOutput ( prices , first _buy ) ;
} ;
$ ( "#output" ) . html ( output _possibilities )
} ) ;
$ ( document ) . ready ( initialize ) ;
$ ( document ) . on ( "input" , update ) ;