source: tol/TolJava/WebTOLJavaDEMO/web/sh/src/shAutoloader.js

devel
Last change on this file was 9887b93, checked in by Humberto Carralero <hacarralero@…>, 15 years ago

Importing TolJava

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3900 be4846ae-f051-483a-831f-b64e7d08527f

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