David Jean Louis avatar David Jean Louis committed 7173ae7

Changed the way js files are loaded, hopefully now they are loaded syncronously (fixes issue #32).

Comments (0)

Files changed (4)

admin_tools/dashboard/templates/admin_tools/dashboard/dashboard.html

 {% load i18n admin_tools_dashboard_tags %}
+<script type="text/javascript" src="{{ media_url }}/admin_tools/js/utils.js"></script>
 <script type="text/javascript" charset="utf-8">
-    // load jquery if it's not loaded yet
-    if (typeof jQuery == 'undefined') {
-        var script_url = '{{ media_url }}/admin_tools/js/jquery/jquery.min.js';
-        document.write(unescape('%3Cscript src="' + script_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
-    // load jquery ui if it's not loaded yet
-    if (typeof jQuery.ui == 'undefined') {
-        var script_url = '{{ media_url }}/admin_tools/js/jquery/jquery-ui.min.js';
-        document.write(unescape('%3Cscript src="' + script_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
-    // load json if it's not loaded yet
-    if (typeof JSON == 'undefined') {
-        var script_url = '{{ media_url }}/admin_tools/js/json.min.js';
-        document.write(unescape('%3Cscript src="' + script_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
-    // load jquery cookie if it's not loaded yet
-    if (typeof jQuery.cookie == 'undefined') {
-        var script_url = '{{ media_url }}/admin_tools/js/jquery/jquery.cookie.min.js';
-        document.write(unescape('%3Cscript src="' + script_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
+    // Load js files syncronously and conditionally
+    var js_files = [
+        {
+            src : '{{ media_url }}/admin_tools/js/jquery/jquery.min.js',
+            test: function() { return typeof(jQuery) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/jquery/jquery-ui.min.js',
+            test: function() { return typeof(jQuery.ui) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/json.min.js',
+            test: function() { return typeof(JSON.stringify) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/jquery/jquery.cookie.min.js',
+            test: function() { return typeof(jQuery.cookie) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/jquery/jquery.dashboard.js',
+            test: function() { return true; }
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/dashboard.js',
+            test: function() { return true; }
+        },
+        {% for js in dashboard.Media.js %}
+        {
+            src : '{{ media_url }}/{{ js }}',
+            test: function() { return true; }
+        },{% endfor %}
+    ];
+    load_script(0, js_files);
 </script>
-<script type="text/javascript" src="{{ media_url }}/admin_tools/js/jquery/jquery.dashboard.js"></script>
-{% if dashboard.Media.js %}
-{% for js in dashboard.Media.js %}
-<script type="text/javascript" src="{{ media_url }}/{{ js }}"></script>{% endfor %}
-{% endif %}
 
 {% if dashboard.title %}
 <h1 class="dashboard-title">{{ dashboard.title }}</h1>
 <script type="text/javascript">
 // <![CDATA[
 jQuery(function($) {
-    $('#{{ dashboard.get_id }}').dashboard({
-        'columns': {{ dashboard.columns }},
-        'load_preferences_function': function(options) {
-            return {% autoescape off %}{{ dashboard_preferences }}{% endautoescape %};
-        },
-        'save_preferences_function': function(options, preferences) {
-            jQuery.post(
-                '{% url admin-tools-dashboard-set-preferences %}',
-                { data: JSON.stringify(preferences) }
-            );
-        }
-    });
-    $(".group-tabs").tabs();
-    $(".group-accordion").accordion({header: '.group-accordion-header'});
+
+    init_dashboard(
+        '{{ dashboard.get_id }}',
+        {{ dashboard.columns }},
+        {% autoescape off %}{{ dashboard_preferences }}{% endautoescape %},
+        '{% url admin-tools-dashboard-set-preferences %}'
+    );
 });
 //]]>
 </script>

admin_tools/media/admin_tools/js/dashboard.js

+var init_dashboard = function(id, columns, preferences, url) {
+    $('#'+id).dashboard({
+        'columns': columns,
+        'load_preferences_function': function(options) {
+            return preferences;
+        },
+        'save_preferences_function': function(options, preferences) {
+            jQuery.post(url, { data: JSON.stringify(preferences) });
+        }
+    });
+    $(".group-tabs").tabs();
+    $(".group-accordion").accordion({header: '.group-accordion-header'});
+};

admin_tools/media/admin_tools/js/utils.js

+var load_script = function(index, js_files) {
+    if (typeof(js_files[index]) != 'undefined') {
+        if (js_files[index].test()) {
+            // add script element
+            //console.log('Loading ' + js_files[index].src);
+            var head = document.getElementsByTagName('head')[0];
+            var s = document.createElement('script');
+            s.src = js_files[index].src;
+            s.type = 'text/javascript';
+            head.appendChild(s);
+            s.onload = load_script(index + 1, js_files);
+        } else {
+            //console.log('Skipped already loaded file ' + js_files[index].src);
+            load_script(index + 1, js_files);
+        }
+    }
+}

admin_tools/menu/templates/admin_tools/menu/menu.html

 {% load i18n admin_tools_menu_tags %}
 {% if menu.children %}
+<script type="text/javascript" src="{{ media_url }}/admin_tools/js/utils.js"></script>
 <script type="text/javascript" charset="utf-8">
-    // load jquery if it's not loaded yet
-    if (typeof jQuery == 'undefined') {
-        var jquery_url = '{{ media_url }}/admin_tools/js/jquery/jquery.min.js';
-        document.write(unescape('%3Cscript src="' + jquery_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
-    // load json if it's not loaded yet
-    if (typeof JSON == 'undefined') {
-        var script_url = '{{ media_url }}/admin_tools/js/json.min.js';
-        document.write(unescape('%3Cscript src="' + script_url + '" type="text/javascript"%3E%3C/script%3E'));
-    }
+    // Load js files syncronously and conditionally
+    var js_files = [
+        {
+            src : '{{ media_url }}/admin_tools/js/jquery/jquery.min.js',
+            test: function() { return typeof(jQuery) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/json.min.js',
+            test: function() { return typeof(JSON.stringify) == 'undefined'; } 
+        },
+        {
+            src : '{{ media_url }}/admin_tools/js/menu.js',
+            test: function() { return true; }
+        },
+        {% for js in menu.Media.js %}
+        {
+            src : '{{ media_url }}/{{ js }}',
+            test: function() { return true; }
+        },{% endfor %}
+    ];
+    load_script(0, js_files);
 </script>
-<script type="text/javascript" charset="utf-8" src="{{ media_url }}/admin_tools/js/menu.js"></script>
 <!--[if IE 6]>
 <script type="text/javascript">
 jQuery(document).ready(function() {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.