Many adjusts for allowing filter type options to be used on filter edition form.

This commit is contained in:
Mateus Machado Luna 2019-05-23 17:07:01 -03:00
parent 9b85c8f389
commit 48865d3de4
10 changed files with 247 additions and 75 deletions

View File

@ -220,6 +220,13 @@ class Admin {
$class = new $metadata_type;
$settings['i18n']['helpers_label'][$class->get_component()] = $class->get_form_labels();
}
$filter_types = $Tainacan_Filters->fetch_filter_types();
foreach ( $filter_types as $index => $filter_type){
$class = new $filter_type;
$settings['i18n']['helpers_label'][$class->get_component()] = $class->get_form_labels();
}
$settings['form_hooks'] = Admin_Hooks::get_instance()->get_registered_hooks();

View File

@ -149,11 +149,10 @@
</b-field>
<component
:errors="formErrors['filter_type_options']"
v-if="(editForm.filter_type_object && editForm.filter_type_object.form_component) || editForm.edit_form == ''"
v-if="(editForm.filter_type_object && editForm.filter_type_object.form_component && editForm.filter_type_object.options) || editForm.edit_form == ''"
:is="editForm.filter_type_object.form_component"
:filter="editForm"
v-model="editForm.filter_type_options"/>
v-model="editForm.filter_type_object.options"/>
<div
v-html="editForm.edit_form"
v-else/>

View File

@ -619,7 +619,7 @@
placement: 'auto',
}">
<a :style="{ fontWeight: 'bold', color: '#454647 !important', lineHeight: '1.5rem' }">
{{ `${$i18n.get('label_all_published_items')}` }}
{{ $i18n.get('label_all_published_items') }}
<span class="has-text-gray">&nbsp;{{ collection && collection.total_items ? ` (${Number(collection.total_items.private) + Number(collection.total_items.publish)})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.private + repositoryTotalItems.publish })` : '' }}</span>
</a>
</li>
@ -635,7 +635,7 @@
<span class="icon has-text-gray">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-public"/>
</span>
{{ `${$i18n.get('label_publish_items')}` }}
{{ $i18n.get('label_publish_items') }}
<span class="has-text-gray">&nbsp;{{ collection && collection.total_items ? ` (${collection.total_items.publish})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.publish })` : '' }}</span>
</a>
</li>
@ -653,7 +653,7 @@
<span class="icon has-text-gray">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-private"/>
</span>
{{ `${$i18n.get('label_private_items')}` }}
{{ $i18n.get('label_private_items') }}
<span class="has-text-gray">&nbsp;{{ collection && collection.total_items ? ` (${collection.total_items.private})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.private })` : '' }}</span>
</a>
</li>
@ -669,7 +669,7 @@
<span class="icon has-text-gray">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-draft"/>
</span>
{{ `${$i18n.get('label_draft_items')}` }}
{{ $i18n.get('label_draft_items') }}
<span class="has-text-gray">&nbsp;{{ collection && collection.total_items ? ` (${collection.total_items.draft})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.draft })` : '' }}</span>
</a>
</li>
@ -686,7 +686,7 @@
<span class="icon has-text-gray">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-delete"/>
</span>
{{ `${$i18n.get('label_trash_items')}` }}
{{ $i18n.get('label_trash_items') }}
<span class="has-text-gray">&nbsp;{{ collection && collection.total_items ? ` (${collection.total_items.trash})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.trash })` : '' }}</span>
</a>
</li>

View File

@ -453,6 +453,8 @@ return apply_filters( 'tainacan-admin-i18n', [
'instruction_click_or_drag_metadatum_create' => __( 'Click or drag and drop to create a new metadatum', 'tainacan' ),
'instruction_drag_and_drop_filter_sort' => __( 'Drag and drop to change filter order', 'tainacan' ),
'instruction_drag_and_drop_metadatum_sort' => __( 'Drag and drop to change metadatum order', 'tainacan' ),
'instruction_select_step_options_to_show' => __( 'Select which Step values to show', 'tainacan' ),
'instruction_select_maximum_options_to_show' => __( 'Select which Maximum of options to show', 'tainacan' ),
// Info. Other feedback to user.
'info_items_tab_all' => __( 'Every published item of this collection, including those visible only to editors.', 'tainacan' ),

View File

@ -124,12 +124,12 @@ class Filter extends Entity {
function get_filter_type_object(){
$class_name = $this->get_filter_type();
if( !class_exists( $class_name ) ){
return false;
}
if (empty($class_name)) {
return null;
}
$object_type = new $class_name();
$object_type->set_options( $this->get_filter_options() );
$object_type->set_options( $this->get_filter_type_options() );
return $object_type;
}
@ -147,7 +147,7 @@ class Filter extends Entity {
*
* @return array Configurations for the filter type object
*/
function get_filter_options(){
function get_filter_type_options(){
return $this->get_mapped_property('filter_type_options');
}
@ -258,6 +258,18 @@ class Filter extends Entity {
$this->add_error($metadatum, $message);
}
$this->add_error('filter_type_options', $is_valid);
return false;
}
/**
* Set Filter type options
*
* @param [string || integer] $value
* @return void
*/
function set_filter_type_options( $value ){
$this->set_mapped_property('filter_type_options', $value);
}
}

View File

@ -5,11 +5,41 @@ use Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanFilterType
*/
abstract class Filter_Type {
private $supported_types = [];
/**
* Array of options specific to this filter type. Stored in filter_type_options property of the Filter object
* @var array
*/
private $options = [];
/**
* The default values for the filter type options array
* @var array
*/
private $default_options = [];
/**
* The name of the web component used by this filter type
* @var string
*/
private $component;
/**
* The name of the web component used by the Form
* @var bool | string
*/
private $form_component = false;
/**
* The html template featuring a preview of how this metadata type componenet
* @var string
*/
private $preview_template = '';
protected $use_max_options = true;
@ -72,11 +102,13 @@ abstract class Filter_Type {
public function _toArray(){
$attributes = [];
$attributes['className'] = get_class($this);
$attributes['component'] = $this->get_component();
$attributes['supported_types'] = $this->get_supported_types();
$attributes['className'] = get_class($this);
$attributes['component'] = $this->get_component();
$attributes['options'] = $this->get_options();
$attributes['supported_types'] = $this->get_supported_types();
$attributes['preview_template'] = $this->get_preview_template();
$attributes['use_max_options'] = $this->get_use_max_options();
$attributes['use_max_options'] = $this->get_use_max_options();
$attributes['form_component'] = $this->get_form_component();
return $attributes;
}
@ -88,6 +120,10 @@ abstract class Filter_Type {
$this->options = ( is_array( $options ) ) ? $options : (!is_array(unserialize( $options )) ? [] : unserialize( $options ));
}
public function set_default_options(Array $options) {
$this->default_options = $options;
}
/**
* Validates the options Array
*
@ -121,11 +157,13 @@ abstract class Filter_Type {
$this->component = $component;
}
/**
* @return mixed
*/
public function get_options() {
return $this->options;
/**
* Gets the options for this filter types including default values for options
* that were not set yet.
* @return array Filter type options
*/
public function get_options() {
return array_merge($this->default_options, $this->options);
}
public function set_use_max_options($use_max_options) {
@ -135,4 +173,38 @@ abstract class Filter_Type {
public function get_use_max_options() {
return $this->use_max_options;
}
/**
* Gets one option from the options array.
*
* Checks if option exist or if it have a default value. Otherwise return an empty string
*
* @param string $key the desired option
* @return mixed the option value, the default value or an empty string
*/
public function get_option($key) {
$options = $this->get_options();
return isset($options[$key]) ? $options[$key] : '';
}
/**
* allow i18n from messages
*/
public function get_form_labels(){
return [];
}
/**
* @return string
*/
public function get_form_component() {
return $this->form_component;
}
/**
* @param $form_component The web component that will render the filter options form
*/
public function set_form_component($form_component){
$this->form_component = $form_component;
}
}

View File

@ -1,54 +1,102 @@
<template>
<div>
<!-- <b-select
expanded
:disabled="disabled"
:id = "id"
:placeholder="$i18n.get('label_selectbox_init')"
:value="value"
:class="{'is-empty': value == undefined || value == ''}"
@blur="$emit('blur')"
@input="onChecked($event)">
<option
v-for="(option, index) in getOptions"
:key="index"
:label="option"
:value="option"
border>{{ option }}</option>
</b-select> -->
<b-field :addons="false">
<label class="label is-inline">
{{ $i18n.getHelperTitle('tainacan-filter-numeric', 'step') }}<span>&nbsp;*&nbsp;</span>
<help-button
:title="$i18n.getHelperTitle('tainacan-filter-numeric', 'step')"
:message="$i18n.getHelperMessage('tainacan-filter-numeric', 'step')"/>
</label>
<div
v-if="!showEditStepOptions"
class="is-flex">
<b-select
name="step_options"
v-model="step"
@input="onUpdateStep"
:placeholder="$i18n.get('instruction_select_step_options_to_show')">
<option value="0.001">0.001</option>
<option value="0.01">0.01</option>
<option value="0.1">0.1</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="100">100</option>
<option value="1000">1000</option>
<option
v-if="step && ![0.001,0.01,0.1,1,2,5,10,100,1000].find( (element) => element == step )"
:value="step">
{{ step }}</option>
</b-select>
<button
class="button is-white is-pulled-right"
:aria-label="$i18n.get('edit')"
@click.prevent="showEditStepOptions = true">
<span
v-tooltip="{
content: $i18n.get('edit'),
autoHide: true,
placement: 'bottom'
}"
class="icon">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-edit has-text-secondary"/>
</span>
</button>
</div>
<div
v-if="showEditStepOptions"
class="is-flex">
<b-input
name="max_options"
v-model="step"
@input="onUpdateStep"
type="number"
step="1" />
<button
@click.prevent="showEditStepOptions = false"
class="button is-white is-pulled-right">
<span
v-tooltip="{
content: $i18n.get('close'),
autoHide: true,
placement: 'bottom'
}"
class="icon">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-close has-text-secondary"/>
</span>
</button>
</div>
</b-field>
</div>
</template>
<script>
export default {
// props: {
// filter: {
// type: Object
// },
// options: {
// type: String
// },
// value: [String, Number, Array],
// id: '',
// disabled: false,
// },
// computed: {
// getOptions(){
// if ( this.options && this.options !== '' ){
// return this.options.split("\n");
// }
// else if ( this.filter && this.filter.filter.metadata_type_options.options ) {
// const metadata = this.filter.filter.metadata_type_options.options;
// return ( metadata ) ? metadata.split("\n") : [];
// }
// return [];
// }
// },
// methods: {
// onChecked(value) {
// this.$emit('input', value);
// },
// }
props: {
filter: {
type: Object
},
value: [String, Number, Array],
id: '',
disabled: false,
},
data() {
return {
step: [Number, String],
showEditStepOptions: false
}
},
methods: {
onUpdateStep(value) {
this.$emit('input', value);
},
},
created() {
if (this.value) {
this.step = this.value.step ? this.value.step : 1;
}
}
}
</script>

View File

@ -62,7 +62,7 @@
<b-numberinput
:aria-labelledby="labelId"
size="is-small"
step="0.01"
:step="step"
@input="emit()"
v-model="value"/>
</div>
@ -111,6 +111,7 @@
filter: {
type: Object // concentrate all attributes metadatum id and type
},
step: [Number, String],
metadatum_id: [Number], // not required, but overrides the filter metadatum id if is set
collection_id: [Number], // not required, but overrides the filter metadatum id if is set
labelId: '',

View File

@ -12,8 +12,11 @@ class Numeric extends Filter_Type {
function __construct(){
$this->set_supported_types(['float']);
$this->set_component('tainacan-filter-numeric');
// $this->set_form_component('tainacan-filter-form-numeric');
$this->set_form_component('tainacan-filter-form-numeric');
$this->set_use_max_options(false);
$this->set_default_options([
'step' => 1
]);
$this->set_preview_template('
<div>
<div>
@ -66,18 +69,46 @@ class Numeric extends Filter_Type {
');
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'step' => [
'title' => __( 'Step', 'tainacan' ),
'description' => __( 'The amount to be increased or decreased when clicking on filter control buttons.', 'tainacan' ),
]
];
}
/**
* @param $filter
* @return string
* @internal param $metadatum
*/
public function render( $filter ){
$options = $this->get_option('options');
return '<tainacan-filter-custom-interval
options="' . $options . '"
return '<tainacan-filter-numeric
step="' . $this->get_option('step') . '"
name="'.$filter->get_name().'"
typeRange="numeric"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-custom-interval>';
}
/**
* @param \Tainacan\Entities\Filter $filter
* @return array|bool true if is validate or array if has error
*/
public function validate_options(\Tainacan\Entities\Filter $filter) {
if ( !in_array($filter->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( empty($this->get_option('step')) ) {
return [
'step' => __('Required step','tainacan')
];
}
return true;
}
}

View File

@ -39,7 +39,7 @@ class Filters extends Repository {
'order' => [
'map' => 'menu_order',
'title' => __( 'Order', 'tainacan' ),
'type' => 'string',
'type' => 'string/integer',
'description' => __( 'Filter order. This metadata is used if filters were manually ordered.', 'tainacan' ),
'validation' => ''
],
@ -76,14 +76,14 @@ class Filters extends Repository {
'color' => [
'map' => 'meta',
'title' => __( 'Color', 'tainacan' ),
'type' => 'integer',
'type' => 'integer/string',
'description' => __( 'Filter color', 'tainacan' ),
'validation' => ''
],
'metadatum' => [
'map' => 'meta',
'title' => __( 'Metadata', 'tainacan' ),
'type' => 'integer',
'type' => 'array/object/string',
'description' => __( 'Filter metadata', 'tainacan' ),
'validation' => ''
],