Skip to content

Commit

Permalink
dcvSearchVersion.js related files update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenny-Jiani committed Nov 20, 2023
1 parent 6c633b2 commit c9b90ee
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<script type="text/javascript" src="{{ site.assets }}js/index-banner-two.min.js?v={{ 'now' | date: "%s"}}"></script>
<script type="text/javascript" src="{{ site.assets }}js/historymanage-two.min.js?v={{ 'now' | date: "%s"}}"></script>
{%- else -%}
<script type="text/javascript" src="{{ site.assets }}js/dcvVersionSearch.min.js?v={{ 'now' | date: "%s"}}"></script>
<!-- <script type="text/javascript" src="{{ site.assets }}js/dcvVersionSearch.min.js?v={{ 'now' | date: "%s"}}"></script> -->
<script type="text/javascript" src="{{ site.assets }}js/docsLangLatestVersion.js?v={{ 'now' | date: "%s"}}"></script>
<script type="text/javascript" src="{{ site.assets }}js/index-banner.min.js?v={{ 'now' | date: "%s"}}"></script>
<script type="text/javascript" src="{{ site.assets }}js/historymanage.min.js?v={{ 'now' | date: "%s"}}"></script>
Expand Down
92 changes: 55 additions & 37 deletions assets/js/historymanage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
function UrlReplace()
var dcvVersionList = []
async function UrlReplace()
{
initHistoryVersionList()
dcvVersionList = await getVersionSearchList();
initHistoryVersionList();

var docUrl = document.URL;
var ver = getUrlVars(docUrl)["ver"];
var matchVer = getUrlVars(docUrl)["matchVer"];
var product = getUrlVars(docUrl)["product"];
var docProduct = getCurrentUrlProductName();

if (ver != undefined && ver != "latest") {
if (product == undefined || product == docProduct) {
var tempVer = findNearestVersion(ver);
Expand All @@ -18,14 +22,14 @@ function UrlReplace()

var productVersion = getUrlVars(docUrl)[product]
if (matchVer == undefined && (ver != undefined || productVersion != undefined)) {
if (product != undefined && product != docProduct && productVersion == undefined && ver == undefined) {
if (product != undefined && product != docProduct && productVersion == undefined && ver != undefined) {
productVersion = getLinkVersion(ver, docUrl, product, getUrlVars(docUrl)["lang"] ? getUrlVars(docUrl)["lang"] : 'core', docProduct)
if (productVersion == -1) {
productVersion = "latest"
}
docUrl = docUrl.replace("ver="+ver, "ver="+productVersion+"&"+product+"="+ver)
window.location.replace(docUrl)
} else if (product != undefined && product != docProduct && ver == undefined && productVersion != undefined) {
} else if (product != undefined && product != docProduct && productVersion != undefined && ver == undefined) {
var curPageVer = getLinkVersion(productVersion, docUrl, product, getUrlVars(docUrl)["lang"] ? getUrlVars(docUrl)["lang"] : 'core', docProduct)
if (curPageVer == -1) {
curPageVer = "latest"
Expand Down Expand Up @@ -100,9 +104,6 @@ function RedirToGivenVersionPage(inputVer, currentUrl = null)
var productParam = getUrlVars(docUrl)["product"];
var langParam = getUrlVars(docUrl)["lang"];

console.log(productParam, langParam)


var historyList = $(".otherVersions");
if (historyList != null)
{
Expand Down Expand Up @@ -151,11 +152,8 @@ function RedirToGivenVersionPage(inputVer, currentUrl = null)
redirectUrl = redirectUrl + productVar
}
if (inputVer == 'latest') {
console.log(redirectUrl + anchorVal)
console.log(1)
window.location.replace(redirectUrl + anchorVal)
} else {
console.log(2)
window.location.replace(`${redirectUrl}${redirectUrl.indexOf("?") > 0?'&':'?'}ver=${inputVer}&matchVer=true${changeVer}${anchorVal}`)
}
return;
Expand All @@ -178,7 +176,6 @@ function RedirToGivenVersionPage(inputVer, currentUrl = null)
if (aTag.length > 0) {
var exp = new RegExp(/[?]+([^=]+)=/gi)
if (exp.exec(aTag[0].href) != null){
console.log(5)
window.location.replace(aTag[0].href + "&ver=" +inputVer+"&matchVer=true"+ changeVer + anchorVal);
return;
} else {
Expand All @@ -196,7 +193,6 @@ function RedirToGivenVersionPage(inputVer, currentUrl = null)
productVar += ("&"+productParam + "=" + getUrlVars(docUrl)[productParam])
}
}
console.log(3)
window.location.replace(`${redirectUrl}${redirectUrl.indexOf("?") > 0?'&':'?'}ver=${inputVer}&matchVer=true${productVar}${changeVer}${anchorVal}`)
return;
}
Expand All @@ -219,7 +215,6 @@ function RedirToGivenVersionPage(inputVer, currentUrl = null)
}
redirectUrl = redirectUrl + productVar
}
console.log(4)
window.location.replace(redirectUrl + anchorVal);
}

Expand All @@ -238,8 +233,6 @@ function GetVersionDiff(inputVer, compareVer)
var inputChar = inputVer ? inputVer.split('.') : inputVer;
var compareChar = compareVer ? compareVer.split('.') : compareVer;

console.log(inputChar, compareChar)

var diff = 0;

var maxLength = Math.max(inputChar.length, compareChar.length);
Expand Down Expand Up @@ -283,15 +276,14 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
verText = p_ver
}

console.log(verText, p_ver)

if(hrefVal == "") return;

if (hrefVal.indexOf("/docs/") <= 0 || hrefVal.indexOf(location.host) < 0) {
window.open(aTag.href)
return
}

console.log(aTag, verText)

// #region hash & src,lang,ver
// get hash string
let hashIndex = hrefVal.indexOf("#")
Expand Down Expand Up @@ -340,6 +332,7 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
verStr = expQueryStr.exec(hrefVal) != null ? ("&ver=" + verText) : ("?ver=" + verText)
}
hrefVal = hrefVal + verStr
console.log(hrefVal, verStr)
// #endregion

// #region 分析出 productVar, langVar
Expand All @@ -366,6 +359,7 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
productVar = `${expQueryStr.exec(hrefVal) == null?'?':'&'}product=${getUrlVars(document.URL)['product']}`;
isNeedAddLang = true
isNeedAddProductVersion = true
console.log(productVar)
}

if (isNeedAddLang) {
Expand All @@ -377,6 +371,7 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
}
}
}
console.log(productVar)
}
// same docs, different language
if (!getUrlVars(hrefVal)["lang"]) {
Expand All @@ -393,7 +388,7 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
hrefVal = hrefVal + productVar
// #endregion

console.log(`2. hrefVal: ${hrefVal}`)
console.log(hrefVal)

if (aTag.target == '_blank') {
if (getUrlVars(originHref)["ver"]!=undefined) {
Expand Down Expand Up @@ -497,12 +492,12 @@ function addParam (aTag, verText, fromSourse=null, needh3=false)
if (fromSourse == "sidebar") {
// request link
if (!$(aTag).hasClass("activeLink")) {
RequestNewPage(aTag, hrefVal + hashStr, needh3)
RequestNewPage(aTag, hrefVal + hashStr, needh3)
}
} else if (fromSourse == "docContainer") {
findCurLinkOnFullTree(aTag, hrefVal + hashStr, needh3)
findCurLinkOnFullTree(aTag, hrefVal + hashStr, needh3)
} else {
window.location.href = hrefVal + hashStr;
window.location.href = hrefVal + hashStr;
}
}
return;
Expand All @@ -514,12 +509,10 @@ function RequestNewPage(aTag, paramLink, needh3=false, redirectUrl = null, onlyL
var fetchUrl = redirectUrl ? redirectUrl : aTag.href
var oldLang = getCurrentUrlLang(document.URL)

console.log(paramLink)
fetch(fetchUrl, {cache: "no-cache"}).then(function(response) {
return response.text()
}).then(function(data) {
var inputVer = getRequestNewPageVersion(paramLink)
console.log(inputVer)
var otherVersions = $(data).find(".otherVersions > li")

var needToSearchHistory = false
Expand All @@ -544,7 +537,6 @@ function RequestNewPage(aTag, paramLink, needh3=false, redirectUrl = null, onlyL
var urlLang = getUrlVars(paramLink)["lang"]
if ($(".languageWrap.multiProgrammingLanguage").length > 0 && isInIOSDos(document.URL, paramLink)) {
var curLang = $(".languageWrap .languageSelectDown > div.on").data("value")
console.log(curLang, urlLang)
if (!$(".languageWrap").hasClass("enableLanguageSelection")) {
let singleLang = ""
if ($(data).find(".language-swift").length > 0) {
Expand Down Expand Up @@ -1031,7 +1023,6 @@ function changeVersion (liTag)
function findNearestVersion(ver) {
var versionList = $(".fullVersionInfo li:not(.hideLi)")
var bestVer = ver, verDiff=null
// console.log(versionList)
for (var i=0; i<versionList.length; i++) {
var tempVer = $(versionList[i]).text().toLowerCase()
if (tempVer == "latest version"){
Expand Down Expand Up @@ -1187,7 +1178,6 @@ function getRequestNewPageVersion(linkUrl) {
// different product
var product = queryProduct || getCurrentUrlProductName(linkUrl)
var lang = queryLang || getCurrentUrlLang(linkUrl, true)
console.log(curVersion, product, lang, getCurrentUrlProductName(linkUrl))
var returnVersion = getLinkVersion(curVersion, linkUrl, product, lang ? lang : 'core', getCurrentUrlProductName(linkUrl))
return returnVersion == -1 ? curVersion : returnVersion
}
Expand Down Expand Up @@ -1216,24 +1206,21 @@ function getLinkVersion(curVersion, linkUrl, curProduct=null, curLang=null, link
// 得到 language
let lang = curLang ? curLang : (getUrlVars(document.URL)["lang"] ? getUrlVars(document.URL)["lang"] : getCurrentUrlLang(document.URL, true))

console.log(product, lang)

lang = lang == "cplusplus" ? "cpp" : lang
lang = ["objectivec-swift", "objectivec", "objc", "swift"].includes(lang) ? "ios" : lang
lang = lang == "core" ? "" : lang

// 找到对应的 matchList
let filteredItems = dcvVersionList.filter(function(item) {
let productVersion = item[product+'Core']
//let productVersion = item[product+'Core']
let productVersion = item.version
let isReturn = false
let matchItems = null
if (productVersion && getFormatVal(productVersion) <= getFormatVal(curVersion)) {
for(var matchItem in item.matchList) {
if (lang && lang != "") {
if (matchItem == lang) {
console.log(matchItem)
var tempMatchItems = item.matchList[matchItem]
console.log(tempMatchItems)
for(var subMatchItems in tempMatchItems) {
if (linkProduct != "dcv") {
if (subMatchItems == linkProduct) {
Expand Down Expand Up @@ -1268,11 +1255,10 @@ function getLinkVersion(curVersion, linkUrl, curProduct=null, curLang=null, link
}
}
item.matchItems = matchItems
item.productVersion = item[product+'Core']
item.productVersion = productVersion
return isReturn
})

console.log(filteredItems)
filteredItems.sort(function(a, b) {
return getFormatVal(b.productVersion) - getFormatVal(a.productVersion)
})
Expand All @@ -1291,7 +1277,6 @@ function getLinkVersion(curVersion, linkUrl, curProduct=null, curLang=null, link
}
}


function titleCase(s) {
var i, ss = s.toLowerCase().split(/\s+/);
for (i = 0; i < ss.length; i++) {
Expand All @@ -1303,7 +1288,8 @@ function titleCase(s) {
//getDCVLangVersion('flutter', 'dbr', '10.0.0')
function getDCVLangVersion(linkLang, curProduct, curVersion) {
let filteredItems = dcvVersionList.filter(function(item){
let productVersion = item[curProduct+'Core']
// let productVersion = item[curProduct+'Core']
let productVersion = item.version
let isReturn = false
let linkLangDCVVersion = null
if (productVersion && getFormatVal(productVersion) <= getFormatVal(curVersion)) {
Expand All @@ -1315,7 +1301,7 @@ function getDCVLangVersion(linkLang, curProduct, curVersion) {
}
}
item.linkLangDCVVersion = linkLangDCVVersion
item.productVersion = item[curProduct+'Core']
item.productVersion = productVersion
return isReturn
})

Expand Down Expand Up @@ -1366,6 +1352,38 @@ function isInIOSDos(curUrl, linkUrl) {
}
}

async function getVersionSearchList() {
var product = getUrlVars(document.URL)["product"] || getCurrentUrlProductName(document.URL)
var lang = getUrlVars(document.URL)["lang"]
var repoType = "Core"
if (lang != undefined) {
if (lang && lang != "core") {
lang = lang.toLowerCase().trim().split(",")[0]
if (['javascript', 'js'].indexOf(lang) >= 0) {
repoType = "web"
}
if (['android', 'objective-c', 'objc', 'swift', 'ios'].indexOf(lang) >= 0) {
repoType = "mobile"
}
if (['c', 'cpp', 'c++', 'csharp', 'dotnet', 'java', 'python'].indexOf(lang) >= 0) {
repoType = "server"
}
} else {
repoType = "core"
}
} else {
repoType = getCurrentUrlRepoType(document.URL);
}

try{
let request = await fetch(`${location.origin}/${getDoumentName(product)}/docs/${repoType}/assets/js/${product}${titleCase(repoType)}VersionSearch.json`, {cache: "no-cache"})
let test = await request.text()
return JSON.parse(JSON.stringify(test))
} catch(error) {
console.log(error)
}
}

window.addEventListener("popstate", function(e) {
findCurLinkOnFullTree(location, location.href, false, true)
}, false)
2 changes: 1 addition & 1 deletion assets/js/historymanage.min.js

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions assets/js/index-banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function FullTreeMenuList(generateDocHead, needh3 = true, pageStartVer = undefin
generateDocHead = false;
}
var verArray = SearchVersion();
console.log(verArray)
if (!useVersionTree) {
var allHerf1 = $(".docContainer .content, #docHead, #AutoGenerateSidebar, .sideBar, #crumbs").find("a");
for (var i = 0; i < allHerf1.length; i++)
Expand Down Expand Up @@ -372,6 +373,9 @@ function SearchVersion() {
else{
curVerFromUrl = "latest"
}
console.log(ver, curVerFromUrl)

var productName = getUrlVars(docUrl)["product"];

var compatiableDiv = document.getElementById( "compatibleInfo");
if (ver == undefined){
Expand All @@ -390,7 +394,6 @@ function SearchVersion() {
else{
curVerTag[0].innerText = "version " + ver;
}
var productName = getUrlVars(docUrl)["product"];
if (productName != undefined) {
var productVersion = getUrlVars(docUrl)[productName];
if (productVersion != undefined) {
Expand All @@ -405,7 +408,6 @@ function SearchVersion() {
if (compatiableDiv != null && compatibleTag != null) {
compatiableDiv.style.display = "block";
compatibleTag[0].innerText = "Version "+ ver;
var productName = getUrlVars(docUrl)["product"];
if (productName != undefined) {
var productVersion = getUrlVars(docUrl)[productName];
if (productVersion != undefined) {
Expand All @@ -424,7 +426,12 @@ function SearchVersion() {
else if (compatiableDiv != null){
compatiableDiv.style.display = "none";
}

if (productName != undefined) {
var productVersion = getUrlVars(docUrl)[productName];
if (productVersion != undefined) {
ver = productVersion
}
}
var verArray = new Array(ver, curVerFromUrl);
return verArray;
}
Expand Down Expand Up @@ -912,4 +919,4 @@ function showSelectMultiPanel(nextSiblings, findItemIndex) {
$(sidebarList[i]).hide()
}
}
}
}
Loading

0 comments on commit c9b90ee

Please sign in to comment.