mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-19 00:28:47 +02:00
CustomFields: implemented the autocomplete behavior for SelectObjectField
SVN:trunk[3983]
This commit is contained in:
84
application/capturewebpage.class.inc.php
Normal file
84
application/capturewebpage.class.inc.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
// Copyright (C) 2016 Combodo SARL
|
||||
//
|
||||
// This file is part of iTop.
|
||||
//
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// iTop is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with iTop. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
/**
|
||||
* Adapter class: when an API requires WebPage and you want to produce something else
|
||||
*
|
||||
* @copyright Copyright (C) 2016 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
require_once(APPROOT."/application/webpage.class.inc.php");
|
||||
|
||||
class CaptureWebPage extends WebPage
|
||||
{
|
||||
protected $aReadyScripts;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct('capture web page');
|
||||
$this->aReadyScripts = array();
|
||||
}
|
||||
|
||||
public function GetHtml()
|
||||
{
|
||||
$trash = $this->ob_get_clean_safe();
|
||||
return $this->s_content;
|
||||
}
|
||||
|
||||
public function GetJS()
|
||||
{
|
||||
$sRet = implode("\n", $this->a_scripts);
|
||||
if (!empty($this->s_deferred_content))
|
||||
{
|
||||
$sRet .= "\n\$('body').append('".addslashes(str_replace("\n", '', $this->s_deferred_content))."');";
|
||||
}
|
||||
return $sRet;
|
||||
}
|
||||
|
||||
public function GetReadyJS()
|
||||
{
|
||||
return "\$(document).ready(function() {\n".implode("\n", $this->aReadyScripts)."\n});";
|
||||
}
|
||||
|
||||
public function GetCSS()
|
||||
{
|
||||
return $this->a_styles;
|
||||
}
|
||||
|
||||
public function GetJSFiles()
|
||||
{
|
||||
return $this->a_linked_scripts;
|
||||
}
|
||||
|
||||
public function GetCSSFiles()
|
||||
{
|
||||
return $this->a_linked_stylesheets;
|
||||
}
|
||||
|
||||
public function output()
|
||||
{
|
||||
throw new Exception(__method__.' should not be called');
|
||||
}
|
||||
|
||||
public function add_ready_script($sScript)
|
||||
{
|
||||
$this->aReadyScripts[] = $sScript;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ use Combodo\iTop\Renderer\FormRenderer;
|
||||
use \Dict;
|
||||
|
||||
require_once('fieldrenderer/consolesimplefieldrenderer.class.inc.php');
|
||||
require_once('fieldrenderer/consoleselectobjectfieldrenderer.class.inc.php');
|
||||
require_once('fieldrenderer/consolesubformfieldrenderer.class.inc.php');
|
||||
|
||||
class ConsoleFormRenderer extends FormRenderer
|
||||
@@ -35,7 +36,7 @@ class ConsoleFormRenderer extends FormRenderer
|
||||
$this->AddSupportedField('HiddenField', 'ConsoleSimpleFieldRenderer');
|
||||
$this->AddSupportedField('StringField', 'ConsoleSimpleFieldRenderer');
|
||||
$this->AddSupportedField('SelectField', 'ConsoleSimpleFieldRenderer');
|
||||
$this->AddSupportedField('SelectObjectField', 'ConsoleSimpleFieldRenderer');
|
||||
$this->AddSupportedField('SelectObjectField', 'ConsoleSelectObjectFieldRenderer');
|
||||
$this->AddSupportedField('SubFormField', 'ConsoleSubFormFieldRenderer');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
// Copyright (C) 2016 Combodo SARL
|
||||
//
|
||||
// This file is part of iTop.
|
||||
//
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// iTop is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with iTop. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
namespace Combodo\iTop\Renderer\Console\FieldRenderer;
|
||||
|
||||
use Combodo\iTop\Form\Field\StringField;
|
||||
use Combodo\iTop\Form\Validator\MandatoryValidator;
|
||||
use Combodo\iTop\Form\Validator\Validator;
|
||||
use \Dict;
|
||||
use \DBObjectSet;
|
||||
use Combodo\iTop\Renderer\FieldRenderer;
|
||||
use Combodo\iTop\Renderer\RenderingOutput;
|
||||
|
||||
class ConsoleSelectObjectFieldRenderer extends FieldRenderer
|
||||
{
|
||||
public function Render()
|
||||
{
|
||||
$oOutput = new RenderingOutput();
|
||||
|
||||
$oOutput->AddHtml('<table class="form-field-container">');
|
||||
$oOutput->AddHtml('<tr>');
|
||||
if ($this->oField->GetLabel() != '')
|
||||
{
|
||||
$oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
|
||||
}
|
||||
$oOutput->AddHtml('<td class="form-field-content">');
|
||||
$sEditType = 'none';
|
||||
if ($this->oField->GetReadOnly())
|
||||
{
|
||||
$oSearch = $this->oField->GetSearch()->DeepClone();
|
||||
$oSearch->AddCondition('id', $this->oField->GetCurrentValue());
|
||||
$oSet = new DBObjectSet($oSearch);
|
||||
$oObject = $oSet->Fetch();
|
||||
if ($oObject)
|
||||
{
|
||||
$sCurrentLabel = $oObject->Get('friendlyname');
|
||||
}
|
||||
else
|
||||
{
|
||||
$sCurrentLabel = '';
|
||||
}
|
||||
$oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
|
||||
$oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
|
||||
}
|
||||
else
|
||||
{
|
||||
$oSearch = $this->oField->GetSearch()->DeepClone();
|
||||
$oSearch->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true);
|
||||
|
||||
$oSet = new \DBObjectSet($oSearch);
|
||||
$oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array('friendlyname')));
|
||||
|
||||
$sTargetClass = $oSearch->GetClass();
|
||||
$oAllowedValues = new DBObjectSet($oSearch);
|
||||
|
||||
$iMaxComboLength = $this->oField->GetMaximumComboLength();
|
||||
$iCount = $oAllowedValues->Count();
|
||||
if ($iCount > $iMaxComboLength)
|
||||
{
|
||||
// Auto-complete
|
||||
$sEditType = 'autocomplete';
|
||||
$aExtKeyParams = array();
|
||||
$aExtKeyParams['iFieldSize'] = 10;
|
||||
$aExtKeyParams['iMinChars'] = $this->oField->GetMinAutoCompleteChars();
|
||||
$sFieldName = $this->oField->GetGlobalId();
|
||||
$sFieldId = $sFieldName;
|
||||
$sFormPrefix = '';
|
||||
$oWidget = new \UIExtKeyWidget($sTargetClass, $sFieldId, '', true);
|
||||
$aArgs = array();
|
||||
$sDisplayStyle = 'select';
|
||||
$sTitle = $this->oField->GetLabel();
|
||||
require_once(APPROOT.'application/capturewebpage.class.inc.php');
|
||||
$oPage = new \CaptureWebPage();
|
||||
$sHTMLValue = $oWidget->Display($oPage, $iMaxComboLength, false /* $bAllowTargetCreation */, $sTitle, $oSet, $this->oField->GetCurrentValue(), $sFieldId, $this->oField->GetMandatory(), $sFieldName, $sFormPrefix, $aArgs, null, $sDisplayStyle);
|
||||
$oOutput->AddHtml($sHTMLValue);
|
||||
$oOutput->AddHtml($oPage->GetHtml());
|
||||
$oOutput->AddJs($oPage->GetJS());
|
||||
$oOutput->AddJs($oPage->GetReadyJS());
|
||||
$oOutput->AddCss($oPage->GetCSS());
|
||||
foreach ($oPage->GetJSFiles() as $sFile)
|
||||
{
|
||||
$oOutput->AddJsFile($sFile);
|
||||
}
|
||||
foreach ($oPage->GetCSSFiles() as $sFile)
|
||||
{
|
||||
$oOutput->AddCssFile($sFile);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Drop-down select
|
||||
$sEditType = 'select';
|
||||
$oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'">');
|
||||
$oOutput->AddHtml('<option value="">'.Dict::S('UI:SelectOne').'</option>');
|
||||
while ($oObject = $oSet->Fetch())
|
||||
{
|
||||
$iObject = $oObject->GetKey();
|
||||
$sLabel = $oObject->Get('friendlyname');
|
||||
// Note : The test is a double equal on purpose as the type of the value received from the XHR is not always the same as the type of the allowed values. (eg : string vs int)
|
||||
$sSelectedAtt = ($this->oField->GetCurrentValue() == $iObject) ? 'selected' : '';
|
||||
$oOutput->AddHtml('<option value="'.$iObject.'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
|
||||
}
|
||||
$oOutput->AddHtml('</select>');
|
||||
}
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
|
||||
var me = this;
|
||||
|
||||
$(this).closest(".field_set").trigger("field_change", {
|
||||
id: $(me).attr("id"),
|
||||
name: $(me).closest(".form_field").attr("data-field-id"),
|
||||
value: $(me).val()
|
||||
})
|
||||
.closest('.form_handler').trigger('value_change');
|
||||
});
|
||||
EOF
|
||||
);
|
||||
}
|
||||
$oOutput->AddHtml('<span class="form_validation"></span>');
|
||||
$oOutput->AddHtml('</td>');
|
||||
|
||||
$oOutput->AddHtml('</tr>');
|
||||
$oOutput->AddHtml('</table>');
|
||||
|
||||
// JS Form field widget construct
|
||||
$aValidators = array();
|
||||
foreach ($this->oField->GetValidators() as $oValidator)
|
||||
{
|
||||
if ($oValidator::GetName() == 'notemptyextkey')
|
||||
{
|
||||
// The autocomplete widget returns an empty string if the value is undefined (and the select has been aligned with this behavior)
|
||||
$oValidator = new MandatoryValidator();
|
||||
}
|
||||
$aValidators[$oValidator::GetName()] = array(
|
||||
'reg_exp' => $oValidator->GetRegExp(),
|
||||
'message' => Dict::S($oValidator->GetErrorMessage())
|
||||
);
|
||||
}
|
||||
$sValidators = json_encode($aValidators);
|
||||
$sFormFieldOptions =
|
||||
<<<EOF
|
||||
{
|
||||
validators: $sValidators,
|
||||
on_validation_callback: function(me, oResult) {
|
||||
var oValidationElement = $(me.element).find('span.form_validation');
|
||||
if (oResult.is_valid)
|
||||
{
|
||||
oValidationElement.html('');
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: escape html entities
|
||||
var sExplain = oResult.error_messages.join(', ');
|
||||
oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
|
||||
oValidationElement.tooltip({
|
||||
items: 'span',
|
||||
tooltipClass: 'form_field_error',
|
||||
content: function() {
|
||||
return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF
|
||||
;
|
||||
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
|
||||
EOF
|
||||
);
|
||||
switch ($sEditType)
|
||||
{
|
||||
case 'autocomplete':
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field('option', 'get_current_value_callback', function(me){ return $(me.element).find('#{$this->oField->GetGlobalId()}').val();});
|
||||
EOF
|
||||
);
|
||||
break;
|
||||
case 'select':
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field('option', 'get_current_value_callback', function(me){ return $(me.element).find('select').val();});
|
||||
EOF
|
||||
);
|
||||
break;
|
||||
|
||||
case 'none':
|
||||
default:
|
||||
// Not editable
|
||||
}
|
||||
|
||||
return $oOutput;
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
namespace Combodo\iTop\Renderer\Console\FieldRenderer;
|
||||
|
||||
use Combodo\iTop\Form\Field\StringField;
|
||||
use \Dict;
|
||||
use Combodo\iTop\Renderer\FieldRenderer;
|
||||
use Combodo\iTop\Renderer\RenderingOutput;
|
||||
@@ -82,46 +81,6 @@ class ConsoleSimpleFieldRenderer extends FieldRenderer
|
||||
$oOutput->AddHtml('<span class="form_validation"></span>');
|
||||
$oOutput->AddHtml('</td>');
|
||||
break;
|
||||
|
||||
case 'Combodo\\iTop\\Form\\Field\\SelectObjectField':
|
||||
$oOutput->AddHtml('<td class="form-field-content">');
|
||||
if ($this->oField->GetReadOnly())
|
||||
{
|
||||
$oSearch = $this->oField->GetSearch()->DeepClone();
|
||||
$oSearch->AddCondition('id', $this->oField->GetCurrentValue());
|
||||
$oSet = new DBObjectSet($oSearch);
|
||||
$oObject = $oSet->Fetch();
|
||||
if ($oObject)
|
||||
{
|
||||
$sCurrentLabel = $oObject->Get('friendlyname');
|
||||
}
|
||||
else
|
||||
{
|
||||
$sCurrentLabel = '';
|
||||
}
|
||||
$oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
|
||||
$oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
|
||||
}
|
||||
else
|
||||
{
|
||||
$oSearch = $this->oField->GetSearch()->DeepClone();
|
||||
$oSet = new \DBObjectSet($oSearch);
|
||||
$oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array('friendlyname')));
|
||||
$oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'">');
|
||||
$oOutput->AddHtml('<option value="0">'.Dict::S('UI:SelectOne').'</option>');
|
||||
while ($oObject = $oSet->Fetch())
|
||||
{
|
||||
$iObject = $oObject->GetKey();
|
||||
$sLabel = $oObject->Get('friendlyname');
|
||||
// Note : The test is a double equal on purpose as the type of the value received from the XHR is not always the same as the type of the allowed values. (eg : string vs int)
|
||||
$sSelectedAtt = ($this->oField->GetCurrentValue() == $iObject) ? 'selected' : '';
|
||||
$oOutput->AddHtml('<option value="'.$iObject.'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
|
||||
}
|
||||
$oOutput->AddHtml('</select>');
|
||||
}
|
||||
$oOutput->AddHtml('<span class="form_validation"></span>');
|
||||
$oOutput->AddHtml('</td>');
|
||||
break;
|
||||
}
|
||||
$oOutput->AddHtml('</tr>');
|
||||
$oOutput->AddHtml('</table>');
|
||||
@@ -146,7 +105,6 @@ EOF
|
||||
);
|
||||
break;
|
||||
case 'Combodo\\iTop\\Form\\Field\\SelectField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\SelectObjectField':
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
|
||||
@@ -210,21 +168,12 @@ EOF
|
||||
switch ($sFieldClass)
|
||||
{
|
||||
case 'Combodo\\iTop\\Form\\Field\\SelectField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\SelectObjectField':
|
||||
$oOutput->AddJs(
|
||||
<<<EOF
|
||||
$("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field('option', 'get_current_value_callback', function(me){ return $(me.element).find('select').val();});
|
||||
EOF
|
||||
);
|
||||
break;
|
||||
|
||||
case 'Combodo\\iTop\\Form\\Field\\HiddenField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\StringField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\HiddenField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\RadioField':
|
||||
case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
|
||||
break;
|
||||
}
|
||||
|
||||
return $oOutput;
|
||||
|
||||
Reference in New Issue
Block a user