perkiset

This little class was modelled (but is not precisely) after the

observer

  pattern. The notion here is that I often have lots of elements on a page that a user *might* edit... and I don't want to leave the page if the page is "dirty."

So, I instantiate one of these classes on a page, then register anything that might get edited by the user (and Im concerned about) with it. I also trap the leaving a page event and check this, so that if a user is editing some content on a web page, for example, and they browser elsewhere I can throw up a "Applauseiscard changes to this <whatever>?" confirm box and the

ajax

  app looks more like a real application.

Here's the code - ping me if you need to see it in action:

Enjoy!
/p


// ----------------------------------------------------------- //
//                      dirtyElement                          //
// ----------------------------------------------------------- //
function dirtyElement() { this.clear(); }
dirtyElement.prototype.clear = function()
{
this.dirtyList = new Array();
this.dirtyIDX = new Array();
this.onChange = null;
this.isDirtyAny = false;
}
dirtyElement.prototype.clearDirty = function(theName)
{
this.dirtyList[theName] = false;
this.isDirtyAny = false;
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX<>;
if (this.dirtyList[thisName]) { this.isDirtyAny = true; }
}
if ( (!this.isDirtyAny) && (this.onChange) ) { this.onChange(false, theName); }
}
dirtyElement.prototype.clearDirtyAll = function()
{
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX<>;
this.dirtyList[thisName] = false;
}
if (this.onChange) { this.onChange(false); }
this.isDirtyAny = false;
}
dirtyElement.prototype.dirty = function(theName)
{
if ( (!this.isDirty(theName)) && (this.onChange)) { this.onChange(true, theName); }
this.dirtyList[theName] = true;
this.isDirtyAny = true;
}
dirtyElement.prototype.isDirty = function(theName)
{
return this.dirtyList[theName];
}
dirtyElement.prototype.proceed = function(theName, theMsg)
{
if (this.isDirty(theName)) { return confirm(theMsg); }
else return true;
}
dirtyElement.prototype.proceedAny = function(theMsg)
{
if (this.isDirtyAny)
{
ok2Go = confirm(theMsg);
if (ok2Go) { this.clearDirtyAll(); }
return ok2Go;
} else return true;
}
dirtyElement.prototype.watch = function(theName) {
this.dirtyList[theName] = false;
var ptr = this.dirtyIDX.length;
this.dirtyIDX[ptr] = theName;
}
// ----------------------------------------------------------- //
//                      dirtyElement                          //
// ----------------------------------------------------------- //
function dirtyElement() { this.clear(); }
dirtyElement.prototype.clear = function()
{
this.dirtyList = new Array();
this.dirtyIDX = new Array();
this.onChange = null;
this.isDirtyAny = false;
}
dirtyElement.prototype.clearDirty = function(theName)
{
this.dirtyList[theName] = false;
this.isDirtyAny = false;
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX<>;
if (this.dirtyList[thisName]) { this.isDirtyAny = true; }
}
if ( (!this.isDirtyAny) && (this.onChange) ) { this.onChange(false, theName); }
}
dirtyElement.prototype.clearDirtyAll = function()
{
for (var i=0; i<this.dirtyIDX.length; i++)
{
var thisName = this.dirtyIDX<>;
this.dirtyList[thisName] = false;
}
if (this.onChange) { this.onChange(false); }
this.isDirtyAny = false;
}
dirtyElement.prototype.dirty = function(theName)
{
if ( (!this.isDirty(theName)) && (this.onChange)) { this.onChange(true, theName); }
this.dirtyList[theName] = true;
this.isDirtyAny = true;
}
dirtyElement.prototype.isDirty = function(theName)
{
return this.dirtyList[theName];
}
dirtyElement.prototype.proceed = function(theName, theMsg)
{
if (this.isDirty(theName)) { return confirm(theMsg); }
else return true;
}
dirtyElement.prototype.proceedAny = function(theMsg)
{
if (this.isDirtyAny)
{
ok2Go = confirm(theMsg);
if (ok2Go) { this.clearDirtyAll(); }
return ok2Go;
} else return true;
}
dirtyElement.prototype.watch = function(theName) {
this.dirtyList[theName] = false;
var ptr = this.dirtyIDX.length;
this.dirtyIDX[ptr] = theName;
}


Perkiset's Place Home   Politics @ Perkiset's