Add editing shipment working better. Details to work out

This commit is contained in:
Karl Cordes 2011-05-02 13:11:32 +10:00
parent fdee1dd0df
commit d3d3b9eb86
8 changed files with 527 additions and 366 deletions

View file

@ -34,12 +34,40 @@ class ShipmentsController extends AppController {
$this->set('shipment', $this->Shipment->read(null, $id));
}
function add() {
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort'));
}
function edit($id = null) {
if(isset($id)) {
if (empty($this->data)) {
$this->data = $this->Shipment->read(null, $id);
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort'));
}
}
}
function ajax_edit() {
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->Shipment->create();
//$this->Shipment->create($this->data);
if ($this->Shipment->save($this->data)) {
if ($this->Shipment->saveAll($this->data)) {
echo "SUCCESS";
}
else {
@ -56,19 +84,24 @@ class ShipmentsController extends AppController {
function ajax_add() {
$this->layout = 'ajax';
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->Shipment->create($this->data);
if ($this->Shipment->saveAll($this->data)) {
echo "SUCCESS";
}
else {
echo "FAILURE";
}
$this->Shipment->create();
if ($this->Shipment->save($this->data)) {
echo "SUCCESS";
}
else {
echo "FAILURE";
echo "NO-DATA";
}
}
/* Moved the Views for Adding/Editing to elements. Called from the Index.

View file

@ -2,13 +2,13 @@
class ShipmentInvoice extends AppModel {
var $name = 'ShipmentInvoice';
var $validate = array(
/*var $validate = array(
'shipment_id' => array('numeric'),
'principle_id' => array('numeric'),
'currency_id' => array('numeric'),
'invoice_number' => array('notempty'),
'approved' => array('numeric')
);
);*/
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(

View file

@ -1,8 +1,10 @@
<?php echo $form->create('Shipment', array('default'=>false));?>
<?php
echo $form->input('user_id', array('type'=>'hidden', 'value'=>$currentuser['User']['id']));
echo $form->input('type', array('options'=>$shipmentType, 'id'=>'shipmentType'));
echo $form->input('id', array('type'=>'hidden'));
?>
<div id="importFields" class="hiddenDefault">
<?
@ -82,7 +84,6 @@ echo $form->input('type', array('options'=>$shipmentType, 'id'=>'shipmentType'))
<?php echo $form->end();?>
<div id="boxFactory" class="hiddenDefault">
<?=$form->input('Box.0.length', array('label'=>'Length (cm)', 'div'=>'length'));?>
<?=$form->input('Box.0.width', array('label'=>'Width (cm)', 'div'=>'width'));?>

View file

@ -1,36 +1,2 @@
<?=$javascript->link('shipment_add');?>
<?php echo $form->create('Shipment');?>
<?php
echo $form->input('shipmentType', array('options'=>$shipmentType, 'id'=>'shipmentType'));
?>
<div id="importFields">
<?
echo $form->input('date_arrived_display', array('label'=>'Date Arrived', 'id'=>'date_arrived_display'));
echo $form->input('date_arrived', array('type'=>'hidden', 'id'=>'date_arrived'));
?>
</div>
<div id="principle">
<? echo $form->input('Principle', array('type'=>'select', 'multiple'=>true)); ?>
</div>
<?
echo $form->input('PurchaseOrder');
echo $form->input('Job');
echo $form->input('freight_forwarder_id');
echo $form->input('airway_bill');
echo $form->input('date_dispatched');
echo $form->input('comments');
?>
<?php echo $form->end();?>
<?=$javascript->link('add_edit_shipment');?>
<?=$this->element('add_shipment');?>

View file

@ -1,37 +1,129 @@
<div class="shipments form">
<?php echo $form->create('Shipment');?>
<fieldset>
<legend><?php __('Edit Shipment');?></legend>
<?php
echo $form->input('id');
echo $form->input('box_count');
echo $form->input('freight_forwarder_id');
echo $form->input('date_arrived');
echo $form->input('date_dispatched');
echo $form->input('airway_bill');
echo $form->input('comments');
echo $form->input('Job');
echo $form->input('Principle');
echo $form->input('PurchaseOrder');
?>
</fieldset>
<?php echo $form->end('Submit');?>
<?=$javascript->link('add_edit_shipment');?>
<?php echo $form->create('Shipment', array('default'=>false));?>
<?php
echo $form->input('user_id', array('type'=>'hidden', 'value'=>$currentuser['User']['id']));
echo $form->input('type', array('options'=>$shipmentType, 'id'=>'shipmentType'));
echo $form->input('id', array('type'=>'hidden'));
?>
<div id="importFields" class="hiddenDefault">
<?
echo $form->input('date_arrived_display', array('label'=>'Date Arrived', 'id'=>'date_arrived_display', 'value'=>$this->element('isEmptyDate', array('date'=>$this->data['Shipment']['date_arrived']))));
echo $form->input('date_arrived', array('type'=>'hidden', 'id'=>'date_arrived'));
?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('Delete', true), array('action' => 'delete', $form->value('Shipment.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('Shipment.id'))); ?></li>
<li><?php echo $html->link(__('List Shipments', true), array('action' => 'index'));?></li>
<li><?php echo $html->link(__('List Freight Forwarders', true), array('controller' => 'freight_forwarders', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Freight Forwarder', true), array('controller' => 'freight_forwarders', 'action' => 'add')); ?> </li>
<li><?php echo $html->link(__('List Boxes', true), array('controller' => 'boxes', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Box', true), array('controller' => 'boxes', 'action' => 'add')); ?> </li>
<li><?php echo $html->link(__('List Shipment Invoices', true), array('controller' => 'shipment_invoices', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Shipment Invoice', true), array('controller' => 'shipment_invoices', 'action' => 'add')); ?> </li>
<li><?php echo $html->link(__('List Jobs', true), array('controller' => 'jobs', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Job', true), array('controller' => 'jobs', 'action' => 'add')); ?> </li>
<li><?php echo $html->link(__('List Principles', true), array('controller' => 'principles', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Principle', true), array('controller' => 'principles', 'action' => 'add')); ?> </li>
<li><?php echo $html->link(__('List Purchase Orders', true), array('controller' => 'purchase_orders', 'action' => 'index')); ?> </li>
<li><?php echo $html->link(__('New Purchase Order', true), array('controller' => 'purchase_orders', 'action' => 'add')); ?> </li>
</ul>
<div id="dateDispatched" class="hiddenDefault">
<?
echo $form->input('date_dispatched_display', array('label'=>'Date Dispatched', 'id'=>'date_dispatched_display', 'value'=>$this->element('isEmptyDate', array('date'=>$this->data['Shipment']['date_dispatched']))));
echo $form->input('date_dispatched', array('type'=>'hidden', 'id'=>'date_dispatched'));
?>
</div>
<div id="principle" class="hiddenDefault">
<? echo $form->input('Principle', array('name'=>'PrincipleSelect', 'type'=>'select', 'multiple'=>false)); ?>
<button id="addPrinciple">Add Principle</button>
<ul id="principlesList" class="habtmList">
<?php foreach($this->data['Principle'] as $principle):?>
<li id="PrincipleID_<?=$principle['id']?>">
<button class="removeFromList">X</button>
<?=$principle['name'];?>
<input type="hidden" name="data[Principle][Principle[]" value="<?=$principle['id']?>">
</li>
<?php endforeach?>
</ul>
</div>
<div id="purchaseOrder" class="hiddenDefault">
<?
echo $form->input('purchase_order', array('id'=>'purchase_order_autocomplete'));?>
<ul id="purchaseOrdersList" class="habtmList">
<?php foreach($this->data['PurchaseOrder'] as $po):?>
<li id="PurchaseOrderID_<?=$po['id']?>">
<button class="removeFromList">X</button>
<?=$po['title'];?>
<input type="hidden" name="data[PurchaseOrder][PurchaseOrder[]" value="<?=$po['id']?>">
</li>
<?php endforeach?>
</ul>
</div>
<div id="job" class="hiddenDefault">
<?
echo $form->input('job', array('id'=>'job_autocomplete'));
?>
<ul id="jobsList" class="habtmList">
<?php foreach($this->data['Job'] as $job):?>
<li id="JobID_<?=$po['job']?>">
<button class="removeFromList">X</button>
<?=$job['title'];?>
<input type="hidden" name="data[PurchaseOrder][PurchaseOrder[]" value="<?=$job['id']?>">
</li>
<?php endforeach?>
</ul>
</div>
<div id="customer" class="hiddenDefault">
<?=$form->input('customer', array('id'=>'customer_autocomplete', 'label'=>'Customer'));?>
<?=$form->input('customer_id', array('type'=>'hidden', 'id'=>'customer_id'));?>
</div>
<div id="addressSpan" class="hiddenDefault">
<span>Shipping Address:</span>
</div>
<div id="address" class="hiddenDefault">
</div>
<div id="freight_forwarder" class="hiddenDefault">
<?=$form->input('freight_forwarder_id');?>
</div>
<div id="airway_bill" class="hiddenDefault">
<?=$form->input('airway_bill');?>
</div>
<div id="boxes" class="hiddenDefault">
<button id="addBox">Add Box</button>
<?php $boxNo = 0;?>
<?php foreach($this->data['Box'] as $box):?>
<div id="" class="boxForm">
<button class="removeBox">X</button>
<?=$form->input("Box.{$boxNo}.length", array('label'=>'Length (cm)', 'div'=>'length'));?>
<?=$form->input("Box.{$boxNo}.width", array('label'=>'Width (cm)', 'div'=>'width'));?>
<?=$form->input("Box.{$boxNo}.height", array('label'=>'Height (cm)', 'div'=>'height'));?>
<?=$form->input("Box.{$boxNo}.weight", array('label'=>'Weight (kg)', 'div'=>'weight'));?>
</div>
<?php
$boxNo++;
endforeach;?>
</div>
<div id="comments" class="hiddenDefault">
<?=$form->input('comments');?>
</div>
<?php echo $form->end();?>
<div id="boxFactory" class="hiddenDefault">
<?=$form->input('length', array('label'=>'Length (cm)', 'div'=>'length'));?>
<?=$form->input('width', array('label'=>'Width (cm)', 'div'=>'width'));?>
<?=$form->input('height', array('label'=>'Height (cm)', 'div'=>'height'));?>
<?=$form->input('weight', array('label'=>'Weight (kg)', 'div'=>'weight'));?>
</div>
<?php debug($this->data); ?>

View file

@ -134,7 +134,7 @@
<?php echo $shipment['Shipment']['comments']; ?>
</td>
<td class="actions">
<button id="<?=$shipment['Shipment']['id']?>" class="editButton">Edit</button>
</td>
</tr>
<?php endforeach; ?>
@ -152,8 +152,12 @@
</div>
<div id="dialog-form" title="New Shipment">
<?=$this->element('add_shipment');?>
<div id="add-form" title="New Shipment">
</div>
<div id="edit-form" title="Edit Shipment">
</div>
<?php debug($shipments); ?>

View file

@ -0,0 +1,289 @@
$(function() {
var addedBoxes = $(".boxForm").length;
$('button').button();
displayReleventFields();
$('#date_arrived_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_arrived'
});
$('#date_dispatched_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_dispatched'
});
/**
* Display the relevent elements depending on this Shipment Type */
$("#shipmentType").change(function() {
displayReleventFields();
});
//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);
if($('#PurchaseOrderID_'+ui.item.id).length == 0) { //This PO is already in the List. Don't do anything.'
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);
if($('#JobID_'+ui.item.id).length == 0) { //This Job is already in the List. Don't do anything.'
addToList('Job', ui.item.id, ui.item.value, $('#jobsList'));
}
}
});
$("#customer_autocomplete").autocomplete({
source: "/customers/completeCustomer",
minLength: 2,
select: function( event, ui ) {
$("#customer_id").val(ui.item.id);
$.get('/addresses/customerAddresses/'+ui.item.id, function(data) {
$("#address").html(data);
$(".selectAddress", data).button();
});
}
})
$("#addBox").button().click(function() {
addBox(addedBoxes);
addedBoxes++;
});
$(".removeBox").live('click',function() {
$(this).parent().remove();
});
$(".selectAddress").live('click', function() {
var ID = $(this).attr('id');
$("#address_id").val(ID);
$(this).removeClass('selectAddress');
$(this).addClass('changeAddress');
$(".selectAddress").button('disable');
$(".selectAddress").parent('div').fadeOut();
$(this).button({
label: 'Select a different address'
});
$(this).button('enable');
});
$(".changeAddress").live('click', function() {
$("#address_id").val('');
$(this).removeClass('changeAddress');
$(this).button({
label: 'Ship to this Address'
});
$(this).addClass('selectAddress');
$(".selectAddress").parent('div').fadeIn();
$('.selectAddress').button('enable');
});
function displayReleventFields() {
var shipmentTypeID = getSelectedID('#shipmentType');
shipmentTypeID = parseInt(shipmentTypeID);
$(".hiddenDefault").hide();
switch(shipmentTypeID) {
case 1: //Import
$("#importFields, #principle, #purchaseOrder, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 2: //Direct
$("#purchaseOrder, #job,#principle, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 3: //Export
$("#job, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 4: //Local
$("#job, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
default:
break;
}
}
function addBox(addedBoxes) {
var model = 'Box';
var newBoxForm = $("#boxFactory").clone();
var boxNo = addedBoxes;
newBoxForm.attr('id','');
newBoxForm.addClass('boxForm');
newBoxForm.show();
//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);
var closeButton = $('<button>X</button>');
closeButton.addClass('removeBox');
closeButton.button();
newBoxForm.prepend(closeButton);
$("#boxes").append(newBoxForm);
}
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);
}
/**
* 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+']';
thisHiddenInput.attr('name', 'data'+modelString+modelString+'[]');
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;
}
/**
* Resets the Form to its default site
*/
function resetForm() {
$(".hiddenDefault").hide();
//$("#ShipmentAddForm")[0].reset();
}
});

View file

@ -1,10 +1,9 @@
$(function() {
resetForm();
var addedBoxes = 0;
$( "#dialog-form" ).dialog({
$( "#add-form" ).dialog({
autoOpen: false,
height: 900,
width: 600,
@ -14,14 +13,14 @@ $(function() {
var thisShipmentInputs = $('#ShipmentAddForm').find('input,select,textarea');
$.post('/shipments/ajax_add', thisShipmentInputs, function(data) {
$( "#dialog-form" ).dialog('close');
//window.location.reload(); //Change this to fetch a new copy of the table and update a <div>.
$( "#add-form" ).dialog('close');
window.location.reload(); //Change this to fetch a new copy of the table and update a <div>.
});
},
Cancel: function() {
$( this ).dialog( "close" );
$("#ShipmentAddForm")[0].reset();
//$("#ShipmentAddForm")[0].reset();
$("#principlesList").empty();
}
},
@ -34,289 +33,66 @@ $(function() {
}
});
$( ".newShipment" ).button().click(function() {
$( "#dialog-form" ).dialog( "open" );
$( "#edit-form" ).dialog({
autoOpen: false,
height: 900,
width: 600,
modal: true,
buttons: {
"Save Shipment": function() {
var thisShipmentInputs = $('#ShipmentEditForm').find('input,select,textarea');
});
$.post('/shipments/ajax_edit', thisShipmentInputs, function(data) {
$( "#edit-form" ).dialog('close');
window.location.reload(); //Change this to fetch a new copy of the table and update a <div>.
});
},
Cancel: function() {
$( this ).dialog( "close" );
//$("#ShipmentEditForm")[0].reset();
$("#principlesList").empty();
}
},
close: function() {
$("#importFields").hide();
$("#principle").hide();
$('#date_arrived_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_arrived'
});
$('#date_dispatched_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_dispatched'
});
/**
* Display the relevent elements depending on this Shipment Type */
$("#shipmentType").change(function() {
var shipmentTypeID = getSelectedID('#shipmentType');
shipmentTypeID = parseInt(shipmentTypeID);
$(".hiddenDefault").hide();
switch(shipmentTypeID) {
case 1: //Import
$("#importFields, #principle, #purchaseOrder, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 2: //Direct
$("#purchaseOrder, #job,#principle, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 3: //Export
$("#job, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
case 4: //Local
$("#job, #dateDispatched, #customer, #address, #addressSpan, #freight_forwarder, #airway_bill, #boxes").show();
break;
default:
break;
}
});
$( ".newShipment" ).button().click(function() {
$.get('/shipments/add/', false, function(data) {
$("#add-form").html(data);
} );
$( "#add-form" ).dialog( "open" );
});
//Remove X button clicked.
$('.removeFromList').live('click', function() {
$(this).parent().remove();
$(".editButton").button().click(function() {
var id = $(this).attr('id');
$.get('/shipments/edit/'+id, false, function(data) {
$("#edit-form").html(data);
} );
$( "#edit-form" ).dialog( "open" );
});
$('#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);
if($('#PurchaseOrderID_'+ui.item.id).length == 0) { //This PO is already in the List. Don't do anything.'
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);
if($('#JobID_'+ui.item.id).length == 0) { //This Job is already in the List. Don't do anything.'
addToList('Job', ui.item.id, ui.item.value, $('#jobsList'));
}
}
});
$("#customer_autocomplete").autocomplete({
source: "/customers/completeCustomer",
minLength: 2,
select: function( event, ui ) {
$("#customer_id").val(ui.item.id);
$.get('/addresses/customerAddresses/'+ui.item.id, function(data) {
$("#address").html(data);
$(".selectAddress", data).button();
});
}
})
$("#addBox").button().click(function() {
addBox(addedBoxes);
addedBoxes++;
});
$(".removeBox").live('click',function() {
$(this).parent().remove();
});
$(".selectAddress").live('click', function() {
var ID = $(this).attr('id');
$("#address_id").val(ID);
$(this).removeClass('selectAddress');
$(this).addClass('changeAddress');
$(".selectAddress").button('disable');
$(".selectAddress").parent('div').fadeOut();
$(this).button({
label: 'Select a different address'
});
$(this).button('enable');
});
$(".changeAddress").live('click', function() {
$("#address_id").val('');
$(this).removeClass('changeAddress');
$(this).button({
label: 'Ship to this Address'
});
$(this).addClass('selectAddress');
$(".selectAddress").parent('div').fadeIn();
$('.selectAddress').button('enable');
});
/**
* Resets the Form to its default site
*/
function resetForm() {
$(".hiddenDefault").hide();
$("#ShipmentAddForm")[0].reset();
}
function addBox(addedBoxes) {
var model = 'Box';
var newBoxForm = $("#boxFactory").clone();
var boxNo = addedBoxes;
newBoxForm.attr('id','');
newBoxForm.addClass('boxForm');
newBoxForm.show();
//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);
var closeButton = $('<button>X</button>');
closeButton.addClass('removeBox');
closeButton.button();
newBoxForm.prepend(closeButton);
$("#boxes").append(newBoxForm);
}
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);
}
/**
* 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+']';
thisHiddenInput.attr('name', 'data'+modelString+modelString+'[]');
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;
}