Skip to content

Commit 9d8eca6

Browse files
committed
Add useful jquery extensions
- TODO: integrate more, migrate other helpers towards this?
1 parent 3d6960e commit 9d8eca6

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

docs/api_jquery_ext.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
JS API: JQuery Extensions
2+
=========================
3+
4+
.. code-block:: html
5+
6+
<script src="/networktables/jquery_ext.js"></script>
7+
8+
.. note:: These functions require `jQuery <http://jquery.com/>`_ to be
9+
loaded first!
10+
11+
.. js:function:: $.nt_toggle(key, function)
12+
13+
When a networktables variable changes, a checkbox element will be updated
14+
when a NT variable changes and when a user clicks it.
15+
16+
Alternatively, you can use this with custom elements, by providing a function
17+
that will be called only when the NT value is changed. The NT value will be
18+
toggled when the user clicks the selected element(s).
19+
20+
:param k: Networktables key
21+
:param fn: (optional) function that accepts a single param, will be called on change
22+
:param evt: (optional) Which event to toggle the value on (defaults to 'click')
23+
24+
Example usage:
25+
26+
.. code-block:: javascript
27+
28+
// this works on a checkbox
29+
$('#my_checkbox').nt_toggle('/SmartDashboard/some_boolean');
30+
31+
// or on a clickable element
32+
$('#my_clickable').nt_toggle('/SmartDashboard/b', function(v) {
33+
this.css('background-color', v ? 'green' : 'gray');
34+
});

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:maxdepth: 4
66

77
api_js
8+
api_jquery_ext
89
api_util
910
api_camera
1011
troubleshooting
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
if ($ === undefined) {
4+
alert("jQuery must be downloaded+included to use jquery_ext.js!");
5+
}
6+
7+
8+
/**
9+
$.nt_toggle(key, function)
10+
11+
When a networktables variable changes, a checkbox element will be updated
12+
when a NT variable changes and when a user clicks it.
13+
14+
Alternatively, you can use this with custom elements, by providing a function
15+
that will be called only when the NT value is changed. The NT value will be
16+
toggled when the user clicks the selected element(s).
17+
18+
:param k: Networktables key
19+
:param fn: (optional) function that accepts a single param, will be called on change
20+
:param evt: (optional) Which event to toggle the value on (defaults to 'click')
21+
22+
*/
23+
$.fn.extend({
24+
nt_toggle: function(k, fn, evt) {
25+
26+
if (fn == null) {
27+
evt = 'change';
28+
fn = function(v) {
29+
// by default, assume that it's a checkbox
30+
$(this).each(function() {
31+
$(this).prop('checked', v);
32+
});
33+
}
34+
}
35+
36+
fn = fn.bind(this);
37+
38+
if (evt == null)
39+
evt = 'click';
40+
41+
// only call the function when the key changes -- not when the user
42+
// clicks it (this allows simultaneous pages to function correctly)
43+
NetworkTables.addKeyListener(k, function(k, v) {
44+
fn(v);
45+
}, true);
46+
47+
return this.each(function() {
48+
$(this).on(evt, function() {
49+
NetworkTables.setValue(k, NetworkTables.getValue(k) ? false : true);
50+
});
51+
});
52+
}
53+
});

0 commit comments

Comments
 (0)