2011-04-12 23:37:34 -07:00
|
|
|
$(function() {
|
|
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
resetForm();
|
2011-04-21 00:38:01 -07:00
|
|
|
|
2011-04-28 21:33:36 -07:00
|
|
|
var addedBoxes = 0;
|
|
|
|
|
|
2011-04-21 00:38:01 -07:00
|
|
|
$( "#dialog-form" ).dialog({
|
|
|
|
|
autoOpen: false,
|
2011-04-28 00:41:36 -07:00
|
|
|
height: 900,
|
|
|
|
|
width: 600,
|
2011-04-21 00:38:01 -07:00
|
|
|
modal: true,
|
|
|
|
|
buttons: {
|
|
|
|
|
"New Shipment": function() {
|
2011-04-28 20:07:30 -07:00
|
|
|
var thisShipmentInputs = $('#ShipmentAddForm').find('input,select,textarea');
|
2011-04-28 00:41:36 -07:00
|
|
|
|
|
|
|
|
$.post('/shipments/ajax_edit', thisShipmentInputs, function(data) {
|
|
|
|
|
$( "#dialog-form" ).dialog('close');
|
2011-04-28 20:07:30 -07:00
|
|
|
window.location.reload(); //Change this to fetch a new copy of the table and update a <div>.
|
2011-04-28 00:41:36 -07:00
|
|
|
});
|
2011-04-21 00:38:01 -07:00
|
|
|
|
|
|
|
|
},
|
|
|
|
|
Cancel: function() {
|
|
|
|
|
$( this ).dialog( "close" );
|
2011-04-28 00:41:36 -07:00
|
|
|
$("#ShipmentAddForm")[0].reset();
|
|
|
|
|
$("#principlesList").empty();
|
2011-04-21 00:38:01 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
close: function() {
|
2011-04-28 00:41:36 -07:00
|
|
|
|
|
|
|
|
$("#importFields").hide();
|
|
|
|
|
$("#principle").hide();
|
|
|
|
|
|
2011-04-21 00:38:01 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$( "#newShipment" ).button().click(function() {
|
|
|
|
|
|
|
|
|
|
$( "#dialog-form" ).dialog( "open" );
|
|
|
|
|
|
|
|
|
|
});
|
2011-04-28 00:41:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$('#date_arrived_display').datepicker({
|
|
|
|
|
showButtonPanel: true,
|
|
|
|
|
dateFormat: 'd M yy',
|
|
|
|
|
altFormat: 'yy-mm-dd',
|
|
|
|
|
altField: '#date_arrived'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
/**
|
|
|
|
|
* Display the relevent elements depending on this Shipment Type */
|
2011-04-28 00:41:36 -07:00
|
|
|
$("#shipmentType").change(function() {
|
|
|
|
|
var shipmentTypeID = getSelectedID('#shipmentType');
|
2011-04-28 20:07:30 -07:00
|
|
|
shipmentTypeID = parseInt(shipmentTypeID);
|
|
|
|
|
$(".hiddenDefault").hide();
|
|
|
|
|
|
|
|
|
|
switch(shipmentTypeID) {
|
|
|
|
|
case 1:
|
|
|
|
|
$("#importFields, #principle, #purchaseOrder, #freight_forwarder, #airway_bill, #boxes").show();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2011-04-28 00:41:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//Remove X button clicked.
|
|
|
|
|
$('.removeFromList').live('click', function() {
|
|
|
|
|
$(this).parent().remove();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$('#addPrinciple').button().click(function() {
|
|
|
|
|
|
|
|
|
|
var principleID = getSelectedID('#PrinciplePrinciple');
|
|
|
|
|
|
|
|
|
|
if($('#PrincipleID_'+principleID).length > 0) { //This Principle is already in the List. Don't do anything.'
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var principleName =getSelectedText('#PrinciplePrinciple');
|
|
|
|
|
|
|
|
|
|
//addPrincipleToList(principleID);
|
|
|
|
|
addToList('Principle', principleID, principleName, $('#principlesList') );
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$( "#purchase_order_autocomplete" ).autocomplete({
|
|
|
|
|
source: "/purchase_orders/autocomplete",
|
|
|
|
|
minLength: 2,
|
|
|
|
|
select: function( event, ui ) {
|
|
|
|
|
//alert(ui.item.value + ":" + ui.item.id);
|
|
|
|
|
//alert($('#PurchaseOrderID_'+ui.item.id).length);
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
if($('#PurchaseOrderID_'+ui.item.id).length == 0) { //This PO is already in the List. Don't do anything.'
|
2011-04-28 00:41:36 -07:00
|
|
|
addToList('PurchaseOrder', ui.item.id, ui.item.value, $('#purchaseOrdersList'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$( "#job_autocomplete" ).autocomplete({
|
|
|
|
|
source: "/jobs/autocomplete",
|
|
|
|
|
minLength: 2,
|
|
|
|
|
select: function( event, ui ) {
|
|
|
|
|
//alert(ui.item.value + ":" + ui.item.id);
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
if($('#JobID_'+ui.item.id).length == 0) { //This Job is already in the List. Don't do anything.'
|
2011-04-28 00:41:36 -07:00
|
|
|
addToList('Job', ui.item.id, ui.item.value, $('#jobsList'));
|
2011-04-28 20:07:30 -07:00
|
|
|
}
|
2011-04-28 00:41:36 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
|
|
|
|
|
$("#addBox").button().click(function() {
|
2011-04-28 21:33:36 -07:00
|
|
|
addBox(addedBoxes);
|
|
|
|
|
addedBoxes++;
|
2011-04-28 20:07:30 -07:00
|
|
|
});
|
|
|
|
|
|
2011-04-28 21:33:36 -07:00
|
|
|
$(".removeBox").live('click',function() {
|
|
|
|
|
$(this).parent().remove();
|
2011-04-28 20:07:30 -07:00
|
|
|
});
|
|
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
/**
|
|
|
|
|
* Resets the Form to its default site
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function resetForm() {
|
|
|
|
|
|
|
|
|
|
$(".hiddenDefault").hide();
|
|
|
|
|
$("#ShipmentAddForm")[0].reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 21:33:36 -07:00
|
|
|
function addBox(addedBoxes) {
|
|
|
|
|
|
|
|
|
|
var model = 'Box';
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
var newBoxForm = $("#boxFactory").clone();
|
2011-04-28 21:33:36 -07:00
|
|
|
|
|
|
|
|
var boxNo = addedBoxes;
|
2011-04-28 20:07:30 -07:00
|
|
|
|
|
|
|
|
newBoxForm.attr('id','');
|
2011-04-28 21:33:36 -07:00
|
|
|
newBoxForm.addClass('boxForm');
|
2011-04-28 20:07:30 -07:00
|
|
|
newBoxForm.show();
|
|
|
|
|
|
2011-04-28 21:33:36 -07:00
|
|
|
//Set the appropriate naming for the fields so cake can work its magic.
|
|
|
|
|
//Should refactor into a function.
|
|
|
|
|
var div = newBoxForm.find('.length');
|
|
|
|
|
var ID = getCakeID(model,boxNo,'length');
|
|
|
|
|
var name = getCakeName(model, boxNo, 'length');
|
|
|
|
|
div.children('label').attr('for', ID);
|
|
|
|
|
div.children('input').attr('id', ID).attr('name', name);
|
|
|
|
|
|
|
|
|
|
div = newBoxForm.find('.width');
|
|
|
|
|
ID = getCakeID(model,boxNo,'width');
|
|
|
|
|
name = getCakeName(model, boxNo, 'width');
|
|
|
|
|
div.children('label').attr('for', ID);
|
|
|
|
|
div.children('input').attr('id', ID).attr('name', name);
|
|
|
|
|
|
|
|
|
|
div = newBoxForm.find('.height');
|
|
|
|
|
ID = getCakeID(model,boxNo,'height');
|
|
|
|
|
name = getCakeName(model, boxNo, 'height');
|
|
|
|
|
div.children('label').attr('for', ID);
|
|
|
|
|
div.children('input').attr('id', ID).attr('name', name);
|
|
|
|
|
|
|
|
|
|
div = newBoxForm.find('.weight');
|
|
|
|
|
ID = getCakeID(model,boxNo,'weight');
|
|
|
|
|
name = getCakeName(model, boxNo, 'weight');
|
|
|
|
|
div.children('label').attr('for', ID);
|
|
|
|
|
div.children('input').attr('id', ID).attr('name', name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
var closeButton = $('<button>X</button>');
|
|
|
|
|
closeButton.addClass('removeBox');
|
|
|
|
|
closeButton.button();
|
|
|
|
|
|
|
|
|
|
newBoxForm.prepend(closeButton);
|
|
|
|
|
|
|
|
|
|
$("#boxes").append(newBoxForm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 21:33:36 -07:00
|
|
|
function getCakeID(model, count, field) {
|
|
|
|
|
return model+count+capitalizeFirstLetter(field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCakeName(model, count, field) {
|
|
|
|
|
return 'data['+model+']['+count+']['+field+']';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capitalizeFirstLetter(string) {
|
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A more generic way of handling the HABTM <ul> <li>[REMOVE BUTTON] NAME [HIDDEN INPUT]</li> </ul>
|
|
|
|
|
*/
|
|
|
|
|
function addToList(modelName, id, value, ULelement) {
|
|
|
|
|
var thisLI = $('<li></li>');
|
|
|
|
|
var thisButton = $('<button>X</button>');
|
|
|
|
|
thisButton.addClass('removeFromList');
|
|
|
|
|
thisButton.button();
|
|
|
|
|
|
|
|
|
|
var thisHiddenInput = $('<input type="hidden">');
|
|
|
|
|
|
|
|
|
|
var modelString = '['+modelName+']';
|
2011-04-28 21:33:36 -07:00
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
thisHiddenInput.attr('name', 'data'+modelString+modelString+'[]');
|
2011-04-28 21:33:36 -07:00
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
thisHiddenInput.attr('value', id);
|
|
|
|
|
|
|
|
|
|
thisLI.attr('id', modelName+'ID_'+id);
|
|
|
|
|
thisLI.html(value);
|
|
|
|
|
thisLI.prepend(thisButton);
|
|
|
|
|
thisLI.append(thisHiddenInput);
|
|
|
|
|
ULelement.append(thisLI);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getSelectedID(elementID) {
|
|
|
|
|
var id;
|
|
|
|
|
$(elementID+" :selected").each(function(i, selected) {
|
|
|
|
|
id = this.value;
|
|
|
|
|
});
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSelectedText(elementID) {
|
|
|
|
|
var text;
|
|
|
|
|
$(elementID+" :selected").each(function(i, selected) {
|
|
|
|
|
text = $(selected).text();
|
|
|
|
|
});
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 20:07:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 00:41:36 -07:00
|
|
|
|
2011-04-21 00:38:01 -07:00
|
|
|
});
|