Refactor form widgets to use new fillFromConfig() helper

This commit is contained in:
Samuel Georges 2015-02-28 14:43:34 +11:00
parent 49f6e64db0
commit f8ae611ec9
7 changed files with 212 additions and 114 deletions

View File

@ -12,10 +12,9 @@ use Backend\Classes\FormWidgetBase;
*/
class CodeEditor extends FormWidgetBase
{
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'codeeditor';
//
// Configurable properties
//
/**
* @var string Code language to display (php, twig)
@ -61,33 +60,41 @@ class CodeEditor extends FormWidgetBase
/**
* @var integer Sets the editor margin size.
*/
public $margin = 10;
public $margin = 0;
/**
* @var $theme Ace Editor theme to use.
*/
public $theme = 'twilight';
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'codeeditor';
/**
* {@inheritDoc}
*/
public function init()
{
// Load the editor system settings
$editorSettings = EditorPreferences::instance();
$this->applyEditorPreferences();
$this->fontSize = $this->getConfig('fontSize', $editorSettings->font_size);
$this->wordWrap = $this->getConfig('wordWrap', $editorSettings->word_wrap);
$this->codeFolding = $this->getConfig('codeFolding', $editorSettings->code_folding);
$this->autoClosing = $this->getConfig('autoClosing', $editorSettings->auto_closing);
$this->tabSize = $this->getConfig('tabSize', $editorSettings->tab_size);
$this->theme = $this->getConfig('theme', $editorSettings->theme);
$this->showInvisibles = $this->getConfig('showInvisibles', $editorSettings->show_invisibles);
$this->highlightActiveLine = $this->getConfig('highlightActiveLine', $editorSettings->highlight_active_line);
$this->useSoftTabs = $this->getConfig('useSoftTabs', !$editorSettings->use_hard_tabs);
$this->showGutter = $this->getConfig('showGutter', $editorSettings->show_gutter);
$this->language = $this->getConfig('language', 'php');
$this->margin = $this->getConfig('margin', 0);
$this->fillFromConfig([
'language',
'showGutter',
'wordWrap',
'codeFolding',
'autoClosing',
'useSoftTabs',
'tabSize',
'fontSize',
'margin',
'theme',
]);
}
/**
@ -135,4 +142,26 @@ class CodeEditor extends FormWidgetBase
$this->addJs('vendor/ace/ext-emmet.js', 'core');
$this->addJs('js/codeeditor.js', 'core');
}
/**
* Looks at the user preferences and overrides any set values.
* @return void
*/
protected function applyEditorPreferences()
{
// Load the editor system settings
$editorSettings = EditorPreferences::instance();
$this->fontSize = $editorSettings->font_size;
$this->wordWrap = $editorSettings->word_wrap;
$this->codeFolding = $editorSettings->code_folding;
$this->autoClosing = $editorSettings->auto_closing;
$this->tabSize = $editorSettings->tab_size;
$this->theme = $editorSettings->theme;
$this->showInvisibles = $editorSettings->show_invisibles;
$this->highlightActiveLine = $editorSettings->highlight_active_line;
$this->useSoftTabs = !$editorSettings->use_hard_tabs;
$this->showGutter = $editorSettings->show_gutter;
}
}

View File

@ -11,10 +11,9 @@ use Backend\Classes\FormWidgetBase;
*/
class ColorPicker extends FormWidgetBase
{
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'colorpicker';
//
// Configurable properties
//
/**
* @var array Default available colors
@ -32,12 +31,23 @@ class ColorPicker extends FormWidgetBase
'#95a5a6', '#7f8c8d',
];
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'colorpicker';
/**
* {@inheritDoc}
*/
public function init()
{
$this->availableColors = $this->getConfig('availableColors', $this->availableColors);
$this->fillFromConfig([
'availableColors',
]);
}
/**

View File

@ -13,10 +13,9 @@ class DatePicker extends FormWidgetBase
{
const TIME_PREFIX = '___time_';
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'datepicker';
//
// Configurable properties
//
/**
* @var bool Display mode: datetime, date, time.
@ -33,14 +32,27 @@ class DatePicker extends FormWidgetBase
*/
public $maxDate = '2020-12-31';
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'datepicker';
/**
* {@inheritDoc}
*/
public function init()
{
$this->mode = strtolower($this->getConfig('mode', $this->mode));
$this->minDate = $this->getConfig('minDate', $this->minDate);
$this->maxDate = $this->getConfig('maxDate', $this->maxDate);
$this->fillFromConfig([
'mode',
'minDate',
'maxDate',
]);
$this->mode = strtolower($this->mode);
}
/**

View File

@ -24,48 +24,59 @@ use Exception;
*/
class FileUpload extends FormWidgetBase
{
//
// Configurable properties
//
/**
* @var int Preview image width
*/
public $imageWidth = 100;
/**
* @var int Preview image height
*/
public $imageHeight = 100;
/**
* @var string Text to display when no file is associated
*/
public $previewNoFilesMessage = 'backend::lang.form.preview_no_files_message';
/**
* @var mixed Collection of acceptable file types.
*/
public $fileTypes = false;
/**
* @var array Options used for generating thumbnails.
*/
public $thumbOptions = [
'mode' => 'crop',
'extension' => 'auto'
];
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'fileupload';
/**
* @var int Preview image width
*/
public $imageWidth;
/**
* @var int Preview image height
*/
public $imageHeight;
/**
* @var string Text to display when no file is associated
*/
public $previewNoFilesMessage;
/**
* @var mixed Collection of acceptable file types.
*/
public $acceptedFileTypes = false;
/**
* {@inheritDoc}
*/
public function init()
{
$this->imageHeight = $this->getConfig('imageHeight', 100);
$this->imageWidth = $this->getConfig('imageWidth', 100);
$this->acceptedFileTypes = $this->getConfig('fileTypes');
$this->previewNoFilesMessage = $this->getConfig(
$this->fillFromConfig([
'imageWidth',
'imageHeight',
'previewNoFilesMessage',
'backend::lang.form.preview_no_files_message'
);
$this->thumbOptions = [
'mode' => 'crop',
'extension' => 'auto'
];
'fileTypes',
'thumbOptions'
]);
$this->checkUploadPostback();
}
@ -133,7 +144,7 @@ class FileUpload extends FormWidgetBase
*/
public function getAcceptedFileTypes($includeDot = false)
{
$types = $this->acceptedFileTypes;
$types = $this->fileTypes;
if ($types === false && starts_with($this->getDisplayMode(), 'image')) {
$types = 'jpg,jpeg,bmp,png,gif,svg';
}

View File

@ -21,6 +21,34 @@ use SystemException;
*/
class RecordFinder extends FormWidgetBase
{
//
// Configurable properties
//
/**
* @var string Field name to use for key.
*/
public $keyFrom = 'id';
/**
* @var string Relation column to display for the name
*/
public $nameFrom;
/**
* @var string Relation column to display for the description
*/
public $descriptionFrom;
/**
* @var string Prompt to display if no record is selected.
*/
public $prompt = 'Click the %s button to find a record';
//
// Object properties
//
/**
* {@inheritDoc}
*/
@ -41,26 +69,6 @@ class RecordFinder extends FormWidgetBase
*/
public $relationModel;
/**
* @var string Field name to use for key.
*/
public $keyFrom = 'id';
/**
* @var string Relation column to display for the name
*/
public $nameFrom;
/**
* @var string Relation column to display for the description
*/
public $descriptionFrom;
/**
* @var string Prompt to display if no record is selected.
*/
public $prompt;
/**
* @var Backend\Classes\WidgetBase Reference to the widget used for viewing (list or form).
*/
@ -76,13 +84,15 @@ class RecordFinder extends FormWidgetBase
*/
public function init()
{
$this->relationName = $this->formField->valueFrom;
$this->relationType = $this->model->getRelationType($this->relationName);
$this->fillFromConfig([
'prompt',
'keyFrom',
'nameFrom',
'descriptionFrom',
]);
$this->prompt = $this->getConfig('prompt', 'Click the %s button to find a record');
$this->keyFrom = $this->getConfig('keyFrom', $this->keyFrom);
$this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom);
$this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom);
$this->relationName = $this->valueFrom;
$this->relationType = $this->model->getRelationType($this->relationName);
if (!$this->model->hasRelation($this->relationName)) {
throw new SystemException(Lang::get('backend::lang.model.missing_relation', [

View File

@ -14,6 +14,29 @@ use Illuminate\Database\Eloquent\Relations\Relation as RelationBase;
*/
class Relation extends FormWidgetBase
{
//
// Configurable properties
//
/**
* @var string Model column to use for the name reference
*/
public $nameFrom = 'name';
/**
* @var string Model column to use for the description reference
*/
public $descriptionFrom = 'description';
/**
* @var string Empty value to use if the relation is singluar (belongsTo)
*/
public $emptyOption;
//
// Object properties
//
/**
* {@inheritDoc}
*/
@ -34,33 +57,20 @@ class Relation extends FormWidgetBase
*/
public $renderFormField;
/**
* @var string Model column to use for the name reference
*/
public $nameFrom = 'name';
/**
* @var string Model column to use for the description reference
*/
public $descriptionFrom = 'description';
/**
* @var string Empty value to use if the relation is singluar (belongsTo)
*/
public $emptyOption;
/**
* {@inheritDoc}
*/
public function init()
{
$this->fillFromConfig([
'nameFrom',
'descriptionFrom',
'emptyOption',
]);
$this->relationName = $this->valueFrom;
$this->relationType = $this->model->getRelationType($this->relationName);
$this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom);
$this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom);
$this->emptyOption = $this->getConfig('emptyOption');
if (!$this->model->hasRelation($this->relationName)) {
throw new SystemException(Lang::get(
'backend::lang.model.missing_relation',

View File

@ -11,23 +11,39 @@ use Backend\Classes\FormWidgetBase;
*/
class RichEditor extends FormWidgetBase
{
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'richeditor';
//
// Configurable properties
//
/**
* @var boolean Determines whether content has HEAD and HTML tags.
*/
public $fullPage = false;
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'richeditor';
/**
* {@inheritDoc}
*/
public function init()
{
$this->fillFromConfig([
'fullPage',
]);
}
/**
* {@inheritDoc}
*/
public function render()
{
$this->fullPage = $this->getConfig('fullPage', $this->fullPage);
$this->prepareVars();
return $this->makePartial('richeditor');
}