// 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(size);
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;
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",2,"doesnotmatter"); //used to be (this.lname,this.p.length+1,this.address)
shiftFolder(this,addressCache,1); //this is a whole new line
this.remove();
} else { //--------'put down' the icon
dragCache = "";
if ( addressCache.state == "link" ) { //-----place link into folder
this.insertLink( 1 );
// this.p[ this.p.length ] = new addLink( addressCache.lname, addressCache.link, this.address, this.p.length );
} else { //---place folder into folder
this.insertFolder( 1 );
// 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
// for new item. Currently, bump doesn't always work.
function bump( position ) {
// ORIGINAL RECURSIVE VERSION has short-comings but may work better because of 'not exists'
//
// if ( this.length >= position ) {
// this.bump( position + 1 );
// }
// position;
// this.p[ position + 1 ] = this.p[position ];
//
// NON RECURSIVE of the same thing (I'll probably stick with this).
// I can combine the creation and shifting if everything works out nicely
// Worry about what happens to the first link.
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);
}
// this.p[i].address = this.p[i-1].address;
// this.p[i].exists = this.p[ i-1 ].exists; // preserve existance
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=1; 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[i].length,object2.address+".p["+n+"].p["+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[1],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 this 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=400,height=200,scrollbars=0');
savedoc=add_link.document;
var instr='<html><head><title>New Link</title></head><body>';
instr+='<form method="post" name="newlink">';
instr+='Name: <input type="text" name="title" value="'+clienthref+'"><br>';
instr+='URL: <input type="text" name="url" value="'+clienttitle+'"><br>';
instr+='<input type="button" value="Save (at bottom)" onclick="javascript:">';
instr+='<input type="button" value="Save (within)" onclick="javascript:">';
instr+='<input type="button" value="Close (no save)" onclick="javascript:window.close();">';
instr+='</form>';
instr+='</body></html>';
savedoc.write(instr);
savedoc.close();
}
//-----------------------------------------------------------------------------
// The following are construction functions.
//-----------------------------------------------------------------------------
//--------------------------------------------------generate the Tree HTML code
function drawTree(depth, folder) {
for (var i=1; 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
//-----------------------------------------------------------------------------
//----------------------------------------------------------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')<br>\n";
saveTree("this.bookmarks",bookmarkpal.bookmarks.p);
parent.frames[4].document.write("<p>writing variable...</p>"+saveVar+"<p>...done</p>"); // debug
parent.frames[7].document.saveBookmarks.hidden_bookmarks=saveVar;
return true;
}
//---------------------------------------------------------generate save string
function saveTree(depth_string,folder) {
for (var i=1; i<folder.length; i++) {
if (folder[i].exists) {
if (folder[i].state=="link") {
saveLink(depth_string+".p["+i+"]",folder[i]);
} else {
saveFolder(depth_string+".p["+i+"]",folder[i]);
if (folder[i].state=="opened" && folder[i].p.length>=0) {
saveTree(depth_string+".p["+i+"]",folder[i].p);
}
}
}
}
}
//-------------------------Writes javascript code for generating individual folder
function saveFolder(depth_string, folder) {
saveVar+=depth_string+"=new addfolder('"+folder.lname+"',"+folder.p.length+",'"+folder.address+"');<BR>\n";
}
//-------------------------Writes javascript code for generating individual link
function saveLink(depth_string,link) {
saveVar+=depth_string+"=new addlink('"+link.lname+"','"+link.link+"','"+link.address+"',"+link.position+");<BR>\n";
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>