// 2008-03-04 json-ugdata.js var etag = ''; var path = '/xcs/ugdata/'; window.onload = function() { toggleView(); document.getElementById("submitAdd").onclick = addItem; document.getElementById("cancelAdd").onclick = getList; document.getElementById("submitUpdate").onclick = updateItem; document.getElementById("cancelUpdate").onclick = getList; document.getElementById("submitNew").onclick = newItem; document.getElementById("submitRefresh").onclick = getList; document.getElementById("submitEdit").onclick = editItem; document.getElementById("submitDelete").onclick = deleteItem; document.getElementById("cancelEdit").onclick = getList; getList(); } function toggleView(view) { var div = view || 'member-list'; document.getElementById('member-list').style.display = (div=='member-list'?'block':'none'); document.getElementById('member-item').style.display = (div=='member-item'?'block':'none'); document.getElementById('member-add').style.display = (div=='member-add'?'block':'none'); document.getElementById('member-edit').style.display = (div=='member-edit'?'block':'none'); } function getList() { ajax.httpGet(path,null,onAjaxComplete,false,'get-list',{'accept':'application/json'}); toggleView('member-list'); } function getItem() { var id = this.getAttribute('item-id'); document.getElementById('submitEdit').setAttribute('item-id',id); document.getElementById('submitDelete').setAttribute('item-id',id); toggleView('member-item'); ajax.httpGet(path+id,null,onAjaxComplete,false,'get-item',{'accept':'application/json'}) return false; } function newItem() { toggleView('member-add'); } function addItem() { var member,data; member = new Object(); member['firstname'] = document.getElementById('a_firstname').value; member['lastname'] = document.getElementById('a_lastname').value; member['birthdate'] = document.getElementById('a_birthdate').value; member['experience'] = document.getElementById('a_experience').value; data = JSON.stringify(member); ajax.httpPost(path,null,onAjaxComplete,false,'add-item','application/json',data); } function editItem() { var id = this.getAttribute('item-id'); toggleView('member-edit'); ajax.httpGet(path+id,null,onAjaxComplete,false,'read-item',{'accept':'application/json'}) } function updateItem() { var id,member,data; id = document.getElementById('u_id').innerHTML; member = new Object(); member['firstname'] = document.getElementById('u_firstname').value; member['lastname'] = document.getElementById('u_lastname').value; member['birthdate'] = document.getElementById('u_birthdate').value; member['experience'] = document.getElementById('u_experience').value; data = JSON.stringify(member); ajax.httpPut(path+id,null,onAjaxComplete,false,'update-item','application/json',data,{'if-match':etag}); } function deleteItem() { var id; if(confirm('ready to delete?')) { id = this.getAttribute('item-id'); ajax.httpDelete(path+id,null,onAjaxComplete,false,'delete-item',{'accept':'application/json'}) } } // handle ajax callbacks function onAjaxComplete(response,headers,context,status,msg) { switch(status) { case 0: // ie abort code break; case 200: // OK case 201: // Created case 202: // Accepted case 204: // NoContent break; case 301: case 302: if(headers.location) { location.href=headers.location; } break; default: // 400 & 500 errors if(headers.location) { location.href=headers.location; } else { alert(status+'\n'+msg); } break; } etag = headers['etag'] || ''; switch(context) { case 'get-list': processGetList(response); break; case 'get-item': processGetItem(response); break; case 'add-item': processAddItem(response,status); break; case 'read-item': processReadItem(response,status); break; case 'update-item': processUpdateItem(response,status); break; case 'delete-item': processDeleteItem(response,status); break; default: alert('Unknown context\n'+context); } } function processGetList(response) { var list,members,data,i,ol,li,a,btn; try { data = JSON.parse(response); members = data.members; } catch(ex) { alert(ex.message); return; } list = document.getElementById("list"); list.innerHTML=''; ol = document.createElement('ol'); for(i=0;i