Changeset 825

Show
Ignore:
Timestamp:
2008-03-02 20:17:54 (10 months ago)
Author:
MaierMan
Message:

#555: Allow Menu Editor to do (some) of its dirty work.
Needs testing!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0.x/chrome/content/integration/elements.js

    r801 r825  
    356356        init: function() { 
    357357                try { 
    358                         var ctx = document.getElementById("dtaCtxCompact").parentNode; 
     358                        this.direct = {}; 
     359                        this.compact = {}; 
     360                         
     361                        var ctxItem = document.getElementById("dtaCtxCompact"); 
     362                        var ctx = ctxItem.parentNode; 
     363                        var cont = document.getElementById('dtaCtxSubmenu'); 
     364                         
     365                        ['SepBack', 'Pref', 'SepPref', 'TDTA', 'DTA', 'SaveT', 'Save', 'SepFront'].forEach( 
     366                                function(id) { 
     367                                        this.compact[id] = document.getElementById('dtaCtx' + id); 
     368                                        var node = document.getElementById('dtaCtx' + id).cloneNode(true); 
     369                                        node.setAttribute('id', node.id + "-direct"); 
     370                                        ctx.insertBefore(node, ctxItem.nextSibling); 
     371                                        this.direct[id] = node; 
     372                                }, 
     373                                this 
     374                        ); 
     375                        // intitalize those to have Menu Editor pick up "good" text 
     376                        [this.direct, this.compact].forEach( 
     377                                function(m) { 
     378                                        m.Save.label = this.getString('dtasavelink'); 
     379                                        m.SaveT.label = this.getString('turbosavelink'); 
     380                                }, 
     381                                this 
     382                        ); 
     383 
    359384                        var menu = document.getElementById("dtaToolsMenu").parentNode; 
    360385                        ctx.addEventListener("popupshowing", function (evt) { DTA_ContextOverlay.onContextShowing(evt); }, false); 
    361386                        menu.addEventListener("popupshowing", function (evt) { DTA_ContextOverlay.onToolsShowing(evt); }, false); 
    362387 
    363                         // prepare ctx object 
    364                         // order is important!                   
    365                         this.ctx = {}; 
    366                         ['SepBack', 'Pref', 'SepPref', 'TDTA', 'DTA', 'SaveT', 'Save', 'SepFront'].forEach( 
    367                                 function (e) { 
    368                                         this.ctx[e] = document.getElementById('dtaCtx' + e); 
    369                                 }, 
    370                                 this 
    371                         ); 
    372388                        this.ctxBase = document.getElementById('dtaCtxCompact'); 
    373                         this.ctxMenu = document.getElementById('dtaCtxSubmenu'); 
    374389                         
    375390                        // prepare tools 
     
    391406        }, 
    392407        get contextMenu() { 
    393                        if (window.gContextMenu !=  null) { 
    394                                return gContextMenu; 
    395                        
    396                        var cm = { 
    397                                onLink: false, 
    398                                onImage: false, 
    399                                target: document.popupNode, 
    400                                fake: true 
    401                        }; 
    402                        if (cm.target) { 
    403                                var node = cm.target; 
    404                                if (node instanceof Components.interfaces.nsIImageLoadingContent && node.currentURI) { 
    405                                        cm.onImage = true; 
    406                                
    407                                while (node && !cm.onLink) { 
    408                                        if (node instanceof HTMLAnchorElement && node.href) { 
    409                                                cm.onLink = true; 
    410                                        }                               
    411                                        node = node.parentNode; 
    412                                
    413                        
    414                        return cm; 
     408                if (window.gContextMenu !=  null) { 
     409                        return gContextMenu; 
     410               
     411                var cm = { 
     412                        onLink: false, 
     413                        onImage: false, 
     414                        target: document.popupNode, 
     415                        fake: true 
     416                }; 
     417                if (cm.target) { 
     418                        var node = cm.target; 
     419                        if (node instanceof Components.interfaces.nsIImageLoadingContent && node.currentURI) { 
     420                                cm.onImage = true; 
     421                       
     422                        while (node && !cm.onLink) { 
     423                                if (node instanceof HTMLAnchorElement && node.href) { 
     424                                        cm.onLink = true; 
     425                                }                              
     426                                node = node.parentNode; 
     427                       
     428               
     429                return cm; 
    415430        }, 
    416431        onContextShowing: function(evt) { 
     
    418433                        var ctx = this.contextMenu; 
    419434                        // get settings 
    420                         var menu = DTA_preferences.getDTA("ctxmenu", "1,1,0").split(",").map(function(e){return parseInt(e);}); 
     435                        var items = DTA_preferences.getDTA("ctxmenu", "1,1,0").split(",").map(function(e){return parseInt(e);}); 
    421436                        var compact = DTA_preferences.getDTA("ctxcompact", false); 
    422                          
    423                         // all hidden... 
    424                         if (menu.indexOf(1) == -1) { 
    425                                 for (var i in this.ctx) { 
    426                                         this.ctx[i].hidden = true; 
    427                                 } 
     437 
     438                        var menu; 
     439                        if (compact) { 
     440                                this.ctxBase.hidden = false; 
     441                                menu = this.compact; 
     442                        } 
     443                        else { 
    428444                                this.ctxBase.hidden = true; 
    429                                 return; 
     445                                menu = this.direct; 
     446                        } 
     447                         
     448                        // hide all 
     449                        for (var i in menu) { 
     450                                this.direct[i].hidden = true; 
     451                                this.compact[i].hidden = true; 
    430452                        } 
    431453                         
     
    436458                        // hovering an image or link 
    437459                        if (ctx && (ctx.onLink || ctx.onImage)) { 
    438                                 if (menu[0]) { 
    439                                         show.push('Save'); 
    440                                 } 
    441                                 if (menu[1]) { 
    442                                         show.push('SaveT'); 
    443                                 } 
    444                                 this.ctx.Save.label = this.getString('dtasave' + (ctx.onLink ? 'link' : 'image')); 
    445                                 this.ctx.SaveT.label = this.getString('turbosave' + (ctx.onLink ? 'link' : 'image')); 
     460                                if (items[0]) { 
     461                                        show.push(menu.Save); 
     462                                } 
     463                                if (items[1]) { 
     464                                        show.push(menu.SaveT); 
     465                                } 
     466                                menu.Save.label = this.getString('dtasave' + (ctx.onLink ? 'link' : 'image')); 
     467                                menu.SaveT.label = this.getString('turbosave' + (ctx.onLink ? 'link' : 'image')); 
    446468                        } 
    447469                        // regular 
    448470                        if (ctx && (ctx.fake || !(ctx.onLink || ctx.onImage))) { 
    449                                 if (menu[0]) { 
    450                                         show.push('DTA'); 
    451                                 } 
    452                                 if (menu[1]) { 
    453                                         show.push('TDTA'); 
     471                                if (items[0]) { 
     472                                        show.push(menu.DTA); 
     473                                } 
     474                                if (items[1]) { 
     475                                        show.push(menu.TDTA); 
    454476                                } 
    455477                                var sel = document.commandDispatcher.focusedWindow.getSelection(); 
    456478                                sel = sel && !sel.isCollapsed; 
    457                                 this.ctx.DTA.label = this.getString('dta' + (sel ? 'selection' : 'regular')); 
    458                                 this.ctx.TDTA.label = this.getString('turbo' + (sel ? 'selection' : 'regular')); 
     479                                menu.DTA.label = this.getString('dta' + (sel ? 'selection' : 'regular')); 
     480                                menu.TDTA.label = this.getString('turbo' + (sel ? 'selection' : 'regular')); 
    459481                        } 
    460482                         
    461483                        // prefs 
    462                         if (menu[2]) { 
    463                                 show.push('Pref'); 
    464                                 if (compact) { 
    465                                         show.push('SepPref'); 
    466                                 } else { 
    467                                         show.push('SepBack'); 
    468                                         show.push('SepFront'); 
    469                                 } 
    470                         } 
    471                          
    472                         // general setup 
    473                         var base = document.getElementById(this.ctxBase.getAttribute('insertafter')); 
    474                         if (compact) { 
    475                                 this.ctxBase.hidden = false;                             
    476                                 base.parentNode.insertBefore(this.ctxBase, base.nextSibling);                            
    477                         } else { 
    478                                 this.ctxBase.hidden = true; 
    479                         } 
    480                          
    481                         // show the items. 
    482                         for (var i in this.ctx) { 
    483                                 var cur = this.ctx[i]; 
    484                                 cur.hidden = show.indexOf(i) == -1; 
    485                                 if (cur.hidden) { 
     484                        if (items[2]) { 
     485                                show.push(menu.Pref); 
     486                                if (compact && (items[0] || items[1])) { 
     487                                        show.push(menu.SepPref); 
     488                                } 
     489                        } 
     490                         
     491                        // show the seperators, if required. 
     492                        var n = menu.SepFront; 
     493                        while ((n = n.previousSibling)) { 
     494                                if (n.hidden) { 
    486495                                        continue; 
    487496                                } 
    488                                 if (compact) { 
    489                                         this.ctxMenu.insertBefore(cur, this.ctxMenu.firstChild); 
    490                                 } 
    491                                 else { 
    492                                         base.parentNode.insertBefore(cur, base); 
    493                                         base = cur; 
    494                                 } 
    495                         } 
    496                          
    497                         // add separators 
    498                         if (!compact) { 
    499                                 var node = this.ctx.SepFront.previousSibling; 
    500                                 while (node && node.hidden) { 
    501                                         node = node.previousSibling; 
    502                                 } 
    503                                 if (node && node.nodeName == 'menuseparator') { 
    504                                         this.ctx.SepFront.hidden = true; 
    505                                 } 
    506                                 node = this.ctx.SepBack.nextSibling; 
    507                                 while (node && node.hidden) { 
    508                                         node = node.nextSibling; 
    509                                 } 
    510                                 if (node && node.nodeName == 'menuseparator') { 
    511                                         this.ctx.SepBack.hidden = true; 
    512                                 }                 
    513                         } 
     497                                if (n.nodeName != 'menuseparator') { 
     498                                        show.push(menu.SepFront); 
     499                                } 
     500                                break; 
     501                        } 
     502                        n = menu.SepBack; 
     503                        while ((n = n.nextSibling)) { 
     504                                if (n.hidden) { 
     505                                        continue; 
     506                                } 
     507                                if (n.nodeName != 'menuseparator') { 
     508                                        show.push(menu.SepBack); 
     509                                } 
     510                                break; 
     511                        } 
     512                         
     513                        show.forEach( 
     514                                function (node) { 
     515                                        node.hidden = false; 
     516                                } 
     517                        ); 
     518 
    514519                } catch(ex) { 
    515520                        DTA_debug.dump("DTAContext(): ", ex);