mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-03-01 22:50:02 -05:00
57 lines
1.9 KiB
Plaintext
57 lines
1.9 KiB
Plaintext
# vim:fileencoding=utf-8
|
|
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
def debounce(func, wait, immediate=False):
|
|
# Returns a function, that, as long as it continues to be invoked, will not
|
|
# be triggered. The function will be called after it stops being called for
|
|
# N milliseconds. If `immediate` is True, trigger the function on the
|
|
# leading edge, instead of the trailing.
|
|
timeout = None
|
|
return def debounce_inner(): # noqa: unused-local
|
|
nonlocal timeout
|
|
context, args = this, arguments
|
|
def later():
|
|
nonlocal timeout
|
|
timeout = None
|
|
if not immediate:
|
|
func.apply(context, args)
|
|
call_now = immediate and not timeout
|
|
window.clearTimeout(timeout)
|
|
timeout = window.setTimeout(later, wait)
|
|
if call_now:
|
|
func.apply(context, args)
|
|
|
|
def to_utf8(string):
|
|
if type(TextEncoder) == 'function':
|
|
return TextEncoder('utf-8').encode(string)
|
|
escstr = encodeURIComponent(string)
|
|
binstr = escstr.replace(/%([0-9A-F]{2})/g, def(match, p1):
|
|
return String.fromCharCode('0x' + p1)
|
|
)
|
|
ua = Uint8Array(binstr.length)
|
|
Array.prototype.forEach.call(binstr, def(ch, i):
|
|
ua[i] = ch.charCodeAt(0)
|
|
)
|
|
return ua
|
|
|
|
def parse_url_params(url=None, allow_multiple=False):
|
|
url = url or window.location.href
|
|
qs = url.indexOf('?')
|
|
ans = {}
|
|
if qs < 0:
|
|
return ans
|
|
q = url.slice(qs + 1, ((url.indexOf('#') + 1) or (url.length + 1)))
|
|
if not q:
|
|
return ans
|
|
pairs = q.replace(/\+/g, " ").split("&")
|
|
for pair in pairs:
|
|
key, val = str.partition(pair, '=')[::2]
|
|
key, val = decodeURIComponent(key), decodeURIComponent(val)
|
|
if allow_multiple:
|
|
if not Object.prototype.hasOwnProperty.call(ans, key):
|
|
ans[key] = []
|
|
ans[key].append(val)
|
|
else:
|
|
ans[key] = val
|
|
return ans
|