edit_inline.js
Summary
EditInline class derived from Widget. See description of
base Widget class at http://trac.zapatec.com:8000/trac/wiki/Widget.
Copyright (c) 2004-2006 by Zapatec, Inc.
http://www.zapatec.com
1700 MLK Way, Berkeley, California,
94709, U.S.A.
All rights reserved.
Zapatec.EditInline = function(objArgs) {
Zapatec.EditInline.SUPERconstructor.call(this, objArgs);
};
Zapatec.EditInline.id = 'Zapatec.EditInline';
Zapatec.inherit(Zapatec.EditInline, Zapatec.Widget);
Zapatec.EditInline.prototype.init = function(objArgs) {
Zapatec.EditInline.SUPERclass.init.call(this, objArgs);
this.isActive = false;
this.isClicked = false;
var self = this;
this.container = document.createElement("textarea");
this.createProperty(this.container, "zpEditInline", this);
this.container.style.position = "absolute";
this.container.style.border = "1px solid black";
this.container.onclick = function(){
self.isClicked = true;
}
Zapatec.Utils.addEvent(document, "keydown",
function(evt){
self.keyEvent(evt);
});
if(this.config.hideOnClick){
Zapatec.Utils.addEvent(document, 'click', function(evt){
setTimeout(function(evt){self.documentClick(evt)}, 1);
});
}
var changeEvent = null;
if(Zapatec.is_ie){
changeEvent = 'paste';
} else if(Zapatec.is_gecko) {
changeEvent = 'input';
} else {
changeEvent = 'change';
}
Zapatec.Utils.addEvent(this.container, changeEvent,
function(){self.resizeToContent()});
if(this.config.showImmediately && this.config.target){
this.show(this.config.target);
}
};
Zapatec.EditInline.prototype.documentClick = function(evt){
if(!this.isActive){
return;
}
if(!this.isClicked){
if(this.config.saveOnClick){
this.saveAndHide();
} else {
this.hide();
}
}
this.isClicked = false;
}
Zapatec.EditInline.prototype.keyEvent = function(evt){
if(!this.isActive){
return;
}
if(!evt){
evt = window.event;
}
var res = Zapatec.Utils.getCharFromEvent(evt);
if(!res){
return;
}
if(this.config.hideOnEsc){
if(res.charCode == 27){
this.hide();
return;
}
}
if(
(
evt.ctrlKey ||
(typeof(evt.metaKey) != 'undefined' && evt.metaKey)
) &&
res.charCode == 13
){
this.saveAndHide();
return;
}
this.resizeToContent();
}
Zapatec.EditInline.prototype.configure = function(objArgs) {
this.defineConfigOption("theme", "");
this.defineConfigOption("target", null);
this.defineConfigOption("showImmediately", true);
this.defineConfigOption("hideOnEsc", true);
this.defineConfigOption("hideOnClick", true);
this.defineConfigOption("editAsText", false);
this.defineConfigOption("loadCallback");
this.defineConfigOption("saveCallback");
this.defineConfigOption("saveOnClick", true);
this.defineConfigOption("hideTarget", true);
Zapatec.EditInline.SUPERclass.configure.call(this, objArgs);
};
Zapatec.EditInline.prototype.show = function(target){
this.fireEvent("showStart");
if(target){
this.hide();
this.config.target = target;
this.setContent("");
}
this.isActive = true;
this.isClicked = !Zapatec.is_ie;
this.container.value = "";
this.setContent(this.config.target.innerHTML);
if(this.config.hideTarget){
this.config.target.zpOrigVisibility = this.config.target.style.visibility;
this.config.target.style.visibility = "hidden";
}
document.body.appendChild(this.container);
var parentCoords = Zapatec.Utils.getElementOffset(this.config.target);
this.container.style.left = parentCoords.left + "px";
this.container.style.top = parentCoords.top + "px";
this.resizeToContent();
this.container.focus();
this.fireEvent("showFinish");
}
Zapatec.EditInline.prototype.setContent = function(newContent){
if(typeof(newContent) != "string"){
return;
}
if(this.config.loadCallback){
newContent = this.config.loadCallback(newContent);
}
if(this.config.editAsText){
newContent = newContent.replace(/\<br(\s*\/)?\>/ig, "\n").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
}
this.container.value = newContent;
this.fireEvent("setContent", newContent);
}
Zapatec.EditInline.prototype.saveContent = function(){
var val = this.container.value;
if(this.config.saveCallback){
val = this.config.saveCallback(val);
}
if(this.config.editAsText){
val = val.replace(/&/g, "&").replace(/\</g, "<").replace(/\>/g, ">").replace(/\<br(\s*\/)?\>\n/ig, "\n").replace(/\n/g, "<br />");
}
this.config.target.innerHTML = val;
this.fireEvent("saveContent", val);
return this.container.value;
}
Zapatec.EditInline.prototype.hide = function(){
if(!this.isActive){
return;
}
this.fireEvent("hideStart");
this.isActive = false;
this.container.parentNode.removeChild(this.container);
if(this.config.hideTarget){
if(Zapatec.is_opera && !this.config.target.zpOrigVisibility){
this.config.target.zpOrigVisibility = "";
}
this.config.target.style.visibility = this.config.target.zpOrigVisibility;
this.config.target.zpOrigVisibility = null;
}
this.config.target = null;
this.fireEvent("hideFinish");
}
Zapatec.EditInline.prototype.saveAndHide = function(){
this.saveContent();
this.hide();
this.fireEvent("saveAndHide");
}
Zapatec.EditInline.prototype.resizeToContent = function(){
var val = this.container.value;
var tmp = val.split('\n');
var maxLength = tmp[0].length;
for(var ii = 1; ii < tmp.length; ii++){
if(tmp[ii].length > maxLength){
maxLength = tmp[ii].length;
}
}
this.container.cols = maxLength + (Zapatec.is_ie || Zapatec.is_khtml ? 1 : 0);
this.container.rows = tmp.length + (Zapatec.is_ie || Zapatec.is_khtml || Zapatec.is_opera ? 1 : 0);
}
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007