tutorial-utils.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function getLabelName(innerHTML) {
  2. var str = innerHTML.toLowerCase();
  3. // Replace all '+' with 'p'
  4. str = str.split('+').join('p');
  5. // Replace all ' ' with '_'
  6. str = str.split(' ').join('_');
  7. // Replace all '#' with 'sharp'
  8. str = str.split('#').join('sharp');
  9. // Replace other special characters with 'ascii' + code
  10. for (var i = 0; i < str.length; i++) {
  11. var charCode = str.charCodeAt(i);
  12. if (!(charCode == 95 || (charCode > 96 && charCode < 123) || (charCode > 47 && charCode < 58)))
  13. str = str.substr(0, i) + 'ascii' + charCode + str.substr(i + 1);
  14. }
  15. return str;
  16. }
  17. function addToggle() {
  18. var $getDiv = $('div.newInnerHTML').last();
  19. var buttonName = $getDiv.html();
  20. var label = getLabelName(buttonName.trim());
  21. $getDiv.attr("title", label);
  22. $getDiv.hide();
  23. $getDiv = $getDiv.next();
  24. $getDiv.attr("class", "toggleable_div label_" + label);
  25. $getDiv.hide();
  26. }
  27. function addButton(label, buttonName) {
  28. var b = document.createElement("BUTTON");
  29. b.innerHTML = buttonName;
  30. b.setAttribute('class', 'toggleable_button label_' + label);
  31. b.onclick = function() {
  32. $('.toggleable_button').css({
  33. border: '2px outset',
  34. 'border-radius': '4px'
  35. });
  36. $('.toggleable_button.label_' + label).css({
  37. border: '2px inset',
  38. 'border-radius': '4px'
  39. });
  40. $('.toggleable_div').css('display', 'none');
  41. $('.toggleable_div.label_' + label).css('display', 'block');
  42. };
  43. b.style.border = '2px outset';
  44. b.style.borderRadius = '4px';
  45. b.style.margin = '2px';
  46. return b;
  47. }
  48. function buttonsToAdd($elements, $heading, $type) {
  49. if ($elements.length === 0) {
  50. $elements = $("" + $type + ":contains(" + $heading.html() + ")").parent().prev("div.newInnerHTML");
  51. }
  52. var arr = jQuery.makeArray($elements);
  53. var seen = {};
  54. arr.forEach(function(e) {
  55. var txt = e.innerHTML;
  56. if (!seen[txt]) {
  57. $button = addButton(e.title, txt);
  58. if (Object.keys(seen).length == 0) {
  59. var linebreak1 = document.createElement("br");
  60. var linebreak2 = document.createElement("br");
  61. ($heading).append(linebreak1);
  62. ($heading).append(linebreak2);
  63. }
  64. ($heading).append($button);
  65. seen[txt] = true;
  66. }
  67. });
  68. return;
  69. }
  70. function addTutorialsButtons() {
  71. $("h2").each(function() {
  72. $heading = $(this);
  73. $smallerHeadings = $(this).nextUntil("h2").filter("h3").add($(this).nextUntil("h2").find("h3"));
  74. if ($smallerHeadings.length) {
  75. $smallerHeadings.each(function() {
  76. var $elements = $(this).nextUntil("h2,h3").filter("div.newInnerHTML");
  77. buttonsToAdd($elements, $(this), "h3");
  78. });
  79. } else {
  80. var $elements = $(this).nextUntil("h2").filter("div.newInnerHTML");
  81. buttonsToAdd($elements, $heading, "h2");
  82. }
  83. });
  84. $(".toggleable_button").first().click();
  85. var $clickDefault = $('.toggleable_button.label_python').first();
  86. if ($clickDefault.length) {
  87. $clickDefault.click();
  88. }
  89. $clickDefault = $('.toggleable_button.label_cpp').first();
  90. if ($clickDefault.length) {
  91. $clickDefault.click();
  92. }
  93. return;
  94. }