N°848 Portal: Option to display LinkedSet as opened in a form

SVN:trunk[4691]
This commit is contained in:
Guillaume Lajarige
2017-04-26 09:10:24 +00:00
parent ec6a8537e9
commit 9d242e1623
3 changed files with 68 additions and 15 deletions

View File

@@ -286,9 +286,7 @@ class ObjectFormManager extends FormManager
$sObjectClass = get_class($this->oObject);
$aFieldsAtts = array();
$aMandatoryAtts = array();
$aReadonlyAtts = array();
$aHiddenAtts = array();
$aFieldsExtraData = array();
if ($this->oForm !== null)
{
@@ -387,6 +385,12 @@ class ObjectFormManager extends FormManager
$oFieldNode->setAttribute('data-form-path', $oForm->GetId());
}
// Checking if field should be displayed opened (For linked set)
if($oFieldNode->hasAttribute('data-field-opened') && ($oFieldNode->getAttribute('data-field-opened') === 'true') )
{
$aFieldsExtraData[$sFieldId]['opened'] = true;
}
// Settings field flags from the data-field-flags attribute
foreach (explode(' ', $sFieldFlags) as $sFieldFlag)
{
@@ -606,9 +610,9 @@ class ObjectFormManager extends FormManager
// Specific operation on field
// - LinkedSet
// - Overriding attributes to display
if (in_array(get_class($oField), array('Combodo\\iTop\\Form\\Field\\LinkedSetField')))
{
// - Overriding attributes to display
if ($this->oApp !== null)
{
// Note : This snippet is inspired from AttributeLinkedSet::MakeFormField()
@@ -628,6 +632,11 @@ class ObjectFormManager extends FormManager
}
$oField->SetAttributesToDisplay($aAttributesToDisplay);
}
// - Displaying as opened
if(array_key_exists($sAttCode, $aFieldsExtraData) && array_key_exists('opened', $aFieldsExtraData[$sAttCode]))
{
$oField->SetDisplayOpened(true);
}
}
$oForm->AddField($oField);

View File

@@ -28,9 +28,13 @@ use \Combodo\iTop\Form\Field\Field;
*/
class LinkedSetField extends Field
{
const DEFAULT_INDIRECT = false;
const DEFAULT_DISPLAY_OPENED = false;
protected $sTargetClass;
protected $sExtKeyToRemote;
protected $bIndirect;
protected $bDisplayOpened;
protected $aAttributesToDisplay;
protected $sSearchEndpoint;
protected $sInformationEndpoint;
@@ -39,7 +43,8 @@ class LinkedSetField extends Field
{
$this->sTargetClass = null;
$this->sExtKeyToRemote = null;
$this->bIndirect = false;
$this->bIndirect = static::DEFAULT_INDIRECT;
$this->bDisplayOpened = static::DEFAULT_DISPLAY_OPENED;
$this->aAttributesToDisplay = array();
$this->sSearchEndpoint = null;
$this->sInformationEndpoint = null;
@@ -107,6 +112,28 @@ class LinkedSetField extends Field
return $this;
}
/**
* Returns if the field should be displayed opened on initialization
*
* @return boolean
*/
public function GetDisplayOpened()
{
return $this->bDisplayOpened;
}
/**
* Sets if the field should be displayed opened on initialization
*
* @param $bDisplayOpened
* @return \Combodo\iTop\Form\Field\LinkedSetField
*/
public function SetDisplayOpened($bDisplayOpened)
{
$this->bDisplayOpened = $bDisplayOpened;
return $this;
}
/**
* Returns a hash array of attributes to be displayed in the linkedset in the form $sAttCode => $sAttLabel
*

View File

@@ -46,7 +46,7 @@ class BsLinkedSetFieldRenderer extends FieldRenderer
*/
public function Render()
{
$oOutput = new RenderingOutput();
$oOutput = new RenderingOutput();
$sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
// Vars to build the table
$sAttributesToDisplayAsJson = json_encode($this->oField->GetAttributesToDisplay());
@@ -61,18 +61,35 @@ class BsLinkedSetFieldRenderer extends FieldRenderer
{
// Rendering field
$sIsEditable = ($this->oField->GetReadOnly()) ? 'false' : 'true';
$sCollapseTogglerVisibleClass = 'glyphicon-menu-down';
$sCollapseTogglerHiddenClass = 'glyphicon-menu-down collapsed';
$sCollapseTogglerId = 'form_linkedset_toggler_' . $this->oField->GetGlobalId();
$sCollapseTogglerIconVisibleClass = 'glyphicon-menu-down';
$sCollapseTogglerIconHiddenClass = 'glyphicon-menu-down collapsed';
$sCollapseTogglerClass = 'form_linkedset_toggler';
$sCollapseTogglerId = $sCollapseTogglerClass . '_' . $this->oField->GetGlobalId();
$sFieldWrapperId = 'form_linkedset_wrapper_' . $this->oField->GetGlobalId();
// Preparing collapsed state
if($this->oField->GetDisplayOpened())
{
$sCollapseTogglerExpanded = 'true';
$sCollapseTogglerIconClass = $sCollapseTogglerIconVisibleClass;
$sCollapseJSInitState = 'true';
}
else
{
$sCollapseTogglerClass .= ' collapsed';
$sCollapseTogglerExpanded = 'false';
$sCollapseTogglerIconClass = $sCollapseTogglerIconHiddenClass;
$sCollapseJSInitState = 'false';
}
$oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
if ($this->oField->GetLabel() !== '')
{
$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')
->AddHtml('<a id="' . $sCollapseTogglerId . '" class="form_linkedset_toggler" data-toggle="collapse" href="#' . $sFieldWrapperId . '" aria-expanded="false" aria-controls="' . $sFieldWrapperId . '">')
->AddHtml('<a id="' . $sCollapseTogglerId . '" class="' . $sCollapseTogglerClass . '" data-toggle="collapse" href="#' . $sFieldWrapperId . '" aria-expanded="' . $sCollapseTogglerExpanded . '" aria-controls="' . $sFieldWrapperId . '">')
->AddHtml($this->oField->GetLabel(), true)
->AddHtml('<span class="text">' . count($aItemIds) . '</span>')
->AddHtml('<span class="glyphicon ' . $sCollapseTogglerHiddenClass . '"></>')
->AddHtml('<span class="glyphicon ' . $sCollapseTogglerIconClass . '"></>')
->AddHtml('</a>')
->AddHtml('</label>');
}
@@ -109,8 +126,8 @@ EOF
<<<EOF
// Collapse handlers
// - Collapsing by default to optimize form space
// It would be better to be able to construct the widget as collapsed, but in this ase, datatables thinks the container is very small and therefore renders the table as if it was in microbox.
$('#{$sFieldWrapperId}').collapse({toggle: false});
// It would be better to be able to construct the widget as collapsed, but in this case, datatables thinks the container is very small and therefore renders the table as if it was in microbox.
$('#{$sFieldWrapperId}').collapse({toggle: {$sCollapseJSInitState}});
// - Change toggle icon class
$('#{$sFieldWrapperId}').on('shown.bs.collapse', function(){
// Creating the table if null (first expand). If we create it on start, it will be displayed as if it was in a micro screen due to the div being "display: none;"
@@ -120,10 +137,10 @@ EOF
}
})
.on('show.bs.collapse', function(){
$('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerHiddenClass}').addClass('{$sCollapseTogglerVisibleClass}');
$('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerIconHiddenClass}').addClass('{$sCollapseTogglerIconVisibleClass}');
})
.on('hide.bs.collapse', function(){
$('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerVisibleClass}').addClass('{$sCollapseTogglerHiddenClass}');
$('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerIconVisibleClass}').addClass('{$sCollapseTogglerIconHiddenClass}');
});
// Places a loader in the empty datatables