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
a192dc7e
Commit
a192dc7e
authored
Nov 22, 2021
by
Thomas Randolph
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add IntersectionObserver utilities
parent
85390301
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
0 deletions
+114
-0
app/assets/javascripts/lib/utils/intersection_observer.js
app/assets/javascripts/lib/utils/intersection_observer.js
+28
-0
spec/frontend/lib/utils/intersection_observer_spec.js
spec/frontend/lib/utils/intersection_observer_spec.js
+86
-0
No files found.
app/assets/javascripts/lib/utils/intersection_observer.js
0 → 100644
View file @
a192dc7e
import
{
memoize
}
from
'
lodash
'
;
import
{
uuids
}
from
'
./uuids
'
;
export
const
create
=
memoize
((
options
=
{})
=>
{
const
id
=
uuids
()[
0
];
return
{
id
,
observer
:
new
IntersectionObserver
((
entries
)
=>
{
entries
.
forEach
((
entry
)
=>
{
entry
.
target
.
dispatchEvent
(
new
CustomEvent
(
`IntersectionUpdate`
,
{
detail
:
{
entry
,
observer
:
id
}
}),
);
if
(
entry
.
isIntersecting
)
{
entry
.
target
.
dispatchEvent
(
new
CustomEvent
(
`IntersectionAppear`
,
{
detail
:
{
observer
:
id
}
}),
);
}
else
{
entry
.
target
.
dispatchEvent
(
new
CustomEvent
(
`IntersectionDisappear`
,
{
detail
:
{
observer
:
id
}
}),
);
}
});
},
options
),
};
});
spec/frontend/lib/utils/intersection_observer_spec.js
0 → 100644
View file @
a192dc7e
import
{
create
}
from
'
~/lib/utils/intersection_observer
'
;
describe
(
'
IntersectionObserver Utility
'
,
()
=>
{
beforeAll
(()
=>
{
global
.
IntersectionObserver
=
class
MockIntersectionObserver
{
constructor
(
callback
)
{
this
.
callback
=
callback
;
this
.
entries
=
[];
}
addEntry
(
entry
)
{
this
.
entries
.
push
(
entry
);
}
trigger
()
{
this
.
callback
(
this
.
entries
);
}
};
});
describe
(
'
create
'
,
()
=>
{
describe
(
'
memoization
'
,
()
=>
{
const
options
=
{
rootMargin
:
'
1px 1px 1px 1px
'
};
let
expectedOutput
;
beforeEach
(()
=>
{
create
.
cache
.
clear
();
expectedOutput
=
create
(
options
);
});
it
(
'
returns the same Observer for the same options input
'
,
()
=>
{
expect
(
expectedOutput
.
id
).
toBe
(
create
(
options
).
id
);
});
it
(
'
creates a new Observer for unique input options
'
,
()
=>
{
expect
(
expectedOutput
.
id
).
not
.
toBe
(
create
({
rootMargin
:
'
1px 2px 3px 4px
'
}));
});
it
(
'
creates a new Observer for the same input options in different object references
'
,
()
=>
{
expect
(
expectedOutput
.
id
).
not
.
toBe
(
create
({
rootMargin
:
'
1px 1px 1px 1px
'
}));
});
});
});
describe
(
'
Observer behavior
'
,
()
=>
{
let
observer
=
null
;
let
id
=
null
;
beforeEach
(()
=>
{
create
.
cache
.
clear
();
({
observer
,
id
}
=
create
());
});
it
.
each
`
isIntersecting | event
${
false
}
|
${
'
IntersectionDisappear
'
}
${
true
}
|
${
'
IntersectionAppear
'
}
`
(
'
should emit the correct event on the entry target based on the computed Intersection
'
,
async
({
isIntersecting
,
event
})
=>
{
const
target
=
document
.
createElement
(
'
div
'
);
observer
.
addEntry
({
target
,
isIntersecting
});
target
.
addEventListener
(
event
,
(
e
)
=>
{
expect
(
e
.
detail
.
observer
).
toBe
(
id
);
});
observer
.
trigger
();
},
);
it
(
'
should always emit an Update event with the entry and the observer
'
,
()
=>
{
const
target
=
document
.
createElement
(
'
div
'
);
const
entry
=
{
target
};
observer
.
addEntry
(
entry
);
target
.
addEventListener
(
'
IntersectionUpdate
'
,
(
e
)
=>
{
expect
(
e
.
detail
.
observer
).
toBe
(
id
);
expect
(
e
.
detail
.
entry
).
toStrictEqual
(
entry
);
});
observer
.
trigger
();
});
});
});
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