mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
(function () {
|
|
|
|
window.initLineItemAutocomplete = function(context) {
|
|
let scope = context || document;
|
|
$(scope).find(".line-item-description").each(function() {
|
|
if ($(this).data("autocomplete-initialized")) return;
|
|
$(this).data("autocomplete-initialized", true);
|
|
|
|
$(this).autocomplete({
|
|
appendTo: "body",
|
|
minLength: 2,
|
|
source: function(request, response) {
|
|
$.getJSON("/items/autocomplete", { q: request.term })
|
|
.done(function(data) {
|
|
response(data.map(function(item) {
|
|
return {
|
|
label: item.text,
|
|
value: item.text,
|
|
id: item.id,
|
|
price: item.price || 0
|
|
};
|
|
}));
|
|
})
|
|
.fail(function(err){
|
|
console.error("Autocomplete error:", err);
|
|
response([]);
|
|
});
|
|
},
|
|
select: function(event, ui) {
|
|
let $input = $(this);
|
|
let row = $input.closest(".line-item");
|
|
|
|
$input.val(ui.item.value); // <-- set description
|
|
row.find(".item-id-field").val(ui.item.id);
|
|
|
|
if (ui.item.price !== undefined && row.find(".price-field").length) {
|
|
row.find(".price-field").val(ui.item.price);
|
|
}
|
|
|
|
updateLineItemTotals();
|
|
|
|
return false; // still prevent default to avoid double entry
|
|
},
|
|
change: function(event, ui) {
|
|
if (!ui.item) {
|
|
let row = $(this).closest(".line-item");
|
|
row.find(".item-id-field").val("");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
// Clear item_id when user types manually
|
|
$(document).on("input", ".line-item-description", function(){
|
|
let row = $(this).closest(".line-item");
|
|
row.find(".item-id-field").val("");
|
|
});
|
|
|
|
function initializeAutocomplete() {
|
|
window.initLineItemAutocomplete(document);
|
|
}
|
|
|
|
$(document).ready(initializeAutocomplete);
|
|
document.addEventListener("turbo:load", initializeAutocomplete);
|
|
|
|
})(); |