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

update filter stub #208

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/Console/Commands/Views/FilterBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Backpack\Generators\Console\Commands\Views;

use Illuminate\Support\Str;

class FilterBackpackCommand extends PublishOrCreateViewBackpackCommand
{
/**
Expand Down Expand Up @@ -45,4 +47,19 @@ class FilterBackpackCommand extends PublishOrCreateViewBackpackCommand
* @var string
*/
protected $stub = 'filter.stub';

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$name = Str::of($name)->camel()->ucfirst()->value();
$stub = $this->files->get($this->getStub());
$stub = str_replace('__FILTER_NAME__', $name, $stub);

return $stub;
}
}
85 changes: 44 additions & 41 deletions src/Console/stubs/filter.stub
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
{{-- Simple Backpack CRUD filter --}}
<li filter-name="{{ $filter->name }}"
filter-type="{{ $filter->type }}"
filter-key="{{ $filter->key }}"
filter-debounce="{{ $filter->options['debounce'] ?? 0 }}"
filter-init-function="{{ $filter->options['init_function'] ?? 'init__FILTER_NAME__Filter' }}"
class="nav-item {{ Request::get($filter->name)?'active':'' }}">
<a class="nav-link" href=""
parameter="{{ $filter->name }}"
>{{ $filter->label }}</a>
<a class="nav-link" href="javascript:void(0);">{{ $filter->label }}</a>
</li>


{{-- ########################################### --}}
{{-- Extra CSS and JS for this particular filter --}}
@push('after_scripts')
@bassetBlock('__FILTER_NAME__-filter.js')
<script>
// init function for simple filter
function init__FILTER_NAME__Filter(filter, filterNavbar) {

{{-- FILTERS EXTRA JS --}}
{{-- push things in the after_scripts section --}}
let filterName = filter.getAttribute('filter-name');
let filterKey = filter.getAttribute('filter-key');
let filterAnchor = filter.querySelector('a');
let filterDebounce = filter.getAttribute('filter-debounce');
let navBarId = filterNavbar.getAttribute('id');
let shouldUpdateUrl = true;

@push('crud_list_scripts')
<script>
jQuery(document).ready(function($) {
$("li[filter-key={{ $filter->key }}] a").click(function(e) {
filterAnchor.addEventListener('click', async function(e) {
e.preventDefault();

var parameter = $(this).attr('parameter');

// behaviour for ajax table
var ajax_table = $("#crudTable").DataTable();
var current_url = ajax_table.ajax.url();
// get the filter value
let filterValue = filter.classList.contains('active') ? true : false;

if (URI(current_url).hasQuery(parameter)) {
var new_url = URI(current_url).removeQuery(parameter, true);
} else {
var new_url = URI(current_url).addQuery(parameter, true);
switch (filterValue) {
case true:
filterValue = null;
break;
default:
filterValue = true;
}

new_url = normalizeAmpersand(new_url.toString());
if (filterValue) {
filter.classList.add('active');
}else{
filter.dispatchEvent(new CustomEvent('backpack:filter:clear'));
}

// replace the datatables ajax url with new_url and reload it
ajax_table.ajax.url(new_url).load();
document.dispatchEvent(new CustomEvent('backpack:filter:changed', {
detail: {
filterName: filterName,
filterValue: filterValue,
shouldUpdateUrl: shouldUpdateUrl,
debounce: filterDebounce,

// add filter to URL
crud.updateUrl(new_url);

// mark this filter as active in the navbar-filters
if (URI(new_url).hasQuery('{{ $filter->name }}', true)) {
$("li[filter-key={{ $filter->key }}]").removeClass('active').addClass('active');
$('#remove_filters_button').removeClass('invisible');
}
else
{
$("li[filter-key={{ $filter->key }}]").trigger("filter:clear");
}
});
}
}));
});

// clear filter event (used here and by the Remove all filters button)
$("li[filter-key={{ $filter->key }}]").on('filter:clear', function(e) {

$("li[filter-key={{ $filter->key }}]").removeClass('active');
filter.addEventListener('backpack:filter:clear', function(e) {
if(e.detail && e.detail.clearAllFilters) {
shouldUpdateUrl = false
}
filter.classList.remove('active');
});
});
};
</script>
@endBassetBlock
@endpush
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}
Loading