@ -1,13 +1,16 @@
var active _timer = 100 ;
var calculation = 0 ;
function capitalizeFirstLetter ( string ) {
return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ;
}
function init _edit ( partID ) {
$ ( 'table#details tr#location td p' ) . hide ( ) ;
$ ( '#location-dropdown' ) . show ( ) ;
$ ( '#container-dropdown' ) . show ( ) ;
$ ( 'table#details tr#type td p' ) . hide ( ) ;
$ ( '#type-dropdown' ) . show ( ) ;
$ ( 'table#details tr#partno td p' ) . hide ( ) ;
$ ( 'input[name=partno-input]' ) . show ( ) ;
$ ( '#magical_autofill' ) . show ( ) ;
// $('#magical_autofill').show();
$ ( 'table#details tr#description td p' ) . hide ( ) ;
$ ( 'input[name=description-input]' ) . show ( ) ;
@ -23,6 +26,16 @@ function init_edit(partID) {
$ ( 'input[name=notes-input]' ) . show ( ) ;
$ ( 'table#details tr#notes td p' ) . hide ( ) ;
$ ( 'input[name=voltage-input]' ) . show ( ) ;
$ ( 'table#details tr#voltage td p' ) . hide ( ) ;
$ ( 'input[name=current-input]' ) . show ( ) ;
$ ( 'table#details tr#current td p' ) . hide ( ) ;
$ ( 'input[name=sleepcurrent-input]' ) . show ( ) ;
$ ( 'table#details tr#sleepcurrent td p' ) . hide ( ) ;
$ ( 'input[name=txrx-input]' ) . show ( ) ;
$ ( 'input[name=txrx-head]' ) . show ( ) ;
$ ( 'table#details tr#txrx td p' ) . hide ( ) ;
// var newButton = '<div class="round-button-left"><a href="#" onclick="save(' + partID + ')"><i class="fa fa-check" aria-hidden="true"></i></a></div>';
// $('.round-button-left').replaceWith(newButton);
$ ( '#edit-button' ) . html ( '<i class="fa fa-check" aria-hidden="true"></i>' ) ;
@ -32,29 +45,32 @@ function init_edit(partID) {
}
function new _entry ( ) {
$ ( 'table#details tr# location td p') . text ( '' ) ;
$ ( 'table#details tr# type td p') . text ( '' ) ;
$ ( 'table#details tr#partno td p' ) . text ( '' ) ;
$ ( 'table#details tr#description td p' ) . text ( '' ) ;
$ ( 'table#details tr#partno td input' ) . val ( '' ) ;
$ ( 'table#details tr#description td input' ) . val ( '' ) ;
$ ( 'input[name=datasheet-url-input]' ) . val ( '' ) ;
// container_onchange();
container _onchange ( ) ;
init _edit ( - 1 ) ;
overlay _in ( ) ;
}
function calculate ( ) {
calculateoverlay _in ( ) ;
}
function end _edit ( ) {
$ ( 'table#details tr#location td p' ) . text ( $ ( 'table#details tr#location td select option:checked' ) . text ( ) ) ;
$ ( 'table#details tr#location td p' ) . show ( ) ;
$ ( '#location-dropdown' ) . hide ( ) ;
$ ( '#container-dropdown' ) . hide ( ) ;
$ ( 'table#details tr#type td p' ) . text ( $ ( 'table#details tr#type td select option:checked' ) . text ( ) ) ;
$ ( 'table#details tr#type td p' ) . show ( ) ;
$ ( '#type-dropdown' ) . hide ( ) ;
$ ( 'table#details tr#partno td p' ) . text ( $ ( 'table#details tr#partno td input' ) . val ( ) ) ;
$ ( 'table#details tr#partno td p' ) . show ( ) ;
$ ( 'table#details tr#partno td input' ) . hide ( ) ;
$ ( '#magical_autofill' ) . hide ( ) ;
// $('#magical_autofill').hide();
$ ( 'table#details tr#description td p' ) . text ( $ ( 'table#details tr#description td input' ) . val ( ) ) ;
$ ( 'table#details tr#description td p' ) . show ( ) ;
@ -73,17 +89,26 @@ function end_edit() {
// $('.round-button-left').replaceWith(newButton);
}
function save ( partID ) {
if ( ! $ ( '# location -dropdown') . val ( ) ) {
alert ( 'Please select a location .') ;
if ( ! $ ( '# type -dropdown') . val ( ) ) {
alert ( 'Please select a component type .') ;
return ;
}
var location_id _v = $ ( '#location -dropdown') . val ( ) ;
var type_id _v = $ ( 'type -dropdown') . val ( ) ;
var partno _v = $ ( 'input[name=partno-input]' ) . val ( ) ;
var description _v = $ ( 'input[name=description-input]' ) . val ( ) ;
var datasheet = $ ( 'table#details tr#datasheet td input' ) [ 0 ] . files ;
var datasheet _url _v = $ ( 'input[name=datasheet-url-input]' ) . val ( ) ;
var notes _v = $ ( 'input[name=notes-input]' ) . val ( ) ;
var total = [ ] ;
var mapped = { } ;
$ ( '.parameter-content' ) . find ( 'input' ) . each ( function ( index , elem ) {
//8 console.log(elem.value);
total . push ( elem . value ) ;
mapped [ elem . name ] = elem . value ;
} ) ;
console . log ( mapped ) ;
if ( partno _v . length == 0 ) {
alert ( 'Please enter a part number.' ) ;
return ;
@ -112,9 +137,11 @@ function save(partID) {
data . append ( 'datasheet-url' , datasheet _url _v ) ;
}
data . append ( 'partno' , partno _v ) ;
data . append ( ' location_id', location _id _v ) ;
data . append ( ' type_id', type _id _v ) ;
data . append ( 'description' , description _v ) ;
data . append ( 'notes' , notes _v ) ;
data . append ( 'parameters' , mapped ) ;
$ . ajax ( {
// Your server script to process the upload
@ -172,18 +199,48 @@ function delete_entry(partID) {
} ,
} ) ;
}
function calculate _entry ( partID , parameters ) {
if ( partID < 0 )
return ;
if ( ! confirm ( 'Add selected entry to calculation?' ) )
return ;
console . log ( parameters ) ;
if ( parameters != null ) {
var jsonParameters = JSON . parse ( data . parameters ) ;
console . log ( jsonParameters ) ;
var count = Object . keys ( jsonParameters ) . length ;
console . log ( count ) ;
// for(var k = 0; k < count; k++) {
var obj = Object . keys ( jsonParameters ) ;
var values = Object . values ( jsonParameters ) ;
var voltage = 0 ;
var currents = [ ] ;
console . log ( obj ) ;
for ( var key in obj ) {
var value = obj [ key ] ;
console . log ( key + ":" + value ) ;
if ( value . includes ( 'voltage' ) ) {
voltage = values [ key ] ;
}
else if ( value . includes ( 'current' ) ) {
}
}
}
}
function show _part _info ( partID ) {
$ . getJSON ( rootURL + 'getpartinfo/' + partID , function ( data ) {
$ ( 'table#details tr#location td p' ) . text ( text _filter ( data . location _descriptor ) ) ; // name is the location friendly name
$ ( '#container-dropdown' ) . val ( data . container _id ) ;
$ ( 'img#map' ) . attr ( 'src' , 'parts/map/' + data . container _id ) ;
$ ( '#location-dropdown' ) . empty ( ) ;
$ . getJSON ( 'parts/getlocationsInContainer/' + data . container _id , function ( json ) {
$ . each ( json , function ( loc _id , loc _name ) {
$ ( '#location-dropdown' ) . append ( '<option value="' + loc _id + '">' + loc _name + '</option>' ) ;
} ) ;
$ ( '#location-dropdown' ) . val ( data . location _id ) ;
$ ( 'img#map' ) . attr ( 'src' , 'parts/map/' + data . type _id ) ;
$ ( '#type-dropdown' ) . empty ( ) ;
$ . getJSON ( '/getTypes' , function ( json ) {
$ . each ( json , function ( type _id , type _name ) {
$ ( '#type-dropdown' ) . append ( '<option value="' + type _id + '">' + type _name + '</option>' ) ;
if ( data . type _id == type _id ) {
$ ( 'table#details tr#type td p' ) . text ( text _filter ( type _name ) ) ; // name is the type friendly name
}
} ) ;
$ ( '#type-dropdown' ) . val ( data . type _id ) ;
} ) ;
$ ( 'table#details tr#partno td p' ) . text ( text _filter ( data . partno ) ) ;
$ ( 'table#details tr#partno td input' ) . val ( text _filter ( data . partno ) ) ;
@ -191,6 +248,38 @@ function show_part_info(partID) {
$ ( 'table#details tr#description td input' ) . val ( text _filter ( data . description ) ) ;
$ ( 'table#details tr#notes td p' ) . text ( text _filter ( data . notes ) ) ;
$ ( 'input[name=notes-input]' ) . val ( text _filter ( data . notes ) ) ;
console . log ( data . parameters ) ;
if ( data . parameters != null ) {
var jsonParameters = JSON . parse ( data . parameters ) ;
console . log ( jsonParameters ) ;
var count = Object . keys ( jsonParameters ) . length ;
console . log ( count ) ;
// for(var k = 0; k < count; k++) {
var obj = Object . keys ( jsonParameters ) ;
var values = Object . values ( jsonParameters ) ;
console . log ( obj ) ;
for ( var key in obj ) {
var value = obj [ key ] ;
console . log ( key + ":" + value ) ;
console . log ( $ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) ) . children ( ) . length ) ;
if ( value . includes ( "input" ) && $ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) ) . children ( ) . length == 0 ) {
$ ( 'table#details' ) . append ( '<tr id=' + value . substring ( 0 , value . indexOf ( "-" ) ) + '><td><p></p><input type="text" name=' + value + ' class="pinfo-input"></td></tr>' )
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td p' ) . text ( capitalizeFirstLetter ( value . substring ( 0 , value . indexOf ( "-" ) ) ) ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td p' ) . show ( ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td input' ) . val ( values [ key ] ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td input' ) . show ( ) ;
}
else {
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td p' ) . text ( capitalizeFirstLetter ( value . substring ( 0 , value . indexOf ( "-" ) ) ) ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td p' ) . show ( ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td input' ) . val ( values [ key ] ) ;
$ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td input' ) . show ( ) ;
}
}
}
// }
// Resetting file input. Super dirty hack. Disgusting
var $el = $ ( '#datasheet-finput' ) ;
$el . wrap ( '<form>' ) . closest ( 'form' ) . get ( 0 ) . reset ( ) ;
@ -209,6 +298,15 @@ function show_part_info(partID) {
$ ( '#delete-button' ) . off ( 'click' ) . on ( 'click' , function ( ) {
delete _entry ( partID ) ;
} ) ;
$ ( '#calculate-add-button' ) . off ( 'click' ) . on ( 'click' , function ( ) {
for ( var key in obj ) {
var value = obj [ key ] ;
console . log ( key + ":" + value ) ;
jsonParameters [ obj [ key ] ] = $ ( 'table#details tr#' + value . substring ( 0 , value . indexOf ( "-" ) ) + ' td input' ) . val ( ) ;
}
calculate _entry ( partID , jsonParameters ) ;
} ) ;
overlay _in ( ) ;
} ) . fail ( function ( ) {
console . log ( "Fetching part info failed" ) ;
@ -221,21 +319,24 @@ function perform_query() {
$ ( '#no-results' ) . hide ( ) ;
var query = $ ( '.search-bar' ) . val ( ) ;
var data = {
l : $ ( '# location ') . is ( ':checked' ) ,
l : $ ( '# type ') . is ( ':checked' ) ,
p : $ ( '#partno' ) . is ( ':checked' ) ,
d : $ ( '#description' ) . is ( ':checked' )
} ;
filter = '0' ;
$ ( "#results" ) . find ( "tr:not(:first)" ) . remove ( ) ; // Delete all table rows
$ . getJSON ( rootURL + 'query/' + filter + '/' + query , data , function ( data ) {
var queryPromise = $ . getJSON ( rootURL + 'query/' + filter + '/' + query , data , function ( data ) {
for ( var i = 0 ; i < data . length ; i ++ ) {
var newRow = $ ( '<tr onclick="show_part_info(' + data [ i ] . id + ')"></tr>' ) ;
newRow . append ( $ ( '<td id="location"></td>' ) . text ( text _filter ( data [ i ] . location _descriptor ) ) ) ;
console . log ( data [ i ] ) ;
var newRow = $ ( '<tr onclick="show_part_info(' + data [ i ] . id + ')"></tr>' ) ;
newRow . append ( $ ( '<td id="type"></td>' ) . text ( text _filter ( data [ i ] . type _descriptor ) ) ) ;
newRow . append ( $ ( '<td id="partno"></td>' ) . text ( text _filter ( data [ i ] . partno ) ) ) ;
newRow . append ( $ ( '<td id="description"></td>' ) . text ( text _filter ( data [ i ] . description ) ) ) ;
$ ( '#results' ) . append ( newRow ) ;
}
if ( data . length == 0 ) {
$ ( '#no-results' ) . show ( ) ;
$ ( '#no-results' ) . animate ( { opacity : 1 } , 2000 ) ;
@ -245,19 +346,23 @@ function perform_query() {
$ ( '#no-results' ) . animate ( { opacity : 1 } , 2000 ) ;
console . log ( "Query failed" ) ;
} ) ;
//});
}
function container _onchange ( ) {
var selected _container _id = $ ( '#container-dropdown' ) . val ( ) ;
if ( selected _container _id > 0 ) {
$ ( 'img#map' ) . attr ( 'src' , 'parts/map/' + selected _container _id ) ;
$ ( '#location-dropdown' ) . empty ( ) ;
$ . getJSON ( 'parts/getlocationsInContainer/' + selected _container _id , function ( data ) {
$ . each ( data , function ( location _id , location _name ) {
$ ( '#location-dropdown' ) . append ( '<option value="' + location _id + '">' + location _name + '</option>' ) ;
var selected _container _id = $ ( '#type-dropdown' ) . val ( ) ;
// if (selected_container_id > 0) {
//$('#type-dropdown').empty();
$ . getJSON ( '/getTypes' , function ( data ) {
$ . each ( data , function ( type _id , type _name ) {
$ ( '#type-dropdown' ) . append ( '<option value="' + type _id + '">' + type _name + '</option>' ) ;
if ( data . type _id == type _id ) {
$ ( 'table#details tr#type td p' ) . text ( text _filter ( type _name ) ) ; // name is the type friendly name
}
} ) ;
} ) ;
}
// }
}
function octopartFetch ( ) {
@ -282,7 +387,7 @@ $(document).ready(function() {
active _timer = setTimeout ( perform _query , 100 ) ;
} ) ;
$ ( '.checkbox' ) . change ( function ( ) {
if ( ! $ ( '# location ') . is ( ':checked' )
if ( ! $ ( '# type ') . is ( ':checked' )
&& ! $ ( '#partno' ) . is ( ':checked' )
&& ! $ ( '#description' ) . is ( ':checked' )
&& ! $ ( '#has-docs' ) . is ( ':checked' ) )
@ -296,4 +401,4 @@ $(document).ready(function() {
// octopartFetch();
// }
// });
} ) ;
} ) ;