-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqmulti.module
232 lines (215 loc) · 6.25 KB
/
jqmulti.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @file
* Code for the jQuery Multi module.
*/
/**
* Implements hook_menu().
*/
function jqmulti_menu() {
$items = array();
$items['admin/config/system/jqmulti'] = array(
'title' => t('jQuery Multi Settings'),
'description' => t('Settings for jQuery Multi module'),
'page callback' => 'drupal_get_form',
'page arguments' => array('jqmulti_admin_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'jqmulti.admin.inc',
);
return $items;
}
/**
* Implements hook_js_alter().
*
* This is where the magic happens. The new jQuery is added and noConflict() is run.
* Finally, scripts are rearranged or added so that the targeted scripts run with
* the newer jQuery.
*/
function jqmulti_js_alter(&$javascript) {
$jquery_path = jqmulti_jquery_path();
if (!$jquery_path) {
return;
}
// Get info about which files are needed.
$files = jqmulti_get_files();
if (empty($files)) {
if (!variable_get('jqmulti_load_always', FALSE)) {
return;
}
}
// Add jQuery and the switch script to the $files array
$switch_path = jqmulti_switch_path();
$switch_options = array(
'weight' => 100,
'group' => -49,
);
$jquery_options = array(
'weight' => -100,
'group' => -51,
);
$files = array_merge(array($jquery_path => $jquery_options), $files, array($switch_path => $switch_options));
// Rearrange / Add JS
$default_group = -50;
$default_weight = 0;
foreach ($files as $file => $options) {
if (!in_array($file, $javascript)) {
$javascript[$file] = drupal_js_defaults();
$javascript[$file]['data'] = $file;
}
$javascript[$file]['group'] = isset($options['group']) ? $options['group'] : $default_group;
$javascript[$file]['weight'] = isset($options['weight']) ? $options['weight'] : $default_weight;
// We increment the default weight, to ensure that files that don't have specified weight are
// included in the order they were added. But we only increment a bit so as to not generate a
// large weight range that might be confusing to deal with for other modules setting weights of files.
$default_weight += 0.01;
}
}
/**
* Returns a list of files that should be loaded with the second jQuery.
*/
function jqmulti_get_files($reset = FALSE) {
// We store the file list in cache, since it's not going to change much.
if (!$reset && $cache = cache_get('jqmulti_files')) {
return $cache->data;
}
// Build files array.
$files = module_invoke_all('jqmulti_files');
$libraries = module_invoke_all('jqmulti_libraries');
// Get also any libraries added by UI.
$ui_libs = variable_get('jqmulti_libraries', array());
foreach ($ui_libs as $ui_lib => $on) {
if ($on && !in_array($ui_lib, array_keys($libraries))) {
$libraries[$ui_lib] = array();
}
}
// Add library files.
foreach ($libraries as $library => $options) {
$lib_files = jqmulti_get_library_files($library);
$lib_files_temp = array();
foreach ($lib_files as $lib_file) {
$lib_files_temp[$lib_file] = $options;
}
$files = array_merge($files, $lib_files_temp);
}
// Make all files keys in the array.
$temp_files = array();
foreach ($files as $key => $value) {
if (is_array($value)) {
$temp_files[$key] = $value;
}
else {
$temp_files[$value] = array();
}
}
$files = $temp_files;
cache_set('jqmulti_files', $files);
return $files;
}
/**
* Returns a list of files for a given library.
* This is not a part of Libraries API for D6.
*/
function jqmulti_get_library_files($library, $reset = FALSE) {
$base_path = 'sites/all/libraries';
$path = $base_path . '/' . $library;
// check whether list of files is in cache
if (!$reset && $cache = cache_get('jqmulti_files_' . $library)) {
return $cache->data;
}
// find list of js files for that library
$files = jqmulti_find_all_js_files($path);
cache_set('jqmulti_files_' . $library, $files);
return $files;
}
/**
* Returns available libraries to be used in the admin form.
*/
function jqmulti_get_available_libraries() {
$libs = libraries_get_libraries();
unset($libs['jquery']);
foreach ($libs as $key => $value) {
$libs[$key] = $key;
}
return $libs;
}
/**
* Returns the path to the switch script.
*/
function jqmulti_switch_path() {
return drupal_get_path('module', 'jqmulti') . '/js/switch.js';
}
/**
* Returns the path to the currently selected version of jQuery.
*/
function jqmulti_jquery_path() {
$version = jqmulti_get_version();
if ($version) {
return libraries_get_path('jquery') . '/jquery-' . $version . '.min.js';
}
return FALSE;
}
/**
* Recursive function that returns a list of all JS files in a directory.
* @param $dir the full path to the directory
*/
function jqmulti_find_all_js_files($dir) {
$result = array();
$root = scandir($dir);
foreach ($root as $value) {
if ($value === '.' || $value === '..') {
continue;
}
$path = "$dir/$value";
if (is_file($path)) {
if (preg_match('/.js$/', $value)) {
$result[] = $path;
}
continue;
}
foreach (jqmulti_find_all_js_files($path) as $value) {
$result[] = $value;
}
}
return $result;
}
/**
* Gets the version of jQuery to load.
* @param $reset whether to reset the cached version number
*/
function jqmulti_get_version($reset = FALSE) {
if (!$reset && $cache = cache_get('jqmulti_version')) {
return $cache->data;
}
$libraries = libraries_get_libraries();
if (isset($libraries['jquery'])) {
$files = jqmulti_get_library_files('jquery', $reset);
if ($files) {
foreach ($files as $file) {
// Get the file name.
$version = jqmulti_jquery_version_from_path($file);
if ($version) {
cache_set('jqmulti_version', $version);
return $version;
}
}
}
}
return FALSE;
}
/**
* Gets the jQuery version from the properly formatted filename.
* @param $path the full path to the jQuery library file
*/
function jqmulti_jquery_version_from_path($path) {
$parts = explode('/', $path);
$filename = array_pop($parts);
$matches = array();
preg_match('/jquery-(.*)\.min\.js/', $filename, $matches);
if (count($matches) == 2) {
return $matches[1];
}
else {
return FALSE;
}
}