// bookmarklib.js
//
// Functions combined with the HTML code in admbookmarks.pm
// and code in bookmarkmenu_toolbar.html
//
//-------------------------------------------------------------------------------
// Here are all of the bookmark methods:
//-------------------------------------------------------------------------------
//-------------------------------------------------------Adds a link to an object
function addLink(name, link, address, position) {
this.lname = name;
this.link = link;
this.address = address;
this.position = position;
this.state = "link";
this.icon = "/res/adm/pages/bookmarkmenu/link.gif";
this.pressed = "/res/adm/pages/bookmarkmenu/link_pressed.gif";
this.hover = hover;
this.remove = remove
this.exists = true;
}
//-----------------------------------------------------Adds a folder to an object
function addFolder(name, size, address) {
this.p = new Array(); //BEN got rid of size in Array Agument
// Currently SIZE doesn't matter
// This probably works cross-platform
this.lname = name;
this.address = address;
this.state = "opened";
this.icon = "/res/adm/pages/bookmarkmenu/folder_opened.gif";
this.pressed = "/res/adm/pages/bookmarkmenu/folder_opened_pressed.gif";
this.swapState = swapState;
this.clickFolder = clickFolder;
this.clickLink = clickLink;
this.hover = hover;
this.remove = remove;
this.bump = bump;
this.insertLink = insertLink;
this.insertFolder = insertFolder;
this.exists = true;
}
//-----------------------------------------Swaps folder state from open to closed
function swapState() {
if (this.state == "closed") {
this.state = "opened";
this.icon = "/res/adm/pages/bookmarkmenu/folder_opened.gif";
this.pressed = "/res/adm/pages/bookmarkmenu/folder_opened_pressed.gif";
} else {
this.state = "closed";
this.icon = "/res/adm/pages/bookmarkmenu/folder_closed.gif";
this.pressed = "/res/adm/pages/bookmarkmenu/folder_closed_pressed.gif";
}
redrawTree();
}
//------------------------------------------swaps the dragged icon into the image
function hover( object ) {
if ( dragCache != "" ) {
object.src = this.pressed;
if (lastDrug != -1) {
parent.frames[4].document.images[lastDrug].src = lastIcon;
}
lastIcon = this.icon;
lastDrug = object.name;
}
}
//------------------------------------------------------The click on link method
function clickLink( object, position ) {
if (dragCache=="") { //---------'pick up' the icon
dragCache=object.src;
lastIcon=this.p[position].icon;
lastDrug=object.name;
addressCache=new addLink(this.p[position].lname,this.p[position].link,this.p[position].address,position);
this.p[position].remove();
} else { //--------'put down' the icon
dragCache="";
if ( addressCache.state == "link" ) { //------differently if a link
this.insertLink( position );
} else { //--------------than a folder
this.insertFolder( position );
}
redrawTree();
}
}
//-----------------------------------------------------The click on Folder method
function clickFolder( object ) {
if (dragCache=="") { //---------'pick up' the icon
dragCache=object.src;
lastIcon=this.icon;
lastDrug=object.name;
addressCache=new addFolder("lame_folder",1,"doesnotmatter"); //used to be (this.lname,this.p.length+1,this.address)
shiftFolder(this,addressCache,0); //this is a whole new line
this.remove();
} else { //--------'put down' the icon
dragCache = "";
if ( addressCache.state == "link" ) { //-----place link into folder
this.insertLink(0);
// this.p[ this.p.length ] = new addLink( addressCache.lname, addressCache.link, this.address, this.p.length );
} else { //---place folder into folder
this.insertFolder(0);
// this.p[ this.p.length ] = new addFolder( addressCache.lname, addressCache.p.length, this.address+".p["+this.p.length+"]" );
// this.p[ this.p.length ].p = addressCache.p;
}
redrawTree();
}
}
//---------Folder method to place link in middle of other links (should simplify code)
function insertLink( position ) {
this.bump(position);
this.p[position]=new addLink(addressCache.lname,addressCache.link,this.address, position);
}
//---------------------------Recursive folder Method to bump items up one to make room
function bump(position) {
// long term: make recursive
var i=this.p.length; // First, create a new spot at the top.
if (this.p[i-1].state=="link") {
this.p[i]=new addLink(this.p[i-1].lname,this.p[i-1].link,this.address,i);
} else {
this.p[i]=new addFolder(this.p[i-1].lname,this.p[i-1].p.length,this.address+".p["+i+"]");
}
this.p[i].exists=this.p[i-1].exists; // preserve existance
i--; // Second, shift others up one
while (i >= position+1 ) {
if ( this.p[i-1].state == "link" ) {
this.p[ i ] = new addLink( this.p[ i-1 ].lname, this.p[i-1].link, this.p[i-1].address, i);
} else {
this.p[ i ] = new addFolder( this.p[ i-1 ].lname, this.p[i-1].p.length, this.address+".p["+ i +"]" );
// move all of the inner folder stuff
shiftFolder(this.p[i-1],this,i);
}
i--;
}
}
//-----------------------------------------------------------Opens the page
function clickOnBookmark(url) {
open(url,opener.clientwindow.name); // <------THIS IS REAL VERSION
// opener.open(url); // <------THIS IS FAKE VERSION
}
//-------------------------------------------------------------------------
// Now we have standard (non-method) functions
//-------------------------------------------------------------------------
//---------------------------------shifts contents of folder up one position
function shiftFolder(object1, object2, n) {
object2.p[n] = new addFolder(object1.lname,object1.length,object2.address+".p["+n+"]");
for (var i=0; i<object1.p.length; i++) {
if (object1.p[i].state=="link") {
object2.p[n].p[i]=new addLink(object1.p[i].lname,object1.p[i].link,object2.p[n].address,i);
} else {
object2.p[n].p[i]=new addFolder(object1.p[i].lname,object1.p.length,object2.address+".p["+n+"].p["+i+"]"); //BEN rid of [i]
shiftFolder(object1.p[i],object2.p[n],i);
}
object2.p[n].p[i].state=object1.p[i].state;
object2.p[n].p[i].icon=object1.p[i].icon;
object2.p[n].p[i].pressed=object1.p[i].pressed;
object2.p[n].p[i].exists=object1.p[i].exists;
}
object2.p[n].state=object1.state;
object2.p[n].icon=object1.icon;
object2.p[n].pressed=object1.pressed;
object2.p[n].exists=object1.exists;
}
//--------Method to place folder in middle of other links (this simplifies code)
function insertFolder(position) {
this.bump(position);
shiftFolder(addressCache.p[0],this,position);
}
//---------------------------delete link&folder from within other links&folders
function remove() {
this.exists = false;
}
//---------------------Function that deletes link from dragging it to the trash
function clickTrash() {
dragCache = "";
redrawTree();
}
//-----------------------------------------------------------------------------
// These functions should be incorporated as methods
// and the trash should be a folder. I'll work on that later.
//-----------------------------------------------------------------------------
function hoverTrash() {
// if ( dragCache != "" ) {
// parent.frames[6].document.images[0].src = lastIcon;
// lastIcon = "folder_trash.gif";
// }
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------Add new link
function newLink() {
var clienthref=opener.clientwindow.location.href;
var clienttitle=opener.opener.document.title;
add_link=window.open('','Link','width=360,height=165,scrollbars=0');
savedoc=add_link.document;
var instr='<html><head><title>New Link</title></head><body bgcolor="#bbbbbb">';
instr+='<center><form method="post" name="newLink">';
instr+='<table width="340" height="150" bgcolor="#ffffff" align="center">';
instr+='<tr><td>';
instr+='Name:<br><input type="text" name="title" size="45" value="'+clienttitle+'"><br>';
instr+='Address:<br><input type="text" name="address" size="45" value="'+clienthref+'"><br>';
instr+='<center>';
instr+='<input type="button" value="Save and Position" onclick="javascript:opener.addNewLink(newLink.title.value,newLink.address.value);">';
instr+='<input type="button" value="Close (no save)" onclick="javascript:window.close();">';
instr+='</center></td></tr></table></form></center>';
instr+='</body></html>';
savedoc.write(instr);
savedoc.close();
}
//-------------------------------------------------------Add the just-named link
function addNewLink(title,address) {
dragCache="/res/adm/pages/bookmarkmenu/folder_opened.gif";
lastIcon="/res/adm/pages/bookmarkmenu/folder_opened.gif";
lastDrug="-1";
addressCache=new addLink(title,address,"doesnotmatter",1);
add_link.close();
}
//----------------------------------------------------------------Add new Folder
function newFolder() {
var clienthref=opener.clientwindow.location.href;
var clienttitle=opener.opener.document.title;
add_link=window.open('','Link','width=360,height=165,scrollbars=0');
savedoc=add_link.document;
var instr='<html><head><title>New Folder</title></head><body bgcolor="#bbbbbb">';
instr+='<center><form method="post" name="newFolder">';
instr+='<table width="340" height="150" bgcolor="#ffffff" align="center">';
instr+='<tr><td>';
instr+='Folder Name:<br><input type="text" name="title" size="45" value=""><br>';
instr+='<center>';
instr+='<input type="button" value="Save and Position" onclick="javascript:opener.addNewFolder(newFolder.title.value);">';
instr+='<input type="button" value="Close (no save)" onclick="javascript:window.close();">';
instr+='</center></td></tr></table></form></center>';
instr+='</body></html>';
savedoc.write(instr);
savedoc.close();
}
//----------------------------------------------------Add the just-named folder
function addNewFolder(title) {
dragCache="/res/adm/pages/bookmarkmenu/folder_opened.gif";
lastIcon="/res/adm/pages/bookmarkmenu/folder_opened.gif";
lastDrug="-1";
addressCache=new addFolder(title,1,"doesnotmatter");
add_link.close();
}
//-----------------------------------------------------------------------------
// The following are construction functions.
//-----------------------------------------------------------------------------
//--------------------------------------------------generate the Tree HTML code
function drawTree(depth, folder) {
for (var i=0; i<folder.length; i++) {
if (folder[i].exists) {
if (folder[i].state=="link") {
drawLink(depth,folder[i]);
} else {
drawFolder(depth,folder[i]);
if (folder[i].state=="opened" && folder[i].p.length>=0) {
drawTree(depth+1,folder[i].p);
}
}
}
}
}
//----------------------------------------Writes HTML code for individual folder
function drawFolder(depth, folder) {
parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign=left nowrap>");
parent.frames[4].document.write("<a href=\"javascript:top.bookmarkpal."+folder.address+".swapState();\" ><IMG src='/res/adm/pages/bookmarkmenu/folder_pointer_"+folder.state+".gif' width=15 height=25 border=noborder ></a>");
image_num++;
if ( depth != 1 ) {
image_num++;
parent.frames[4].document.write("<A href=\"javascript:top.bookmarkpal."+folder.address+".clickFolder(document.images["+image_num+"]);\" onmouseover=\"top.bookmarkpal."+folder.address+".hover(document.images["+image_num+"])\" ><IMG src='/res/adm/pages/bookmarkmenu/folder_spacer.gif' width="+ 20*( depth - 1 ) +" height=25 border=noborder></A>");
}
parent.frames[4].document.write("<A href=\"javascript:top.bookmarkpal."+folder.address+".clickFolder(document.images["+image_num+"]);\" onmouseover=\"top.bookmarkpal."+folder.address+".hover(document.images["+image_num+"])\" ><IMG src='"+folder.icon+"' width=25 height=25 border=noborder name="+image_num+"></A>");
image_num++;
parent.frames[4].document.write("<TD valign=middle align=left nowrap><FONT face='Arial, Helvetica'> "+folder.lname+"</FONT></TABLE>");
}
//--------------------------------------Writes HTML code for individual link
function drawLink(depth, folder) {
parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign = middle nowrap>");
image_num++;
parent.frames[4].document.write("<A href=\"javascript:top.bookmarkpal."+folder.address+".clickLink(document.images["+image_num+"], "+folder.position+");\" onmouseover=\"top.bookmarkpal."+folder.address+".p["+ folder.position +"].hover(document.images["+image_num+"])\" ><IMG src='/res/adm/pages/bookmarkmenu/folder_spacer.gif' width="+ 20 * depth +" height=25 border=noborder></A>");
parent.frames[4].document.write("<A href=\"javascript:top.bookmarkpal."+folder.address+".clickLink(document.images["+image_num+"], "+folder.position+");\" onmouseover=\"top.bookmarkpal."+folder.address+".p["+ folder.position +"].hover(document.images["+image_num+"])\" ><IMG src='"+folder.icon+"' width=25 height=25 border=noborder name="+image_num+" ></A>");
image_num++;
parent.frames[4].document.write("<TD valign=middle align=left nowrap><FONT size=-1 face='Arial, Helvetica'><A href=\"javascript:top.clickOnBookmark\( '"+folder.link+"\' );\">"+folder.lname+"</a></FONT></TABLE>");
}
//--------------------Calls Build and draws functions. This function also takes
// care of housekeeping
function initializeTree() {
// bookmarkpal = new buildUserTree();
redrawTree();
}
//--------------------------------Redraws screen without initiallizing anything
function redrawTree() {
// parent.frames[4].document.clear(); //--Remove this line later. clear has depreciated from Javascript
parent.frames[4].document.open();
image_num=0;
parent.frames[4].document.write("<BODY>");
// parent.frames[4].document.write("<p>So far so good</p>"); //--debug
drawTree(1,bookmarkpal.bookmarks.p); //----giving error: bookmarkpal not defined
// parent.frames[4].document.write("<p>Still doing good, after DrawTree</p>");
parent.frames[4].document.write("</BODY>");
}
//-----------------------------------------------------------------------------
// Saves bookmarks by building and then 'put'ing the string that generates the
// bookmarks
//-----------------------------------------------------------------------------
//--------------recovers old bookmarks this is a debug function remove it later
function recover() {
parent.frames[7].document.saveBookmarks.hiddenbookmarks.value='recover';
parent.frames[7].document.saveBookmarks.submit();
}
//----------------------------------------------------------starts save process
function save() {
// parent.frames[4].document.clear(); // debug
// parent.frames[4].document.write("<P>Debug: Beginning save process...</P>"); // debug
saveVar="this.bookmarks=new addFolder(\"Stuff\","+bookmarkpal.bookmarks.p.length+",\"bookmarks\");\n";
saveTree("bookmarks",bookmarkpal.bookmarks.p);
// parent.frames[4].document.write("<p>writing variable...</p>"+saveVar+"<p>...done</p>"); // debug
parent.frames[7].document.saveBookmarks.hiddenbookmarks.value=saveVar;
return true;
}
//---------------------------------------------------------generate save string
function saveTree(depth_string,folder) {
var n=0; // n renumbers links because
for (var i=0; i<folder.length; i++) { // of deleted links
if (folder[i].exists) {
if (folder[i].state=="link") {
saveLink(depth_string,folder[i], n);
} else {
saveFolder(depth_string+".p["+n+"]",folder[i]);
if (folder[i].p.length>=0) {
saveTree(depth_string+".p["+n+"]",folder[i].p);
}
}
n++;
}
}
}
//-------------------------Writes javascript code for generating individual folder
function saveFolder(depth_string, folder) {
saveVar+="this."+depth_string+"=new addFolder(\""+folder.lname+"\","+folder.p.length+",\""+depth_string+"\");\n";
}
//-------------------------Writes javascript code for generating individual link
function saveLink(depth_string,link,newpos) {
saveVar+="this."+depth_string+".p["+newpos+"]=new addLink(\""+link.lname+"\",\""+link.link+"\",\""+depth_string+"\","+newpos+");\n";
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>