A lot of cleanups to the Livewire stuff, and got it kinda-sorta basically working.

This commit is contained in:
Brady Wetherington 2021-10-04 20:02:27 -07:00
parent d80604f2ac
commit bbeedc026d
3 changed files with 31 additions and 97 deletions

View file

@ -15,43 +15,21 @@ class CustomFieldSetDefaultValuesForModel extends Component
public $fields;
public $model_id;
public function __construct()
{
\Log::info("INSTANTIATING A THING!!!"); // WORKS!
\Log::info("MY COMPONENT ID IS: ".$this->id);
}
public function foo()
{
\Log::info("Uh, foo?");
}
public function mount()
{
$this->model = AssetModel::find($this->model_id); // It's possible to do some clever route-model binding here, but let's keep it simple, shall we?
$this->fieldset_id = $this->model->fieldset_id;
$this->fields = CustomFieldset::find($this->fieldset_id)->fields;
$this->add_default_values = ( $this->model->defaultValues->count() > 0);
\Log::error("Model ID is: ".$this->model_id." And its fieldset is: ".$this->fieldset_id);
\Log::error("Mount at least fired, that's got to count for something, yeah?"); //WORKS! YAY!
}
public function updatingFielsetId()
{
\Log::error("ABOUT TO UPDATE FIELDSET ID!!!");
}
public function updatedFieldsetId()
{
\Log::error("UPDATED FIELDSET ID!!!!!!");
$this->fields = CustomFieldset::find($this->fieldset_id)->fields;
}
public function render()
{
//return '<div>Hi: {{ $this->add_default_values ? "TRUTH" : "FALSEHOOD" }} yeah?</div>';
return view('livewire.custom-field-set-default-values-for-model');
}
}

View file

@ -1,31 +1,20 @@
<div>
<span> {{-- This <span> doesn't seem to fix it, neither does a div? --}}
<div class="form-group{{ $errors->has('custom_fieldset') ? ' has-error' : '' }}">
<label for="custom_fieldset" class="col-md-3 control-label">{{ trans('admin/models/general.fieldset') }}</label>
<span wire:ignore> {{-- wire:ignore is because Select 2 mangles the dom in many awful ways, and so does iCheckbox --}}
<div class="col-md-9">
{{ Form::select('custom_fieldset', Helper::customFieldsetList(),old('custom_fieldset', 0000 /*$item->fieldset_id*/), array('class'=>'select2 js-fieldset-field', 'style'=>'width:350px', 'aria-label'=>'custom_fieldset', 'wire:model' => 'fieldset_id','id' => 'glooobits')) }} {{-- when we have this wrapped in 'ignore', the wire:model won't work --}}
{{ Form::select('custom_fieldset', Helper::customFieldsetList(), old('custom_fieldset', $fieldset_id), array('class'=>'select2 js-fieldset-field', 'style'=>'width:350px', 'aria-label'=>'custom_fieldset', 'wire:model' => 'fieldset_id', 'id' => 'glooobits')) }} {{-- when we have this wrapped in 'ignore', the wire:model won't work --}}
{!! $errors->first('custom_fieldset', '<span class="alert-msg" aria-hidden="true"><br><i class="fas fa-times"></i> :message</span>') !!}
<label class="m-l-xs">
{{-- {{ Form::checkbox('add_default_values', 1, Request::old('add_default_values'), ['class' => 'js-default-values-toggler']) }} --}}
{{-- I'm not sure that *this* checkboxy thing will render right, because of things. It's not *in* its own view, right? So that's a problem --}}
{{-- DELETE this and references to it: js-default-values-toggler --}}
<input id="add_default_values" {{-- wire:click="foo"--}} wire:model="add_default_values" type='checkbox' name='add_default_values' value='1' class='minimal'{{ Request::old('add_default_values',$add_default_values)? " checked='checked'" : "" }} />
{{ Form::checkbox('add_default_values', 1, Request::old('add_default_values', $add_default_values), ['class' => 'minimal', 'wire:model' => "add_default_values", 'id' => 'add_default_values']) }}
{{ trans('admin/models/general.add_default_values') }}
</label>
</span>
</div>
</div>
@if($this->add_default_values) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}}
@if( $this->add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}}
<div>
<div class="form-group">
<?php
\Log::error("Fieldset ID is: ".$fieldset_id);
?>
{{-- GET READY TO ADD ME SOME CRAAAAAAZY DEFAULT VALUES MOTHER FLIPPER! For, of course, fieldset: {{ $fieldset_id }} --}}
{{-- @livewire('custom-fields-for-fieldset',['fieldset_id' => $fieldset_id]) --}}
{{-- NOTE: This stuff could work well also for the 'view this asset and do its custom fields' thing --}}
{{-- I don't know if we break *here* or if we break per field element? --}}
@foreach ($fields as $field)
<div class="form-group">
@ -36,17 +25,21 @@
@if ($field->element == "text")
<input class="form-control m-b-xs" type="text" value="{{ $field->defaultValue($model_id) }}" id="default-value{{ $field->id }}" name="default_values[{{ $field->id }}]">
@elseif($field->element == "textarea")
<textarea x-if="field.type == 'textarea'" class="form-control" :value="getValue(field)" :id="'default-value' + field.id" :name="'default_values[' + field.id + ']'"></textarea><br>
<textarea class="form-control" id="default-value{{ $field->id }}" name="default_values[{{ $field->id }}]">{{ $field->defaultValue($model_id) }}</textarea><br>
@elseif($field->element == "listbox")
<select Z-if="field.element == 'listbox'" class="form-control m-b-xs" :name="'default_values[' + field.id + ']'">
<select class="form-control m-b-xs" name="default_values[{{ $field->id }}]">
<option value=""></option>
@foreach($field->field_values as $field_value)
<option Q-for="field_value in field.field_values_array" :value="field_value" :selected="getValue(field) == field_value">{{ $field_value }}</option>
@foreach(explode("\r\n", $field->field_values) as $field_value)
<option value="{{$field_value}}" {{ $field->defaultValue($model_id) == $field_value ? 'selected="selected"': '' }}>{{ $field_value }}</option>
@endforeach
</select>
@elseif($field->element == "radio")
@foreach(explode("\r\n", $field->field_values) as $field_value)
<input type='radio' name="default_values[{{ $field->id }}]" value="{{$field_value}}" {{ $field->defaultValue($model_id) == $field_value ? 'checked="checked"': '' }} />{{ $field_value }}<br />
@endforeach
@elseif($field->element == "checkbox")
<input type='checkbox' />
<input type='checkbox' name="default_values[{{ $field->id }}]" {{ $field->defaultValue($model_id) ? 'checked="checked"': '' }}/>
@else
<span class="help-block form-error">
Unknown field element: {{ $field->element }}
@ -61,38 +54,33 @@
</div>
@endif
<script>
/* FIXME - see if we ccan do this and get @this.set() support?
Though I don't like having $() sometimes, and document.addEventListener other times?
document.addEventListener("DOMContentLoaded", () => {
3 Livewire.hook('component.initialized', (component) => {})
4
*/
// *still* haven't figured out why this doesn't seem to work at all...
// And even if it did, I hate having $(function () {}) as my DOM-ready checker in some places, and
// DOMContentLoaded in another...
/* document.addEventListener("DOMContentLoaded", function () {
Livewire.hook('component.initialized', function (component) {
$('#glooobits').on('select2:select',function (event) { //'change' seems to be the jquery-compatible version but I think the select2 versions might be nicer.
console.log("Select2 has changed!!!!!")
console.dir(event)
@this.set('fieldset_id',event.params.data.id)
// Livewire.first().set('fieldset_id',event.params.data.id) // I still don't know why @this does'nt work here?
})
})
}) */
</script>
@push('js')
<script>
// HEADS UP - this doesn't work at all right now. So you can ignore it.
console.log("pushed JS is ready")
$(function () {
console.log(" - DUMPING ALL LIVEWIRE COMPOPNENTS!")
console.dir(Livewire.all());
console.log("DOMReady is fired, about to add event listener")
//console.log("Well, can we even reference the damned thing: ".$('#gloobits'))
//var that = @this; //see if this even works?! FIXME PLS! - this is the right way to do it
$('#glooobits').on('select2:select',function (event) { //'change' seems to be the jquery-compatible version but I think the select2 versions might be nicer.
console.log("Select2 has changed!!!!!")
console.dir(event)
{{-- @this.set('fieldset_id',event.params.data.id) --}}
Livewire.first().set('fieldset_id',event.params.data.id) // I still don't know why @this does'nt work here?
})
$('#add_default_values').on('ifToggled',function (event) {
console.log("toggled!")
console.dir(event.target)
Livewire.first().set('add_default_values',event.target.checked)
})
})
function whatever(something) {
console.log("Whatever fired")
}
</script>
@endpush
</div>
</span>

View file

@ -34,29 +34,7 @@
</div>
<!-- Custom Fieldset -->
<div>
{{-- <div class="form-group {{ $errors->has('custom_fieldset') ? ' has-error' : '' }}">
<label for="custom_fieldset" class="col-md-3 control-label">{{ trans('admin/models/general.fieldset') }}</label>
<div class="col-md-7">
{{ Form::select('custom_fieldset', Helper::customFieldsetList(),old('custom_fieldset', $item->fieldset_id), array('class'=>'select2 js-fieldset-field', 'style'=>'width:350px', 'aria-label'=>'custom_fieldset')) }}
{!! $errors->first('custom_fieldset', '<span class="alert-msg" aria-hidden="true"><br><i class="fas fa-times"></i> :message</span>') !!}
<label class="m-l-xs">
{{-- {{ Form::checkbox('add_default_values', 1, Request::old('add_default_values'), ['class' => 'js-default-values-toggler']) }} --}}
{{-- I'm not sure that *this* checkboxy thing will render right, because of things. It's not *in* its own view, right? So that's a problem --}}
{{-- <input wire:click="foo" wire:model="add_default_values" type='checkbox' name='add_default_values' value='1' class='js-default-values-toggler'{{ Request::old('add_default_values')? " checked='checked'" : "" }} />
{{ trans('admin/models/general.add_default_values') }}
</label>
</div>
</div> --}}
{{-- <fieldset-default-values
model-id="{{ $item->id ?: '' }}"
fieldset-id="{{ !empty($item->fieldset) ? $item->fieldset->id : Request::old('custom_fieldset') }}"
previous-input="{{ json_encode(Request::old('default_values')) }}">
</fieldset-default-values> --}}
@livewire('custom-field-set-default-values-for-model',["model_id" => $item->id])
</div>
@livewire('custom-field-set-default-values-for-model',["model_id" => $item->id])
@include ('partials.forms.edit.notes')
@include ('partials.forms.edit.requestable', ['requestable_text' => trans('admin/models/general.requestable')])
@ -80,14 +58,4 @@
@include ('partials.forms.edit.image-upload')
@stop
@push('js')
console.log("hi there")
@endpush
{{-- @section('moar_scripts')
<script nonce="{{ csrf_token() }}">
new Vue({
el: '#app'
});
</script>
@endsection --}}
@stop