Annotation of loncom/html/res/adm/pages/bookmarkmenu/bookmarklib.js, revision 1.6

1.1       tyszkabe    1: // bookmarklib.js
                      2: //
1.2       tyszkabe    3: // Functions combined with the HTML code in admbookmarks.pm
1.6     ! tyszkabe    4: // and code in bookmarkmenu_toolbar.html
1.1       tyszkabe    5: //
                      6: 
                      7: 
                      8: //-------------------------------------------------------------------------------
                      9: // Here are all of the bookmark methods:
                     10: //-------------------------------------------------------------------------------
                     11: 
                     12: //-------------------------------------------------------Adds a link to an object
                     13: function addLink(name, link, address, position) {
                     14:        this.lname = name;
                     15:        this.link = link;
                     16:        this.address = address;
                     17:        this.position = position;
                     18:        this.state = "link";
1.2       tyszkabe   19:        this.icon = "/res/adm/pages/bookmarkmenu/link.gif";
                     20:        this.pressed = "/res/adm/pages/bookmarkmenu/link_pressed.gif";
1.1       tyszkabe   21:        this.hover = hover;
                     22:        this.remove = remove
                     23:        this.exists = true;
                     24: }
                     25: 
                     26: //-----------------------------------------------------Adds a folder to an object
                     27: function addFolder(name, size, address) {
                     28:        this.p = new Array(size);
                     29:        this.lname = name;
                     30:        this.address = address;
                     31:        this.state = "opened";
1.2       tyszkabe   32:        this.icon = "/res/adm/pages/bookmarkmenu/folder_opened.gif";
                     33:        this.pressed = "/res/adm/pages/bookmarkmenu/folder_opened_pressed.gif";
1.1       tyszkabe   34:        this.swapState = swapState;
                     35:        this.clickFolder = clickFolder;
                     36:        this.clickLink = clickLink;
                     37:        this.hover = hover;
                     38:        this.remove = remove;
                     39:        this.bump = bump;
                     40:        this.insertLink = insertLink;
                     41:        this.insertFolder = insertFolder;
                     42:        this.exists = true;
                     43: }
                     44: 
                     45: //-----------------------------------------Swaps folder state from open to closed
                     46: function swapState() {
                     47:        if (this.state == "closed") {
                     48:            this.state = "opened";
1.3       tyszkabe   49:            this.icon = "/res/adm/pages/bookmarkmenu/folder_opened.gif";
                     50:            this.pressed = "/res/adm/pages/bookmarkmenu/folder_opened_pressed.gif";
1.1       tyszkabe   51:        } else {
                     52:            this.state = "closed";
1.3       tyszkabe   53:            this.icon = "/res/adm/pages/bookmarkmenu/folder_closed.gif";
                     54:            this.pressed = "/res/adm/pages/bookmarkmenu/folder_closed_pressed.gif";
1.1       tyszkabe   55:        }
1.4       tyszkabe   56:        redrawTree();
1.1       tyszkabe   57:     }
                     58: 
                     59: //------------------------------------------swaps the dragged icon into the image
                     60: function hover( object ) {
                     61:     if ( dragCache != "" ) {
                     62:         object.src = this.pressed;
                     63:         parent.frames[4].document.images[lastDrug].src = lastIcon;
                     64:         lastIcon = this.icon;
                     65:         lastDrug = object.name;
                     66:     }
                     67: }
                     68: 
                     69: //------------------------------------------------------The click on link  method
                     70: function clickLink( object, position ) {
                     71:     if (dragCache=="") {                            //---------'pick up' the icon
                     72:         dragCache=object.src;
                     73:         lastIcon=this.p[position].icon;
                     74:         lastDrug=object.name;
                     75:         addressCache=new addLink(this.p[position].lname,this.p[position].link,this.p[position].address,position);
                     76:         this.p[position].remove();
                     77:     } else {                                        //--------'put down' the icon
                     78:         dragCache="";
                     79:         if ( addressCache.state == "link" ) {       //------differently if a link
                     80:           this.insertLink( position );
                     81:         } else {                                    //--------------than a folder
                     82:           this.insertFolder( position );
                     83:         }
                     84:         redrawTree();
                     85:     }
                     86: }
                     87: 
                     88: //-----------------------------------------------------The click on Folder method
                     89: function clickFolder( object ) {
                     90:     if (dragCache=="") {                            //---------'pick up' the icon
                     91:         dragCache=object.src;
                     92:         lastIcon=this.icon;
                     93:         lastDrug=object.name;
1.2       tyszkabe   94:         addressCache=new addFolder("lame_folder",2,"doesnotmatter"); //used to be (this.lname,this.p.length+1,this.address)
                     95:       shiftFolder(this,addressCache,1);               //this is  a whole new line
1.1       tyszkabe   96:         this.remove();
                     97:     } else {                                        //--------'put down' the icon
                     98:         dragCache = "";
                     99:            if ( addressCache.state == "link" ) {    //-----place link into folder
                    100:               this.insertLink( 1 );
                    101: //              this.p[ this.p.length ] = new addLink( addressCache.lname, addressCache.link, this.address, this.p.length );
                    102:            } else {                                 //---place folder into folder
                    103:                 this.insertFolder( 1 );
                    104: //              this.p[ this.p.length ] = new addFolder( addressCache.lname, addressCache.p.length, this.address+".p["+this.p.length+"]" );
                    105: //              this.p[ this.p.length ].p = addressCache.p;
                    106:            }
                    107:     redrawTree();
                    108:     }
                    109: }
                    110: 
                    111: //---------Folder method to place link in middle of other links (should simplify code)
                    112: function insertLink( position ) {
                    113:         this.bump( position );
                    114:         this.p[ position ] = new addLink( addressCache.lname, addressCache.link, this.address, position );
                    115: }
                    116: 
                    117: 
                    118: //---------------------------Recursive folder Method to bump items up one to make room 
                    119: //                                   for new item. Currently, bump doesn't always work.
                    120: function bump( position ) {
                    121: // ORIGINAL RECURSIVE VERSION has short-comings but may work better because of 'not exists'
                    122: //
                    123: //        if ( this.length >= position ) {
                    124: //           this.bump( position + 1 );
                    125: //        }
                    126: //        position;
                    127: //        this.p[ position + 1 ] = this.p[position ];
                    128: //
                    129: // NON RECURSIVE of the same thing (I'll probably stick with this).
                    130: // I can combine the creation and shifting if everything works out nicely
                    131: // Worry about what happens to the first link.
                    132:         var i=this.p.length;                   // First, create a new spot at the top.
                    133:         if (this.p[i-1].state=="link") {
                    134:            this.p[i]=new addLink(this.p[i-1].lname,this.p[i-1].link,this.address,i);
                    135:         } else {
                    136:           this.p[i]=new addFolder(this.p[i-1].lname,this.p[i-1].p.length,this.address+".p["+i+"]"); 
                    137:         }
                    138:         this.p[i].exists=this.p[i-1].exists;                    // preserve existance
                    139:         i--;                                           // Second, shift others up one
                    140:         while ( i >= position + 1 ) {
                    141:            if ( this.p[i-1].state == "link" ) {
                    142:               this.p[ i ] = new addLink( this.p[ i-1 ].lname, this.p[i-1].link, this.p[i-1].address, i);
                    143:            } else {
                    144:               this.p[ i ] = new addFolder( this.p[ i-1 ].lname, this.p[i-1].p.length, this.address+".p["+ i +"]" );
                    145:                                                 // move all of the inner folder stuff
                    146:               shiftFolder( this.p[i-1], this, i);
                    147:            }
                    148: //           this.p[i].address = this.p[i-1].address;
                    149: //           this.p[i].exists = this.p[ i-1 ].exists;           // preserve existance
                    150:            i--;
                    151:         }
                    152: }
                    153: 
                    154: //-----------------------------------------------------------Opens the page
                    155: function clickOnBookmark(url) {
1.3       tyszkabe  156:        open(url,opener.clientwindow.name); // <------THIS IS REAL VERSION
                    157: //       opener.open(url);                    // <------THIS IS FAKE VERSION
1.1       tyszkabe  158: }
                    159: 
                    160: 
                    161: 
                    162: 
                    163: //-------------------------------------------------------------------------
                    164: // Now we have standard (non-method) functions
                    165: //-------------------------------------------------------------------------
                    166: 
                    167: 
                    168: //---------------------------------shifts contents of folder up one position
                    169: function shiftFolder(object1, object2, n) {
                    170:       object2.p[n] = new addFolder(object1.lname,object1.length,object2.address+".p["+n+"]");
                    171:       for (var i=1; i<object1.p.length; i++) {
                    172:         if (object1.p[i].state=="link") {
                    173:           object2.p[n].p[i]=new addLink(object1.p[i].lname,object1.p[i].link,object2.p[n].address,i);
                    174:         } else {
                    175:           object2.p[n].p[i]=new addFolder(object1.p[i].lname,object1.p[i].length,object2.address+".p["+n+"].p["+i+"]");
                    176:           shiftFolder(object1.p[i],object2.p[n],i);
                    177:         }
                    178:         object2.p[n].p[i].state=object1.p[i].state;
                    179:         object2.p[n].p[i].icon=object1.p[i].icon;
                    180:         object2.p[n].p[i].pressed=object1.p[i].pressed;
                    181:         object2.p[n].p[i].exists=object1.p[i].exists;      
                    182:       }
                    183:       object2.p[n].state=object1.state;
                    184:       object2.p[n].icon=object1.icon;
                    185:       object2.p[n].pressed=object1.pressed;
                    186:       object2.p[n].exists=object1.exists;      
                    187: }
                    188: 
                    189: 
                    190: //--------Method to place folder in middle of other links (this simplifies code)
                    191: function insertFolder(position) {
                    192:         this.bump(position);
                    193:         shiftFolder(addressCache.p[1],this,position);
                    194: }
                    195: 
                    196: 
                    197: //---------------------------delete link&folder from within other links&folders
                    198: function remove() {
                    199:         this.exists = false;
                    200: }
                    201: 
                    202: //---------------------Function that deletes link from dragging it to the trash
                    203: function clickTrash() {
                    204:         dragCache = "";
                    205:         redrawTree();
                    206: }
                    207: 
                    208: //-----------------------------------------------------------------------------
                    209: // These functions should be incorporated as methods
                    210: // and the trash should be a folder. I'll work on this later.
                    211: //-----------------------------------------------------------------------------
                    212: function hoverTrash() {
                    213: //        if ( dragCache != "" ) {
                    214: //           parent.frames[6].document.images[0].src = lastIcon;
                    215: //           lastIcon = "folder_trash.gif";
                    216: //        }       
                    217: }
                    218: //----------------------------------------------------------------------------
                    219: 
1.4       tyszkabe  220: //----------------------------------------------------------------Add new link
                    221: function newLink() {
1.6     ! tyszkabe  222:    var clienthref=opener.clientwindow.location.href;
        !           223:    var clienttitle=opener.opener.document.title;
        !           224:    add_link=window.open('','Link','width=400,height=200,scrollbars=0');
        !           225:    savedoc=add_link.document;
        !           226:    var instr='<html><head><title>New Link</title></head><body>';
        !           227:    instr+='<form method="post" name="newlink">';
        !           228:    instr+='<input type="radio" name="drag" value="drag" checked>Point to position<br>';
        !           229:    instr+='<input type="radio" name="drag" value="append" checked>Append link to end<br>';
        !           230:    instr+='Name: <input type="text" name="title" defaultvalue="'+clienthref+'"><br>';
        !           231:    instr+='URL: <input type="text" name="url" defaultvalue="'+clienttitle+'">';
        !           232:    instr+='</form>';
        !           233:    instr+='</body></html>';
        !           234:    savedoc.write(instr);
        !           235:    savedoc.close();
1.4       tyszkabe  236: }
1.1       tyszkabe  237: 
                    238: //-----------------------------------------------------------------------------
                    239: // The following are construction functions.
                    240: //-----------------------------------------------------------------------------
                    241: 
                    242: //--------------------------------------------------generate the Tree HTML code
                    243: function drawTree(depth, folder) {
                    244:    for (var i=1; i<folder.length; i++) {
                    245:       if (folder[i].exists) {
                    246:          if (folder[i].state=="link") {
                    247:             drawLink(depth,folder[i]);
                    248:          } else {
                    249:             drawFolder(depth,folder[i]);
                    250:             if (folder[i].state=="opened" && folder[i].p.length>=0) {
                    251:                drawTree(depth+1,folder[i].p);
                    252:             }
                    253:          }
                    254:       }
                    255:    }
                    256: }
                    257: 
                    258: //----------------------------------------Writes HTML code for individual folder
                    259: function drawFolder(depth, folder) {
                    260:        parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign=left nowrap>");
1.2       tyszkabe  261:        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>");
1.1       tyszkabe  262:        image_num++;
                    263:        if ( depth != 1 ) {
                    264:            image_num++;
1.2       tyszkabe  265:            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>");
1.1       tyszkabe  266:        }
                    267:        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>");
                    268:        image_num++;
                    269:        parent.frames[4].document.write("<TD valign=middle align=left nowrap><FONT face='Arial, Helvetica'> "+folder.lname+"</FONT></TABLE>");
                    270: }
                    271: 
                    272: //--------------------------------------Writes HTML code for individual link
                    273: function drawLink(depth, folder) {
                    274:        parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign = middle nowrap>");
                    275:        image_num++;
1.2       tyszkabe  276:        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>");
1.1       tyszkabe  277: 
                    278:        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>");
                    279:        image_num++;
                    280:        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>");
                    281: }
                    282: 
                    283: //--------------------Calls Build and draws functions. This function also takes
                    284: //                                                         care of housekeeping
                    285: function initializeTree() {
1.5       tyszkabe  286: //       bookmarkpal = new buildUserTree();
1.1       tyszkabe  287:        redrawTree();
                    288: }
                    289: 
                    290: //--------------------------------Redraws screen without initiallizing anything
                    291: function redrawTree() {
1.5       tyszkabe  292: //       parent.frames[4].document.clear();  //--Remove this line later. clear has depreciated from Javascript
                    293:        parent.frames[4].document.open();
                    294:        image_num=0;
1.1       tyszkabe  295:        parent.frames[4].document.write("<BODY>");
1.5       tyszkabe  296: //       parent.frames[4].document.write("<p>So far so good</p>");  //--debug
                    297:        drawTree(1,bookmarkpal.bookmarks.p);     //----giving error: bookmarkpal not defined
                    298: //       parent.frames[4].document.write("<p>Still doing good, after DrawTree</p>");
1.1       tyszkabe  299:        parent.frames[4].document.write("</BODY>");
                    300: }
                    301: 
1.2       tyszkabe  302: //-----------------------------------------------------------------------------
                    303: // Saves bookmarks by building and then 'put'ing the string that generates the
                    304: // bookmarks
                    305: //-----------------------------------------------------------------------------
                    306: 
                    307: //----------------------------------------------------------starts save process
                    308: function save() {
1.5       tyszkabe  309:    parent.frames[4].document.clear();                                    // debug
1.4       tyszkabe  310:    parent.frames[4].document.write("<P>Debug: Beginning save process...</P>");   // debug
1.3       tyszkabe  311:    saveVar="this.bookmarks=new addFolder('Stuff',"+bookmarkpal.bookmarks.p.length+",'bookmarks')<br>\n";
                    312:    saveTree("this.bookmarks",bookmarkpal.bookmarks.p);
                    313:    parent.frames[4].document.write("<p>writing variable...</p>"+saveVar+"<p>...done</p>");      // debug
                    314:    parent.frames[7].document.saveBookmarks.hidden_bookmarks=saveVar;
                    315:    return true;
1.2       tyszkabe  316: }
                    317: 
                    318: 
1.3       tyszkabe  319: //---------------------------------------------------------generate save string
1.2       tyszkabe  320: function saveTree(depth_string,folder) {
                    321:    for (var i=1; i<folder.length; i++) {
                    322:       if (folder[i].exists) {
                    323:          if (folder[i].state=="link") {
1.3       tyszkabe  324:             saveLink(depth_string+".p["+i+"]",folder[i]);
1.2       tyszkabe  325:          } else {
1.3       tyszkabe  326:             saveFolder(depth_string+".p["+i+"]",folder[i]);
1.2       tyszkabe  327:             if (folder[i].state=="opened" && folder[i].p.length>=0) {
                    328:                saveTree(depth_string+".p["+i+"]",folder[i].p);
                    329:             }
                    330:          }
                    331:       }
                    332:    }
1.1       tyszkabe  333: }
1.2       tyszkabe  334: 
                    335: 
                    336: //-------------------------Writes javascript code for generating individual folder
                    337: function saveFolder(depth_string, folder) {
1.3       tyszkabe  338:        saveVar+=depth_string+"=new addfolder('"+folder.lname+"',"+folder.p.length+",'"+folder.address+"');<BR>\n";
1.2       tyszkabe  339: }
                    340: 
                    341: 
                    342: //-------------------------Writes javascript code for generating individual link
                    343: function saveLink(depth_string,link) {
1.3       tyszkabe  344:        saveVar+=depth_string+"=new addlink('"+link.lname+"','"+link.link+"','"+link.address+"',"+link.position+");<BR>\n";
1.5       tyszkabe  345: }
                    346: 
                    347: 
                    348: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>