-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_template.mjs
53 lines (48 loc) · 1.99 KB
/
_template.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Module template - use this as a boilerplate for writing your own modules. Then import them into your frontend by
* assigning them to a node using the data-requires attribute.
*
* E.g. save it as myfirstmodule.mjs, and then add it with <div data-requires="myfirstmodule.mjs">
* (after following the basic usage instructions @ https://github.com/c-kick/js-modules?tab=readme-ov-file#basic-usage)
*/
/**
* Imports, for example purposes
*/
import {isVisible} from "./hnl.helpers.mjs"; //a function
import eventHandler from "./hnl.eventhandler.mjs"; //a class
import {hnlLogger} from "./hnl.logger.mjs"; //an object
/**
* The name for this module, used in logging and identifying dynamically loaded modules
* @type {string}
*/
export const NAME = 'exampleModule';
/**
* init
* Exported function that is called (if present) when the module has been imported via the data-requires method,
* as described in, and handled by, the hnl.dynamicimports module.
* @param elements {object} Holds *all* DOM elements that had 'data-requires' specified for this module
* 'this' will be the module object context
*/
export function init(elements){
/**
* Do stuff here. You can safely assume the page is ready now, as the importing of dynamically loaded modules depends
* on reading data-attributes, which can only be safely traversed and read then the page is ready,
* as handled in the 'docReady' handler of the eventHandler module.
*/
/**
example function that takes all the elements (those with 'data-requires' set for this module) and checks if they
are visible on each scroll/resize. This is useful for doing fancy things like fading-in/-up elements as soon as
they enter the user's view.
@uses docShift
@uses isVisible
*/
eventHandler.addListener('docShift', () => {
elements.forEach(function(element){
isVisible(element, function(visible) {
if (visible) {
hnlLogger.log(NAME, `${element} visible? ${(visible ? 'Yes' : 'No')}`);
}
})
})
});
}