/
jglidemenu.module
519 lines (483 loc) · 18.6 KB
/
jglidemenu.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
/**
* @file
* Module to enable jglidemenu for drupal menu block.
*
* Maintainer: mahesh sankhala (msankhala)
* uses script jglidemenu.js you can get it from here http://sonicradish.com/labs/jGlideMenu/current/index.html
*/
/**
* Implements hook_help().
*/
function jglidemenu_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help/jglidemenu#description':
$output .= t('Enable drupal menu block as jGlideMenu block. It uses <a href="here http://sonicradish.com/labs/jGlideMenu/current/index.html">jglidemenu.js</a> script. you can see <a href="http://sonicradish.com/labs/jGlideMenu/current/static_demo.html">demo</a> here.');
break;
}
return $output;
}
/**
* Implements hook_menu().
*/
function jglidemenu_menu() {
$items['admin/config/user-interface/jglidemenu'] = array(
'title' => 'jGlideMenu',
'description' => 'Configure jGlideMenu.',
'page callback' => 'drupal_get_form',
'page arguments' => array('jglidemenu_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'jglidemenu.admin.inc',
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function jglidemenu_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'system_theme_settings':
// This is a global setting, so only insert the field
// on the global settings page.
if (arg(4) && arg(4) != 'global') {
return;
}
// Have to add a custom submit handler since this form doesn't use
// the standard system submit handler.
$form['#submit'][] = 'jglidemenu_system_theme_settings_submit';
// Add global theme setting for a custom CSS file.
$form['jglidemenu_custom_css'] = array(
'#type' => 'textfield',
'#title' => t('Path to custom jglidemenu menus CSS file'),
'#description' => t('To override the default jglidemenu CSS layout, enter the path to your custom CSS file. It should be a relative path from the root of your Drupal install (e.g. sites/all/themes/example/mymenu.css).'),
'#default_value' => variable_get('jglidemenu_custom_css', ''),
// Field appears below submit buttons without this -- yucky.
'#weight' => 0,
);
break;
}
}
/**
* Records the jglidemenu menu custom CSS file per theme.
*/
function jglidemenu_system_theme_settings_submit($form, &$form_state) {
variable_set('jglidemenu_custom_css', $form_state['values']['jglidemenu_custom_css']);
}
/**
* Implements hook_init().
*
* We are adding the JavaScript and CSS here rather than theme_jglidemenu_menu
* because when block caching is enabled none of it would get fired
* and the menus are unstyled.
*/
function jglidemenu_init() {
// Add Superfish JavaScript, if enabled.
//if (variable_get('jglidemenu_menus_js', 1) == 1) {
// The script, from http://users.tpg.com.au/j_birch/plugins/superfish.
//drupal_add_js(drupal_get_path('module', 'jglidemenu_menus') . '/superfish/js/superfish.js');
// Add the Superfish options variables.
drupal_add_js(array(
'jglidemenu_menus_options' => array(
'delay' => variable_get('jglidemenu_scrollspeed', 750),
'speed' => variable_get('jglidemenu_verticalscroll', 'normal'),
),
), array('type' => 'setting', 'scope' => JS_DEFAULT));
//add jglidemenu block specific settings to Drupal.settings
for ($delta = 1; $delta <= variable_get('jglidemenu_number', '2'); $delta++) {
drupal_add_js(array(
'jglidemenu_menus_options' => array(
'jglidemenu_depth_'.$delta => variable_get('jglidemenu_depth_' . $delta, '-1'),
'jglidemenu_type_'.$delta => variable_get('jglidemenu_type_' . $delta, 'right')
),
), array('type' => 'setting', 'scope' => JS_DEFAULT));
}
// Add the bgIframe plugin.
//drupal_add_js(drupal_get_path('module', 'jglidemenu_menus') . '/superfish/js/jquery.bgiframe.min.js');
// Add the HoverIntent plugin.
//drupal_add_js(drupal_get_path('module', 'jglidemenu_menus') . '/superfish/js/jquery.hoverIntent.minified.js');
// The jglidemenu menus implementation.
drupal_add_js(drupal_get_path('module', 'jglidemenu') . '/js/jglidemenu.069.js');
//}
// Add main CSS functionality.
// drupal_add_css(drupal_get_path('module', 'jglidemenu_menus') . '/css/jglidemenu_menus.css', array('group' => CSS_DEFAULT, 'basename' => 'jglidemenu_menus.css'));
// Add custom CSS layout if specified.
if ($custom = variable_get('jglidemenu_custom_css', '')) {
drupal_add_css($custom, array('group' => CSS_DEFAULT, 'basename' => 'jglidemenu_custom.css'));
}
// Fall back to default layout.
else {
drupal_add_css(drupal_get_path('module', 'jglidemenu') . '/css/jglidemenu_default.css', array('group' => CSS_DEFAULT, 'basename' => 'jglidemenu_default.css'));
}
}
/**
* Implements hook_block_info().
*/
function jglidemenu_block_info() {
$blocks = array();
for ($i = 1; $i <= variable_get('jglidemenu_number', '2'); $i++) {
$blocks[$i]['info'] = variable_get('jglidemenu_name_' . $i, 'jglidemenu ' . $i) . ' (jglidemenu)';
// We have too many things changing per user, per page to cache.
$blocks[$i]['cache'] = DRUPAL_NO_CACHE;
}
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function jglidemenu_block_configure($delta) {
$form['jglidemenu_name_' . $delta] = array(
'#type' => 'textfield',
'#title' => t('Menu Name'),
'#default_value' => variable_get('jglidemenu_name_' . $delta, 'jglidemenu ' . $delta),
);
$form['jglidemenu_menu_' . $delta] = array(
'#type' => 'select',
'#title' => t('Menu Parent'),
'#description' => t('The menu parent from which to show a jglidemenu.'),
'#default_value' => variable_get('jglidemenu_menu_' . $delta, 'navigation:0'),
'#options' => menu_get_menus(),
);
$form['jglidemenu_depth_' . $delta] = array(
'#type' => 'select',
'#title' => t('Menu Depth'),
'#description' => t('The depth of the menu, i.e. the number of child levels starting with the parent selected above. Leave set to -1 to display all children and use 0 to display no children.'),
'#default_value' => variable_get('jglidemenu_depth_' . $delta, -1),
'#options' => drupal_map_assoc(range(-1, 5)),
);
$form['jglidemenu_type_' . $delta] = array(
'#type' => 'select',
'#title' => t('Menu Style'),
'#description' => t('right: menu items are listed on top of each other and slide to the right for children menu item.') . '<br />' . t('left: menu items are listed on top of each other and slide to the left'),
'#default_value' => variable_get('jglidemenu_type_' . $delta, 'right'),
'#options' => drupal_map_assoc(array('right', 'left')),
);
return $form;
}
/**
* Implements hook_block_save().
*/
function jglidemenu_block_save($delta, $edit) {
variable_set('jglidemenu_name_' . $delta, $edit['jglidemenu_name_' . $delta]);
variable_set('jglidemenu_menu_' . $delta, $edit['jglidemenu_menu_' . $delta]);
variable_set('jglidemenu_depth_' . $delta, $edit['jglidemenu_depth_' . $delta]);
variable_set('jglidemenu_type_' . $delta, $edit['jglidemenu_type_' . $delta]);
}
/**
* Implements hook_block_view().
*/
function jglidemenu_block_view($delta) {
// Build the jglidemenu menu for the block.
list($menu_name) = explode(':', variable_get('jglidemenu_menu_' . $delta, 'navigation:0'));
$direction = variable_get('jglidemenu_type_' . $delta, 'right');
$depth = variable_get('jglidemenu_depth_' . $delta, '-1');
if ($output = theme('jglidemenu', array('id' => $delta, 'menu_name' => $menu_name, 'direction' => $direction, 'depth' => $depth))) {
$block['content'] = $output['content'];
// if (variable_get('jglidemenu_type_' . $delta, 'right') == 'down') {
// $class = 'jglidemenu-hide-title';
// }
// else {
$class = 'jglidemenu-show-title';
//}
// If we're building the navigation block
// use the same block title logic as menu module.
global $user;
if ($output['subject'] == t('navigation') && $user->uid) {
$subject = $user->name;
}
else {
$subject = $output['subject'];
}
$block['subject'] = '<span class="' . $class . '">' . check_plain($subject) . '</span>';
}
else {
$block['content'] = FALSE;
}
return $block;
}
/**
* Implements hook_theme().
*/
function jglidemenu_theme() {
return array(
'jglidemenu_tree' => array(
'variables' => array('menu_name' => NULL, 'mlid' => NULL, 'depth' => -1, 'menu' => NULL),
),
'jglidemenu_build' => array(
'variables' => array('menu' => NULL, 'depth' => -1, 'trail' => NULL),
),
'jglidemenu' => array(
'variables' => array('id' => NULL, 'menu_name' => NULL, 'mlid' => NULL, 'direction' => 'right', 'depth' => -1, 'menu' => NULL),
),
'jglidemenu_main_menu' => array(
'variables' => array('direction' => 'right', 'depth' => -1),
),
'jglidemenu_secondary_menu' => array(
'variables' => array('direction' => 'right', 'depth' => -1),
),
);
}
/**
* Builds the active trail from the page's menu data.
*
* @param $page_menu
* The menu data for a page.
*
* @return
* An array of parent menu item ids.
*/
function jglidemenu_build_page_trail($page_menu) {
$trail = array();
foreach ($page_menu as $item) {
if ($item['link']['in_active_trail']) {
$trail[] = $item['link']['mlid'];
}
if ($item['below']) {
$trail = array_merge($trail, jglidemenu_build_page_trail($item['below']));
}
}
return $trail;
}
/**
* Builds the final jglidemenu menu.
*
* @param $menu_name
* The top-level menu name that contains the menu to use (e.g. navigation
* or main-menu) for Drupal menus. For custom $menus this is just the
* name for menu display.
* @param $mlid
* The menu ID from which to start building the items, i.e. the parent
* of the displayed menu.
* @param $depth
* The number of children levels to display. Use -1 to display all children
* and use 0 to display no children.
* @param $menu
* Optional. A custom menu array to use for theming -- it should have
* the same structure as that returned by menu_tree_all_data().
*
* @return
* An HTML string of properly nested jglidemenu menu lists.
*/
function theme_jglidemenu_tree($variables) {
$menu_name = $variables['menu_name'];
$mlid = $variables['mlid'];
$depth = $variables['depth'];
$menu = $variables['menu'];
// Load the full menu array.
$menu = isset($menu) ? $menu : menu_tree_all_data($menu_name);
if (isset($menu)) {
$page_menu = menu_tree_page_data($menu_name);
$trail = jglidemenu_build_page_trail($page_menu);
unset($page_menu);
}
// Allow i18n module to translate strings where available.
if (module_exists('i18n_menu')) {
$menu = i18n_menu_localize_tree($menu);
}
// Assume depth == 0 by default, overriden if mlid is specified.
$parent_depth = 0;
// For custom $menus and menus built all the way from the top-level we
// don't need to "create" the specific sub-menu and we need to get the title
// from the $menu_name since there is no "parent item" array.
// Create the specific menu if we have a mlid.
if (!empty($mlid)) {
// Load the parent menu item.
$item = menu_link_load($mlid);
$title = check_plain($item['title']);
// The depth for our parent item, if it exists.
$parent_depth = ($item['depth']) ? $item['depth'] : 0;
// Narrow down the full menu to the specific sub-tree we need.
for ($p = 1; $p < 10; $p++) {
if ($sub_mlid = $item["p$p"]) {
$subitem = menu_link_load($sub_mlid);
// Menu sets these ghetto-ass keys in _menu_tree_check_access().
$menu = $menu[(50000 + $subitem['weight']) . ' ' . $subitem['title'] . ' ' . $subitem['mlid']]['below'];
}
}
}
// Otherwise just set a title and move on.
else {
// Get the title from the DB since we don't have it in the $menu.
$result = db_query("SELECT title FROM {menu_custom} WHERE menu_name = :menu_name", array(':menu_name' => $menu_name))->fetchField();
$title = check_plain($result);
}
$output['content'] = '';
$output['subject'] = $title;
if ($menu) {
// Set the total menu depth counting from this parent if we need it.
$depth = ($depth > 0) ? ($parent_depth + $depth) : $depth;
$output['content'] .= theme('jglidemenu_build', array('menu' => $menu, 'depth' => $depth, 'trail' => $trail));
}
return $output;
}
/**
* Helper function that builds the nested lists of a jglidemenu menu.
*
* @param $menu
* Menu array from which to build the nested lists.
* @param $depth
* The number of children levels to display. Use -1 to display all children
* and use 0 to display no children.
* @param $trail
* An array of parent menu items.
*/
function theme_jglidemenu_build($variables) {
$menu = $variables['menu'];
$depth = $variables['depth'];
$trail = $variables['trail'];
$output = '';
// Prepare to count the links so we can mark first, last, odd and even.
$index = 0;
$count = 0;
foreach ($menu as $menu_count) {
if ($menu_count['link']['hidden'] == 0) {
$count++;
}
}
// Get to building the menu.
foreach ($menu as $menu_item) {
$mlid = $menu_item['link']['mlid'];
// Check to see if it is a visible menu item.
if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
// Check our count and build first, last, odd/even classes.
$index++;
$first_class = $index == 1 ? ' first ' : '';
$oddeven_class = $index % 2 == 0 ? ' even ' : ' odd ';
$last_class = $index == $count ? ' last ' : '';
// Build class name based on menu path
// e.g. to give each menu item individual style.
// Strip funny symbols.
$clean_path = str_replace(array('http://', 'www', '<', '>', '&', '=', '?', ':', '.'), '', $menu_item['link']['href']);
// Convert slashes to dashes.
$clean_path = str_replace('/', '-', $clean_path);
$class = 'menu-path-' . $clean_path;
if ($trail && in_array($mlid, $trail)) {
$class .= ' active-trail';
}
// If it has children build a jglidemenu little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below'])) && $depth != 0) {
// Keep passing children into the function 'til we get them all.
if ($menu_item['link']['depth'] <= $depth || $depth == -1) {
$children = array(
'#theme' => 'jglidemenu_build',
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#menu' => $menu_item['below'],
'#depth' => $depth,
'#trail' => $trail,
);
}
else {
$children = '';
}
// Set the class to parent only of children are displayed.
$parent_class = ($children && ($menu_item['link']['depth'] <= $depth || $depth == -1)) ? 'menuparent ' : '';
$element = array(
'#below' => $children,
'#title' => $menu_item['link']['link_title'],
'#href' => $menu_item['link']['href'],
'#localized_options' => $menu_item['link']['localized_options'],
'#attributes' => array(
'class' => array('menu-' . $mlid, $parent_class, $class, $first_class, $oddeven_class, $last_class),
),
);
$variables['element'] = $element;
$output .= theme('menu_link', $variables);
}
else {
$element = array(
'#below' => '',
'#title' => $menu_item['link']['link_title'],
'#href' => $menu_item['link']['href'],
'#localized_options' => $menu_item['link']['localized_options'],
'#attributes' => array(
'class' => array('menu-' . $mlid, $class, $first_class, $oddeven_class, $last_class),
),
);
$variables['element'] = $element;
$output .= theme('menu_link', $variables);
}
}
}
return $output;
}
/**
* Theme function to allow any menu tree to be themed as a jglidemenu menu.
*
* @param $id
* The jglidemenu menu ID.
* @param $menu_name
* The top parent menu name from which to build the full menu.
* @param $mlid
* The menu ID from which to build the displayed menu.
* @param $direction
* Optional. The direction the menu expands. Default is 'right'.
* @param $depth
* The number of children levels to display. Use -1 to display all children
* and use 0 to display no children.
* @param $menu
* Optional. A custom menu array to use for theming --
* it should have the same structure as that returned
* by menu_tree_all_data(). Default is the standard menu tree.
*
* @return
* An HTML string of jglidemenu menu links.
*/
function theme_jglidemenu($variables) {
$output = array(
'content' => '',
'subject' => '',
);
$id = $variables['id'];
$menu_name = $variables['menu_name'];
$mlid = $variables['mlid'];
$direction = $variables['direction'];
$depth = $variables['depth'];
$menu = $variables['menu'];
if ($menu_tree = theme('jglidemenu_tree', array('menu_name' => $menu_name, 'mlid' => $mlid, 'depth' => $depth, 'menu' => $menu))) {
if ($menu_tree['content']) {
$output['content'] = '<ul class="jglidemenu jglidemenu-' . $direction . '" id="jglidemenu-' . $id . '">' . $menu_tree['content'] . '</ul>' . "\n";
$output['subject'] = $menu_tree['subject'];
}
}
return $output;
}
/**
* Theme the main menu as a jglidemenu menu.
*
* @param $direction
* Optional. The direction the menu expands. Default is 'down'.
* @param $depth
* The number of children levels to display. Use -1 to display all children
* and use 0 to display no children.
*
* @return
* An HTML string of jglidemenu main menu links.
*/
function theme_jglidemenu_main_menu($variables) {
$direction = $variables['direction'];
$depth = $variables['depth'];
$menu_name = variable_get('menu_main_links_source', 'main-menu');
$output = theme('jglidemenu', array('id' => 0, 'menu_name' => $menu_name, 'mlid' => 0, 'direction' => $direction, 'depth' => $depth));
return $output['content'];
}
/**
* Theme the secondary menu as a jglidemenu menu.
*
* @param $direction
* Optional. The direction the menu expands. Default is 'down'.
* @param $depth
* The number of children levels to display. Use -1 to display all children
* and use 0 to display no children.
*
* @return
* An HTML string of jglidemenu secondary menu links.
*/
function theme_jglidemenu_secondary_menu($variables) {
$direction = $variables['direction'];
$depth = $variables['depth'];
$menu_name = variable_get('menu_secondary_links_source', 'user-menu');
$output = theme('jglidemenu', array('id' => 0, 'menu_name' => $menu_name, 'mlid' => 0, 'direction' => $direction, 'depth' => $depth));
return $output['content'];
}