d94ba6831b6da3719ac3d6b0b28663a12df9f7b2 max Fri Sep 7 02:09:38 2018 -0700 allowing links in longer hub lists in hgHubConnect, refs #22012 diff --git src/hg/hgHubConnect/hgHubConnect.c src/hg/hgHubConnect/hgHubConnect.c index 265a318..4a395ea 100644 --- src/hg/hgHubConnect/hgHubConnect.c +++ src/hg/hgHubConnect/hgHubConnect.c @@ -138,93 +138,107 @@ { int len = strlen(string); if ( string[len - 1] == ',') string[len - 1] = 0; else if (len > 2 && endsWith(string,", ")) string[len - 2] = 0; } return string; } #define GENLISTWIDTH 40 static void printGenomeList(char *hubUrl, struct slName *genomes, int row, boolean withLink) /* print supported assembly names from sl list */ { -/* List of associated genomes. */ -struct dyString *dy = newDyString(100); -struct dyString *dyShort = newDyString(100); -char *trimmedName = NULL; +struct dyString *dyHtml = newDyString(1024); +struct dyString *dyShortHtml = newDyString(1024); + +// create two strings: one shortened to GENLISTWIDTH characters +// and another one with all genomes int charCount = 0; struct slName *genome = genomes; for(; genome; genome = genome->next) { - trimmedName = trackHubSkipHubName(genome->name); - dyStringPrintf(dy,"%s, ", trimmedName); - if (dyShort->stringSize == 0 || (dyShort->stringSize+strlen(trimmedName)<=GENLISTWIDTH)) - dyStringPrintf(dyShort,"%s, ", trimmedName); - charCount += strlen(trimmedName); - } -char *genomesString = removeLastComma( dyStringCannibalize(&dy)); -char *genomesShort = removeLastComma( dyStringCannibalize(&dyShort)); -char tempHtml[1024+strlen(genomesString)+strlen(genomesShort)]; -if (strlen(genomesShort) > GENLISTWIDTH) // If even the first element is too long, truncate it. - genomesShort[GENLISTWIDTH] = 0; -if (strlen(genomesShort)==strlen(genomesString)) - { - struct dyString *tempHtmlSrc = newDyString(1000); - genome = genomes; - for(; genome; genome = genome->next) + char *trimmedName = trackHubSkipHubName(genome->name); + char *shortName = cloneString(trimmedName); + // If even the first element is too long, truncate its short name. + if (genome==genomes && strlen(trimmedName) > GENLISTWIDTH) + shortName[GENLISTWIDTH] = 0; + + // append to dyShortHtml if necessary + if (charCount == 0 || (charCount+strlen(trimmedName)<=GENLISTWIDTH)) { - trimmedName = trackHubSkipHubName(genome->name); if (withLink) - dyStringPrintf(tempHtmlSrc,"<a href='hgTracks?hubUrl=%s&genome=%s'>%s</a>" , hubUrl, genome->name, trimmedName); + dyStringPrintf(dyShortHtml,"<a href='hgTracks?hubUrl=%s&genome=%s'>%s</a>" , hubUrl, genome->name, shortName); else - dyStringPrintf(tempHtmlSrc,"%s" , trimmedName); + dyStringPrintf(dyShortHtml,"%s" , shortName); + dyStringPrintf(dyShortHtml,", "); + } + freeMem(shortName); - if (genome->next) - dyStringPrintf(tempHtmlSrc,", "); + charCount += strlen(trimmedName); + // always append to dyHtml + if (withLink) + dyStringPrintf(dyHtml,"<a href='hgTracks?hubUrl=%s&genome=%s'>%s</a>" , hubUrl, genome->name, trimmedName); + else + dyStringPrintf(dyHtml,"%s" , trimmedName); + + if (genome->next) + { + dyStringPrintf(dyHtml,", "); } - safef(tempHtml, sizeof tempHtml, "%s", dyStringCannibalize(&tempHtmlSrc)); + } + +char *longHtml = dyStringCannibalize(&dyHtml); +char *shortHtml = dyStringCannibalize(&dyShortHtml); +shortHtml = removeLastComma(shortHtml); + +if (charCount < GENLISTWIDTH) + ourPrintCell(shortHtml); else { char id[256]; + char tempHtml[1024+strlen(longHtml)+strlen(shortHtml)]; safef(tempHtml, sizeof tempHtml, - "<span id=Short%d>[+] %s...</span>" - "<span id=Full%d style=\"display:none\">[-]<br>%s</span>" - , row, genomesShort - , row, genomesString); + "<span id=Short%d><span style='cursor:default' id='Short%dPlus'>[+] </span>%s...</span>" + "<span id=Full%d style=\"display:none\"><span style='cursor:default' id='Full%dMinus'>[-]<br></span>%s</span>" + , row, row, shortHtml + , row, row, longHtml); - safef(id, sizeof id, "Short%d", row); + safef(id, sizeof id, "Short%dPlus", row); jsOnEventByIdF("click", id, "document.getElementById('Short%d').style.display='none';" "document.getElementById('Full%d').style.display='inline';" "return false;" , row, row); - safef(id, sizeof id, "Full%d", row); + safef(id, sizeof id, "Full%dMinus", row); jsOnEventByIdF("click", id, "document.getElementById('Full%d').style.display='none';" "document.getElementById('Short%d').style.display='inline';" "return false;" , row, row); - } ourPrintCell(tempHtml); } +freeMem(longHtml); +freeMem(shortHtml); +} + static void printGenomes(struct trackHub *thub, int row, boolean withLink) /* print supported assembly names from trackHub */ { /* List of associated genomes. */ struct trackHubGenome *genomes = thub->genomeList; struct slName *list = NULL, *el; for(; genomes; genomes = genomes->next) { el = slNameNew(genomes->name); slAddHead(&list, el); } slReverse(&list); printGenomeList(thub->url, list, row, withLink); }