-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathdistributions_and_transfers.js
121 lines (110 loc) · 3.49 KB
/
distributions_and_transfers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import $ from 'jquery';
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
function new_option(item, selected) {
if (selected == null) {
selected = false;
}
let content = `<option value="${item.item_id}"`;
if (selected) {
content += " selected";
}
content += ">";
content += item.item_name;
if (
$("select.storage-location-source").attr("id") !==
"audit_storage_location_id"
) {
content += ` (${item.quantity})`;
}
content += "</option>\n";
return content;
};
// Workaround to refresh item dropdown results for select2.
function rerenderDropdown(element) {
const oldScrollTop = element.data('select2').$results.scrollTop();
element.select2('close').select2('open');
element.data('select2').$results.scrollTop(oldScrollTop);
}
function populate_dropdowns(objects, inventory) {
objects.each(function(_, element) {
const selected = Number(
$(element)
.find(":selected")
.val()
);
let options = "";
$.each(inventory, function(index) {
const item_id = Number(inventory[index].item_id);
options += new_option(inventory[index], selected === item_id);
});
$(element)
.find("option")
.remove()
.end()
.append(options);
// If this select element is currently open, the option list is
// now stale and needs to be refreshed.
if ($(element).data('select2')?.isOpen()) {
rerenderDropdown($(element))
}
});
}
function request_storage_location_and_populate_item(item_to_populate) {
const control = $("select.storage-location-source");
if (control.length > 0 && control.val() !== "") {
return $.ajax({
url: control
.data("storage-location-inventory-path")
.replace(":id", control.val()),
dataType: "json",
success(data) {
return populate_dropdowns($(item_to_populate), data);
}
});
}
};
$(function() {
let control = $("select.storage-location-source");
const storage_location_required =
$("form.storage-location-required").length > 0;
const default_item = $(".line-item-fields select");
$(document).on("change", "select.storage-location-source", function() {
const default_item = $(".line-item-fields select");
control = $("select.storage-location-source");
if (storage_location_required && !control.val()) {
$("#__add_line_item").addClass("disabled");
}
if (storage_location_required && control.val()) {
$("#__add_line_item").removeClass("disabled");
}
request_storage_location_and_populate_item(default_item);
});
$(document).on(
"form-input-after-insert",
"form.storage-location-required",
function(e) {
const insertedItem = $(e.detail);
request_storage_location_and_populate_item($("select", insertedItem));
insertedItem
.find("input.__barcode_item_lookup")
.attr("id", `_barcode-lookup-${$(".nested-fields").length - 1}`);
control = $("select.storage-location-source");
$.ajax({
url: control
.data("storage-location-inventory-path")
.replace(":id", control.val()),
dataType: "json",
success(data) {
return populate_dropdowns($("select", insertedItem), data);
}
});
}
);
$(function() {
if (storage_location_required && !control.val()) {
$("#__add_line_item").addClass("disabled");
}
request_storage_location_and_populate_item(default_item);
});
});