-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
145 lines (131 loc) · 3.66 KB
/
utils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function debounce (func, wait, immediate) {
var timeout
return function () {
var context = this
var args = arguments
var later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
};
function throttle (func, wait, options) {
var timeout, context, args
var previous = 0
if (!options) options = {}
var later = function () {
previous = options.leading === false ? 0 : new Date().getTime()
timeout = null
func.apply(context, args)
if (!timeout) context = args = null
}
var throttled = function () {
var now = new Date().getTime()
if (!previous && options.leading === false) previous = now
var remaining = wait - (now - previous)
context = this
args = arguments
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
previous = now
func.apply(context, args)
if (!timeout) context = args = null
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
}
return throttled
}
function sidebarPaddingR () {
var innerWidth = window.innerWidth
var clientWidth = document.body.clientWidth
var paddingRight = innerWidth - clientWidth
if (innerWidth !== clientWidth) {
$('body').css('padding-right', paddingRight)
}
}
// iPadOS
function isIpad () {
return navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1
}
function isTMobile () {
var ua = navigator.userAgent
var pa = /iPad|iPhone|iPod|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP|IEMobile|Symbian/g
return window.screen.width < 992 && pa.test(ua)
}
function isMobile () {
return this.isIpad() || this.isTMobile()
}
function isDesktop () {
return !this.isMobile()
}
function scrollToDest (name, offset = 0) {
var scrollOffset = $(name).offset()
$('body,html').animate({
scrollTop: scrollOffset.top - offset
})
};
function loadScript (url, callback) {
var script = document.createElement('script')
script.type = 'text/javascript'
if (script.readyState) { // IE
script.onreadystatechange = function () {
if (script.readyState === 'loaded' ||
script.readyState === 'complete') {
script.onreadystatechange = null
callback()
}
}
} else { // Others
script.onload = function () {
callback()
}
}
script.src = url
document.body.appendChild(script)
};
function snackbarShow (text, showAction, duration) {
var a = (typeof showAction !== 'undefined') ? showAction : false
var d = (typeof duration !== 'undefined') ? duration : 2000
var position = GLOBAL_CONFIG.Snackbar.position
var bg = document.documentElement.getAttribute('data-theme') === 'light' ? GLOBAL_CONFIG.Snackbar.bgLight : GLOBAL_CONFIG.Snackbar.bgDark
Snackbar.show({
text: text,
backgroundColor: bg,
showAction: a,
duration: d,
pos: position
})
}
const Cookies = {
get: function (name) {
const value = `; ${document.cookie}`
const parts = value.split(`; ${name}=`)
if (parts.length === 2) return parts.pop().split(';').shift()
},
set: function (name, value, days) {
var expires = ''
if (days) {
var date = new Date()
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000))
expires = '; expires=' + date.toUTCString()
}
document.cookie = name + '=' + (value || '') + expires + '; path=/'
}
}
/**
* lazyload
*/
if (GLOBAL_CONFIG.islazyload) {
window.lazyLoadOptions = {
elements_selector: 'img',
threshold: 0
}
}