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

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">';
1.7     ! tyszkabe  228:    instr+='Name: <input type="text" name="title" value="'+clienthref+'"><br>';
        !           229:    instr+='URL: <input type="text" name="url" value="'+clienttitle+'"><br>';
        !           230:    instr+='<input type="button" value="Save (at bottom)" onclick="javascript:">';
        !           231:    instr+='<input type="button" value="Save (within)" onclick="javascript:">';
        !           232:    instr+='<input type="button" value="Close (no save)" onclick="javascript:window.close();">';
1.6       tyszkabe  233:    instr+='</form>';
                    234:    instr+='</body></html>';
                    235:    savedoc.write(instr);
                    236:    savedoc.close();
1.4       tyszkabe  237: }
1.1       tyszkabe  238: 
                    239: //-----------------------------------------------------------------------------
                    240: // The following are construction functions.
                    241: //-----------------------------------------------------------------------------
                    242: 
                    243: //--------------------------------------------------generate the Tree HTML code
                    244: function drawTree(depth, folder) {
                    245:    for (var i=1; i<folder.length; i++) {
                    246:       if (folder[i].exists) {
                    247:          if (folder[i].state=="link") {
                    248:             drawLink(depth,folder[i]);
                    249:          } else {
                    250:             drawFolder(depth,folder[i]);
                    251:             if (folder[i].state=="opened" && folder[i].p.length>=0) {
                    252:                drawTree(depth+1,folder[i].p);
                    253:             }
                    254:          }
                    255:       }
                    256:    }
                    257: }
                    258: 
                    259: //----------------------------------------Writes HTML code for individual folder
                    260: function drawFolder(depth, folder) {
                    261:        parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign=left nowrap>");
1.2       tyszkabe  262:        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  263:        image_num++;
                    264:        if ( depth != 1 ) {
                    265:            image_num++;
1.2       tyszkabe  266:            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  267:        }
                    268:        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>");
                    269:        image_num++;
                    270:        parent.frames[4].document.write("<TD valign=middle align=left nowrap><FONT face='Arial, Helvetica'> "+folder.lname+"</FONT></TABLE>");
                    271: }
                    272: 
                    273: //--------------------------------------Writes HTML code for individual link
                    274: function drawLink(depth, folder) {
                    275:        parent.frames[4].document.write("<TABLE border=0 cellspacing=0 cellpadding=0><TR><TD valign = middle nowrap>");
                    276:        image_num++;
1.2       tyszkabe  277:        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  278: 
                    279:        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>");
                    280:        image_num++;
                    281:        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>");
                    282: }
                    283: 
                    284: //--------------------Calls Build and draws functions. This function also takes
                    285: //                                                         care of housekeeping
                    286: function initializeTree() {
1.5       tyszkabe  287: //       bookmarkpal = new buildUserTree();
1.1       tyszkabe  288:        redrawTree();
                    289: }
                    290: 
                    291: //--------------------------------Redraws screen without initiallizing anything
                    292: function redrawTree() {
1.5       tyszkabe  293: //       parent.frames[4].document.clear();  //--Remove this line later. clear has depreciated from Javascript
                    294:        parent.frames[4].document.open();
                    295:        image_num=0;
1.1       tyszkabe  296:        parent.frames[4].document.write("<BODY>");
1.5       tyszkabe  297: //       parent.frames[4].document.write("<p>So far so good</p>");  //--debug
                    298:        drawTree(1,bookmarkpal.bookmarks.p);     //----giving error: bookmarkpal not defined
                    299: //       parent.frames[4].document.write("<p>Still doing good, after DrawTree</p>");
1.1       tyszkabe  300:        parent.frames[4].document.write("</BODY>");
                    301: }
                    302: 
1.2       tyszkabe  303: //-----------------------------------------------------------------------------
                    304: // Saves bookmarks by building and then 'put'ing the string that generates the
                    305: // bookmarks
                    306: //-----------------------------------------------------------------------------
                    307: 
                    308: //----------------------------------------------------------starts save process
                    309: function save() {
1.5       tyszkabe  310:    parent.frames[4].document.clear();                                    // debug
1.4       tyszkabe  311:    parent.frames[4].document.write("<P>Debug: Beginning save process...</P>");   // debug
1.3       tyszkabe  312:    saveVar="this.bookmarks=new addFolder('Stuff',"+bookmarkpal.bookmarks.p.length+",'bookmarks')<br>\n";
                    313:    saveTree("this.bookmarks",bookmarkpal.bookmarks.p);
                    314:    parent.frames[4].document.write("<p>writing variable...</p>"+saveVar+"<p>...done</p>");      // debug
                    315:    parent.frames[7].document.saveBookmarks.hidden_bookmarks=saveVar;
                    316:    return true;
1.2       tyszkabe  317: }
                    318: 
                    319: 
1.3       tyszkabe  320: //---------------------------------------------------------generate save string
1.2       tyszkabe  321: function saveTree(depth_string,folder) {
                    322:    for (var i=1; i<folder.length; i++) {
                    323:       if (folder[i].exists) {
                    324:          if (folder[i].state=="link") {
1.3       tyszkabe  325:             saveLink(depth_string+".p["+i+"]",folder[i]);
1.2       tyszkabe  326:          } else {
1.3       tyszkabe  327:             saveFolder(depth_string+".p["+i+"]",folder[i]);
1.2       tyszkabe  328:             if (folder[i].state=="opened" && folder[i].p.length>=0) {
                    329:                saveTree(depth_string+".p["+i+"]",folder[i].p);
                    330:             }
                    331:          }
                    332:       }
                    333:    }
1.1       tyszkabe  334: }
1.2       tyszkabe  335: 
                    336: 
                    337: //-------------------------Writes javascript code for generating individual folder
                    338: function saveFolder(depth_string, folder) {
1.3       tyszkabe  339:        saveVar+=depth_string+"=new addfolder('"+folder.lname+"',"+folder.p.length+",'"+folder.address+"');<BR>\n";
1.2       tyszkabe  340: }
                    341: 
                    342: 
                    343: //-------------------------Writes javascript code for generating individual link
                    344: function saveLink(depth_string,link) {
1.3       tyszkabe  345:        saveVar+=depth_string+"=new addlink('"+link.lname+"','"+link.link+"','"+link.address+"',"+link.position+");<BR>\n";
1.5       tyszkabe  346: }
                    347: 
                    348: 
                    349: 

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