0e917cf5aad98c54a7ac5d1fdcf505f692dedaff
tdreszer
  Thu Apr 25 12:58:58 2013 -0700
Reworked the haplotypes section of hgGene for persistence of cart vars (they will timeout after 3 days) and added reset to default.  Given that the section is scrolled out of visible range and that some display options can be burdensome, it seemed fitting that cart vars will expire when not actively used.
diff --git src/hg/js/alleles.js src/hg/js/alleles.js
index 6644157..1e27139 100644
--- src/hg/js/alleles.js
+++ src/hg/js/alleles.js
@@ -1,29 +1,35 @@
 // JavaScript Especially for the Gene Haplotype Alleles section of hgGene CGI
 
 // Tell jslint and browser to use strict mode for this file:
 "use strict";
 
 // Alleles supports gene haplotype alleles section
 var alleles = (function()
 {
 
     var sectionName         = "haplotypes";  // This define is used throughout
     var seqCharsPerPos      = 1;  // These globals should only need to be calculated once
     var seqPxPerPos         = 7;
     var ajaxUpdates         = 0;
 
+    // persistence between ajax calls
+    var persistRareHapsShown = false; 
+    var persistScoresShown   = false; 
+    var persistSortReverse   = false;
+    var persistSortColId     = '';
+
 
     function initSortTable()
     { // Initialize the sortable table
         var allelesTable = $('table#alleles.sortable');
         if (allelesTable.length === 1) {
             sortTable.initialize(allelesTable[0],false);
             sortTable.sortCaseSensitive(true);
         }
     }
 
     function update(content, status)
     { // Update the geneAlleles section based upon ajax request
         hideLoadingImage(this.loadingId);  // Do this first
         
         var geneAlleles = $('div#' + sectionName);
@@ -67,66 +73,71 @@
                 dataType: "html",
                 trueSuccess: update,
                 success: catchErrorOrDispatch,
                 error: errorHandler,
                 //async: false,
                 //cmd: cmd,
                 loadingId: showLoadingImage(sectionName),
                 cache: false
             });
     }
     
     function afterSort(obj)
     {   // The sort table is controlled by utls.js, but this is some allele specific stuff
     
         var thisId = $(obj).attr('id');
-        if (alleles.sortColId != thisId)
-            alleles.sortColId  = thisId;
+        if (persistSortColId != thisId)
+            persistSortColId  = thisId;
         else
-            alleles.sortReverse = ( !alleles.sortReverse );
+            persistSortReverse = ( !persistSortReverse );
         
         // Sorted variant is hilited special
         if ($(obj).hasClass('var')) {
             hiliteId = thisId;
             hiliteSpecial( hiliteId );
         }
     }
 
     function persistThroughUpdates()
     { // When an ajax update occurs, restore the non-ajax settings state
         
         // See if table was sorted previously
-        if (alleles.sortColId != '') {
+        if (persistSortColId != '') {
     
-            var col = $('table#alleles').find('TH#' + alleles.sortColId);
+            var col = $('table#alleles').find('TH#' + persistSortColId);
             if (col != undefined && col.length != 0) {
                 var colIx = Number( $(col).attr('cellIndex') );
                 $(col).click();
-                if (alleles.sortReverse) {// click twice if reverse order!
-                    alleles.sortReverse = false; // Needed so that it is set again
-                    setTimeout("$('table#alleles').find('TH#" + alleles.sortColId + 
+                if (persistSortReverse) {// click twice if reverse order!
+                    persistSortReverse = false; // Needed so that it is set again
+                    setTimeout("$('table#alleles').find('TH#" + persistSortColId + 
                                                                           "').click();", 50);
                 }
             }
         }
         
         // Make sure that sort command saves the coumn for persistence
         $('table#alleles').find('TH').click(function (e) { afterSort(this); });
         
         // Want to show rare haplotypes if they were seen before ajax update
-        if (alleles.rareHaplotypesShown) {
-            alleles.rareAlleleToggle( $('input#allelesShowHide') );
+        if (persistRareHapsShown) {
+            alleles.rareAlleleToggle( $('input#' + sectionName + "_rareHaps"), false );
+        }
+        
+        // Want to show scores if they were seen before ajax update
+        if (persistScoresShown) {
+            alleles.scoresToggle( $('input#' + sectionName + "_score"), false );
         }
         
         // Persist on lighlite as red
         if (hiliteId != '')
             hiliteSpecial( hiliteId );
     }
 
     function propgateTitle(obj)
     { // Adds this objects title to all other objs with the same class
       // Relies upon a span and fixed width text
         // Find pointer position within text
     
         // Is there a title?
         var theTitle = obj.title;
         if (theTitle == undefined || theTitle.length === 0)
@@ -201,37 +212,37 @@
         if (hilites.length > 0) {
             hiliteRemove();
             hiliteAllDiffs();
             if (hiliteId != '')
                 hiliteSpecial( hiliteId );
         }
     }
     
     function hiliteAllDiffs()
     {   // Adds all hilites
     
         // TODO:
         // - handle indels
     
         // Don't even bother if full sequence isn't showing
-        var fullSeq = $('input#'+sectionName+'FullSeq');
+        var fullSeq = $('input#'+sectionName+'_fullSeq');
         if (fullSeq != undefined && $(fullSeq).val().indexOf('Hide') === -1)
             return;
         
         // DNA view or AA view?
         var spans;
-        var dnaView = $('input#'+sectionName+'DnaView');
+        var dnaView = $('input#'+sectionName+'_dnaView');
         if (dnaView != undefined && $(dnaView).val().indexOf('DNA') === -1) {
             spans = $('table#alleles').find('TH.seq').find('B');
         } else { // AA view
             spans = $('table#alleles').find('TH.seq').find('span');
         }
         // At this point, hilighting should be identical
         $(spans).each(function (ix) {
             var xPx     = $(this).position().left;
             var widthPx = $(this).width();
             if (widthPx === 0)
                 widthPx = seqPxPerPos;
             var div = hiliteAdd(xPx,widthPx);
             
             // Can we give it a title and class?
             var varClass = $( this ).attr("class").split(' ');
@@ -242,93 +253,114 @@
                         var variant = $('table#alleles').find('TH.var#' + varClass[ix]);
                         if (variant != undefined && variant.length === 1) {
                             $(div).attr('title',$(variant).attr('title'));
                             $(div).addClass(varClass[ix]);
                             break;
                         }
                     }
                 }
             }
         });
     }
 
     // - - - - - Public methods - - - - - 
     return { // returns an object with public methods
     
-        // persistence between ajax calls
-        rareHaplotypesShown: false, 
-        sortReverse:         false,
-        sortColId:           '',
-    
         positionTitle: function (e, obj)
         { // sets the current title to show the position of the pointer
           // Relies upon a span and fixed width text
           // Note: charsPerPos == 3 to show aa position when showing DNA triplets
     
             var e = e || window.event;
             var over = ((e.pageX - $(obj).offset().left) / seqPxPerPos) + 0.5; // round up
             $(obj).attr('title',over.toFixed(0)); // title is simply position
         },
     
-        rareAlleleToggle: function (btn)
+        rareAlleleToggle: function (btn,setCart)
         { // toggle the visibility of rare alleles
-            $('tr.allele.rare').toggle();
-            if ($(btn).val().indexOf('Show') != -1) {
-                $('span#alleleCounts').text( 'All gene haplotypes shown: ' + 
-                                             $('tr.allele:visible').length + ' of ' +
-                                             $('tr.allele').length );
+            var trs = $('table#alleles tbody tr.allele');
+            persistRareHapsShown = ($(btn).val().indexOf('Show') != -1);
+            if (persistRareHapsShown) {
+                $(trs).filter('.rare').removeClass('hidden');
+                var counts = $(trs).filter(':visible').length + ' of ' + $(trs).length;
+                $('span#alleleCounts').text( 'All gene haplotypes shown: ' + counts );
                 $('span#alleleCounts').addClass('textAlert'); 
                 $(btn).val('Hide rare haplotypes');
-                alleles.rareHaplotypesShown = true;
+                if (setCart == undefined || setCart)
+                    setCartVar(sectionName + '_rareHaps','set');
             } else {
-                $('span#alleleCounts').text( 'Common gene haplotypes shown: ' + 
-                                             $('tr.allele:visible').length + ' of ' +
-                                             $('tr.allele').length );
+                $(trs).filter('.rare').addClass('hidden');
+                var counts = $(trs).filter(':visible').length + ' of ' + $(trs).length;
+                $('span#alleleCounts').text( 'Common gene haplotypes shown: ' + counts );
                 $('span#alleleCounts').removeClass('textAlert'); 
                 $(btn).val('Show rare haplotypes');
-                alleles.rareHaplotypesShown = false;
+                if (setCart == undefined || setCart)
+                    setCartVar(sectionName + '_rareHaps','[]');
+            }
+            hilitesResize();
+        },
+        
+        scoresToggle: function (btn,setCart)
+        { // toggle the visibility of scores
+            persistScoresShown = ($(btn).val().indexOf('Include') != -1);
+            if (persistScoresShown) {
+                $('table#alleles').find('.score').removeClass('hidden');
+                $(btn).val('Hide scoring');
+                if (setCart == undefined || setCart)
+                    setCartVar(sectionName + '_score','set');
+            } else {
+                $('table#alleles').find('.score').addClass('hidden');
+                $(btn).val('Include scoring');
+                if (setCart == undefined || setCart)
+                    setCartVar(sectionName + '_score','[]');
             }
             hilitesResize();
         },
         
         toggleButton: function (btn,val)
         { // toggles feature that requires ajax
           // Note that ID is same as cart variable and value is boolean "exists"
             if (val != '') {
-                ajaxRequest(btn.id + '=' + val);
+                ajaxRequest(btn.id + '=set');
             } else {
                 ajaxRequest(btn.id + '=[]'); // Will remove cart variable
             }
         },
 
+        setAndRefresh: function  (varName,val)
+        { // Resets all display options to defaults
+            ajaxRequest(varName + '=' + val);
+            return false; // Fake link
+        },
+        
         initialize: function  (sectionId)
         { // Initialize or reinitailze (after ajax) the sortable table
         
             // This whole section could be renamed
             //if (sectionId != undefined && sectionId.length !== 0)
             //    sectionName = sectionId;
             
             initSortTable();
             
             var tbl = $('table#alleles');
             $(tbl).find('TH.var').each(function (ix) { 
                 propgateTitle(this); 
             });
             
             // Calculate some useful constants:
             seqCharsPerPos = 1; // Most cases
-            var tripletButton = normed( $('input#'+sectionName+'Triplets') );
+            var tripletButton = normed( $('input#'+sectionName+'_triplets') );
             if (tripletButton != undefined && $(tripletButton).val().indexOf('Show') === -1)
                 seqCharsPerPos = 3;
             seqPxPerPos = 7;
             var fullSeqHeader = normed( $(tbl).find('TH.seq') );
             if (fullSeqHeader != undefined)
                 {
                 var seqLen = $(fullSeqHeader).text().length;
                 if (seqCharsPerPos === 3)
                     seqLen /= 2;
                 seqPxPerPos = Math.round( $(fullSeqHeader).width() / (seqLen / seqCharsPerPos));
                 if ($.browser.msie) 
                     seqPxPerPos = $(fullSeqHeader).width() / (seqLen / seqCharsPerPos);
                     //seqPxPerPos += 0.075; 
                 
                 }