/*
mvctable.js v0.3 Monday, May 31, 1999
Mitch Gould - humanfact@generalpicture.com
by Mitch Gould, humanfact@generalpicture.com
This script demonstrates dynamic documents in Netscape
Navigator and Internet Explorer 5 in the following ways:
(a) it exercises W3C's standard Document Object Model API.
(b) it creates, modifies, and destroys a table.
(c) it partially illustrates the concept of model-view-controller.
(To fully illustrate the MVC concept, modifications made to the
data view would need to be propagated back to the data model.)
*/
// A. Data initialization.
var datacount = 0
model = new Array()
// Allocate the first dataset, a set of quotations, from this array.
arrayquotes = new Array(
"Oh dear! I shall be too late.",
"Curiouser and curiouser!",
"Who are -you-?",
"We're all mad here.",
"Twinkle, twinkle, little bat.",
"Off with her head!",
"I make you a present of everything I've said...",
"Once, I was a real Turtle.",
"Sentence firstverdict afterwards.")
// When a link is clicked, the quotations' sources will be revealed
// from this array.
arraysources = new Array(
"White Rabbit",
"Alice",
"Caterpillar",
"Cheshire Cat",
"Dormouse",
"Red Queen",
"Dutchess",
"Mock Turtle",
"Red Queen")
// B. MVC-DOM methods.
// Establish a new data model and assign it to a new view.
function startController() {
// Populate the model with initial data.
datamodel = refreshModel(arrayquotes)
// Get the document body element.
var docbod = getBody()
/*
Create a view in the body of the document and fill it
with the initial data.
*/
createView(docbod, model)
}
// Copy the specified dataset into the model.
function refreshModel(arraycurrent) {
// Populate the model with the current datastore.
for(var i=0; i < arraycurrent.length; i = i + 1)
{
model[i] = arraycurrent[i]
}
return model
}
/*
this could be generalized to provide alternatives to a
a table view, such as a select object, a tree, or even
a textarea. this sample produces a one-column table.
Multi-column tables are more complex.
*/
function createView(bodyelement, themodel) {
table = document.createelement("TABLE")
table.border = 1
table.id = "viewtable"
tablebody = document.createelement("TBODY")
for(var i=0; i < model.length; i++)
{
currentrow = document.createelement("TR")
currentcell = document.createelement("TD")
currentcell.appendchild(document.createtextnode(model[i]))
currentrow.appendchild(currentcell)
tablebody.appendchild(currentrow)
}
table.appendchild(tablebody)
bodyelement.appendchild(table)
return table
}
// refresh the model first, then the view.
function refreshview(dataset) {
// populate the model with new data.
refreshmodel(dataset)
tablebody = document.getelementsbytagname("TBODY").item(0)
var count = 0
replacealltext(tablebody)
}
// One can also destroy HTML objects using the DOM.
function destroyView() {
objecttodestroy = document.getelementbyid("viewtable")
body = getbody()
body.removechild(objecttodestroy)
// now destroy the buttons.
objecttodestroy = document.getelementbyid("whosaid")
body.removechild(objecttodestroy)
objecttodestroy = document.getelementbyid("goaway")
body.removechild(objecttodestroy)
}
// c. dom tree-navigation and utilities.
/*
One must climb the trunk, branches, and twigs to get
(or set!) the fruit. The recursive nature of this
algorithm reflects an essential fractal nature of
documents.
*/
function replaceAllText(startelem) {
// Climb the object tree, replacing its text nodes with
// new data.
for (var i=0; i < startelem.childnodes.length; i = i + 1) {
switch (startelem.childnodes.item(i).nodetype) {
case 1: // element nodetype
replacealltext(startelem.childnodes.item(i))
break;
case 3: // text nodetype
if (datacount < model.length) {
settext(startelem.childnodes.item(i), model[datacount])
datacount = datacount + 1
} else {
settext(startelem.childnodes.item(i)," - ? - ")
}
break;
} //endswitch
} //endfor
} //endfunction
/*
many operations on dynamic documents require one to start
from the document's body element.
*/
function getBody()
{
if(navigator.appName != "Netscape") {
resultelement = document.body;
} else {
resultelement = document.getelementsbytagname("body").item(0);
}
return resultelement;
}
// utility function to overwrite text nodes.
function settext(tagtoset, valuetoset) {
tagtoset.nodevalue = valuetoset
}
// d. this can't start until the page loads.
window.onload = startcontroller