18c0e75c5e7d652b13826dc310b1e5301e981757
[yaffs-website] / web / modules / contrib / superfish / superfish.module
1 <?php
2
3 /**
4  * @file
5  * jQuery Superfish plugin for Drupal menus.
6  */
7
8 use Drupal\Component\Utility\Unicode;
9 use Drupal\Core\Block\BlockPluginInterface;
10 use Drupal\Component\Utility\SafeMarkup;
11 use Drupal\Core\Routing\RouteMatchInterface;
12
13 /**
14  * Implements hook_block_view_BASE_BLOCK_ID_alter().
15  */
16 function superfish_block_view_superfish_alter(array &$build, BlockPluginInterface $block) {
17   $menu_name = $block->getDerivativeId();
18   $build['#contextual_links']['menu'] = array(
19     'route_parameters' => array('menu' => $menu_name),
20   );
21 }
22
23 /**
24  * Implements hook_libraries_info().
25  */
26 function superfish_libraries_info() {
27   $libraries['superfish'] = array(
28     'name' => 'superfish',
29     'vendor url' => 'http://github.com/mehrpadin/Superfish-for-Drupal',
30     'download url' => 'https://github.com/mehrpadin/Superfish-for-Drupal/zipball/2.x',
31     'version callback' => 'superfish_library_version',
32     'files' => array(
33       'js' => array(
34         'superfish.js',
35         'jquery.hoverIntent.minified.js',
36         'sfsmallscreen.js',
37         'sftouchscreen.js',
38         'supersubs.js',
39         'supposition.js'
40       ),
41       'css' => array(
42         'css/superfish.css',
43         'style/black/black.css',
44         'style/blue/blue.css',
45         'style/coffee/coffee.css',
46         'style/default/default.css',
47         'style/white/white.css'
48       )
49     )
50   );
51   return $libraries;
52 }
53
54 /**
55  * Implements hook_library_info_build().
56  */
57 function superfish_library_info_build() {
58   $libraries = [];
59   if ($superfish_library_path = superfish_library_path()) {
60     $superfish_library_path = '/' . $superfish_library_path;
61     $libraries = [
62                         'superfish' => [
63                                 'remote' => 'http://github.com/mehrpadin/Superfish-for-Drupal',
64                                 'version' => '2.0',
65                                 'license' => [
66                                         'name' => 'MIT',
67                                         'gpl-compatible' => false,
68                                 ],
69                                 'js' => [
70                                         'js/superfish.js' => [],
71                                 ],
72                                 'dependencies' => [
73                                         'core/jquery',
74                                         'core/drupal',
75                                         'core/drupalSettings',
76                                         'core/jquery.once',
77                                 ],
78                         ],
79                         'init' => [
80                                 'js' => [
81                                         $superfish_library_path . '/superfish.js' => [],
82                                 ],
83                                 'css' => [
84                                         'base' => [
85                                                 $superfish_library_path . '/css/superfish.css' => [],
86                                         ]
87                                 ],
88                         ],
89                         'superfish_hoverintent' => [
90                                 'js' => [
91                                         $superfish_library_path . '/jquery.hoverIntent.minified.js' => [
92                                                 'minified' => true
93                                         ],
94                                 ],
95                                 'dependencies' => [
96                                         'superfish/init',
97                                 ],
98                         ],
99                         'superfish_smallscreen' => [
100                                 'js' => [
101                                         $superfish_library_path . '/sfsmallscreen.js' => [],
102                                 ],
103                                 'dependencies' => [
104                                         'superfish/init',
105                                 ],
106                         ],
107                         'superfish_touchscreen' => [
108                                 'js' => [
109                                         $superfish_library_path . '/sftouchscreen.js' => [],
110                                 ],
111                                 'dependencies' => [
112                                         'superfish/init',
113                                 ],
114                         ],
115                         'superfish_supersubs' => [
116                                 'js' => [
117                                         $superfish_library_path . '/supersubs.js' => [],
118                                 ],
119                                 'dependencies' => [
120                                         'superfish/init',
121                                 ],
122                         ],
123                         'superfish_supposition' => [
124                                 'js' => [
125                                         $superfish_library_path . '/supposition.js' => [],
126                                 ],
127                                 'dependencies' => [
128                                         'superfish/init',
129                                 ]
130                         ],
131                         'superfish_style_black' => [
132                                 'css' => [
133                                         'theme' => [
134                                                 $superfish_library_path . '/style/black/black.css' => [],
135                                         ]
136                                 ],
137                                 'dependencies' => [
138                                         'superfish/init',
139                                 ],
140                         ],
141                         'superfish_style_blue' => [
142                                 'css' => [
143                                         'theme' => [
144                                                 $superfish_library_path . '/style/blue/blue.css' => [],
145                                         ],
146                                 ],
147                                 'dependencies' => [
148                                         'superfish/init',
149                                 ]
150                         ],
151                         'superfish_style_coffee' => [
152                                 'css' => [
153                                         'theme' => [
154                                                 $superfish_library_path . '/style/coffee/coffee.css' => [],
155                                         ],
156                                 ],
157                                 'dependencies' => [
158                                         'superfish/init',
159                                 ],
160                         ],
161                         'superfish_style_default' => [
162                                 'css' => [
163                                         'theme' => [
164                                                 $superfish_library_path . '/style/default/default.css' => [],
165                                         ],
166                                 ],
167                                 'dependencies' => [
168                                         'superfish/init',
169                                 ],
170                         ],
171                         'superfish_style_white' => [
172                                 'css' => [
173                                         'theme' => [
174                                                 $superfish_library_path . '/style/white/white.css' => []
175                                         ],
176                                 ],
177                                 'dependencies' => [
178                                         'superfish/init',
179                                 ],
180                         ],
181     ];
182     if ($easing_library_path = superfish_library_path('easing')) {
183       $easing_library_path = '/' . $easing_library_path;
184       $libraries += [
185                                 'superfish_easing' => [
186                                         'js' => [
187                                                 $easing_library_path . '/jquery.easing.js' => [],
188                                         ],
189                                 ],
190                         ];
191     }
192   }
193   return $libraries;
194 }
195
196 /**
197  * Returns the Superfish library folder location.
198  */
199 function superfish_library_path($library = 'superfish') {
200   $directory = false;
201   // Ensure the Libraries API module is installed and working.
202   if (function_exists('libraries_get_path') && ($directory = libraries_get_path($library))) {
203     return $directory;
204   }
205   // Otherwise use the default directory.
206   if (file_exists('profiles/' . drupal_get_profile() . '/libraries/' . $library)) {
207     $directory = 'profiles/' . drupal_get_profile() . '/libraries/' . $library;
208   }
209   elseif (file_exists('libraries/' . $library)) {
210     $directory = 'libraries/' . $library;
211   }
212   elseif (file_exists('sites/all/libraries/' . $library)) {
213     $directory = 'sites/all/libraries/' . $library;
214   }
215   return $directory;
216 }
217
218 /**
219  * Verifies Superfish library is present.
220  */
221 function superfish_library_check() {
222   if (function_exists('libraries_get_libraries')) {
223     $library = libraries_get_libraries();
224     if (isset($library['superfish'])) {
225       return TRUE;
226     }
227   }
228   elseif (($directory = superfish_library_path()) && file_exists($directory . '/superfish.js')) {
229     return TRUE;
230   }
231   return FALSE;
232 }
233
234 /**
235  * Checks Superfish library version.
236  */
237 function superfish_library_version() {
238   // Get the library version.
239   if (($directory = superfish_library_path()) && file_exists($directory . '/VERSION')) {
240     $version = file_get_contents($directory . '/VERSION');
241     // Removing blank lines and white spaces.
242     $version = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", trim($version));
243     if (!empty($version)) {
244       return $version;
245     }
246     return FALSE;
247   }
248   return FALSE;
249 }
250
251 /**
252  * Generate a list of available slide-in effects.
253  */
254 function superfish_effects() {
255   $output = array(
256     'none' => '- ' . t('None') . ' -',
257     'vertical' => t('Vertical'),
258     'horizontal' => t('Horizontal'),
259     'diagonal' => t('Diagonal')
260   );
261
262   if (superfish_library_path('easing')) {
263     $easing_types = array(
264       'easeInSine_vertical' => 'easeInSine (' . t('Vertical') . ')',
265       'easeInSine_horizontal' => 'easeInSine (' . t('Horizontal') . ')',
266       'easeInSine_diagonal' => 'easeInSine (' . t('Diagonal') . ')',
267       'easeInQuint_vertical' => 'easeInQuint (' . t('Vertical') . ')',
268       'easeInQuint_horizontal' => 'easeInQuint (' . t('Horizontal') . ')',
269       'easeInQuint_diagonal' => 'easeInQuint (' . t('Diagonal') . ')',
270       'easeInQuart_vertical' => 'easeInQuart (' . t('Vertical') . ')',
271       'easeInQuart_horizontal' => 'easeInQuart (' . t('Horizontal') . ')',
272       'easeInQuart_diagonal' => 'easeInQuart (' . t('Diagonal') . ')',
273       'easeInQuad_vertical' => 'easeInQuad (' . t('Vertical') . ')',
274       'easeInQuad_horizontal' => 'easeInQuad (' . t('Horizontal') . ')',
275       'easeInQuad_diagonal' => 'easeInQuad (' . t('Diagonal') . ')',
276       'easeInExpo_vertical' => 'easeInExpo (' . t('Vertical') . ')',
277       'easeInExpo_horizontal' => 'easeInExpo (' . t('Horizontal') . ')',
278       'easeInExpo_diagonal' => 'easeInExpo (' . t('Diagonal') . ')',
279       'easeInElastic_vertical' => 'easeInElastic (' . t('Vertical') . ')',
280       'easeInElastic_horizontal' => 'easeInElastic (' . t('Horizontal') . ')',
281       'easeInElastic_diagonal' => 'easeInElastic (' . t('Diagonal') . ')',
282       'easeInCubic_vertical' => 'easeInCubic (' . t('Vertical') . ')',
283       'easeInCubic_horizontal' => 'easeInCubic (' . t('Horizontal') . ')',
284       'easeInCubic_diagonal' => 'easeInCubic (' . t('Diagonal') . ')',
285       'easeInCirc_vertical' => 'easeInCirc (' . t('Vertical') . ')',
286       'easeInCirc_horizontal' => 'easeInCirc (' . t('Horizontal') . ')',
287       'easeInCirc_diagonal' => 'easeInCirc (' . t('Diagonal') . ')',
288       'easeInBounce_vertical' => 'easeInBounce (' . t('Vertical') . ')',
289       'easeInBounce_horizontal' => 'easeInBounce (' . t('Horizontal') . ')',
290       'easeInBounce_diagonal' => 'easeInBounce (' . t('Diagonal') . ')',
291       'easeInBack_vertical' => 'easeInBack (' . t('Vertical') . ')',
292       'easeInBack_horizontal' => 'easeInBack (' . t('Horizontal') . ')',
293       'easeInBack_diagonal' => 'easeInBack (' . t('Diagonal') . ')'
294     );
295     $output = array_merge($output, $easing_types);
296   }
297   return $output;
298 }
299
300 /**
301  * Helper function to clean up arrays.
302  */
303 function superfish_array_remove_empty($haystack) {
304   foreach ($haystack as $key => $value) {
305     if (is_array($value)) {
306       $haystack[$key] = superfish_array_remove_empty($haystack[$key]);
307     }
308     elseif (empty($value) && is_bool($value) !== TRUE) {
309       if ($haystack[$key] != '0') {
310         unset($haystack[$key]);
311       }
312     }
313   }
314   return $haystack;
315 }
316
317 /**
318  * Implements hook_theme().
319  */
320 function superfish_theme($existing, $type, $theme, $path) {
321   return array(
322     'superfish' => array(
323       'render element' => 'element',
324       'file' => 'superfish.theme.inc',
325     ),
326     'superfish_menu_items' => array(
327       'render element' => 'element',
328       'file' => 'superfish.theme.inc',
329     ),
330     'superfish_help' => array(
331       'variables' => array(
332         'title' => NULL,
333         'content' => NULL,
334       ),
335       'template' => 'superfish--help',
336     ),
337   );
338 }
339
340 /**
341  * Implements hook_help().
342  */
343 function superfish_help($route_name, RouteMatchInterface $route_match) {
344   switch ($route_name) {
345     case 'help.page.superfish':
346       $output = array(
347         '#theme' => 'superfish_help',
348         '#title' => t('About Superfish'),
349         '#content' => t('<a href="@url_module">Superfish module</a> integrates <a href="@url_library">jQuery Superfish</a> plugin to your Drupal menu blocks. Please refer to the module <a href="@url_documentation">documentation</a> for more information.',
350                         array(
351                                 '@url_library' => 'https://github.com/mehrpadin/Superfish-for-Drupal',
352                                 '@url_module' => 'https://www.drupal.org/project/superfish',
353                                 '@url_documentation' => 'https://www.drupal.org/node/1125896'
354                         )
355                 ),
356       );
357       return $output;
358   }
359 }