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