1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Enable loading of Javascript and CSS data with AJAX loads. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Implementation of hook_menu(). |
---|
10 | */ |
---|
11 | function ajax_load_init() { |
---|
12 | // Add on every page because we can't know in advance if an AJAX method |
---|
13 | // will be called. |
---|
14 | drupal_add_js(drupal_get_path('module', 'ajax_load') .'/ajax_load.js'); |
---|
15 | } |
---|
16 | |
---|
17 | /** |
---|
18 | * Implementation of hook_theme_registry_alter(). |
---|
19 | */ |
---|
20 | function ajax_load_theme_registry_alter(&$theme_registry) { |
---|
21 | // Prepend a template preprocess function to page.tpl.php. |
---|
22 | array_unshift($theme_registry['page']['preprocess functions'], '_ajax_load_preprocess_page'); |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Template preprocess function for page.tpl.php. |
---|
27 | */ |
---|
28 | function _ajax_load_preprocess_page(&$variables) { |
---|
29 | $base_path = base_path(); |
---|
30 | $resources = ajax_data_get_data(); |
---|
31 | $js_settings = array(); |
---|
32 | |
---|
33 | // Build the list of css files on the page when compression is enabled. |
---|
34 | if (variable_get('preprocess_css', FALSE)) { |
---|
35 | $files = array(); |
---|
36 | foreach ($resources['css'] as $data) { |
---|
37 | foreach ($data as $paths) { |
---|
38 | foreach (array_keys($paths) as $path) { |
---|
39 | $files[$base_path . $path] = 1; |
---|
40 | } |
---|
41 | } |
---|
42 | } |
---|
43 | if (!empty($files)) { |
---|
44 | $js_settings['css'] = array_keys($files); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | // Build the list of javascript files on the page when compression is enabled. |
---|
49 | if (variable_get('preprocess_js', FALSE)) { |
---|
50 | $files = array(); |
---|
51 | foreach ($resources['scripts'] as $type => $paths) { |
---|
52 | if (in_array($type, array('core', 'module', 'theme'))) { |
---|
53 | foreach (array_keys($paths) as $path) { |
---|
54 | $files[$base_path . $path] = 1; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | if (!empty($files)) { |
---|
59 | $js_settings['scripts'] = array_keys($files); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | // Let the client-side know which individual files are present on the page |
---|
64 | // when compression is enabled. |
---|
65 | if (!empty($js_settings)) { |
---|
66 | drupal_add_js(array('AjaxLoad' => $js_settings), 'setting'); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Implementation of hook_ajax_data_alter(). |
---|
72 | */ |
---|
73 | function ajax_load_ajax_data_alter(&$data) { |
---|
74 | $extra = ajax_data_get_data(); |
---|
75 | $array = FALSE; |
---|
76 | // Detect whether the data being altered is an array. |
---|
77 | if (is_array($data)) { |
---|
78 | $data = (object) $data; |
---|
79 | $array = TRUE; |
---|
80 | } |
---|
81 | $data->scripts = $extra['scripts']; |
---|
82 | $data->css = $extra['css']; |
---|
83 | if (!isset($data->__callbacks)) { |
---|
84 | $data->__callbacks = array(); |
---|
85 | } |
---|
86 | // Set the AjaxLoad custom event as a callback. |
---|
87 | $data->__callbacks[] = 'Drupal.AjaxLoad.loadFiles'; |
---|
88 | // Cast back to an array if necessary. |
---|
89 | if ($array) { |
---|
90 | $data = (array) $data; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Return an array of data representing the scripts and CSS files on a page. |
---|
96 | */ |
---|
97 | function ajax_data_get_data() { |
---|
98 | $return = array(); |
---|
99 | $return['scripts'] = array(); |
---|
100 | foreach (array('header', 'footer') as $scope) { |
---|
101 | $javascript = drupal_add_js(NULL, NULL, $scope); |
---|
102 | foreach ($javascript as $type => $data) { |
---|
103 | if (!$data) { |
---|
104 | unset($javascript[$type]); |
---|
105 | } |
---|
106 | elseif ($type == 'setting') { |
---|
107 | $javascript[$type] = call_user_func_array('array_merge_recursive', $data); |
---|
108 | } |
---|
109 | } |
---|
110 | $return['scripts'] = array_merge_recursive($return['scripts'], $javascript); |
---|
111 | } |
---|
112 | $return['css'] = drupal_add_css(); |
---|
113 | |
---|
114 | // Allow the color.module alter the CSS array as if it was processed from |
---|
115 | // a regular page request. See phptemplate_preprocess_page() in garland. |
---|
116 | if (module_exists('color')) { |
---|
117 | init_theme(); |
---|
118 | _color_page_alter($return); |
---|
119 | } |
---|
120 | |
---|
121 | // Deal with jQuery Update module, if exists. |
---|
122 | if (module_exists('jquery_update')) { |
---|
123 | ajax_load_jquery_update_preprocess($return); |
---|
124 | } |
---|
125 | |
---|
126 | return $return; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Replace Drupal core's jquery.js with the new one from jQuery Update module. |
---|
131 | * |
---|
132 | * @see jquery_update_preprocess_page() |
---|
133 | */ |
---|
134 | function ajax_load_jquery_update_preprocess(&$variables) { |
---|
135 | // Only do this for pages that have JavaScript on them. |
---|
136 | if (!empty($variables['scripts'])) { |
---|
137 | |
---|
138 | // Perform the logic if either jQuery Update's jquery.js is newer than |
---|
139 | // core's, or if we're using a different compression type. |
---|
140 | if (variable_get('jquery_update_replace', TRUE) || |
---|
141 | variable_get('jquery_update_compression_type', 'pack') != 'pack') { |
---|
142 | |
---|
143 | // Replace jquery.js first. |
---|
144 | $new_jquery = array(jquery_update_jquery_path() => $variables['scripts']['core']['misc/jquery.js']); |
---|
145 | $variables['scripts']['core'] = array_merge($new_jquery, $variables['scripts']['core']); |
---|
146 | unset($variables['scripts']['core']['misc/jquery.js']); |
---|
147 | |
---|
148 | // Loop through each of the required replacements. |
---|
149 | foreach (jquery_update_get_replacements() as $type => $replacements) { |
---|
150 | foreach ($replacements as $find => $replace) { |
---|
151 | // If the file to replace is loaded on this page... |
---|
152 | if (isset($variables['scripts'][$type][$find])) { |
---|
153 | // Create a new entry for the replacement file, and unset the original one. |
---|
154 | $replace = JQUERY_UPDATE_REPLACE_PATH . '/' . $replace; |
---|
155 | $variables['scripts'][$type][$replace] = $variables['scripts'][$type][$find]; |
---|
156 | unset($variables['scripts'][$type][$find]); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|