Lines 1-110
Link Here
|
1 |
function deleteItemBlock(index) { |
1 |
function addItem( node, unique_item_fields ) { |
2 |
var aDiv = document.getElementById(index); |
2 |
var index = $(node).parent().attr('id'); |
3 |
aDiv.parentNode.removeChild(aDiv); |
3 |
var current_qty = parseInt($("#quantity").val()); |
4 |
var quantity = document.getElementById('quantity'); |
4 |
var max_qty; |
5 |
quantity.setAttribute('value',parseFloat(quantity.getAttribute('value'))-1); |
5 |
if($("#quantity_to_receive").length != 0){ |
|
|
6 |
max_qty = parseInt($("#quantity_to_receive").val()); |
7 |
} else { |
8 |
max_qty = 99999; |
9 |
} |
10 |
if ( $("#items_list table").find('tr[idblock="' + index + '"]').length == 0 ) { |
11 |
if ( current_qty < max_qty ) { |
12 |
if ( current_qty < max_qty - 1 ) |
13 |
cloneItemBlock(index, unique_item_fields); |
14 |
addItemInList(index, unique_item_fields); |
15 |
$("#" + index).find("a[name='buttonPlus']").text("Update"); |
16 |
$("#quantity").val(current_qty + 1); |
17 |
} else if ( current_qty >= max_qty ) { |
18 |
alert(window.MSG_ADDITEM_JS_CANT_RECEIVE_MORE_ITEMS |
19 |
|| "You can't receive any more items."); |
20 |
} |
21 |
} else { |
22 |
if ( current_qty < max_qty ) |
23 |
cloneItemBlock(index, unique_item_fields); |
24 |
var tr = constructTrNode(index); |
25 |
$("#items_list table").find('tr[idblock="' + index + '"]:first').replaceWith(tr); |
26 |
} |
27 |
$("#" + index).hide(); |
28 |
} |
29 |
|
30 |
function showItem(index) { |
31 |
$("#outeritemblock").children("div").each(function(){ |
32 |
if ( $(this).attr('id') == index ) { |
33 |
$(this).show(); |
34 |
} else { |
35 |
if ( $("#items_list table").find('tr[idblock="' + $(this).attr('id') + '"]').length == 0 ) { |
36 |
$(this).remove(); |
37 |
} else { |
38 |
$(this).hide(); |
39 |
} |
40 |
} |
41 |
}); |
42 |
} |
43 |
|
44 |
function constructTrNode(index, unique_item_fields) { |
45 |
var fields = ['barcode', 'homebranch', 'holdingbranch', 'notforloan', |
46 |
'restricted', 'location', 'itemcallnumber', 'copynumber', |
47 |
'stocknumber', 'ccode', 'itype', 'materials', 'itemnotes']; |
48 |
|
49 |
var result = "<tr idblock='" + index + "'>"; |
50 |
var edit_link = "<a href='#itemfieldset' style='text-decoration:none' onclick='showItem(\"" + index + "\");'>" |
51 |
+ (window.MSG_ADDITEM_JS_EDIT || "Edit") + "</a>"; |
52 |
var del_link = "<a style='cursor:pointer' " |
53 |
+ "onclick='deleteItemBlock(this, \"" + index + "\", \"" + unique_item_fields + "\");'>" |
54 |
+ (window.MSG_ADDITEM_JS_DELETE || "Delete") + "</a>"; |
55 |
result += "<td>" + edit_link + "</td>"; |
56 |
result += "<td>" + del_link + "</td>"; |
57 |
for(i in fields) { |
58 |
var field = fields[i]; |
59 |
var field_elt = $("#" + index) |
60 |
.find("[name='kohafield'][value='items."+field+"']") |
61 |
.prevAll("[name='field_value']")[0]; |
62 |
var field_value; |
63 |
if($(field_elt).is('select')) { |
64 |
field_value = $(field_elt).find("option:selected").text(); |
65 |
} else { |
66 |
field_value = $(field_elt).val(); |
67 |
} |
68 |
result += "<td>" + field_value + "</td>"; |
69 |
} |
70 |
result += "</tr>"; |
71 |
|
72 |
return result; |
73 |
} |
74 |
|
75 |
function addItemInList(index, unique_item_fields) { |
76 |
$("#items_list").show(); |
77 |
var tr = constructTrNode(index, unique_item_fields); |
78 |
$("#items_list table tbody").append(tr); |
79 |
} |
80 |
|
81 |
function deleteItemBlock(node_a, index, unique_item_fields) { |
82 |
$("#" + index).remove(); |
83 |
var current_qty = parseInt($("#quantity").val()); |
84 |
var max_qty; |
85 |
if($("#quantity_to_receive").length != 0) { |
86 |
max_qty = parseInt($("#quantity_to_receive").val()); |
87 |
} else { |
88 |
max_qty = 99999; |
89 |
} |
90 |
$("#quantity").val(current_qty - 1); |
91 |
$(node_a).parents('tr').remove(); |
92 |
if(current_qty - 1 == 0) |
93 |
$("#items_list").hide(); |
94 |
|
95 |
if ( $("#quantity").val() <= max_qty - 1) { |
96 |
if ( $("#outeritemblock").children("div :visible").length == 0 ) { |
97 |
$("#outeritemblock").children("div:last").show(); |
98 |
} |
99 |
} |
100 |
if ( $("#quantity").val() == 0 && $("#outeritemblock > div").length == 0) { |
101 |
cloneItemBlock(0, unique_item_fields); |
102 |
} |
6 |
} |
103 |
} |
7 |
function cloneItemBlock(index) { |
104 |
|
8 |
var original = document.getElementById(index); //original <div> |
105 |
function cloneItemBlock(index, unique_item_fields) { |
9 |
var clone = clone_with_selected(original) |
106 |
var original; |
|
|
107 |
if(index) { |
108 |
original = $("#" + index); //original <div> |
109 |
} |
110 |
var dont_copy_fields = new Array(); |
111 |
if(unique_item_fields) { |
112 |
var dont_copy_fields = unique_item_fields.split(' '); |
113 |
for(i in dont_copy_fields) { |
114 |
dont_copy_fields[i] = "items." + dont_copy_fields[i]; |
115 |
} |
116 |
} |
117 |
|
10 |
var random = Math.floor(Math.random()*100000); // get a random itemid. |
118 |
var random = Math.floor(Math.random()*100000); // get a random itemid. |
11 |
// set the attribute for the new 'div' subfields |
119 |
var clone = $("<div id='itemblock"+random+"'></div>") |
12 |
clone.setAttribute('id',index + random);//set another id. |
120 |
$.ajax({ |
13 |
var NumTabIndex; |
121 |
url: "/cgi-bin/koha/services/itemrecorddisplay.pl", |
14 |
NumTabIndex = parseInt(original.getAttribute('tabindex')); |
122 |
dataType: 'html', |
15 |
if(isNaN(NumTabIndex)) NumTabIndex = 0; |
123 |
data: { |
16 |
clone.setAttribute('tabindex',NumTabIndex+1); |
124 |
frameworkcode: 'ACQ' |
17 |
var CloneButtonPlus; |
125 |
}, |
18 |
var CloneButtonMinus; |
126 |
success: function(data, textStatus, jqXHR) { |
19 |
// try{ |
127 |
/* Create the item block */ |
20 |
var jclone = $(clone); |
128 |
$(clone).append(data); |
21 |
CloneButtonPlus = $("a.addItem", jclone).get(0); |
129 |
/* Change all itemid fields value */ |
22 |
CloneButtonPlus.setAttribute('onclick',"cloneItemBlock('" + index + random + "')"); |
130 |
$(clone).find("input[name='itemid']").each(function(){ |
23 |
CloneButtonMinus = $("a.delItem", jclone).get(0); |
131 |
$(this).val(random); |
24 |
CloneButtonMinus.setAttribute('onclick',"deleteItemBlock('" + index + random + "')"); |
132 |
}); |
25 |
CloneButtonMinus.setAttribute('style',"display:inline"); |
133 |
/* Add buttons + and Clear */ |
26 |
// change itemids of the clone |
134 |
var buttonPlus = '<a name="buttonPlus" style="cursor:pointer; margin:0 1em;" onclick="addItem(this,\'' + unique_item_fields + '\')">Add</a>'; |
27 |
var elems = clone.getElementsByTagName('input'); |
135 |
var buttonClear = '<a name="buttonClear" style="cursor:pointer;" onclick="clearItemBlock(this)">' + (window.MSG_ADDITEM_JS_CLEAR || 'Clear') + '</a>'; |
28 |
for( i = 0 ; elems[i] ; i++ ) |
136 |
$(clone).append(buttonPlus).append(buttonClear); |
29 |
{ |
137 |
/* Copy values from the original block (input) */ |
30 |
if(elems[i].name.match(/^itemid/)) { |
138 |
$(original).find("input[name='field_value']").each(function(){ |
31 |
elems[i].value = random; |
139 |
var kohafield = $(this).siblings("input[name='kohafield']").val(); |
|
|
140 |
if($(this).val() && dont_copy_fields.indexOf(kohafield) == -1) { |
141 |
$(this).parent("div").attr("id").match(/^(subfield.)/); |
142 |
var id = RegExp.$1; |
143 |
var value = $(this).val(); |
144 |
$(clone).find("div[id^='"+id+"'] input[name='field_value']").val(value); |
145 |
} |
146 |
}); |
147 |
/* Copy values from the original block (select) */ |
148 |
$(original).find("select[name='field_value']").each(function(){ |
149 |
var kohafield = $(this).siblings("input[name='kohafield']").val(); |
150 |
if($(this).val() && dont_copy_fields.indexOf(kohafield) == -1) { |
151 |
$(this).parent("div").attr("id").match(/^(subfield.)/); |
152 |
var id = RegExp.$1; |
153 |
var value = $(this).val(); |
154 |
$(clone).find("div[id^='"+id+"'] select[name='field_value']").val(value); |
155 |
} |
156 |
}); |
157 |
|
158 |
$("#outeritemblock").append(clone); |
32 |
} |
159 |
} |
33 |
} |
160 |
}); |
34 |
// } |
|
|
35 |
//catch(e){ // do nothig if ButtonPlus & CloneButtonPlus don't exist. |
36 |
//} |
37 |
// insert this line on the page |
38 |
original.parentNode.insertBefore(clone,original.nextSibling); |
39 |
var quantity = document.getElementById('quantity'); |
40 |
quantity.setAttribute('value',parseFloat(quantity.getAttribute('value'))+1); |
41 |
} |
161 |
} |
42 |
function check_additem() { |
162 |
|
43 |
var barcodes = document.getElementsByName('barcode'); |
163 |
function clearItemBlock(node) { |
44 |
var success = true; |
164 |
var index = $(node).parent().attr('id'); |
45 |
for(i=0;i<barcodes.length;i++){ |
165 |
var block = $("#"+index); |
46 |
for(j=0;j<barcodes.length;j++){ |
166 |
$(block).find("input[type='text']").each(function(){ |
47 |
if( (i > j) && (barcodes[i].value == barcodes[j].value) && barcodes[i].value !='') { |
167 |
$(this).val(""); |
48 |
barcodes[i].className='error'; |
168 |
}); |
49 |
barcodes[j].className='error'; |
169 |
$(block).find("select").each(function(){ |
50 |
success = false; |
170 |
$(this).find("option:first").attr("selected", true); |
51 |
} |
171 |
}); |
52 |
} |
172 |
} |
53 |
} |
173 |
|
54 |
// TODO : Add AJAX function to test against barcodes already in the database, not just |
174 |
function check_additem(unique_item_fields) { |
55 |
// duplicates within the form. |
175 |
var success = true; |
56 |
return success; |
176 |
var data = new Object(); |
|
|
177 |
data['field'] = new Array(); |
178 |
data['value'] = new Array(); |
179 |
var array_fields = unique_item_fields.split(' '); |
180 |
$(".error").empty(); // Clear error div |
181 |
|
182 |
// Check if a value is duplicated in form |
183 |
for ( field in array_fields ) { |
184 |
var fieldname = array_fields[field]; |
185 |
var values = new Array(); |
186 |
$("[name='kohafield'][value=items."+array_fields[field]+"]").each(function(){ |
187 |
var input = $(this).prevAll("input[name='field_value']")[0]; |
188 |
if($(input).val()) { |
189 |
values.push($(input).val()); |
190 |
data['field'].push(fieldname); |
191 |
data['value'].push($(input).val()); |
192 |
} |
193 |
}); |
194 |
|
195 |
var sorted_arr = values.sort(); |
196 |
for (var i = 0; i < sorted_arr.length - 1; i += 1) { |
197 |
if (sorted_arr[i + 1] == sorted_arr[i]) { |
198 |
$(".error").append( |
199 |
fieldname + " '" + sorted_arr[i] + "' " |
200 |
+ (window.MSG_ADDITEM_JS_IS_DUPLICATE || "is duplicated") |
201 |
+ "<br/>"); |
202 |
success = false; |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
// If there is a duplication, we raise an error |
208 |
if ( success == false ) { |
209 |
$(".error").show(); |
210 |
return false; |
211 |
} |
212 |
|
213 |
$.ajax({ |
214 |
url: '/cgi-bin/koha/acqui/check_uniqueness.pl', |
215 |
async: false, |
216 |
dataType: 'json', |
217 |
data: data, |
218 |
success: function(data) { |
219 |
for (field in data) { |
220 |
success = false; |
221 |
for (var i=0; i < data[field].length; i++) { |
222 |
var value = data[field][i]; |
223 |
$(".error").append( |
224 |
field + " '" + value + "' " |
225 |
+ (window.MSG_ADDITEM_JS_ALREADY_EXISTS_IN_DB |
226 |
|| "already exists in database") |
227 |
+ "<br />" |
228 |
); |
229 |
} |
230 |
} |
231 |
} |
232 |
}); |
233 |
|
234 |
if ( success == false ) { |
235 |
$(".error").show(); |
236 |
} |
237 |
return success; |
57 |
} |
238 |
} |
58 |
|
239 |
|
59 |
function clone_with_selected (node) { |
|
|
60 |
var origin = node.getElementsByTagName("select"); |
61 |
var tmp = node.cloneNode(true) |
62 |
var selectelem = tmp.getElementsByTagName("select"); |
63 |
for (var i=0; i<origin.length; i++) { |
64 |
selectelem[i].selectedIndex = origin[i].selectedIndex; |
65 |
} |
66 |
origin = null; |
67 |
selectelem = null; |
68 |
return tmp; |
69 |
} |
70 |
|
71 |
$(document).ready(function(){ |
72 |
$(".cloneItemBlock").click(function(){ |
73 |
var clonedRow = $(this).parent().parent().clone(true); |
74 |
clonedRow.insertAfter($(this).parent().parent()).find("a.deleteItemBlock").show(); |
75 |
// find ID of cloned row so we can increment it for the clone |
76 |
var count = $("input[id^=volinf]",clonedRow).attr("id"); |
77 |
var current = Number(count.replace("volinf","")); |
78 |
var increment = current + 1; |
79 |
// loop over inputs |
80 |
var inputs = ["volinf","barcode"]; |
81 |
jQuery.each(inputs,function() { |
82 |
// increment IDs of labels and inputs in the clone |
83 |
$("label[for="+this+current+"]",clonedRow).attr("for",this+increment); |
84 |
$("input[name="+this+"]",clonedRow).attr("id",this+increment); |
85 |
}); |
86 |
// loop over selects |
87 |
var selects = ["homebranch","location","itemtype","ccode"]; |
88 |
jQuery.each(selects,function() { |
89 |
// increment IDs of labels and selects in the clone |
90 |
$("label[for="+this+current+"]",clonedRow).attr("for",this+increment); |
91 |
$("input[name="+this+"]",clonedRow).attr("id",this+increment); |
92 |
$("select[name="+this+"]",clonedRow).attr("id",this+increment); |
93 |
// find the selected option and select it in the clone |
94 |
var selectedVal = $("select#"+this+current).find("option:selected").attr("value"); |
95 |
$("select[name="+this+"] option[value="+selectedVal+"]",clonedRow).attr("selected","selected"); |
96 |
}); |
97 |
|
98 |
var quantityrec = parseFloat($("#quantityrec").attr("value")); |
99 |
quantityrec++; |
100 |
$("#quantityrec").attr("value",quantityrec); |
101 |
return false; |
102 |
}); |
103 |
$(".deleteItemBlock").click(function(){ |
104 |
$(this).parent().parent().remove(); |
105 |
var quantityrec = parseFloat($("#quantityrec").attr("value")); |
106 |
quantityrec--; |
107 |
$("#quantityrec").attr("value",quantityrec); |
108 |
return false; |
109 |
}); |
110 |
}); |