Skip to content

Commit b45139f

Browse files
committed
Enable passing of multiple attributes for auto fill after store/update. Merge duplicate code for easier handling.
1 parent 8627277 commit b45139f

File tree

9 files changed

+115
-222
lines changed

9 files changed

+115
-222
lines changed

resources/views/fields/inc/button-add.blade.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
@php($attribute = $field['on_the_fly']['attribute'] ?? json_encode($field['on_the_fly']['attributes']) ?? 'name')
12
<span class="input-group-btn">
23
<button
3-
href="#"
4-
type="button"
5-
class="btn btn-primary"
6-
style="border-radius: 0px"
7-
data-toggle="modal"
8-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
9-
data-load-url="{{ $field['on_the_fly']['create_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name').'&create_modal_view='.($field['on_the_fly']['create_modal_view'] ?? 'webfactor::modal.create').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
4+
href="#"
5+
type="button"
6+
class="btn btn-primary"
7+
style="border-radius: 0px"
8+
data-toggle="modal"
9+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
10+
data-load-url="{{ $field['on_the_fly']['create_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create?field_name='.$field['name'].'&attribute='.$attribute.'&create_modal_view='.($field['on_the_fly']['create_modal_view'] ?? 'webfactor::modal.create').'&attribute='.$attribute }}">
1011
<i class="fa fa-plus"></i>
1112
</button>
1213
</span>

resources/views/fields/inc/button-edit.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@php($attribute = $field['on_the_fly']['attribute'] ?? json_encode($field['on_the_fly']['attributes']) ?? 'name')
12
<span class="input-group-btn">
23
<button
34
href="#"
@@ -7,7 +8,7 @@ class="btn btn-warning {{ isset($field['value']) ?: 'disabled'}}"
78
data-toggle="modal"
89
data-id="{{ $field['value'] ?? '' }}"
910
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
10-
data-load-url="{{ $field['on_the_fly']['edit_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&edit_modal_view='.($field['on_the_fly']['edit_modal_view'] ?? 'webfactor::modal.edit').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
11+
data-load-url="{{ $field['on_the_fly']['edit_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&edit_modal_view='.($field['on_the_fly']['edit_modal_view'] ?? 'webfactor::modal.edit').'&attribute='. $attribute }}">
1112
<i class="fa fa-edit"></i>
1213
</button>
1314
</span>
Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1 @@
1-
@extends('webfactor::modal.create_layout')
2-
3-
@section('header')
4-
<h3 class="box-title">{{ trans('backpack::crud.add_a_new') }} {{ $crud->entity_name }}</h3>
5-
@endsection
6-
7-
@php
8-
$requestData = [];
9-
10-
foreach ($request->input() as $key => $item) {
11-
$requestData[] = [
12-
'name' => $key,
13-
'value' => $item,
14-
];
15-
}
16-
@endphp
17-
18-
@section('content')
19-
<div class="row">
20-
<div class="col-md-10 col-md-offset-1">
21-
@include('crud::inc.grouped_errors')
22-
23-
<!-- load the view from the application if it exists, otherwise load the one in the package -->
24-
@if(view()->exists('vendor.backpack.crud.form_content'))
25-
@include('vendor.backpack.crud.form_content', ['fields' => $fields])
26-
@else
27-
@include('crud::form_content', ['fields' => $fields])
28-
@endif
29-
</div>
30-
</div>
31-
@endsection
32-
33-
@section('footer')
34-
@include('webfactor::modal.inc.create_form_save_buttons')
35-
@endsection
36-
37-
@push('crud_fields_scripts')
38-
<script>
39-
$("#create_{{ $entity }}").submit(function (e) {
40-
e.preventDefault(); // avoid to execute the actual submit of the form.
41-
42-
var requestData = <?php echo json_encode($requestData); ?>;
43-
44-
$.ajax({
45-
type: "PUT",
46-
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
47-
data
48-
:
49-
$("#create_{{ $entity }}").serialize() + '&' + $.param(requestData), // serializes the form's elements.
50-
success
51-
:
52-
53-
function (data) {
54-
new PNotify({
55-
type: "success",
56-
title: "{{ trans('backpack::base.success') }}",
57-
text: "{{ trans('backpack::crud.insert_success') }}"
58-
});
59-
60-
$("#{{ $entity }}_create_modal").modal('hide');
61-
62-
// provide auto-fill
63-
64-
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
65-
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
66-
} else {
67-
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
68-
}
69-
70-
searchfield.select2('open');
71-
72-
// Get the search box within the dropdown or the selection
73-
// Dropdown = single, Selection = multiple
74-
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
75-
// This is undocumented and may change in the future
76-
var userInput = $("#create_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
77-
78-
search.val(userInput[0]['value']);
79-
search.trigger('input');
80-
setTimeout(function () {
81-
$('.select2-results__option').trigger("mouseup");
82-
}, 200);
83-
},
84-
error: function (data) {
85-
new PNotify({
86-
type: "error",
87-
title: "{{ trans('backpack::base.error') }}",
88-
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
89-
});
90-
}
91-
});
92-
});
93-
</script>
94-
95-
@endpush
1+
@extends('webfactor::modal.save', ['action' => 'create'])

resources/views/modal/delete.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@extends('webfactor::modal.delete_layout')
1+
@extends('webfactor::modal.layout', ['action' => $delete ])
22

33
@section('header')
44
<h3 class="box-title">{{ trans('backpack::crud.delete') }} {{ $crud->entity_name }}</h3>

resources/views/modal/delete_layout.blade.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,2 @@
1-
@extends('webfactor::modal.edit_layout')
1+
@extends('webfactor::modal.save', ['action' => 'edit'])
22

3-
@section('header')
4-
<h3 class="box-title">{{ trans('backpack::crud.edit') }} {{ $crud->entity_name }}</h3>
5-
@endsection
6-
7-
@section('content')
8-
<div class="row">
9-
<div class="col-md-10 col-md-offset-1">
10-
@include('crud::inc.grouped_errors')
11-
12-
<!-- load the view from the application if it exists, otherwise load the one in the package -->
13-
@if(view()->exists('vendor.backpack.crud.form_content'))
14-
@include('vendor.backpack.crud.form_content', ['fields' => $fields, 'action' => 'edit'])
15-
@else
16-
@include('crud::form_content', ['fields' => $fields, 'action' => 'edit'])
17-
@endif
18-
</div>
19-
</div>
20-
@endsection
21-
22-
@section('footer')
23-
@include('webfactor::modal.inc.edit_form_save_buttons')
24-
@endsection
25-
26-
@push('crud_fields_scripts')
27-
<script>
28-
$("#edit_{{ $entity }}").submit(function (e) {
29-
30-
$.ajax({
31-
type: "PATCH",
32-
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
33-
data: $("#edit_{{ $entity }}").serialize(), // serializes the form's elements.
34-
success: function (data) {
35-
new PNotify({
36-
type: "success",
37-
title: "{{ trans('backpack::base.success') }}",
38-
text: "{{ trans('backpack::crud.update_success') }}"
39-
});
40-
41-
$("#{{ $entity }}_edit_modal").modal('hide');
42-
43-
// provide auto-fill
44-
45-
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
46-
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
47-
} else {
48-
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
49-
}
50-
51-
searchfield.val(null).trigger('change');
52-
searchfield.select2('open');
53-
54-
// Get the search box within the dropdown or the selection
55-
// Dropdown = single, Selection = multiple
56-
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
57-
// This is undocumented and may change in the future
58-
var userInput = $("#edit_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
59-
60-
search.val(userInput[0]['value']);
61-
search.trigger('input');
62-
setTimeout(function () {
63-
$('.select2-results__option').trigger("mouseup");
64-
}, 200);
65-
},
66-
error: function (data) {
67-
new PNotify({
68-
type: "error",
69-
title: "{{ trans('backpack::base.error') }}",
70-
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
71-
});
72-
}
73-
});
74-
75-
e.preventDefault(); // avoid to execute the actual submit of the form.
76-
});
77-
</script>
78-
79-
@endpush

resources/views/modal/edit_layout.blade.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

resources/views/modal/create_layout.blade.php renamed to resources/views/modal/layout.blade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
@stack('crud_fields_styles')
2-
<form method="post" id="{{ 'create_'.$entity }}" accept-charset="UTF-8">
3-
{{--'files'=>$crud->hasUploadFields('create')])--}}
2+
<form method="post" id="{{ $entity.'_'.$action }}" accept-charset="UTF-8">
43
{{ csrf_field() }}
54
<div class="modal-header">
6-
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_create_modal" aria-label="Close">
5+
<button type="button"
6+
class="close"
7+
data-toggle="modal"
8+
data-target="#{{ $entity.'_'.$action }}_modal"
9+
aria-label="Close">
710
<span aria-hidden="true">&times;</span>
811
</button>
912
@yield('header')
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
@extends('webfactor::modal.layout', ['action' => $action ])
2+
3+
@section('header')
4+
<h3 class="box-title">{{ trans('backpack::crud.' . $action) }} {{ $crud->entity_name }}</h3>
5+
@endsection
6+
7+
@section('content')
8+
<div class="row">
9+
<div class="col-md-10 col-md-offset-1">
10+
@include('crud::inc.grouped_errors')
11+
12+
<!-- load the view from the application if it exists, otherwise load the one in the package -->
13+
@if(view()->exists('vendor.backpack.crud.form_content'))
14+
@include('vendor.backpack.crud.form_content', ['fields' => $fields, 'action' => $action])
15+
@else
16+
@include('crud::form_content', ['fields' => $fields, 'action' => $action])
17+
@endif
18+
</div>
19+
</div>
20+
@endsection
21+
22+
@section('footer')
23+
@include('webfactor::modal.inc.' . $action . '_form_save_buttons')
24+
@endsection
25+
26+
@push('crud_fields_scripts')
27+
<script>
28+
var action = '{{$action}}';
29+
var modalId = "#{{$entity}}_" + action;
30+
$(modalId).submit(function (e) {
31+
32+
e.preventDefault(); // avoid to execute the actual submit of the form.
33+
34+
$.ajax({
35+
type: action === 'create' ? 'PUT' : 'PATCH',
36+
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
37+
data: $(modalId).serialize(), // serializes the form's elements.
38+
success: function (data) {
39+
new PNotify({
40+
type: "success",
41+
title: "{{ trans('backpack::base.success') }}",
42+
text: "{{ trans('backpack::crud.update_success') }}"
43+
});
44+
45+
$(modalId + "_modal" ).modal('hide');
46+
47+
// provide auto-fill
48+
49+
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
50+
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
51+
} else {
52+
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
53+
}
54+
55+
searchfield.val(null).trigger('change');
56+
searchfield.select2('open');
57+
58+
// Get the search box within the dropdown or the selection
59+
// Dropdown = single, Selection = multiple
60+
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
61+
// This is undocumented and may change in the future
62+
var searchText = '';
63+
try {
64+
var attribute = JSON.parse(@json($request->input('attribute')));
65+
if (Array.isArray(attribute)) {
66+
searchText = attribute.map(function (attr) {
67+
let input = $(modalId + " [name='" + attr + "']").serializeArray()[0];
68+
return input ? input['value'] : '';
69+
}).join(' ');
70+
}
71+
} catch (e) {
72+
let input = $(modalId + " [name='{{ $request->input('attribute') }}']").serializeArray()[0];
73+
if (input) {
74+
searchText = input['value'];
75+
}
76+
}
77+
78+
search.val(searchText);
79+
search.trigger('input');
80+
setTimeout(function () {
81+
$('.select2-results__option').trigger("mouseup");
82+
}, 200);
83+
},
84+
error: function (data) {
85+
new PNotify({
86+
type: "error",
87+
title: "{{ trans('backpack::base.error') }}",
88+
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
89+
});
90+
}
91+
});
92+
93+
});
94+
</script>
95+
96+
@endpush

0 commit comments

Comments
 (0)