newest ajax-select-widget from Nathan Gray

This commit is contained in:
Ralf Becker 2007-05-25 22:13:26 +00:00
parent ff5ced8be3
commit 77a318ae69
4 changed files with 103 additions and 20 deletions

View File

@ -25,10 +25,13 @@
{ {
var $public_functions = array( var $public_functions = array(
'pre_process' => True, 'pre_process' => True,
'post_process' => True 'post_process' => True,
'ajax_search' => True,
); );
var $human_name = 'AJAX Select'; // this is the name for the editor var $human_name = 'AJAX Select'; // this is the name for the editor
private $debug = false;
function ajax_select_widget($ui='') function ajax_select_widget($ui='')
{ {
@ -59,7 +62,21 @@
*/ */
function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl) function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
{ {
//echo "<p>ajax_select_widget::pre_process('$name',$value," . print_r($cell, true) . "," . print_r($extension_data, true) . ")</p>\n"; if($this->debug) {
echo __METHOD__ . '<br />';
printf("Name:%20s<br />", $name);
echo 'Value:';
_debug_array($value);
echo 'Cell:';
_debug_array($cell);
echo 'Readonlys:';
_debug_array($readonlys);
echo 'Extension_data:';
_debug_array($extension_data);
}
// Get Options // Get Options
if(!is_array($cell['size'])) { if(!is_array($cell['size'])) {
@ -69,7 +86,9 @@
$options['id_field'], $options['id_field'],
$options['template'], $options['template'],
$options['filter'], $options['filter'],
$options['filter2'] $options['filter2'],
$options['link'],
$options['icon']
) = explode(',', $cell['size']); ) = explode(',', $cell['size']);
} else { } else {
$options = $cell['size']; $options = $cell['size'];
@ -128,6 +147,21 @@
$widget =& new etemplate('etemplate.ajax_select_widget'); $widget =& new etemplate('etemplate.ajax_select_widget');
$widget->no_onclick = True; $widget->no_onclick = True;
// Link if readonly & link is set
$search =& $widget->get_widget_by_name('search');
if(($cell['readonly'] || $readonlys['search']) && $options['link']) {
$search['type'] = 'label';
$search['no_lang'] = 1;
$search['size'] = ',' . $options['link'];
} else {
$search['type'] = 'text';
$search['size'] = '';
}
// Icon
$icon =& $widget->get_widget_by_path('/0/1A');
$icon['name'] = $options['icon'];
$cell['obj'] = &$widget; $cell['obj'] = &$widget;
// Save options for post_processing // Save options for post_processing
@ -173,27 +207,29 @@
if($count == 1) { if($count == 1) {
$value = $results[0][$extension_data['id_field']]; $value = $results[0][$extension_data['id_field']];
echo 'Match found';
return true; return true;
} else { } elseif ($count > 1) {
$GLOBALS['egw_info']['etemplate']['validation_errors'][$name] = lang("More than 1 match for '%1'",$value_in['search']); $GLOBALS['egw_info']['etemplate']['validation_errors'][$name] = lang("More than 1 match for '%1'",$value_in['search']);
$loop = true; $loop = true;
return false; return false;
} else {
$value = $value_in['search'];
return true;
} }
} }
} }
} elseif (!$value_in['value'] && !$value_in['search']) { } elseif (!$value_in['value'] && !$value_in['search']) {
$value = null; $value = null;
$loop = $extension_data['required']; $loop = $GLOBALS['egw_info']['etemplate']['loop'] || $extension_data['required'];
return !$extension_data['required']; return !$extension_data['required'];
} else { } else {
$value = $value_in['value']; $value = $value_in['value'];
$loop = false; $loop = $GLOBALS['egw_info']['etemplate']['loop'] || false;
return true; return true;
} }
} }
function change($id, $value, $set_id, $query) { function ajax_search($id, $value, $set_id, $query) {
$base_id = substr($id, 0, strrpos($id, '[')); $base_id = substr($id, 0, strrpos($id, '['));
$result_id = ($set_id ? $set_id : $base_id . '[results]'); $result_id = ($set_id ? $set_id : $base_id . '[results]');
$response = new xajaxResponse(); $response = new xajaxResponse();
@ -233,8 +269,7 @@
if(!$query['template'] || $query['template'] == 'etemplate.ajax_select_widget.row') { if(!$query['template'] || $query['template'] == 'etemplate.ajax_select_widget.row') {
$query['template'] = 'etemplate.ajax_select_widget.row'; $query['template'] = 'etemplate.ajax_select_widget.row';
} }
foreach($result_list as $key => $nul) { foreach($result_list as $key => &$row) {
$row =& $result_list[$key]; // $key => &$row is php5!
if(!is_array($row)) { if(!is_array($row)) {
continue; continue;
} }

View File

@ -44,8 +44,7 @@ function ajax_select_widget_setup(widget_id, onchange, options) {
var widget = document.getElementById(widget_id + '[search]'); var widget = document.getElementById(widget_id + '[search]');
if(widget) { if(widget) {
widget.form.disableautocomplete = true; widget.setAttribute('autocomplete', 'off');
widget.form.autocomplete = 'off';
if(widget.addEventListener) { if(widget.addEventListener) {
widget.addEventListener('keyup', change, true); widget.addEventListener('keyup', change, true);
@ -118,7 +117,7 @@ function change(e, value) {
selects[i].style.visibility = 'hidden'; selects[i].style.visibility = 'hidden';
} }
} }
xajax_doXMLHTTP("etemplate.ajax_select_widget.change", id, value, set_id, query); xajax_doXMLHTTP("etemplate.ajax_select_widget.ajax_search.etemplate", id, value, set_id, query);
} }

View File

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!-- $Id$ -->
<overlay>
<template id="etemplate.ajax_select_widget.row" template="" lang="" group="0" version="">
<grid>
<columns>
<column/>
<column/>
</columns>
<rows>
<row>
<description no_lang="1" id="id_field"/>
<description no_lang="1" id="title"/>
</row>
</rows>
</grid>
</template>
</overlay>

View File

@ -0,0 +1,31 @@
<?xml version="1.0"?>
<!-- $Id$ -->
<overlay>
<template id="etemplate.ajax_select_widget" template="" lang="" group="0" version="">
<grid>
<columns>
<column/>
<column/>
</columns>
<rows>
<row>
<textbox id="search"/>
<textbox id="value" class="select_value"/>
</row>
</rows>
</grid>
<styles>
.select_value {
display: none;
}
.resultBox {
background-color: white;
}
.resultBox div:hover {
background-color: #D3DCE3;
}
</styles>
</template>
</overlay>