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
/
Copy pathjquery.treevue.import.js
79 lines (67 loc) · 2.83 KB
/
jquery.treevue.import.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
(function ($) {
'use strict';
var namespace,
nodeNum = 0; // A number used to give each checkbox a unique ID
/**
* Create a new li node for in a treevue
*
* @param data The descriptive data for the node
* @return Node A jquery object containing the new li node
*/
function createTreeNode(data) {
var box, boxId, ulNode,
liNode = $('<li></li>');
if (data.type) {
liNode.attr('data-treevue-type', data.type);
}
// Add a checkbox and label:
if (data.selectable === true || (data.selectable !== false &&
(data.selected || data.disabled || data.subselector))) {
// Give each node a unique ID
boxId = namespace + (data.id || "treevue-node-" + (nodeNum += 1));
box = $('<input type="checkbox" />').appendTo(liNode).
prop('checked', data.selected).
prop('disabled', data.disabled).
attr('value', data.value).
attr('id', boxId);
if (data.subselector) {
box.attr('data-type', 'subselector');
}
$('<label />').text(data.label).attr('for', boxId).appendTo(liNode);
} else { // No checkbox, just add text
liNode.text(data.label);
}
if ($.isArray(data.children) && data.children.length > 0) {
// Create a ul element with it's children
ulNode = $('<ul/>').appendTo(liNode).
append($.map(data.children, createTreeNode));
if (data.collapsed) {
ulNode.attr('aria-hidden', true);
}
}
return liNode;
}
/**
* Create a treevue based on a json object
*
* The json contains an array with objects, where each object represetns
* a node in the tree. The following properties are allowed for each node
* label: string The text label for the tree
* value: mixed The value set for the checkbox
* id: string The id for the item (the checkbox)
* selected: bool Is the item selected
* disabled: bool Is the item disabled
* subselector: bool Is the item a subselector
* collapsed: bool Is the item collapsed by default
* children: array An array containing child nodes
*
* @param Array An array containing node objects for treevue
* @return Node A treevue ul element
*/
$.treevue = function (json, ns, options) {
namespace = (ns ? ns + '-' : '');
return $('<ul />').
append($.map(json, createTreeNode)).
treevue(options);
};
}(jQuery));