N°3887 - Harmonize number of autocomplete displayed results throughout the app

This commit is contained in:
Molkobain
2021-04-06 11:56:30 +02:00
parent f6be3736fd
commit 4c289edac7
6 changed files with 62 additions and 16 deletions

View File

@@ -748,7 +748,7 @@ JS
// Current extkey value, so we can display event if it is not available anymore (eg. archived).
$iCurrentExtKeyId = (is_null($oObj) || $this->sAttCode === '') ? 0 : $oObj->Get($this->sAttCode);
$oValuesSet = new ValueSetObjects($sFilter, 'friendlyname'); // Bypass GetName() to avoid the encoding by htmlentities
$iMax = 150;
$iMax = MetaModel::GetConfig()->Get('max_autocomplete_results');
$oValuesSet->SetLimit($iMax);
$oValuesSet->SetSort(false);
$oValuesSet->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', $this->bSearchMode);
@@ -756,9 +756,8 @@ JS
$aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'start_with');
asort($aValuesContains);
$aValues = $aValuesContains;
if (sizeof($aValues) < $iMax)
{
$aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'contains');
if (sizeof($aValues) < $iMax) {
$aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'contains');
asort($aValuesContains);
$iSize = sizeof($aValuesContains);
foreach ($aValuesContains as $sKey => $sFriendlyName)

View File

@@ -6845,6 +6845,15 @@ class AttributeExternalKey extends AttributeDBFieldVoid
return $this->GetOptional('min_autocomplete_chars', MetaModel::GetConfig()->Get('min_autocomplete_chars'));
}
/**
* @return int
* @since 3.0.0
*/
public function GetMaxAutoCompleteResults(): int
{
return MetaModel::GetConfig()->Get('max_autocomplete_results');
}
public function AllowTargetCreation()
{
return $this->GetOptional('allow_target_creation', MetaModel::GetConfig()->Get('allow_target_creation'));
@@ -6889,8 +6898,7 @@ class AttributeExternalKey extends AttributeDBFieldVoid
public function MakeFormField(DBObject $oObject, $oFormField = null)
{
if ($oFormField === null)
{
if ($oFormField === null) {
// Later : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value
$sFormFieldClass = static::GetFormFieldClass();
$oFormField = new $sFormFieldClass($this->GetCode());
@@ -6899,11 +6907,11 @@ class AttributeExternalKey extends AttributeDBFieldVoid
// Setting params
$oFormField->SetMaximumComboLength($this->GetMaximumComboLength());
$oFormField->SetMinAutoCompleteChars($this->GetMinAutoCompleteChars());
$oFormField->SetMaxAutoCompleteResults($this->GetMaxAutoCompleteResults());
$oFormField->SetHierarchical(MetaModel::IsHierarchicalClass($this->GetTargetClass()));
// Setting choices regarding the field dependencies
$aFieldDependencies = $this->GetPrerequisiteAttributes();
if (!empty($aFieldDependencies))
{
if (!empty($aFieldDependencies)) {
$oTmpAttDef = $this;
$oTmpField = $oFormField;
$oFormField->SetOnFinalizeCallback(function () use ($oTmpField, $oTmpAttDef, $oObject) {

View File

@@ -640,7 +640,7 @@ class ObjectController extends BrickController
$oSet = new DBObjectSet($oSearch, array(), array('this' => $oHostObject, 'ac_query' => '%'.$sQuery.'%'));
$oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array('friendlyname')));
// Note : This limit is also used in the field renderer by typeahead to determine how many suggestions to display
$oSet->SetLimit(MetaModel::GetConfig()->Get('max_display_limit'));
$oSet->SetLimit(MetaModel::GetConfig()->Get('max_autocomplete_results'));
// - Retrieving objects
while ($oItem = $oSet->Fetch())

View File

@@ -2711,11 +2711,11 @@ EOF
$oSearch = DBObjectSearch::FromOQL("SELECT $sTargetClass WHERE friendlyname LIKE :needle");
$oSet = new DBObjectSet($oSearch, array(), array('needle' => "%$sNeedle%"));
$oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array()));
$oSet->SetLimit(5);
$oSet->SetLimit(MetaModel::GetConfig()->Get('max_autocomplete_results'));
// Note: We have to this manually because of a bug in DBSearch not checking the user prefs. by default.
$oSet->SetShowObsoleteData(utils::ShowObsoleteData());
while($oObject = $oSet->Fetch()) {
while ($oObject = $oSet->Fetch()) {
// Note $oObject finalclass might be different than $sTargetClass
$sObjectClass = get_class($oObject);
$iObjectId = $oObject->GetKey();

View File

@@ -26,6 +26,7 @@ use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator;
use DBObjectSet;
use DBSearch;
use FieldExpression;
use MetaModel;
use ScalarExpression;
/**
@@ -43,10 +44,22 @@ class SelectObjectField extends Field
/** @var \DBSearch $oSearch */
protected $oSearch;
/** @var int $iMaximumComboLength */
/**
* @see \Config 'max_combo_length'
* @var int $iMaximumComboLength
*/
protected $iMaximumComboLength;
/** @var int $iMinAutoCompleteChars */
/**
* @see \Config 'min_autocomplete_chars'
* @var int $iMinAutoCompleteChars
*/
protected $iMinAutoCompleteChars;
/**
* @see \Config 'max_autocomplete_results'
* @var int
* @since 3.0.0
*/
protected $iMaxAutoCompleteResults;
/** @var bool $bHierarchical */
protected $bHierarchical;
/** @var int $iControlType */
@@ -61,8 +74,9 @@ class SelectObjectField extends Field
{
parent::__construct($sId, $onFinalizeCallback);
$this->oSearch = null;
$this->iMaximumComboLength = null;
$this->iMinAutoCompleteChars = 3;
$this->iMaximumComboLength = MetaModel::GetConfig()->Get('max_combo_length');
$this->iMinAutoCompleteChars = MetaModel::GetConfig()->Get('min_autocomplete_chars');
$this->iMaxAutoCompleteResults = MetaModel::GetConfig()->Get('max_autocomplete_results');
$this->bHierarchical = false;
$this->iControlType = self::CONTROL_SELECT;
$this->sSearchEndpoint = null;
@@ -104,6 +118,21 @@ class SelectObjectField extends Field
return $this;
}
/**
* @see static::$iMaxAutoCompleteResults
*
* @param int $iMaxAutoCompleteResults
*
* @return $this;
* @since 3.0.0
*/
public function SetMaxAutoCompleteResults(int $iMaxAutoCompleteResults)
{
$this->iMaxAutoCompleteResults = $iMaxAutoCompleteResults;
return $this;
}
/**
* @param bool $bHierarchical
*
@@ -187,6 +216,16 @@ class SelectObjectField extends Field
return $this->iMinAutoCompleteChars;
}
/**
* @see static::$iMaxAutoCompleteResults
* @return int
* @since 3.0.0
*/
public function GetMaxAutoCompleteResults(): int
{
return $this->iMaxAutoCompleteResults;
}
/**
* @return bool
*/

View File

@@ -266,7 +266,7 @@ EOF
},{
name: '{$this->oField->GetId()}',
source: oAutocompleteSource_{$this->oField->GetId()},
limit: 20,
limit: {$this->oField->GetMaxAutoCompleteResults()},
display: 'name',
templates: {
suggestion: Handlebars.compile('<div>{{name}}</div>'),