Skip to content

Commit 3efc4fe

Browse files
committed
Merge branch 'master' into address-field
2 parents 60509a1 + 13a69f6 commit 3efc4fe

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ All Notable changes to `Backpack CRUD` will be documented in this file
2020
- Nothing
2121

2222

23+
## [3.1.23] - 2016-09-27
24+
25+
### Added
26+
- autoFocus() and autoFocusOnFirstField() - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
27+
28+
2329
## [3.1.22] - 2016-09-27
2430

2531
### Fixed

src/CrudPanel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
use Backpack\CRUD\PanelTraits\Reorder;
1717
use Backpack\CRUD\PanelTraits\Update;
1818
use Backpack\CRUD\PanelTraits\ViewsAndRestoresRevisions;
19+
use Backpack\CRUD\PanelTraits\AutoFocus;
1920

2021
class CrudPanel
2122
{
22-
use Create, Read, Update, Delete, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, ViewsAndRestoresRevisions;
23+
use Create, Read, Update, Delete, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, ViewsAndRestoresRevisions, AutoFocus;
2324

2425
// --------------
2526
// CRUD variables

src/PanelTraits/AutoFocus.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\PanelTraits;
4+
5+
trait AutoFocus
6+
{
7+
public $autoFocusOnFirstField = true;
8+
9+
public function getAutoFocusOnFirstField()
10+
{
11+
return $this->autoFocusOnFirstField;
12+
}
13+
14+
public function setAutoFocusOnFirstField($value)
15+
{
16+
return $this->autoFocusOnFirstField = (bool) $value;
17+
}
18+
19+
public function enableAutoFocus()
20+
{
21+
return $this->setAutoFocusOnFirstField(true);
22+
}
23+
24+
public function disableAutoFocus()
25+
{
26+
return $this->setAutoFocusOnFirstField(false);
27+
}
28+
}

src/resources/views/form_content.blade.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,43 @@
3636
@stack('crud_fields_scripts')
3737

3838
<script>
39-
// Ctrl+S and Cmd+S trigger Save button click
40-
$(document).keydown(function(e) {
41-
if ((e.which == '115' || e.which == '83' ) && (e.ctrlKey || e.metaKey))
42-
{
43-
e.preventDefault();
44-
// alert("Ctrl-s pressed");
45-
$("button[type=submit]").trigger('click');
46-
return false;
47-
}
48-
return true;
49-
});
39+
jQuery('document').ready(function($){
40+
41+
// Ctrl+S and Cmd+S trigger Save button click
42+
$(document).keydown(function(e) {
43+
if ((e.which == '115' || e.which == '83' ) && (e.ctrlKey || e.metaKey))
44+
{
45+
e.preventDefault();
46+
// alert("Ctrl-s pressed");
47+
$("button[type=submit]").trigger('click');
48+
return false;
49+
}
50+
return true;
51+
});
52+
53+
@if( $crud->autoFocusOnFirstField )
54+
//Focus on first field
55+
@php
56+
$focusField = array_first($fields, function($field){
57+
return isset($field['auto_focus']) && $field['auto_focus'] == true;
58+
})
59+
@endphp
60+
61+
@if($focusField)
62+
window.focusField = $('[name="{{$focusField['name']}}"]').eq(0),
63+
@else
64+
var focusField = $('form').find('input, textarea, select').not('[type="hidden"]').eq(0),
65+
@endif
66+
67+
fieldOffset = focusField.offset().top,
68+
scrollTolerance = $(window).height() / 2;
69+
70+
focusField.trigger('focus');
71+
72+
if( fieldOffset > scrollTolerance ){
73+
$('html, body').animate({scrollTop: (fieldOffset - 30)});
74+
}
75+
@endif
76+
});
5077
</script>
51-
@endsection
78+
@endsection

0 commit comments

Comments
 (0)