Updated copyright notice as there are changes in this year
[mailer.git] / js / ajax-loader.js
1 /**
2  * JavaScript for loading more JavaScripts with AJAX
3  * --------------------------------------------------------------------
4  * $Revision::                                                        $
5  * $Date::                                                            $
6  * $Tag:: 0.2.1-FINAL                                                 $
7  * $Author::                                                          $
8  * --------------------------------------------------------------------
9  * Copyright (c) 2003 - 2009 by Roland Haeder
10  * Copyright (c) 2009 - 2013 by Mailer Developer Team
11  * For more information visit: http://mxchange.org
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
26  * MA  02110-1301  USA
27  */
28
29 // Loaded scripts counter
30 var resourceCounter = 0;
31
32 // This array holds a copy of resources which shall be loaded
33 var loadResources = new Array();
34
35 // Waiting for resources being loaded
36 function loadScriptsLocked () {
37         // Has all been loaded?
38         if (resourceCounter == loadResources.length) {
39                 // Then release ready()
40                 $.holdReady(false);
41         } else {
42                 // Recursive call again
43                 window.setTimeout('loadScriptsLocked()', 100);
44         }
45 }
46
47 // Loads more JavaScript resources
48 function loadScripts (resources) {
49         // Abort here if resources is not defined
50         if (resources == undefined) {
51                 // This is really bad ...
52                 throw new 'loadScripts() called with no resources (JavaScript files) to load!';
53         } // END - if
54
55         // Make ready() as holded
56         $.holdReady(true);
57
58         // Transfer all resources to global array
59         loadResources = resources;
60
61         // Begin the loop for all JavaScript files
62         for (var i = 0; i < resources.length; i++) {
63                 // Is the script set?
64                 if (resources[i] == undefined) {
65                         // Array element is missing, which means someone mades a boo boo ...
66                         throw new ('resources[' + i + '] not set. Please fix your array.');
67                 } // END - if
68
69                 // Load next script
70                 $.getScript(resources[i], function (data, textStatus) {
71                         // Did something bad happen?
72                         if (textStatus != 'success') {
73                                 // Throw an exception here
74                                 throw new 'Cannot load script ' + resources[i] +  ' with status ' + textStatus + '!';
75                         } // END - if
76
77                         // Count it as loaded
78                         resourceCounter++;
79                 });
80
81                 // Wait here a little
82                 $('body').delay(100);
83         } // END - for
84
85         // Wait until all scripts are loaded (asynchronously)
86         loadScriptsLocked();
87
88         // All loaded
89         return true;
90 }