bookinventory/resources/js/app.js

156 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2020-12-26 10:46:23 +00:00
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
require('./bootstrap');
$(document).ready(function(e) {
var authorOptions = {
placeholder: 'Authors',
tags: true,
ajax: {
url: '/authors/search',
dataType: 'json'
}
};
var locationOptions = {
placeholder: 'Location',
tags: true
};
$('#add-form .select2-author').select2(authorOptions);
$('#add-form .select2-location').select2(locationOptions);
$('#add-form').on('click', '.remove-row', function(e) {
e.preventDefault();
$(this).closest('.form-row').remove();
});
$('#add-form').on('keydown', '.barcode_input', function(e) {
if (e.keyCode === 13) {
e.preventDefault();
}
});
$('#add-form').on('keyup', '.barcode_input', function (e) {
if (e.keyCode === 13) {
// Do something
e.preventDefault();
var $this = $(this),
$row = $this.closest('.form-row'),
barcodeValue = $this.val();
if (barcodeValue === '') {
return;
}
$.get('/lookup/' + barcodeValue, function(res) {
2020-12-27 00:34:07 +00:00
if (res.warning) {
bootbox.alert({
title: 'Warning',
message: res.warning,
callback: function() {
$('.barcode_input').filter(function() {
return !this.value;
}).focus();
}
});
}
2020-12-26 10:46:23 +00:00
2020-12-27 00:34:07 +00:00
if (res.success) {
$row.find('input[name*=name]').val(res.data.title);
2020-12-26 10:46:23 +00:00
2020-12-27 00:34:07 +00:00
var $authors = $row.find('.select2-author');
2020-12-26 10:46:23 +00:00
2020-12-27 00:34:07 +00:00
$authors.children('option').remove();
2020-12-26 10:46:23 +00:00
2020-12-27 00:34:07 +00:00
for (var i = 0; i < res.data.authors.length; i++) {
$authors.append($('<option>', {value: res.data.authors[i], text: res.data.authors[i], selected: 'selected'}));
}
$authors.trigger('change');
}
2020-12-26 10:46:23 +00:00
}, 'json');
var count = emptyRowCount();
if (count < 1) {
var firstIndex = 0,
$container = $row.closest('.row-container');
for (var i = 0; i < 200; i++) {
if ($container.find('.form-row[data-index=' + i + ']').length < 1) {
firstIndex = i;
break;
}
}
var $template = $('#add-template'),
$clone = $template.clone();
$clone.attr('data-index', firstIndex);
$clone.find('input, select').each(function() {
$(this).attr('name', $(this).attr('name').replace('__INDEX__', firstIndex));
});
$clone.removeAttr('id');
$clone.find('input[type=text]').val('');
$clone.removeClass('invisible').addClass('form-row');
$container.append($clone);
setTimeout(function() {
$clone.find('.barcode_input').focus();
$clone.find('.select2-author').select2(authorOptions);
}, 150);
}
}
});
2020-12-27 00:34:07 +00:00
$('.remove-item').click(function(e) {
e.preventDefault();
var $this = $(this),
2020-12-27 09:43:58 +00:00
$row = $this.closest('tr, .action-container');
2020-12-27 00:34:07 +00:00
bootbox.confirm('Are you sure?', function(result) {
if (!result) {
return;
}
axios.delete($row.data('url')).then(function(res) {
if (res.data.success) {
2020-12-27 09:43:58 +00:00
if ($row.hasClass('action-container')) {
window.location = '/';
} else {
$row.remove();
}
2020-12-27 00:34:07 +00:00
}
});
})
})
2020-12-26 10:46:23 +00:00
});
function emptyRowCount() {
var $barcodes = $('#add-form .barcode_input');
var count = 0;
$barcodes.each(function() {
if ($(this).val() == '') {
count++;
}
});
return count;
}