This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handsontable.inc
192 lines (167 loc) · 5.86 KB
/
Handsontable.inc
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
<?php
/**
* @file
* Defines callbacks and resources needed for the 'handsontable' form element.
*/
class Handsontable {
/**
* Loads the required resources for displaying the Handsontable element.
* @param $form_state
*/
public static function addRequiredResources(&$form_state) {
drupal_add_css(drupal_get_path('module', 'handsontable') . '/node_modules/@bower_components/handsontable/dist/handsontable.full.min.css', array(
'group' => CSS_DEFAULT,
'every_page' => TRUE
));
drupal_add_js(drupal_get_path('module', 'handsontable') . '/node_modules/@bower_components/handsontable/dist/handsontable.full.min.js', array(
'group' => CSS_DEFAULT,
'every_page' => TRUE
));
}
/**
* The '#process' callback for the handsontable_row form element.
*
* @param array $element
* The handsontable_row form element.
* @param array $form_state
* The Drupal form state.
* @param array $form
* The complete Drupal form definition.
*
* @return array
* The handsontable_row form element.
*/
public static function process(array $element, array &$form_state, array $form = NULL) {
self::addRequiredResources($form_state);
$children = array_intersect_key($element, array_flip(element_children($element)));
$handsontable_rows = array_filter($children, ['self', 'FilterChildren']);
$handsontable_row = reset($handsontable_rows);
if ( count($handsontable_rows) === 0 ) {
drupal_set_message(t('Attempted to create handsontable %name without defining a handsontable_row.', array('%name' => $element['#name'])), 'error');
return $element;
}
// Config JS
$element['#attached']['js'] = [
drupal_get_path('module', 'handsontable') . '/js/handsontable_field.config.js'
];
// Determine already set values
$values = [];
$has_data = false;
foreach (element_children($element) as $key) {
$values[$key] = [];
foreach (element_children($element[$key]) as $subKey) {
if (isset($element[$key][$subKey]['#default_value'])) {
if ( !empty($element[$key][$subKey]['#default_value']) ) {
$has_data = true;
}
// TODO: We probably have to handle order of columns here
$values[$key][] = $element[$key][$subKey]['#default_value'];
}
else {
$values[$key][] = NULL;
}
}
}
// TODO: I don't like this $has_data check, can we do this more pretty.
// It is required to let handsontable know we have an empty table, and to make the number
// of rows work in an empty table
if ( !$has_data ) {
$values = null;
}
// Discover columns, they are the childs of the first handsontable_row element
$children = array_intersect_key($handsontable_row, array_flip(element_children($handsontable_row)));
$colHeaders = [];
$columns = [];
foreach ($children as $name => $child) {
$colHeaders[] = isset($child['#title']) ? $child['#title'] : 'no-title';
$columns[] = [
'name' => $name,
'type' => isset($child['#autocomplete_path']) ? 'autocomplete' : 'text',
'path' => isset($child['#autocomplete_path']) ? $child['#autocomplete_path'] : null
];
}
// Pass settings
$element['#attached']['js'][] = [
'data' => [
'handsontable' => [
'ids' => [$element['#id']],
'names' => [$element['#name']],
'data' => [$values],
'colHeaders' => [$colHeaders],
'columns' => [$columns],
'startRows' => [isset($element['#rows']) && is_numeric($element['#rows']) ? $element['#rows'] : 0]
]
],
'type' => 'setting',
];
// Add AJAX callback
$element['addRow'] = self::createChangeCallback($element, $form);
// This hidden input will contain the number of handsontable rows
// This is used in the AJAX callback
$element['rows'] = [
'#type' => 'hidden',
'#prefix' => '<div id="' . $element['#id'] . '-rows">',
'#suffix' => '</div>'
];
return $element;
}
/**
* Creates an invisible AJAX callback button that allows change number of handsontable rows.
*
* @param array $element
* The handsontable form element.
* @param array $complete_form
* The completed form.
*
* @return \FormElement The "add" button.
*
*/
protected static function createChangeCallback(array &$element, array &$complete_form) {
$add['#type'] = 'button';
$add['#id'] = $element['#id'] . '-callback';
// TODO: This doesn't look entirely correct to build the "edit-" yourself.
// It seemed to be required though to have the right triggering element in
// the callback
$add['#name'] = 'edit-' . $element['#name'] . '-callback';
$add['#attributes'] = array('style' => 'display:none');
$add['#ajax'] = array(
'params' => array(
'target' => $element['#hash']
),
'callback' => 'xml_form_elements_ajax_callback',
'method' => 'settings'
);
return $add;
}
/**
* Checks if a child element has the 'handsontable_row' #type.
*
* @param array $child
* The child to determine.
*
* @return bool
* TRUE if it is a tabpanel, FALSE otherwise.
*/
public static function FilterChildren(array $child) {
$ret = ($child['#type'] == 'handsontable_row') ? TRUE : FALSE;
return $ret;
}
}
/**
* Implements theme_hook().
*
* @param array $variables
* An array of theming variables.
* @return string
*/
function theme_handsontable($variables) {
$element = $variables['element'];
unset($element['#prefix']);
unset($element['#suffix']);
$output = '<div class="handsontable-container">';
$output .= '<div id="' . $element['#id'] . '"></div>';
$output .= '<div id="' . $element['#id'] . '-table" class="handsontable"></div>';
$output .= $element['#children'];
$output .= '</div>';
return $output;
}