Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
6f198583
Commit
6f198583
authored
Aug 07, 2019
by
Kushal Pandya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `autofocus` directive for input elements
parent
224db2f8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
0 deletions
+77
-0
app/assets/javascripts/vue_shared/directives/autofocusonshow.js
...sets/javascripts/vue_shared/directives/autofocusonshow.js
+39
-0
spec/javascripts/vue_shared/directives/autofocusonshow_spec.js
...javascripts/vue_shared/directives/autofocusonshow_spec.js
+38
-0
No files found.
app/assets/javascripts/vue_shared/directives/autofocusonshow.js
0 → 100644
View file @
6f198583
/**
* Input/Textarea Autofocus Directive for Vue
*/
export
default
{
/**
* Set focus when element is rendered, but
* is not visible, using IntersectionObserver
*
* @param {Element} el Target element
*/
inserted
(
el
)
{
if
(
'
IntersectionObserver
'
in
window
)
{
// Element visibility is dynamic, so we attach observer
el
.
visibilityObserver
=
new
IntersectionObserver
(
entries
=>
{
entries
.
forEach
(
entry
=>
{
// Combining `intersectionRatio > 0` and
// element's `offsetParent` presence will
// deteremine if element is truely visible
if
(
entry
.
intersectionRatio
>
0
&&
entry
.
target
.
offsetParent
)
{
entry
.
target
.
focus
();
}
});
});
// Bind the observer.
el
.
visibilityObserver
.
observe
(
el
,
{
root
:
document
.
documentElement
});
}
},
/**
* Detach observer on unbind hook.
*
* @param {Element} el Target element
*/
unbind
(
el
)
{
if
(
el
.
visibilityObserver
)
{
el
.
visibilityObserver
.
disconnect
();
}
},
};
spec/javascripts/vue_shared/directives/autofocusonshow_spec.js
0 → 100644
View file @
6f198583
import
autofocusonshow
from
'
~/vue_shared/directives/autofocusonshow
'
;
/**
* We're testing this directive's hooks as pure functions
* since behaviour of this directive is highly-dependent
* on underlying DOM methods.
*/
describe
(
'
AutofocusOnShow directive
'
,
()
=>
{
describe
(
'
with input invisible on component render
'
,
()
=>
{
let
el
;
beforeAll
(()
=>
{
setFixtures
(
'
<div id="container" style="display: none;"><input id="inputel"/></div>
'
);
el
=
document
.
querySelector
(
'
#inputel
'
);
});
it
(
'
should bind IntersectionObserver on input element
'
,
()
=>
{
spyOn
(
el
,
'
focus
'
);
autofocusonshow
.
inserted
(
el
);
expect
(
el
.
visibilityObserver
).
toBeDefined
();
expect
(
el
.
focus
).
not
.
toHaveBeenCalled
();
});
it
(
'
should stop IntersectionObserver on input element on unbind hook
'
,
()
=>
{
el
.
visibilityObserver
=
{
disconnect
:
()
=>
{},
};
spyOn
(
el
.
visibilityObserver
,
'
disconnect
'
);
autofocusonshow
.
unbind
(
el
);
expect
(
el
.
visibilityObserver
).
toBeDefined
();
expect
(
el
.
visibilityObserver
.
disconnect
).
toHaveBeenCalled
();
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment