name=fi_oulu_Containers
title=Container management
description=For technicians. Quickly register pins, pucks, etc., by barcode, or empty them after a shipment.
admin_only=no
version=1
height=1
width=1
url=/api/containertype?pagesize=1000&pagenum=1&sortby=name
---- Below this line is deemed to be HTML content if url above is empty, Javascript otherwise. If Javascript, you MUST define an object BRICKNAME_functions containing at least a function onSuccess(transport,box){}, transport contains a responseJSON object, box is the homepage brick itself.
document.fi_oulu_Containers_functions={

	onSuccess:function(transport,box){
		box.containertypes=transport.responseJSON.rows;
		box.containertypes.sort(function(a,b){
			if(a.containercategoryname<b.containercategoryname){ return -1; }
			if(a.containercategoryname>b.containercategoryname){ return 1;  }
			if(a.name<b.name){ return -1; }
			if(a.name>b.name){ return 1;  }
			return 0;
		});
		if(!isAdmin && !isTechnician && !isShipper){
			document.fi_oulu_Containers_functions.showContainerCreate();
		} else { 
			document.fi_oulu_Containers_functions.showMenu();
		}
	},
	
	showMenu:function(){
		var box=$("fi_oulu_Containers");
		var title=box.down("h2");
		title.innerHTML="Container management";
		title.onclick=document.fi_oulu_Containers_functions.showMenu;
		title.style.cursor="pointer";
		var bb=box.down(".boxbody");
		bb.innerHTML="";
		var frm=box.form({
			action:'#',
			method:'post'
		});
		var ff=frm.formField({ label:'Register new containers',content:'&nbsp;' });
		ff.onclick=document.fi_oulu_Containers_functions.showContainerCreate;
		ff.style.cursor="pointer";
		ff=frm.formField({ label:'Help',content:'&nbsp;' });
		ff.onclick=document.fi_oulu_Containers_functions.showHelp;
		ff.style.cursor="pointer";
	},
	
	/*
	 * Container creation
	 */
	
	showContainerCreate:function(){
		var box=$("fi_oulu_Containers");
		var title=box.down("h2");
		title.innerHTML="Container management &bull; New containers";
		var bb=box.down(".boxbody");
		bb.innerHTML="";
		var frm=box.form({
			action:'/api/container/',
			method:'post'
		});
		frm.hiddenField('projectid',sharedProject['id']);
		frm.roleField({
				label:"Container type", 
				name:'containertypeid', 
				otherType:'containertype',
				labelField:'name',
				headers:['Name','Category'],
				cellTemplates:['{{name}}','{{containercategoryname}}']
		});
		frm.textField({ label:"Barcode", name:'name' });
		frm.createButton();
		frm.select("label").last().style.display="none"; //need it to prevent autosubmit, but hide it.
		var msg=document.createElement("label");
		msg.id="fi_oulu_Containers_msg";
		msg.innerHTML='Set your barcode scanner to send Enter after scanning';
		msg.style.textAlign="center";
		frm.appendChild(msg);
		frm.onsubmit=function(){ document.fi_oulu_Containers_functions.createContainer(frm); return false; }
		frm.name.focus();
	},
	createContainer:function(frm){
		var barcode=frm.name.value.trim();
		var containertypeid=frm.containertypeid.value;
		var msg=$("fi_oulu_Containers_msg");
		if(""==barcode || ""==containertypeid){
			msg.innerHTML="<span>Type and barcode are required.</span>";
			msg.addClassName("invalidfield");
			return false;
		}
		if(!isAdmin && !isTechnician && !isShipper){
			var containerType=frm.containertypeid.up("label").roleRecord;
			if(1!=containerType.userscancreate){
				msg.innerHTML="<span>Only technicians and shippers can create a "+containerType.containercategoryname.toLowerCase()+".</span>";
				msg.addClassName("invalidfield");
				return false;
			}
		}
		msg.innerHTML="<span>Creating..."+frm.name.value+"</span>";
		msg.removeClassName("invalidfield");
		msg.addClassName("updating");
		frm.request({
			onSuccess:function(transport){ document.fi_oulu_Containers_functions.createContainer_onSuccess(transport, frm); },
			onFailure:function(transport){ document.fi_oulu_Containers_functions.createContainer_onFailure(transport, frm); }
		});
		return false;
	},
	createContainer_onSuccess:function(transport, frm){
		var created=transport.responseJSON.created;
		var msg=$("fi_oulu_Containers_msg");
		msg.innerHTML='<span>'+frm.containertypeid.up("label").roleRecord.name+' <a href="/container/'+created.id+'">'+created.name+'</a> created.</span>';
		msg.removeClassName("updating");
		frm.name.value="";
		frm.name.focus();
	},
	createContainer_onFailure:function(transport, frm){
		if(401==transport.status){ return ui.handleSessionExpired(); }
		var txt=transport.responseText;
		var err="";
		if(-1!==txt.indexOf("may already exist")){
			err='Container called '+frm.name.value+' already exists.';
		} else {
			if(transport.responseJSON && transport.responseJSON.error){
				txt=transport.responseJSON.error;
			}
			err='Could not create '+frm.containertypeid.up("label").roleRecord.name+' '+frm.name.value;
			alert("Could not create. The server said:\n\n"+txt);
		}
		var msg=$("fi_oulu_Containers_msg");
		msg.innerHTML='<span>'+err+'</span>';
		msg.addClassName("invalidfield");
		msg.removeClassName("updating");
		frm.name.value="";
		frm.name.focus();
	},
	
	/*
	 * Help
	 */
	 showHelp: function(){
		var box=$("fi_oulu_Containers");
		var title=box.down("h2");
		title.innerHTML="Container management &bull; Help";
		var bb=box.down(".boxbody");

		var txt='<h3>Container management</h3>';
		txt+='<p>This brick lets you work with containers other than crystallization plates, for example pins, pucks, and dewars. Plates should not be created this way.</p>';
		txt+='<p>This is mainly intended for lab technicians. However, regular IceBear users may have permission to create some kinds of container. Technicians will see a menu of options, but regular users will only see the container creation form.</p>';
		txt+='<p>It is assumed that your barcode scanner sends a carriage return after a successful scan. To test this, open up your favourite text editor and scan a barcode several times; you should see each scan appearing on a separate line. See your scanner\'s manual for details on configuring this. (You will probably need to scan a specific barcode found within the manual.)</p>';
		
		txt+='<h3>Creating new containers</h3>';
		txt+='<p>Here, you can quickly create many containers of the same type. This is useful when first setting up IceBear, and when you buy new containers (say, a box of pins).</p>';
		txt+='<p>First, specify the type of container you want to create. Click the <strong>Change...</strong> button, then choose the type from the list.</p>';
		txt+='<p>Then, click in the barcode box and scan the barcode of your first new container.</p>';
		txt+='<p>Non-technicians may not be allowed to create every kind of container. If you don\'t have permission, you will get a message telling you this.</p>';
		txt+='<p>If a container with that barcode already exists, the new container will not be created and you will see an error message. It is assumed that containers of different types will not have the same barcode.</p>';
		txt+='<p>If creation succeeds, you will see a confirmation message. There is a link to the container\'s IceBear page.</p>';
		txt+='<p>To create more containers of the same type, simply scan all their barcodes. The container type will stay the same until you change it.</p>';

		bb.innerHTML=txt;
	 },
	
}