-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-class.js
101 lines (85 loc) · 2.51 KB
/
js-class.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* JS Class
*/
var JSClass = new (function () {
// _ Configuration
const _config = {
app: 'JSClass',
dev:
typeof document == 'undefined'
? true
: ['local', 'test'].map((search) => {
return document.location.hostname.search(`-${search}`) >
-1;
}).includes(true),
logging: false,
api: 'IntersectionObserver',
ajax_url: '/ajax/load/data/progressive/',
};
// _ Variables
const _vars = {
};
// _ Private methods
const _private = {
// initialize
init() {
_private.log('start');
_config.logging = _config.dev;
},
// logging
log() {
if (_config.logging)
console.log(`${_config.app}:\n\n\t`, ...arguments, `\n\n`);
},
// if the required object is not available yet
// it will wait before executing callback
require(object, callback) {
const timeout = 5; // in ms
if (typeof window[object] !== 'undefined') {
callback();
} else {
_private.log('Waiting for object', object);
setTimeout(() => {
_private.require(object, callback);
}, timeout);
}
},
// IntersectionObserver API
observer() {
/**
* _ Usage: _private.observer().observe(element);
*/
return new window[_config.api]((entries, self) => {
entries.forEach((entry) => {
// this will check if element is visible in screen
if (entry.isIntersecting) {
const target = entry.target;
// stop observing element
self.unobserve(target);
// log
_private.log('isIntersecting', target);
}
});
});
},
helloWorld() {
_private.log('Hello, World!')
},
};
// _ Public methods
const _public = {
// exposing vars and private methods for debugging
debug: _config.dev
? {
_vars: _vars,
_private: _private,
}
: false,
};
// _ Call stack
return (() => {
_private.init();
_private.helloWorld();
return _public;
})();
})();