You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shAutoloader.js 2.6 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * SyntaxHighlighter
  3. * http://alexgorbatchev.com/SyntaxHighlighter
  4. *
  5. * SyntaxHighlighter is donationware. If you are using it, please donate.
  6. * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
  7. *
  8. * @version
  9. * 3.0.83 (July 02 2010)
  10. *
  11. * @copyright
  12. * Copyright (C) 2004-2010 Alex Gorbatchev.
  13. *
  14. * @license
  15. * Dual licensed under the MIT and GPL licenses.
  16. */
  17. (function() {
  18. var sh = SyntaxHighlighter;
  19. /**
  20. * Provides functionality to dynamically load only the brushes that a needed to render the current page.
  21. *
  22. * There are two syntaxes that autoload understands. For example:
  23. *
  24. * SyntaxHighlighter.autoloader(
  25. * [ 'applescript', 'Scripts/shBrushAppleScript.js' ],
  26. * [ 'actionscript3', 'as3', 'Scripts/shBrushAS3.js' ]
  27. * );
  28. *
  29. * or a more easily comprehendable one:
  30. *
  31. * SyntaxHighlighter.autoloader(
  32. * 'applescript Scripts/shBrushAppleScript.js',
  33. * 'actionscript3 as3 Scripts/shBrushAS3.js'
  34. * );
  35. */
  36. sh.autoloader = function()
  37. {
  38. var list = arguments,
  39. elements = sh.findElements(),
  40. brushes = {},
  41. scripts = {},
  42. all = SyntaxHighlighter.all,
  43. allCalled = false,
  44. allParams = null,
  45. i
  46. ;
  47. SyntaxHighlighter.all = function(params)
  48. {
  49. allParams = params;
  50. allCalled = true;
  51. };
  52. function addBrush(aliases, url)
  53. {
  54. for (var i = 0; i < aliases.length; i++)
  55. brushes[aliases[i]] = url;
  56. };
  57. function getAliases(item)
  58. {
  59. return item.pop
  60. ? item
  61. : item.split(/\s+/)
  62. ;
  63. }
  64. // create table of aliases and script urls
  65. for (i = 0; i < list.length; i++)
  66. {
  67. var aliases = getAliases(list[i]),
  68. url = aliases.pop()
  69. ;
  70. addBrush(aliases, url);
  71. }
  72. // dynamically add <script /> tags to the document body
  73. for (i = 0; i < elements.length; i++)
  74. {
  75. var url = brushes[elements[i].params.brush];
  76. if (!url)
  77. continue;
  78. scripts[url] = false;
  79. loadScript(url);
  80. }
  81. function loadScript(url)
  82. {
  83. var script = document.createElement('script'),
  84. done = false
  85. ;
  86. script.src = url;
  87. script.type = 'text/javascript';
  88. script.language = 'javascript';
  89. script.onload = script.onreadystatechange = function()
  90. {
  91. if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete'))
  92. {
  93. done = true;
  94. scripts[url] = true;
  95. checkAll();
  96. // Handle memory leak in IE
  97. script.onload = script.onreadystatechange = null;
  98. script.parentNode.removeChild(script);
  99. }
  100. };
  101. // sync way of adding script tags to the page
  102. document.body.appendChild(script);
  103. };
  104. function checkAll()
  105. {
  106. for(var url in scripts)
  107. if (scripts[url] == false)
  108. return;
  109. if (allCalled)
  110. SyntaxHighlighter.highlight(allParams);
  111. };
  112. };
  113. })();