/* * InlineEditor v.4 * * Copyright (c) 2006 Mackley F. Pexton. All rights reserved. * * This is open software and is licensed under the terms of the * GNU General Public License * Instructions and source code are available at http://www.acmebase.org/InlineEditor. * Send correspondence and feedback to: mack_pexton[at]acmebase.org. */ /****************************************************************************** InlineEditor v.4 -- edit a document element inline on a page The edit_inline() function changes a document element (e.g. a div tag) into an edit form allowing the user to change its contents with out refreshing the page. Upon saving (or canceling) the changes, the edit form changes back to the original display. Usage:
some content
Arguments: edit_element -- either an document element or element's id that is to be edited. field_name -- the name of the input field posted (sent to server program) url -- the editor form's action property, the server program saving the edits. button -- is an optional element or element id of an "Edit" button that triggers the editor. The edit_inline() program can be used as an onclick handler attached to the element to be edited or it can be invoked from a button. A parameter named "return_func" is added to the url identifying the JavaScript function to call from the server's returned page. The server adds two arguments to the function before executing it: the first is a true/false status whether the input was saved or not, and the second is an optional error messages to display upon return. Edit form appearance can be customized by assigning your style settings to InlineEditor.CSS. Note for Internet Explorer usage: BEWARE IE removes newlines from the HTML source when it renders a document UNLESS the source is contained within
 tags. Other browsers are not as
stupid.

******************************************************************************/


function edit_inline(edit_element,field_name,url,button) {
	// Avoid re-invoking editor on same element.
	var e = getRef(edit_element);
	for (editor in InlineEditor.editors) {
		if (e == InlineEditor.editors[editor].edit_element) { return; }
	}

	// Create new inline editor
	var editor = new InlineEditor(edit_element,field_name,url,button)
	editor.edit();
}

//
// InlineEditor control object.
//

/////// Configuration Variables ///////////////////////////

InlineEditor.CSS = '';			// optional styles for edit form (surround with \n'
 + this.CSS
 + '\n'
 + '\n'
 + '
\n' + '
\n' + '\n' + '
\n' + '\n' + '\n' + '
\n' + '
\n' + '
\n'; + '\n' + '\n'; } // // Class functions // InlineEditor.non_edit_keycodes = { 16:1, // shift 17:1, // ctl 18:1, // alt 19:1, // pause 20:1, // caps lock 27:1, // escape 33:1, // page up 34:1, // page down 35:1, // end 36:1, // home 37:1, // arrow left 38:1, // arrow up 39:1, // arrow right 40:1, // arrow down 44:1, // print scrn 45:1, // insert 91:1, // windows key 112:1, // F1 113:1, // F2 114:1, // F3 115:1, // F4 116:1, // F5 117:1, // F6 118:1, // F7 119:1, // F8 120:1, // F9 121:1, // F10 122:1, // F11 123:1, // F12 144:1, // numlock 145:1 // scroll lock }; InlineEditor.is_edit_key = function(k) { // return true if keyCode k alters an input field. return (k && ! InlineEditor.non_edit_keycodes[k]) ? true : false; } InlineEditor.enable_save = function(evt,ctl) { // Input field keyup handler. if (evt.keyCode && InlineEditor.is_edit_key(evt.keyCode)) { ctl.form.save.disabled = false; ctl.onkeyup=null // remove handler } } // The following atrocity is for IE's benefit. // IE innerHTML trashes all newlines if originating element is not
 or white-space:pre.
// You also cannot assign innerHTML to a variable and retrieve it without IE trashing newlines.
// Setting any kind of an element's innerHTML removes newlines unless surrounded by 

// The editbuf object provides a place to store innerHTML with newlines and methods to set a
// DOM element's innerHTML.

InlineEditor.editbuf = function(s) {
	this.buf = document.createTextNode(s != null ? s : '');	
}
InlineEditor.editbuf.prototype.set = function(s) {
	this.buf = document.createTextNode(s);
}
InlineEditor.editbuf.prototype.get = function() {
	return this.buf.data;
}
InlineEditor.editbuf.prototype.setInnerHTML = function(e) {
	if (navigator.userAgent.match(/MSIE/)) {
		// Save edit buffer of source code (with newlines) in node for next time.
		e.editBuf = this;

		if (e.nodeName == 'PRE') {
			// To trick IE into displaying newlines in 
 tags, add two more.
			e.innerHTML = '
'+this.get()+'
'; } else { e.innerHTML = this.get(); } } else { e.innerHTML = this.get(); } } InlineEditor.editbuf.prototype.getInnerHTML = function(e) { // Retreive previous edit buffer (with newlines) if previously saved in node. if (e.editBuf) { this.set(e.editBuf.get()); } else { this.set(e.innerHTML); } return this.get(); } // // Global support functions referenced by InlineEditor. // function getRef(id) { if (typeof id == "string") return document.getElementById(id); return id; } function getElementHeight(e) { if (!e) return; var result = 0; if (e.offsetHeight) { result = e.offsetHeight; } else if (e.clip && e.clip.height) { result = e.clip.height; } else if (e.style && e.style.pixelHeight) { result = e.style.pixelHeight; } return parseInt(result); }