forked from simonexmachina/jquery-json-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.json-list.js
118 lines (116 loc) · 3.37 KB
/
jquery.json-list.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
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
(function($) {
$.fn.jsonList = function( options ) {
return this.each(function() {
new JSONList().init(this, options);
});
};
var JSONList = function() {};
JSONList.prototype = {
init: function( el, options ) {
this.el = $(el);
// fill options with default values
this.options = options = $.extend({
type: 'groupedItems',
url: null,
data: null,
groupLabel: 'name',
itemLabel: 'name',
onSuccess: function( jsonList ) {},
onListItem: function( listItem, data, isGroup ) {},
onResponse: function( data, textStatus ) {}
}, options);
var self = this;
$(this)
.bind('success', this.options.onSuccess)
.bind('listItem', this.options.onListItem)
.bind('response', this.options.onResponse);
$.getJSON(options.url, options.data, function(data, textStatus) {
self.handleResponse(data, textStatus);
});
},
handleResponse: function( data, textStatus ) {
$(this).trigger('response', [data, textStatus]);
if( this.options.type == 'groupedItems' ) {
this.createListForGroupedItems(data, textStatus);
}
$(this).trigger('success', [this.el]);
},
createListForGroupedItems: function( data, textStatus ) {
var opts = $.extend({
groups: 'groups',
items: 'items'
}, this.options),
groups = {},
items = {};
this.groups = groups;
this.items = items;
$.each(data[opts.items], function(i, item) {
items[item.id] = item;
});
$.each(data[opts.groups], function(i, group) {
var children = [];
if( group.children ) {
$.each(group.children, function(i, childId) {
children.push(items[childId]);
});
}
group.children = children;
group.isSub = false;
groups[group.id] = group;
});
$.each(groups, function(id, group) {
var subGroups = [];
if( group.subGroups ) {
$.each(group.subGroups, function(i, childId) {
subGroups.push(groups[childId]);
//delete groups[childId]; -- instead of delete, add information that is subgrup
groups[childId].isSub = true;
});
}
group.subGroups = subGroups;
});
this.createList(groups);
},
createList: function( groups ) {
if( this.el.is('ol, ul') )
this.appendGroupItems(groups, this.el);
else
this.el.append(this.appendGroupItems(groups, $('<ol>'), false));
},
appendGroupItems: function( groups, list, ignoreVerification ) {
var self = this;
$.each(groups, function(id, group) {
if (!group.isSub || ignoreVerification) {
var listItem = $('<li>' + self.getGroupLabel(group) + '</li>');
$(self).trigger('listItem', [listItem, group, true]);
if( group.subGroups || group.children ) {
var subList = $('<ol>');
if( group.subGroups ) {
self.appendGroupItems(group.subGroups, subList, true);
}
if( group.children ) {
self.appendItems(group.children, subList);
}
listItem.append(subList);
}
list.append(listItem);
}
});
return list;
},
appendItems: function( items, list ) {
var self = this;
$.each(items, function(id, item) {
var listItem = $('<li>' + self.getItemLabel(item) + '</li>');
$(self).trigger('listItem', [listItem, item, false]);
list.append(listItem);
});
},
getGroupLabel: function( group ) {
return group[this.options.groupLabel];
},
getItemLabel: function( item ) {
return item[this.options.itemLabel];
}
};
}(jQuery));