main.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. $(function () {
  2. var MOBILECONS = 767;
  3. function showCaptionFromAlt(selector) {
  4. $(selector).each(function () {
  5. $(this).after('<span class="img-caption">' + $(this).attr('alt') + '</span>');
  6. });
  7. };
  8. // Function to start searching when user clicked in the search icon
  9. $('#search').on('submit', function (e) {
  10. // Remove default events from form
  11. if (e.preventDefault) {
  12. e.preventDefault();
  13. } else {
  14. e.returnValue = false;
  15. }
  16. // Activation keys are pressed, which causes the search function
  17. $('#search-query').trigger('input');
  18. })
  19. // Captions are not automatically shown anymore by default; uncomment to reenable them
  20. //showCaptionFromAlt("article img");
  21. // Control the sizes of sidebar and content part (saved, dynamic changed, etc.)
  22. function apiDocSizeControl() {
  23. "use srtict"
  24. function resizableTOC() {
  25. // Get started space parameters
  26. var startSidebarWidth = $('#sidetoggle').width();
  27. var contentMargin = + $($('.article.grid-right')[0]).css('marginLeft').split('px')[0];
  28. if (localStorage.getItem('sizes') != null) {
  29. // Get the sizes from local storage
  30. var sizes = JSON.parse(localStorage.getItem('sizes'));
  31. sidebarSizeDivide = sizes.sidebarWidth - startSidebarWidth;
  32. // Set width to sidebar
  33. $('#sidetoggle').css('width', sizes.sidebarWidth);
  34. // Set sizes for content part
  35. $($('.article.grid-right')[0]).css({
  36. 'marginLeft': contentMargin + sidebarSizeDivide
  37. });
  38. // Wait, while filter is availiable
  39. var filterTimer = setInterval(function () {
  40. if ($($('.sidefilter')[0]).length > 0) {
  41. var filter = $($('.sidefilter')[0]);
  42. // Set filter width
  43. filter.css('width', sizes.sidebarWidth);
  44. filter.show();
  45. // Clear inteval for filter
  46. clearInterval(filterTimer);
  47. }
  48. }, 100);
  49. $('.container.body-content.hide-when-search').show();
  50. // If object "sizes" don't saved
  51. } else {
  52. $('.container.body-content.hide-when-search').show();
  53. }
  54. // Start resizable function
  55. $('#sidetoggle').resizable({
  56. containment: ".container.body-content.hide-when-search",
  57. handles: 'e',
  58. maxWidth: 570,
  59. minWidth: 140,
  60. resize: function (event, ui) {
  61. // Get difference in sizes between start and end
  62. var sidebarSizeDivide = ui.size.width - startSidebarWidth;
  63. // Create "Sizes" object
  64. var sizes = {
  65. sidebarWidth: ui.size.width,
  66. }
  67. // Set sizes for content part
  68. $($('.article.grid-right')[0]).css({
  69. 'marginLeft': contentMargin + sidebarSizeDivide
  70. });
  71. $($('.sidefilter')[0]).css('width', sizes.sidebarWidth);
  72. localStorage.setItem('sizes', JSON.stringify(sizes))
  73. }
  74. });
  75. }
  76. // If user on the one of the doc page (API, Manual, ReleaseNotes)
  77. if ($('#sidetoggle').length > 0) {
  78. resizableTOC();
  79. var filterTimer = setInterval(function () {
  80. if ($($('.sidefilter')[0]).length > 0) {
  81. var filter = $($('.sidefilter')[0])
  82. filter.show();
  83. // Clear inteval for filter
  84. clearInterval(filterTimer);
  85. }
  86. }, 100);
  87. // If user on start page or somewhere else
  88. } else {
  89. $('.container.body-content.hide-when-search').show();
  90. localStorage.clear();
  91. }
  92. }
  93. function apiSidebarStructureControl() {
  94. var tocInterval = setInterval(function () {
  95. if ($('#toc').length > 0) {
  96. clearInterval(tocInterval);
  97. if (localStorage.getItem('sidebarStructure') != null) {
  98. getActiveItems();
  99. }
  100. setActiveItems();
  101. }
  102. }, 100)
  103. }
  104. function setActiveItems() {
  105. $('#toc ul li a').on('click', function () {
  106. var activeItems = [];
  107. $('#toc ul li.in a').each(function () {
  108. if ($(this).parent().hasClass('in')) {
  109. activeItems.push($(this).attr('title'));
  110. };
  111. });
  112. var sidebarStructure = {
  113. activeItems: activeItems,
  114. scroll: $('#sidetoc').scrollTop()
  115. }
  116. localStorage.setItem('sidebarStructure', JSON.stringify(sidebarStructure));
  117. })
  118. }
  119. function getActiveItems() {
  120. var sidebarStructureOut = JSON.parse(localStorage.getItem('sidebarStructure'));
  121. var savedActiveItems = sidebarStructureOut.activeItems;
  122. for (var i = 0; i < savedActiveItems.length; i++) {
  123. $('#toc ul li a[title="' + savedActiveItems[i] + '"]').each(function () {
  124. $(this).parent().addClass('in');
  125. })
  126. }
  127. $('#sidetoc').scrollTop(sidebarStructureOut.scroll)
  128. }
  129. if ($(window).width() > MOBILECONS) {
  130. apiDocSizeControl();
  131. apiSidebarStructureControl();
  132. } else {
  133. localStorage.clear();
  134. }
  135. $(window).on('resize', function () {
  136. removeResizable();
  137. if ($(window).width() > MOBILECONS) {
  138. $('.container.body-content.hide-when-search').show();
  139. } else {
  140. localStorage.clear();
  141. }
  142. })
  143. function removeResizable() {
  144. if ($(window).width() <= MOBILECONS) {
  145. $($('.article.grid-right')[0]).removeAttr('style');
  146. $($('#sidetoggle')[0]).removeAttr('style');
  147. var filterTimer = setInterval(function () {
  148. if ($($('.sidefilter')[0]).length > 0) {
  149. var filter = $($('.sidefilter')[0])
  150. filter.removeAttr('style');
  151. // Clear inteval for filter
  152. clearInterval(filterTimer);
  153. }
  154. }, 100);
  155. }
  156. }
  157. function redirectToCurrentDocVersion() {
  158. $('#stride-current-version').on('change', function () {
  159. var hostVersion = window.location.host;
  160. var pathVersion = window.location.pathname;
  161. var targetVersion = $("#stride-current-version").val();
  162. // Generate page URL in other version
  163. var newAddress = '//' + hostVersion + '/' + targetVersion + '/' + pathVersion.substring(pathVersion.indexOf('/', 1) + 1);
  164. // Check if address exists
  165. $.get(newAddress)
  166. .fail(function() {
  167. // It didn't work, let's just go to top page of the section (i.e. manual, api, release notes, etc.)
  168. newAddress = '//' + hostVersion + '/' + targetVersion + '/' + pathVersion.split('/')[2];
  169. if (pathVersion.split('/').length >= 4)
  170. newAddress += '/' + pathVersion.split('/')[3];
  171. })
  172. .always(function() {
  173. // Go to page
  174. $(window).attr('location', newAddress);
  175. });
  176. })
  177. }
  178. // Language check function
  179. var siteLang = [];
  180. $('#x_head_langList li *[data-language]').each(function () {
  181. siteLang.push($(this).data('language'))
  182. });
  183. var currentLang = window.location.pathname.split("/")[1];
  184. var changedItem = $('*[data-language="' + currentLang + '"]');
  185. var savedText = changedItem.text();
  186. changedItem.replaceWith($('<span>' + savedText + '</span>'));
  187. for (var i = 0; i < siteLang.length; i++) {
  188. if (siteLang[i] != currentLang) {
  189. var changedItemInner = $('*[data-language="' + siteLang[i] + '"]')
  190. var savedTextInner = changedItemInner.text();
  191. changedItemInner.replaceWith($('<a data-language="' + siteLang[i] + '">' + savedTextInner + '</a>'));
  192. }
  193. }
  194. $('#x_head_langList li a').on('click', function () {
  195. var patt = /\/(en|jp)\//;
  196. var lang = "/" + $(this).data('language') + "/";
  197. window.location.href = window.location.href.replace(patt, lang);
  198. });
  199. $('body').on('click', function (event) {
  200. if ($(event.target).hasClass('page-link')) {
  201. $('html, body').scrollTop(0)
  202. }
  203. });
  204. lowercaseBreadcrumbs();
  205. function lowercaseBreadcrumbs() {
  206. $('#breadcrumb .breadcrumb li a').each(function () {
  207. if ($(this).attr('href').toLowerCase() != 'releasenotes') {
  208. $(this).attr('href', $(this).attr('href').toLowerCase())
  209. }
  210. })
  211. };
  212. $('img').filter(function () { return $(this).parent().is(":not(.stride-documentation-image)"); }).each(function () {
  213. $(this).wrap('<a class="maximize_image" href=' + $(this).attr('src') + '></a>').parent().html()
  214. });
  215. $('.maximize_image').magnificPopup({
  216. type: 'image'
  217. });
  218. function loadVersions() {
  219. $.getJSON('/versions.json', function(data) {
  220. $("#stride-current-version").empty();
  221. data.versions.forEach(function(version) {
  222. var url = version;
  223. $("#stride-current-version").append('<option value="' + url + '">' + version + '</option>');
  224. });
  225. var urlSplits = window.location.pathname.split('/');
  226. var urlVersion = urlSplits[1];
  227. if (urlVersion == 'latest') {
  228. urlVersion = data.versions[0];
  229. }
  230. $("#stride-current-version").val(urlVersion).change();
  231. redirectToCurrentDocVersion();
  232. });
  233. };
  234. loadVersions();
  235. });
  236. function toggleLangDropDown(){
  237. document.getElementById('x_head_langList').classList.toggle('lang-btn-drpdown-show');
  238. }