aboutsummaryrefslogtreecommitdiff
path: root/doc/ref/javascript/lib.texi
blob: e5b133831dc0c0094866a8ba43696efe02192506 (plain)
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

@node lib
@subsection lib.js

General procedures which in theory could be used anywhere.


@node Default prototype extensions
@subsubsection Default prototype extensions

HTMLElement extensions

@defmethod HTMLElement addEventListener name proc
Replace the default @code{addEventListener} with a version that stores
all listeners in the dictionary @var{listeners}.
@end defmethod

@defivar HTMLElement listeners
Dictionary of all registered listeners to this element.
Keys are taken from @code{addEventListener}.
@end defivar

@defmethod DOMTokenList find regexp
Finds the first element of the DOMTokenList whichs value matches
the supplied regexp. Returns a pair of the index and the value.
@end defmethod

@defmethod Object format args ...
Returns a string representation of the given object.
Allows extending for custom types,
@ref{date-format}
@end defmethod

@node General
@subsubsection General

@defun zip args ...
Takes a list of lists, and returns a single list of tuples.
@example
» zip([1,2,3,4,5], "Hello")
← [[1,'H'],[2,'e'],[3,'l'],[4,'l'],[5,'o']]
@end example
@end defun

@defun makeElement name [attr=@{@}]
Creates a new DOM element of type @var{name}, with all keys in
@var{attr} transfered to it. For example, the equivalent of
@example
<input type='number'/>
@end example
would be
@verbatim
values.push(makeElement('input', {
    type: 'number',
}));
@end verbatim
.
@end defun

@defun round_time time fraction
TODO
@end defun

@defun date_to_percent date
Retuns how far along the date specified by @var{date} is, between 0
and 100, where 00:00 maps to 0, and 23:59 to ~100.
@end defun

@defun gensym [pxrefix='gensym']
Generates a new string which is (hopefully) globally unique.
Compare with @code{gensym} from Lisp.
@end defun

@defun asList thing
Ensures that @var{thing} is a list. Returning it outright if it
already is one, otherwise wrapping it in a list.
@end defun

@node Date
@subsubsection Date

Some extensions to the builtin class ``Date'' is made.

@defivar Date utc
Boolean indicating if the given timestamp is in UTC or local time.
true means UTC.
@end defivar

@defivar Date dateonly
Boolean indicating if the time component of the Date object should be disregarded.
@end defivar

@defun parseDate str
Takes a string @var{str}, which should be in ISO-8601 date-format, and
returns a javascript Date object.
Handles date-times, with and without seconds, trailing `Z' for
time-zones, and dates without times.
If no time is given the @code{dateonly} attribute is set to yes.
@end defun

@defun copyDate date
Creates a new instance of the given Date @var{date}, also transfers my
custom fields.
@end defun

@defun to_local date
@anchor{to_local}
Returns a Date object (which may be new) which is guaranteed in local
time.
This means that the @var{utc} field is @code{false}, and that
@code{to_local(current_time())} should show what your wall-clock shows.
@end defun

@defmethod Date format str args ...
@anchor{date-format}
Formats a Date object according to the format specification @var{str}.
Keeping with Guile each format specifier starts with a ~.

@c table formatting borrowed from Gulie Reference (SRFI-19 Date to string)
@multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
@item @nicode{~~} @tab literal ~
@c Almost all fields are left padded. How do I signify this
@c with a single footnote?
@item @nicode{~Y} @tab year, left-padding with zeroes.
@item @nicode{~m} @tab month number, left padded with zeroes.
@item @nicode{~d} @tab day of month.
@item @nicode{~H} @tab hour
@item @nicode{~M} @tab minute
@item @nicode{~S} @tab second
@item @nicode{~Z} @tab 'Z' if Date is UTC, otherwise nothing

@item @nicode{~L} @tab Converts the date to local time
(@pxref{to_local}) (doesn't modify source object). Outputs nothing
@end multitable
@end defmethod

@defun format_date date str
Equivalent to @code{(@var{date}).format(@var{str})}.
@c TODO link
@end defun