/** * Sneep Javascript Library - sneep.js * * This library should be loaded by the EPrints Javascript autoloader. It * has a dependency on Prototype and Scriptaculous which should be included * with EPrints by default. */ /***************************************************** * Sneep JS Functions *****************************************************/ function sneepLoadSneepLayer(url) { var type = url.match(/type=(\w*)&?/)[1]; new Ajax.Request(url, { method: 'get', onCreate: function() { $('sneep_item_panel_'+type).update('

Loading '+type+'s...

'); }, onComplete: function(transport) { $('sneep_item_panel_'+type).update(transport.responseText+ ''); } }); } function sneepInitForm(type) { $$(".ep_sneep_"+type+"_form_container").each(function(element) { var submitButton = new Element('input', { type: 'button', id: 'sneep_'+type+'_submit', value: $('sneep_'+type+'_submit').readAttribute('value') }); submitButton.observe('click', sneepFormSubmitAdd); $('sneep_'+type+'_submit').remove(); element.appendChild(submitButton); var inputcallback = new Element('input', { type: 'hidden', name: 'callback', value: '1' }); element.appendChild(inputcallback); }); } function sneepFormSubmitAdd(event) { event.stop(); var type = event.target.id.match(/sneep_(\w*?)_submit/)[1]; $('ep_sneep_'+type+'_form').request({ onCreate: function() { sneepClearErrorMessage(); $$('.ep_sneep_'+type+'_input_content').each(function(element) { element.disable(); }); event.target.disable(); $$('.ep_sneep_'+type+'_form_container').each(function(element) { loader = new Element('span', { id: 'ep_sneep_loader' }); loader.update(' Adding '+type+'...'); element.appendChild(loader); }); }, onSuccess: function(transport) { var response = transport.responseText; //.evalJSON(); try { var json = response.evalJSON(); } catch(err) { //alert( "ERROR! "+err ); return } if (json == null) { alert('json is null'); return; } if (json.status == 0) { sneepRenderErrorMessage(json.content); } else { // If we successfully created the sneep then add it to the list. var no_sneeps = $$('#ep_sneep_no_'+type+'s').first(); if (no_sneeps != null) { no_sneeps.remove(); } if (json.status == 1) { $$('.ep_sneep_'+type+'_list').each(function (element) { var newcontent = json.content+element.innerHTML+ ''; element.update(newcontent); }); } } $('ep_sneep_loader').remove(); $$('.ep_sneep_input_content').each(function(element) { element.clear(); element.enable(); }); $('sneep_'+type+'_submit').enable(); } }); } function sneepParseUrl(url) { matches = url.match(/sneepid=(\d*)&type=(\w*)/); return { sneepid: matches[1], sneeptype: matches[2] }; } function sneepRegisterEditHandlers() { $$(".ep_sneep_editlink").each(function(link) { link.stopObserving('click'); link.observe('click', sneepHandleEdit); }); } /* sf2 */ function sneepDeregisterEditHandlers() { $$(".ep_sneep_editlink").each(function(link) { link.stopObserving('click'); link.observe('click', function(event) { event.stop(); return false; }); }); } function sneepHandleEdit(event) { event.stop(); var element = event.element(); var parsedUrl = sneepParseUrl(element.readAttribute('href')); var sneepid = parsedUrl.sneepid; var sneeptype = parsedUrl.sneeptype; var sneepdivid = 'sneep-'+sneeptype+'-'+sneepid; var titleid = sneepdivid+'-title'; var contentid = sneepdivid+'-content'; var origtitle = null; var origcontent = null; if ((sneeptype == 'note' || sneeptype == 'tag') && $(titleid) != null) { origtitle = $(titleid).firstChild.innerHTML; } if (sneeptype == 'comment' || sneeptype == 'note') { origcontent = $(contentid).innerHTML; } var sneepformid = 'ep_sneep_'+sneeptype+'_form_'+sneepid; var sneepeditform = new Element('form', { id: sneepformid, method: 'post', action: element.readAttribute('href') } ); sneepeditform.update($(sneepdivid).innerHTML); sneepeditform.observe('submit', sneepUpdateSubmit.bindAsEventListener({ sneeptype: sneeptype })); $(sneepdivid).update(); $(sneepdivid).appendChild(sneepeditform); if (origtitle != null) { var edittitle = origtitle.trim(); $(titleid).update(''); } if (origcontent != null) { var editcontent = origcontent.trim(); $(contentid).update(''); } var footer = new Element('p'); var callback = new Element('input', { type:'hidden', name:'callback', value:'1' }); var sneepid = new Element('input', { type:'hidden', name:'sneepid', value:sneepid }); var action = new Element('input', { type:'hidden', name:'action', value: 'edit' }); var update = new Element('input', { type:'submit', value:'Update' }); var cancel = new Element('input', { type:'button', value:'Cancel' }); cancel.observe('click', function(event) { if( $(titleid) != null ) $(titleid).innerHTML = origtitle; $(contentid).innerHTML = origcontent; event.stop(); sneepRegisterEditHandlers(); return false; }); footer.appendChild(callback); footer.appendChild(sneepid); footer.appendChild(action); footer.appendChild(update); footer.appendChild(cancel); sneepRegisterDeleteHandlers(); sneepDeregisterEditHandlers(); $(contentid).appendChild(footer); } function sneepUpdateSubmit(event, sneeptype) { var sneeptype = this.sneeptype; event.stop(); var form = event.element(); new Ajax.Request(form.readAttribute('action'), { parameters: form.serialize(true), onComplete: function(transport) { var response = transport.responseJSON; /* sf2: trick to append the child Nodes otherwise you get twice the 'sneep-'+sneeptype+'-'+sneepid div */ /* sf2: added direct calls to the ...Handlers() methods */ var el = $('sneep-'+sneeptype+'-'+response.sneepid); var tmpEl = new Element('div'); tmpEl.update(response.content); el.innerHTML = ""; var children = tmpEl.childElements()[0].childElements(); for(var i=0;i=0 && isWhitespace(str.charAt(j)) ; j--) ; return str.substring(0,j+1); } function trim(str) { return ltrim(rtrim(str)); } function isWhitespace(charToCheck) { var whitespaceChars = " \t\n\r\f"; return (whitespaceChars.indexOf(charToCheck) != -1); }