1 | |
---|
2 | (function ($) { |
---|
3 | |
---|
4 | Drupal.AjaxLoad = Drupal.AjaxLoad || { externalStyles: [], externalScripts: [], loadPending: [] }; |
---|
5 | |
---|
6 | /** |
---|
7 | * Load JavaScript and CSS files and data. |
---|
8 | */ |
---|
9 | Drupal.AjaxLoad.loadFiles = function (target, response) { |
---|
10 | // Initialize the list of currently loaded external stylesheets. |
---|
11 | if (!Drupal.AjaxLoad.externalStyles.length) { |
---|
12 | if (Drupal.settings.AjaxLoad && Drupal.settings.AjaxLoad['css']) { |
---|
13 | $.each(Drupal.settings.AjaxLoad['css'], function(i, path) { |
---|
14 | Drupal.AjaxLoad.externalStyles.push(path); |
---|
15 | }); |
---|
16 | } |
---|
17 | else { |
---|
18 | $('link[type="text/css"]').each(function() { |
---|
19 | Drupal.AjaxLoad.externalStyles.push($(this).attr('href')); |
---|
20 | }); |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | // Initialize the list of currently loaded external scripts. |
---|
25 | if (!Drupal.AjaxLoad.externalScripts.length) { |
---|
26 | if (Drupal.settings.AjaxLoad && Drupal.settings.AjaxLoad['scripts']) { |
---|
27 | $.each(Drupal.settings.AjaxLoad['scripts'], function(i, path) { |
---|
28 | Drupal.AjaxLoad.externalScripts.push(path); |
---|
29 | }); |
---|
30 | } |
---|
31 | else { |
---|
32 | $('script[src]').each(function() { |
---|
33 | Drupal.AjaxLoad.externalScripts.push($(this).attr('src')); |
---|
34 | }); |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | // Handle scripts. Do this first so that behaviors can access them easily. |
---|
39 | if (response.scripts) { |
---|
40 | // Each Ajax operation needs its own counter. |
---|
41 | var index = Drupal.AjaxLoad.loadPending.length; |
---|
42 | Drupal.AjaxLoad.loadPending[index] = 0; |
---|
43 | if (!response.__customSettings && response.scripts.setting) { |
---|
44 | $.extend(true, Drupal.settings, response.scripts.setting); |
---|
45 | } |
---|
46 | // Inline scripts will be handled separately. |
---|
47 | var types = ['core', 'module', 'theme']; |
---|
48 | $.each(types, function (i, type) { |
---|
49 | if (response.scripts[type]) { |
---|
50 | $.each(response.scripts[type], function (src, data) { |
---|
51 | // Load scripts. |
---|
52 | src = Drupal.settings.basePath + src; |
---|
53 | // Test if the script already exists. |
---|
54 | var found = false; |
---|
55 | for (var j = 0; j < Drupal.AjaxLoad.externalScripts.length; j++) { |
---|
56 | if (Drupal.AjaxLoad.externalScripts[j].indexOf(src) == 0) { |
---|
57 | found = true; |
---|
58 | break; |
---|
59 | } |
---|
60 | } |
---|
61 | if (!found) { |
---|
62 | Drupal.AjaxLoad.loadPending[index]++; |
---|
63 | $.getScript(src, function () { |
---|
64 | Drupal.AjaxLoad.externalScripts.push(src); |
---|
65 | Drupal.AjaxLoad.loadComplete(index, target, response); |
---|
66 | }); |
---|
67 | } |
---|
68 | }); |
---|
69 | } |
---|
70 | }); |
---|
71 | // Ensure Drupal behaviors are attached to new content, even when no |
---|
72 | // new external scripts have been loaded. |
---|
73 | if (Drupal.AjaxLoad.loadPending[index] == 0) { |
---|
74 | Drupal.attachBehaviors(target); |
---|
75 | // Ensure inline scripts are parsed after all external scripts have loaded. |
---|
76 | Drupal.AjaxLoad.loadInline(response); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | // Handle stylesheets. |
---|
81 | if (response.css) { |
---|
82 | var types = ['module', 'theme']; |
---|
83 | $.each(response.css, function (media, files) { |
---|
84 | $.each(types, function (i, type) { |
---|
85 | if (files[type]) { |
---|
86 | $.each(files[type], function (src, data) { |
---|
87 | // Load stylesheets. |
---|
88 | src = Drupal.settings.basePath + src; |
---|
89 | // Test if the stylesheet already exists. |
---|
90 | var found = false; |
---|
91 | for (var j = 0; j < Drupal.AjaxLoad.externalStyles.length; j++) { |
---|
92 | if (Drupal.AjaxLoad.externalStyles[j].indexOf(src) == 0) { |
---|
93 | found = true; |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | if (!found) { |
---|
98 | Drupal.AjaxLoad.externalStyles.push(src); |
---|
99 | $('<link type="text/css" rel="stylesheet" media="' + media + '" href="' + src + '" />').appendTo('head'); |
---|
100 | } |
---|
101 | }); |
---|
102 | } |
---|
103 | }); |
---|
104 | }); |
---|
105 | } |
---|
106 | }; |
---|
107 | |
---|
108 | /** |
---|
109 | * Parse inline scripts after all external scripts have loaded. |
---|
110 | */ |
---|
111 | Drupal.AjaxLoad.loadInline = function(response) { |
---|
112 | // Handle inline scripts. |
---|
113 | if (response.scripts.inline) { |
---|
114 | $.each(response.scripts.inline, function (i, script) { |
---|
115 | // document.write calls would mess things up. |
---|
116 | if (script.code.indexOf('document.write') == -1) { |
---|
117 | eval(script.code); |
---|
118 | } |
---|
119 | }); |
---|
120 | } |
---|
121 | }; |
---|
122 | |
---|
123 | /** |
---|
124 | * When all scripts have loaded, attach behaviors. |
---|
125 | */ |
---|
126 | Drupal.AjaxLoad.loadComplete = function(index, target, response) { |
---|
127 | Drupal.AjaxLoad.loadPending[index]--; |
---|
128 | if (Drupal.AjaxLoad.loadPending[index] == 0) { |
---|
129 | Drupal.attachBehaviors(target); |
---|
130 | // Ensure inline scripts are parsed after all external scripts have loaded. |
---|
131 | Drupal.AjaxLoad.loadInline(response); |
---|
132 | } |
---|
133 | }; |
---|
134 | |
---|
135 | })(jQuery); |
---|