You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ELAB-partsearch/static/script.js

248 lines
8.9 KiB

function overlay_in() {
$('.shadow').css({'display' : 'block'});
$('.overlay').css({'display' : 'block'});
$('.shadow').animate({'opacity' : 0.7});
$('.overlay').animate({'top' : '10%'});
}
function overlay_out() {
$('.shadow').animate({'opacity' : 0.0}, function () {
$('.overlay').css({'display' : 'none'});
});
$('.overlay').animate({'top' : '-70%'}, function () {
$('.shadow').css({'display' : 'none'});
});
end_edit();
}
function init_edit(partID) {
var block_input = $('<input type="text" class="part-edit-input" id="block-input" placeholder="Location">')
block_input.val($('#block-info').text());
$('#block-info').replaceWith(block_input);
var type_input = $('<input type="text" class="part-edit-input" id="type-input" placeholder="Manufacturer">')
type_input.val($('#type-info').text());
$('#type-info').replaceWith(type_input);
var partno_input = $('<input type="text" class="part-edit-input" id="partno-input" placeholder="Part number">')
partno_input.val($('#partno-info').text());
$('#partno-info').replaceWith(partno_input);
var description_input = $('<input type="text" class="part-edit-input" id="description-input" placeholder="Description">')
description_input.val($('#description-info').text());
$('#description-info').replaceWith(description_input);
var quantity_input = $('<input type="text" class="part-edit-input" id="quantity-input" placeholder="Quantity">')
quantity_input.val($('#quantity-info').text());
$('#quantity-info').replaceWith(quantity_input);
var notes_input = $('<input type="text" class="part-edit-input" id="notes-input" placeholder="Notes">')
notes_input.val($('#notes-info').text());
$('#notes-info').replaceWith(notes_input);
var datasheet_input = $('<input type="file" class="part-edit-file" id="datasheet-input" accept=".pdf">')
$('#datasheet-info').replaceWith(datasheet_input);
var newButton = '<div class="round-button"><a href="#" onclick="save(' + partID + ')"><i class="fa fa-check" aria-hidden="true"></i></a></div>';
$('.round-button').replaceWith(newButton);
}
function new_entry() {
$('#block-info').text('');
$('#type-info').text('');
$('#partno-info').text('');
$('#description-info').text('');
$('#quantity-info').text('');
$('#notes-info').text('');
init_edit(-1);
overlay_in();
}
function end_edit() {
var block_par = '<p id="block-info">' + $('#block-input').val() + '</p>';
$('#block-input').replaceWith(block_par);
var type_par = '<p id="type-info">' + $('#type-input').val() + '</p>';
$('#type-input').replaceWith(type_par);
var partno_par = '<p id="partno-info">' + $('#partno-input').val() + '</p>';
$('#partno-input').replaceWith(partno_par);
var description_par = '<p id="description-info">' + $('#description-input').val() + '</p>';
$('#description-input').replaceWith(description_par);
var quantity_par = '<p id="quantity-info">' + $('#quantity-input').val() + '</p>';
$('#quantity-input').replaceWith(quantity_par);
var notes_par = '<p id="notes-info">' + $('#notes-input').val() + '</p>';
$('#notes-input').replaceWith(notes_par);
var datasheet_par = '<p id="datasheet-info"><i class="fa fa-circle-o-notch fa-spin fa-fw"></i></p>';
$('#datasheet-input').replaceWith(datasheet_par);
var newButton = '<div class="round-button"><a href="#" onclick="init_edit()"><i class="fa fa-pencil" aria-hidden="true"></i></a></div>';
$('.round-button').replaceWith(newButton);
}
function save(partID) {
var block_v = $('#block-input').val();
var type_v = $('#type-input').val();
var partno_v = $('#partno-input').val();
var description_v = $('#description-input').val();
var quantity_v = $('#quantity-input').val();
var notes_v = $('#notes-input').val();
var datasheet = $('#datasheet-input')[0].files;
if(! partno_v.match(/^([a-zA-Z0-9]+\s*){1,3}$/g)) {
alert('Invalid part number format. Accepted: <Part-number-line-1> <Part-number-line-2> <Part-number-line-3>');
return;
}
if(! block_v.match(/^[0-9]+$/g)) {
alert('Invalid location field. Only numbers accepted.');
return;
}
partnos = partno_v.split(' ');
var data = new FormData();
if (datasheet.length == 1)
data.append('datasheet-file', datasheet[0]);
data.append('block', block_v);
data.append('type', type_v);
data.append('partno', partnos[0]);
if (partnos.length > 1)
data.append('partnoalt', partnos[1]);
if(partnos.length > 2)
data.append('partnoalt2', partnos[2]);
data.append('description', description_v);
data.append('quantity', quantity_v);
data.append('notes', notes_v);
$.ajax({
// Your server script to process the upload
url: 'https://www.elab.kth.se/parts/alter/' + partID,
type: 'POST',
data: data,
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false,
contentType: false,
processData: false,
success: function(data) {
var datasheet_par = '<p id="datasheet-info"><i class="fa fa-check"></i></p>';
$('#datasheet-info').replaceWith(datasheet_par);
},
error: function() {
alert("Couldn't update the part information. Please retry.");
}
});
end_edit();
}
function text_filter(string) {
if (string != null)
return string;
else
return '';
}
function show_part_info(partID) {
$.getJSON('https://www.elab.kth.se/parts/getpartinfo/' + partID, function(data) {
$('#block-info').text(text_filter(data.block));
$('#type-info').text(text_filter(data.type));
$('#partno-info').text(text_filter(data.partno) + ' ' + text_filter(data.partnoalt) + ' ' + text_filter(data.partnoalt2));
$('#description-info').text(text_filter(data.description));
$('#quantity-info').text(text_filter(data.quantity));
$('#notes-info').text(text_filter(data.notes));
if (data.datasheet != null)
$('#datasheet-info').text('<a href="https://www.elab.kth.se/parts/getfile/' + data.datasheet + '"><i class="fa fa-file-text" aria-hidden="true"></i></a>');
else
$('#datasheet-info').text(' ');
$('#edit-button').click(function() {
init_edit(partID);
});
overlay_in();
}).fail(function() {
console.log( "Fetching part info failed" );
});
}
$(document).ready(function() {
$('.search-bar').on('keyup', function() {
var query = $('.search-bar').val();
var filter = 0;
if($('#type').is(':checked'))
filter += 1;
if($('#partno').is(':checked'))
filter += 2 + 4 + 8;
if($('#description').is(':checked'))
filter += 16;
if($('#notes').is(':checked'))
filter += 32;
$.getJSON('https://www.elab.kth.se/parts/query/' + filter + '/' + query, function(data) {
var newResults = '<div class="results">';
for(var i = 0; i < data.length; i++) { // Create new view for results
newResults += '<div class="results-row">';
newResults += '<a href=# onclick="show_part_info(' + data[i].id + ')">'
newResults += '<div class="results-block">';
if (data[i].block != null)
newResults += data[i].block;
newResults += '</div>';
newResults += '<div class="results-type">';
if (data[i].type != null)
newResults += data[i].type;
newResults += '</div>';
newResults += '<div class="results-partno">';
if (data[i].partno != null)
newResults += data[i].partno;
newResults += '</div>';
newResults += '<div class="results-description">';
if (data[i].description != null)
newResults += data[i].description;
newResults += '</div>';
newResults += '<div class="results-notes">';
if (data[i].notes != null)
newResults += '<div class="tooltip"><i class="fa fa-sicky-note"></i><span class="tooltiptext">' + data[i].notes + '</span></div>';
//newResults += data[i].notes;
newResults += '</div>';
newResults += '<div class="results-datasheet">';
if (data[i].datasheet != null)
newResults += '<a href="https://www.elab.kth.se/parts/getfile/' + data[i].datasheet + '"><i class="fa fa-file-text" aria-hidden="true"></i></a>';
newResults += '</div>';
newResults += '</a>'
newResults += '</div>';
}
newResults += '</div>';
if(data.length == 0) {
newResults = '<div class="results">';
newResults += '<h3>No results.</h3>';
newResults += '</div>';
}
$('.results').replaceWith(newResults);
}).fail(function() {
var newResults = '<div class="results">';
newResults += '<h3>No results.</h3>';
newResults += '</div>';
$('.results').replaceWith(newResults);
console.log( "Query failed" );
});
});
$('.toggle-btn').on('change', function() {
// Re-perform query
});
//$('.results-row').on('click', );
});