PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/SHFB/Source/PresentationStyles/VS2010/scripts/branding.js

#
JavaScript | 588 lines | 410 code | 118 blank | 60 comment | 134 complexity | d14b43fbe209061e9114955cda83dc87 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // The IDs of all code snippet sets on the same page are stored so that we can keep them in synch when a tab is
  2. // selected.
  3. var allTabSetIds = new Array();
  4. // The IDs of language-specific text (LST) spans are used as dictionary keys so that we can get access to the
  5. // spans and update them when the user changes to a different language tab. The values of the dictionary
  6. // objects are pipe separated language-specific attributes (lang1=value|lang2=value|lang3=value). The language
  7. // ID can be specific (cs, vb, cpp, etc.) or may be a neutral entry (nu) which specifies text common to multiple
  8. // languages. If a language is not present and there is no neutral entry, the span is hidden for all languages
  9. // to which it does not apply.
  10. var allLSTSetIds = new Object();
  11. // Help 1 persistence support. This code must appear inline.
  12. var isHelp1;
  13. var curLoc = document.location + ".";
  14. if(curLoc.indexOf("mk:@MSITStore") == 0)
  15. {
  16. isHelp1 = true;
  17. curLoc = "ms-its:" + curLoc.substring(14, curLoc.length - 1);
  18. document.location.replace(curLoc);
  19. }
  20. else
  21. if(curLoc.indexOf("ms-its:") == 0)
  22. isHelp1 = true;
  23. else
  24. isHelp1 = false;
  25. // The OnLoad method
  26. function OnLoad(defaultLanguage)
  27. {
  28. var defLang;
  29. if(typeof(defaultLanguage) == "undefined" || defaultLanguage == null || defaultLanguage == "")
  30. defLang = "vb";
  31. else
  32. defLang = defaultLanguage;
  33. // This is a hack to fix the URLs for the background images on certain styles. Help Viewer 1.0 doesn't
  34. // mind if you put the relative URL in the styles for fix up later in script. However, Help Viewer 2.0 will
  35. // abort all processing and won't run any startup script if it sees an invalid URL in the style. As such, we
  36. // put a dummy attribute in the style to specify the image filename and use this code to get the URL from the
  37. // Favorites icon and then substitute the background image icons in the URL and set it in each affected style.
  38. // This works in either version of the help viewer.
  39. var iconPath = undefined;
  40. try
  41. {
  42. var linkEnum = document.getElementsByTagName("link");
  43. for(var idx = 0; idx < linkEnum.length; idx++)
  44. {
  45. var link = linkEnum[idx];
  46. if(link.rel.toLowerCase() == "shortcut icon")
  47. iconPath = link.href.toString();
  48. }
  49. }
  50. catch(e) { }
  51. finally { }
  52. if(iconPath)
  53. {
  54. try
  55. {
  56. var styleSheetEnum = document.styleSheets;
  57. for(var idx = 0; idx < styleSheetEnum.length; idx++)
  58. {
  59. var styleSheet = styleSheetEnum[idx];
  60. // Ignore sheets at ms-help URLs
  61. if(styleSheet.href != null && styleSheet.href.substr(0, 8) == "ms-help:")
  62. continue;
  63. // Ignore errors (Help Viewer 2). styleSheet.rules is inaccessible due to security restrictions
  64. // for all style sheets not defined within the page.
  65. try
  66. {
  67. // Get sheet rules
  68. var rules = styleSheet.rules;
  69. if(rules == null)
  70. rules = styleSheet.cssRules;
  71. if(rules != null)
  72. if(rules.length != 0)
  73. for(var ruleNdx = 0; ruleNdx != rules.length; ruleNdx++)
  74. {
  75. var rule = rules.item(ruleNdx);
  76. var selectorText = rule.selectorText.toLowerCase();
  77. // The selector text may show up grouped or individually for these
  78. if(selectorText == ".oh_codesnippetcontainertableftactive, .oh_codesnippetcontainertableft, .oh_codesnippetcontainertableftdisabled" ||
  79. selectorText == ".oh_codesnippetcontainertableftactive" ||
  80. selectorText == ".oh_codesnippetcontainertableft" ||
  81. selectorText == ".oh_codesnippetcontainertableftdisabled")
  82. {
  83. rule.style.backgroundImage = "url(" + iconPath.replace("favicon.ico", "tabLeftBG.gif") + ")";
  84. }
  85. if(selectorText == ".oh_codesnippetcontainertabrightactive, .oh_codesnippetcontainertabright, .oh_codesnippetcontainertabrightdisabled" ||
  86. selectorText == ".oh_codesnippetcontainertabrightactive" ||
  87. selectorText == ".oh_codesnippetcontainertabright" ||
  88. selectorText == ".oh_codesnippetcontainertabrightdisabled")
  89. {
  90. rule.style.backgroundImage = "url(" + iconPath.replace("favicon.ico", "tabRightBG.gif") + ")";
  91. }
  92. if(selectorText == ".oh_footer")
  93. {
  94. rule.style.backgroundImage = "url(" + iconPath.replace("favicon.ico", "footer_slice.gif") + ")";
  95. }
  96. }
  97. }
  98. catch(e) { }
  99. finally { }
  100. }
  101. }
  102. catch(e) { }
  103. finally { }
  104. }
  105. // In MS Help Viewer, the transform the topic is ran through can move the footer. Move it back where it
  106. // belongs if necessary.
  107. try
  108. {
  109. var footer = document.getElementById("OH_footer")
  110. if(footer)
  111. {
  112. var footerParent = document.body;
  113. if(footer.parentElement != footerParent)
  114. {
  115. footer.parentElement.removeChild(footer);
  116. footerParent.appendChild(footer);
  117. }
  118. }
  119. }
  120. catch(e) { }
  121. finally { }
  122. var language = GetLanguageCookie("CodeSnippetContainerLanguage", defLang);
  123. // If LST exists on the page, set the LST to show the user selected programming language
  124. UpdateLST(language);
  125. // If code snippet groups exist, set the current language for them
  126. if(allTabSetIds.length > 0)
  127. {
  128. var i = 0;
  129. while(i < allTabSetIds.length)
  130. {
  131. var tabCount = 1;
  132. // The tab count may vary so find the last one in this set
  133. while(document.getElementById(allTabSetIds[i] + "_tab" + tabCount) != null)
  134. tabCount++;
  135. tabCount--;
  136. // If not grouped, skip it
  137. if(tabCount < 2)
  138. {
  139. // Disable the Copy Code link if in Chrome
  140. if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1)
  141. document.getElementById(allTabSetIds[i] + "_copyCode").style.display = "none";
  142. }
  143. else
  144. SetCurrentLanguage(allTabSetIds[i], language, tabCount);
  145. i++;
  146. }
  147. }
  148. }
  149. // This function executes in the OnLoad event and ChangeTab action on code snippets. The function parameter
  150. // is the user chosen programming language. This function iterates through the "allLSTSetIds" dictionary object
  151. // to update the node value of the LST span tag per the user's chosen programming language.
  152. function UpdateLST(language)
  153. {
  154. for(var lstMember in allLSTSetIds)
  155. {
  156. var devLangSpan = document.getElementById(lstMember);
  157. if(devLangSpan != null)
  158. {
  159. // There may be a carriage return before the LST span in the content so the replace function below
  160. // is used to trim the whitespace at the end of the previous node of the current LST node.
  161. if(devLangSpan.previousSibling != null && devLangSpan.previousSibling.nodeValue != null)
  162. devLangSpan.previousSibling.nodeValue = devLangSpan.previousSibling.nodeValue.replace(/\s+$/, "");
  163. var langs = allLSTSetIds[lstMember].split("|");
  164. var k = 0;
  165. var keyValue;
  166. while(k < langs.length)
  167. {
  168. keyValue = langs[k].split("=");
  169. if(keyValue[0] == language)
  170. {
  171. devLangSpan.innerHTML = keyValue[1];
  172. // Help 1 and MS Help Viewer workaround. Add a space if the following text element starts
  173. // with a space to prevent things running together.
  174. if (devLangSpan.parentNode != null && devLangSpan.parentNode.nextSibling != null) {
  175. if (devLangSpan.parentNode.nextSibling.nodeValue != null &&
  176. !devLangSpan.parentNode.nextSibling.nodeValue.substring(0, 1).match(/[.,);:!/?]/)) {
  177. devLangSpan.innerHTML = keyValue[1] + " ";
  178. }
  179. }
  180. break;
  181. }
  182. k++;
  183. }
  184. // If not found, default to the neutral language. If there is no neutral language entry, clear the
  185. // content to hide it.
  186. if(k >= langs.length)
  187. {
  188. if(language != "nu")
  189. {
  190. k = 0;
  191. while(k < langs.length)
  192. {
  193. keyValue = langs[k].split("=");
  194. if(keyValue[0] == "nu")
  195. {
  196. devLangSpan.innerHTML = keyValue[1];
  197. // Help 1 and MS Help Viewer workaround. Add a space if the following text element
  198. // starts with a space to prevent things running together.
  199. if (devLangSpan.parentNode != null && devLangSpan.parentNode.nextSibling != null) {
  200. if (devLangSpan.parentNode.nextSibling.nodeValue != null &&
  201. !devLangSpan.parentNode.nextSibling.nodeValue.substring(0, 1).match(/[.,);:!/?]/)) {
  202. devLangSpan.innerHTML = keyValue[1] + " ";
  203. }
  204. }
  205. break;
  206. }
  207. k++;
  208. }
  209. }
  210. if(k >= langs.length)
  211. devLangSpan.innerHTML = "";
  212. }
  213. }
  214. }
  215. }
  216. // Get the selected language cookie
  217. function GetLanguageCookie(cookieName, defaultValue)
  218. {
  219. if(isHelp1)
  220. {
  221. try
  222. {
  223. var globals = Help1Globals;
  224. var value = globals.Load(cookieName);
  225. if(value == null)
  226. value = defaultValue;
  227. return value;
  228. }
  229. catch(e)
  230. {
  231. return defaultValue;
  232. }
  233. }
  234. var cookie = document.cookie.split("; ");
  235. for(var i = 0; i < cookie.length; i++)
  236. {
  237. var crumb = cookie[i].split("=");
  238. if(cookieName == crumb[0])
  239. return unescape(crumb[1])
  240. }
  241. return defaultValue;
  242. }
  243. // Set the selected language cookie
  244. function SetLanguageCookie(name, value)
  245. {
  246. if(isHelp1)
  247. {
  248. try
  249. {
  250. var globals = Help1Globals;
  251. globals.Save(name, value);
  252. }
  253. catch(e)
  254. {
  255. }
  256. return;
  257. }
  258. var today = new Date();
  259. today.setTime(today.getTime());
  260. // Set the expiration time to be 60 days from now (in milliseconds)
  261. var expires_date = new Date(today.getTime() + (60 * 1000 * 60 * 60 * 24));
  262. document.cookie = name + "=" + escape(value) + ";expires=" + expires_date.toGMTString() + ";path=/";
  263. }
  264. // Add a language-specific text ID
  265. function AddLanguageSpecificTextSet(lstId)
  266. {
  267. var keyValue = lstId.split("?")
  268. allLSTSetIds[keyValue[0]] = keyValue[1];
  269. }
  270. // Add a language tab set ID
  271. function AddLanguageTabSet(tabSetId)
  272. {
  273. allTabSetIds.push(tabSetId);
  274. }
  275. // Switch the active tab for all of other code snippets
  276. function ChangeTab(tabSetId, language, snippetIdx, snippetCount)
  277. {
  278. SetLanguageCookie("CodeSnippetContainerLanguage", language);
  279. SetActiveTab(tabSetId, snippetIdx, snippetCount);
  280. // If LST exists on the page, set the LST to show the user selected programming language
  281. UpdateLST(language);
  282. var i = 0;
  283. while(i < allTabSetIds.length)
  284. {
  285. // We just care about other snippets
  286. if(allTabSetIds[i] != tabSetId)
  287. {
  288. // Other tab sets may not have the same number of tabs
  289. var tabCount = 1;
  290. while(document.getElementById(allTabSetIds[i] + "_tab" + tabCount) != null)
  291. tabCount++;
  292. tabCount--;
  293. // If not grouped, skip it
  294. if(tabCount > 1)
  295. SetCurrentLanguage(allTabSetIds[i], language, tabCount);
  296. }
  297. i++;
  298. }
  299. }
  300. // Sets the current language in the specified tab set
  301. function SetCurrentLanguage(tabSetId, language, tabCount)
  302. {
  303. var tabIndex = 1;
  304. while(tabIndex <= tabCount)
  305. {
  306. var tabTemp = document.getElementById(tabSetId + "_tab" + tabIndex);
  307. if(tabTemp != null && tabTemp.innerHTML.indexOf("'" + language + "'") != -1)
  308. break;
  309. tabIndex++;
  310. }
  311. if(tabIndex > tabCount)
  312. {
  313. // Select the first non-disabled tab
  314. tabIndex = 1;
  315. if(document.getElementById(tabSetId + "_tab1").className.indexOf("OH_CodeSnippetContainerTabDisabled") != -1)
  316. {
  317. tabIndex++;
  318. while(tabIndex <= tabCount)
  319. {
  320. var tab = document.getElementById(tabSetId + "_tab" + tabIndex);
  321. if(tab.className.indexOf("OH_CodeSnippetContainerTabDisabled") == -1)
  322. {
  323. tab.className = "OH_CodeSnippetContainerTabActiveNotFirst";
  324. document.getElementById(tabSetId + "_code_Div" + j).style.display = "block";
  325. break;
  326. }
  327. tabIndex++;
  328. }
  329. // Disable left most image if first tab is disabled
  330. document.getElementById(tabSetId + "_tabimgleft").className = "OH_CodeSnippetContainerTabLeftDisabled";
  331. }
  332. }
  333. SetActiveTab(tabSetId, tabIndex, tabCount);
  334. // Disable right most image if last tab is disabled
  335. if(document.getElementById(tabSetId + "_tab" + tabCount).className.indexOf("OH_CodeSnippetContainerTabDisabled") != -1)
  336. document.getElementById(tabSetId + "_tabimgright").className = "OH_CodeSnippetContainerTabRightDisabled";
  337. }
  338. // Set the active tab within a tab set
  339. function SetActiveTab(tabSetId, tabIndex, tabCount)
  340. {
  341. var i = 1;
  342. while(i <= tabCount)
  343. {
  344. var tabTemp = document.getElementById(tabSetId + "_tab" + i);
  345. if(tabTemp.className == "OH_CodeSnippetContainerTabActive")
  346. tabTemp.className = "OH_CodeSnippetContainerTabFirst";
  347. else
  348. if(tabTemp.className == "OH_CodeSnippetContainerTabActiveNotFirst")
  349. tabTemp.className = "OH_CodeSnippetContainerTab";
  350. else
  351. if(tabTemp.className.indexOf("OH_CodeSnippetContainerTabDisabled") != -1)
  352. {
  353. tabTemp.firstChild.style.color = "#a8a8a8";
  354. tabTemp.firstChild.style.fontWeight = "normal";
  355. }
  356. var codeTemp = document.getElementById(tabSetId + "_code_Div" + i);
  357. if(codeTemp.style.display != "none")
  358. codeTemp.style.display = "none";
  359. i++;
  360. }
  361. if(document.getElementById(tabSetId + "_tab" + tabIndex).className.indexOf("OH_CodeSnippetContainerTabDisabled") == -1)
  362. {
  363. if(tabIndex == 1)
  364. document.getElementById(tabSetId + "_tab" + tabIndex).className = "OH_CodeSnippetContainerTabActive";
  365. else
  366. document.getElementById(tabSetId + "_tab" + tabIndex).className = "OH_CodeSnippetContainerTabActiveNotFirst";
  367. }
  368. else
  369. {
  370. document.getElementById(tabSetId + "_tab" + tabIndex).firstChild.style.color = "black";
  371. document.getElementById(tabSetId + "_tab" + tabIndex).firstChild.style.fontWeight = "bold";
  372. }
  373. document.getElementById(tabSetId + "_code_Div" + tabIndex).style.display = "block";
  374. // Change the CSS of the first/last image div according the currently selected tab
  375. if(tabIndex == 1 && document.getElementById(tabSetId + "_tab" + tabIndex).className.indexOf("OH_CodeSnippetContainerTabDisabled") == -1)
  376. document.getElementById(tabSetId + "_tabimgleft").className = "OH_CodeSnippetContainerTabLeftActive";
  377. else
  378. if(document.getElementById(tabSetId + "_tabimgleft").className != "OH_CodeSnippetContainerTabLeftDisabled")
  379. document.getElementById(tabSetId + "_tabimgleft").className = "OH_CodeSnippetContainerTabLeft";
  380. if(tabIndex == tabCount && document.getElementById(tabSetId + "_tab" + tabIndex).className.indexOf("OH_CodeSnippetContainerTabDisabled") == -1)
  381. document.getElementById(tabSetId + "_tabimgright").className = "OH_CodeSnippetContainerTabRightActive";
  382. else
  383. if(document.getElementById(tabSetId + "_tabimgright").className != "OH_CodeSnippetContainerTabRightDisabled")
  384. document.getElementById(tabSetId + "_tabimgright").className = "OH_CodeSnippetContainerTabRight";
  385. // Show copy code button if not in Chrome
  386. if(navigator.userAgent.toLowerCase().indexOf("chrome") == -1)
  387. document.getElementById(tabSetId + "_copyCode").style.display = "inline";
  388. else
  389. document.getElementById(tabSetId + "_copyCode").style.display = "none";
  390. }
  391. // Copy the code from the active tab of the given tab set to the clipboard
  392. function CopyToClipboard(tabSetId)
  393. {
  394. var tabTemp, contentId;
  395. var i = 1;
  396. do
  397. {
  398. contentId = tabSetId + "_code_Div" + i;
  399. tabTemp = document.getElementById(contentId);
  400. if(tabTemp != null && tabTemp.style.display != "none")
  401. break;
  402. i++;
  403. } while(tabTemp != null);
  404. if(tabTemp == null)
  405. return;
  406. if(window.clipboardData)
  407. {
  408. try
  409. {
  410. window.clipboardData.setData("Text", document.getElementById(contentId).innerText);
  411. }
  412. catch(e)
  413. {
  414. alert("Permission denied. Enable copying to the clipboard.");
  415. }
  416. }
  417. else if(window.netscape)
  418. {
  419. try
  420. {
  421. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  422. var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(
  423. Components.interfaces.nsIClipboard);
  424. if(!clip)
  425. return;
  426. var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(
  427. Components.interfaces.nsITransferable);
  428. if(!trans)
  429. return;
  430. trans.addDataFlavor("text/unicode");
  431. var str = new Object();
  432. var len = new Object();
  433. var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(
  434. Components.interfaces.nsISupportsString);
  435. var copytext = document.getElementById(contentId).textContent;
  436. str.data = copytext;
  437. trans.setTransferData("text/unicode", str, copytext.length * 2);
  438. var clipid = Components.interfaces.nsIClipboard;
  439. clip.setData(trans, null, clipid.kGlobalClipboard);
  440. }
  441. catch(e)
  442. {
  443. alert("Permission denied. Enter \"about:config\" in the address bar and double-click the \"signed.applets.codebase_principal_support\" setting to enable copying to the clipboard.");
  444. }
  445. }
  446. }
  447. // Help 1 persistence object. This requires a hidden input element on the page with a class of "userDataStyle"
  448. // defined in the style sheet that implements the user data binary behavior:
  449. // <input type="hidden" id="userDataCache" class="userDataStyle" />
  450. var Help1Globals =
  451. {
  452. UserDataCache: function()
  453. {
  454. var userData = document.getElementById("userDataCache");
  455. return userData;
  456. },
  457. Load: function(key)
  458. {
  459. var userData = this.UserDataCache();
  460. userData.load("userDataSettings");
  461. var value = userData.getAttribute(key);
  462. return value;
  463. },
  464. Save: function(key, value)
  465. {
  466. var userData = this.UserDataCache();
  467. userData.setAttribute(key, value);
  468. userData.save("userDataSettings");
  469. }
  470. };