danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * Provide a javascript based dropbutton menu.
|
danielebarchiesi@0
|
6 *
|
danielebarchiesi@0
|
7 * An example are the edit/disable/delete links on the views listing page.
|
danielebarchiesi@0
|
8 *
|
danielebarchiesi@0
|
9 * The dropbutton menu will show up as a button with a clickable twisty pointer
|
danielebarchiesi@0
|
10 * to the right. When clicked the button will expand, showing the list of links.
|
danielebarchiesi@0
|
11 *
|
danielebarchiesi@0
|
12 * The dropbutton will stay open until either the user has moved the mouse
|
danielebarchiesi@0
|
13 * away from the box for > .5 seconds, or can be immediately closed by
|
danielebarchiesi@0
|
14 * clicking the twisty again. The code is smart enough that if the mouse
|
danielebarchiesi@0
|
15 * moves away and then back within the .5 second window, it will not
|
danielebarchiesi@0
|
16 * re-close.
|
danielebarchiesi@0
|
17 *
|
danielebarchiesi@0
|
18 * Multiple dropbuttons can be placed per page.
|
danielebarchiesi@0
|
19 *
|
danielebarchiesi@0
|
20 * If only one link is passed to the theme function, the function will render
|
danielebarchiesi@0
|
21 * a ctools-button with no twisty. The twisty is only rendered when 2 or more
|
danielebarchiesi@0
|
22 * links are provided. The wrapping element will be classed with both
|
danielebarchiesi@0
|
23 * ctools-button and ctools-dropbutton when a dropbutton is rendered.
|
danielebarchiesi@0
|
24 *
|
danielebarchiesi@0
|
25 * If the user does not have javascript enabled, the link will not appear,
|
danielebarchiesi@0
|
26 * and instead by default the list of links will appear as a normal inline
|
danielebarchiesi@0
|
27 * list.
|
danielebarchiesi@0
|
28 *
|
danielebarchiesi@0
|
29 * The menu is minimally styled by default, and to make it look different
|
danielebarchiesi@0
|
30 * will require your own CSS. You can apply your own class to the
|
danielebarchiesi@0
|
31 * dropbutton to help style it.
|
danielebarchiesi@0
|
32 *
|
danielebarchiesi@0
|
33 * The twisty is rendered as a border on a widthless and heightless element.
|
danielebarchiesi@0
|
34 * There is no image for the twisty.
|
danielebarchiesi@0
|
35 * The color of the twisty is the color of the link by default. To adjust the
|
danielebarchiesi@0
|
36 * size of the twisty, adjust the border widths on .ctools-twisty. The adjust
|
danielebarchiesi@0
|
37 * the color of the twisty, assign a new color to the .ctools-button class or
|
danielebarchiesi@0
|
38 * assign a color to .ctools-twisty. You shouldn't need to adjust border-color.
|
danielebarchiesi@0
|
39 *
|
danielebarchiesi@0
|
40 * Use the theme() function to render dropbutton e.g.
|
danielebarchiesi@0
|
41 * theme('links__ctools_dropbutton', array()) where array contains a renderable
|
danielebarchiesi@0
|
42 * array of links.
|
danielebarchiesi@0
|
43 */
|
danielebarchiesi@0
|
44
|
danielebarchiesi@0
|
45 /**
|
danielebarchiesi@0
|
46 * Delegated implementation of hook_theme()
|
danielebarchiesi@0
|
47 */
|
danielebarchiesi@0
|
48 function ctools_dropbutton_theme(&$items) {
|
danielebarchiesi@0
|
49 $items['links__ctools_dropbutton'] = array(
|
danielebarchiesi@0
|
50 'variables' => array('title' => NULL, 'links' => NULL, 'image' => FALSE, 'class' => NULL),
|
danielebarchiesi@0
|
51 'file' => 'includes/dropbutton.theme.inc',
|
danielebarchiesi@0
|
52 );
|
danielebarchiesi@0
|
53 }
|
danielebarchiesi@0
|
54
|
danielebarchiesi@0
|
55 /**
|
danielebarchiesi@0
|
56 * Create a dropbutton menu.
|
danielebarchiesi@0
|
57 *
|
danielebarchiesi@0
|
58 * @param $title
|
danielebarchiesi@0
|
59 * The text to place in the clickable area to activate the dropbutton. This
|
danielebarchiesi@0
|
60 * text is indented to -9999px by default.
|
danielebarchiesi@0
|
61 * @param $links
|
danielebarchiesi@0
|
62 * A list of links to provide within the dropbutton, suitable for use
|
danielebarchiesi@0
|
63 * in via Drupal's theme('links').
|
danielebarchiesi@0
|
64 * @param $image
|
danielebarchiesi@0
|
65 * If true, the dropbutton link is an image and will not get extra decorations
|
danielebarchiesi@0
|
66 * that a text dropbutton link will.
|
danielebarchiesi@0
|
67 * @param $class
|
danielebarchiesi@0
|
68 * An optional class to add to the dropbutton's container div to allow you
|
danielebarchiesi@0
|
69 * to style a single dropbutton however you like without interfering with
|
danielebarchiesi@0
|
70 * other dropbuttons.
|
danielebarchiesi@0
|
71 */
|
danielebarchiesi@0
|
72 function theme_links__ctools_dropbutton($vars) {
|
danielebarchiesi@0
|
73 // Check to see if the number of links is greater than 1;
|
danielebarchiesi@0
|
74 // otherwise just treat this like a button.
|
danielebarchiesi@0
|
75 if (!empty($vars['links'])) {
|
danielebarchiesi@0
|
76 $is_drop_button = (count($vars['links']) > 1);
|
danielebarchiesi@0
|
77
|
danielebarchiesi@0
|
78 // Add needed files
|
danielebarchiesi@0
|
79 if ($is_drop_button) {
|
danielebarchiesi@0
|
80 ctools_add_js('dropbutton');
|
danielebarchiesi@0
|
81 ctools_add_css('dropbutton');
|
danielebarchiesi@0
|
82 }
|
danielebarchiesi@0
|
83 ctools_add_css('button');
|
danielebarchiesi@0
|
84
|
danielebarchiesi@0
|
85 // Provide a unique identifier for every button on the page.
|
danielebarchiesi@0
|
86 static $id = 0;
|
danielebarchiesi@0
|
87 $id++;
|
danielebarchiesi@0
|
88
|
danielebarchiesi@0
|
89 // Wrapping div
|
danielebarchiesi@0
|
90 $class = 'ctools-no-js';
|
danielebarchiesi@0
|
91 $class .= ($is_drop_button) ? ' ctools-dropbutton' : '';
|
danielebarchiesi@0
|
92 $class .= ' ctools-button';
|
danielebarchiesi@0
|
93 if (!empty($vars['class'])) {
|
danielebarchiesi@0
|
94 $class .= ($vars['class']) ? (' ' . implode(' ', $vars['class'])) : '';
|
danielebarchiesi@0
|
95 }
|
danielebarchiesi@0
|
96
|
danielebarchiesi@0
|
97 $output = '';
|
danielebarchiesi@0
|
98
|
danielebarchiesi@0
|
99 $output .= '<div class="' . $class . '" id="ctools-button-' . $id . '">';
|
danielebarchiesi@0
|
100
|
danielebarchiesi@0
|
101 // Add a twisty if this is a dropbutton
|
danielebarchiesi@0
|
102 if ($is_drop_button) {
|
danielebarchiesi@0
|
103 $vars['title'] = ($vars['title'] ? check_plain($vars['title']) : t('open'));
|
danielebarchiesi@0
|
104
|
danielebarchiesi@0
|
105 $output .= '<div class="ctools-link">';
|
danielebarchiesi@0
|
106 if ($vars['image']) {
|
danielebarchiesi@0
|
107 $output .= '<a href="#" class="ctools-twisty ctools-image">' . $vars['title'] . '</a>';
|
danielebarchiesi@0
|
108 }
|
danielebarchiesi@0
|
109 else {
|
danielebarchiesi@0
|
110 $output .= '<a href="#" class="ctools-twisty ctools-text">' . $vars['title'] . '</a>';
|
danielebarchiesi@0
|
111 }
|
danielebarchiesi@0
|
112 $output .= '</div>'; // ctools-link
|
danielebarchiesi@0
|
113 }
|
danielebarchiesi@0
|
114
|
danielebarchiesi@0
|
115 // The button content
|
danielebarchiesi@0
|
116 $output .= '<div class="ctools-content">';
|
danielebarchiesi@0
|
117
|
danielebarchiesi@0
|
118 // Check for attributes. theme_links expects an array().
|
danielebarchiesi@0
|
119 $vars['attributes'] = (!empty($vars['attributes'])) ? $vars['attributes'] : array();
|
danielebarchiesi@0
|
120
|
danielebarchiesi@0
|
121 // Remove the inline and links classes from links if they exist.
|
danielebarchiesi@0
|
122 // These classes are added and styled by Drupal core and mess up the default
|
danielebarchiesi@0
|
123 // styling of any link list.
|
danielebarchiesi@0
|
124 if (!empty($vars['attributes']['class'])) {
|
danielebarchiesi@0
|
125 $classes = $vars['attributes']['class'];
|
danielebarchiesi@0
|
126 foreach ($classes as $key => $class) {
|
danielebarchiesi@0
|
127 if ($class === 'inline' || $class === 'links') {
|
danielebarchiesi@0
|
128 unset($vars['attributes']['class'][$key]);
|
danielebarchiesi@0
|
129 }
|
danielebarchiesi@0
|
130 }
|
danielebarchiesi@0
|
131 }
|
danielebarchiesi@0
|
132
|
danielebarchiesi@0
|
133 // Call theme_links to render the list of links.
|
danielebarchiesi@0
|
134 $output .= theme_links(array('links' => $vars['links'], 'attributes' => $vars['attributes'], 'heading' => ''));
|
danielebarchiesi@0
|
135 $output .= '</div>'; // ctools-content
|
danielebarchiesi@0
|
136 $output .= '</div>'; // ctools-dropbutton
|
danielebarchiesi@0
|
137 return $output;
|
danielebarchiesi@0
|
138 }
|
danielebarchiesi@0
|
139 else {
|
danielebarchiesi@0
|
140 return '';
|
danielebarchiesi@0
|
141 }
|
danielebarchiesi@0
|
142 }
|
danielebarchiesi@0
|
143
|