This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.treevue.export.js
59 lines (50 loc) · 1.88 KB
/
jquery.treevue.export.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
(function ($) {
'use strict';
var collapseCls = 'treevue-collapsed',
disableCls = 'treevue-disabled';
function treenodeToJson(node) {
var children, checkbox, type,
obj = {};
node = $(node);
checkbox = node.find(':checkbox:first()');
children = node.find('ul:first() > li, ol:first() > li');
type = node.attr('data-treevue-type');
if (type) {
obj.type = type;
}
if (children.length > 0) {
obj.children = $.map(children, treenodeToJson);
if (node.hasClass(collapseCls)) {
obj.collapsed = true;
}
}
// Check if it has a checkbox
if (checkbox.closest('li').is(node)) {
if (/treevue-node-/.test(checkbox.attr('id')) === false) {
obj.id = checkbox.attr('id');
}
obj.selected = checkbox.prop('checked');
$.each({ // set optional properties only if truthy
disabled: node.hasClass(disableCls),
value: checkbox.attr('value'),
subselector: (checkbox.attr('data-type') === 'subselector'),
label: $.trim(node.find('label:first()').text())
}, function (key, value) {
if (value) {
obj[key] = value;
}
});
} else { // Label when there is no checkbox
obj.label = $.trim(node.clone().
find('ul, ol, [aria-hidden]').remove().end().
text());
}
return obj;
}
$.fn.treevueJson = function () {
if (!this.first().is('.treevue')) {
throw new Error('This node is not a treevue');
}
return $.map(this.first().children(), treenodeToJson);
};
}(jQuery));