Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.1][Feature] New field type - Toggle Field Visibility #165

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/resources/views/fields/toggle.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<!-- radio toggle -->
@php
$optionPointer = 0;
$optionValue = old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : '' ));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ternaries in ternaries. Icck.

@endphp

<div @include('crud::inc.field_wrapper_attributes') >

<div>
<label>{!! $field['label'] !!}</label>
</div>

@if( isset($field['options']) && is_array($field['options']) )

@foreach ($field['options'] as $value => $label )
@php ($optionPointer++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the $loop variable supported in the versions of Blade we support? https://laravel.com/docs/5.4/blade#loops


@if( isset($field['inline']) && $field['inline'] )

<label class="radio-inline" for="{{$field['name']}}_{{$optionPointer}}">
<input data-field-toggle="{{ json_encode($field['hide_when']) }}" type="radio" id="{{$field['name']}}_{{$optionPointer}}" name="{{$field['name']}}" value="{{$value}}" {{$optionValue!=='' && $optionValue == $value ? ' checked': ''}}> {!! $label !!}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$optionValue !== '' - notice the spaces (may as well be consistent with the following ==).

</label>

@else

<div class="radio">
<label for="{{$field['name']}}_{{$optionPointer}}">
<input data-field-toggle="{{ json_encode($field['hide_when']) }}" type="radio" id="{{$field['name']}}_{{$optionPointer}}" name="{{$field['name']}}" value="{{$value}}" {{$optionValue!=='' && $optionValue == $value ? ' checked': ''}}> {!! $label !!}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above re spacing.

</label>
</div>

@endif

@endforeach

@endif

{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif

</div>

{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields))

{{-- FIELD CSS - will be loaded in the after_styles section --}}
@push('crud_fields_styles')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NL?

@endpush

{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
<script>
jQuery(document).ready(function($){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're spelling jQuery here but using $ in the function?


window.$hiddenFields = window.$hiddenFields || {};

var $toggle = function( $radio ){

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NL?

$hideWhen = $radio.data('field-toggle'),
$value = $radio.val(),
$radioSet = $radio.attr('name');

$hiddenFields[ $radioSet ] = $hiddenFields[ $radioSet ] || [];

if( Object.keys($hiddenFields[ $radioSet ]).length ){
$.each($hiddenFields[ $radioSet ], function(idx, field){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have to ask, everything has gotten the evil $ that isn't needed in JS, why not the function parameters? 👿

field.show();
});
$hiddenFields[ $radioSet ] = [];
}

if( typeof $hideWhen[ $value ] !== undefined ){
$.each($hideWhen[ $value ], function(idx, name){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my impish questin above :P


var f = $('[name="'+name+'"]').parents('.form-group');

if( f.length ){
$hiddenFields[ $radioSet ].push(f);
f.hide();
}
});
}
};

$('input[data-field-toggle]').on('change', function(){
return $toggle( $(this) );
});

$('input[data-field-toggle]:checked').each(function(){
return $toggle( $(this) );
});
});
</script>
@endpush

@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}