-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.listview.js
181 lines (153 loc) · 4.69 KB
/
jquery.listview.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* jQuery ListView plugin v1.0 beta (not even there yet !)
*
* Copyright 2012, Marco Leong
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function($){
$.fn.extend({
settings: {
formHiddenInputField: '<input id="listview-selected-hidden-field" type="hidden">',
searchable: true
},
listView: function(options) {
this.initialize(options);
},
initialize: function(options){
this.settings = (options) ? (this.settings || options): this.settings;
$(this).wrap('<div class="nestable-container"></div>');
//initialize left property
$(this).addClass('nestable-view');
if(this.settings.searchable) {
$(this).before('<input style="width:100%;"type="text" id="listview-search-field" />');
}
$(this).after(this.settings.formHiddenInputField);
var that = this;
$("#listview-search-field").keyup(function(event){
if (event.which == 13) {
event.preventDefault();
}
var results = that.findNode($(this).val());
$('.nestable-view').hide();
// $(this).
if($('#listview-search-results').length < 1){
$(this).after('<ul id="listview-search-results"></ul>');
}
var resultsItem = $.map(results, function(item){
return "<li style='display:block'><a href='#'>"+item+"</a></li>";
});
// console.log(resultsItem);
$("#listview-search-results").html('');
$.each(resultsItem, function(i,el){
$("#listview-search-results").append($(el));
});
$("#listview-search-results > li").each(function(i,el){
$(el).children("a").click(function(e){
var value = that.getTextInNode(this);
$("#listview-selected-hidden-field").val(value);
e.preventDefault();
});
});
});
var lvl = 0;
this.addProperty($(this), lvl);
this.bindClickEvents($(this));
},
addProperty: function(ele,lvl){
var that = this;
//check if it have element to work with
if(ele.length > 0){
ele.each(function(i,ele){
// ul
$(ele).attr('data-level', lvl);
});
// all left except the first layer.
if(lvl !== 0){
$(ele).css({'left':'100%'});
}else{
$(ele).css({'left':'0px'});
}
lvl += 1;
this.deepness = lvl;
// recursively add for every ul element
this.addProperty($(ele).children().children(),lvl);
}else{
return false;
}
},
bindClickEvents: function(ele){
var root = $(this);
//check is there any node need to bind.
if(ele.children().length > 0){
// add a "Parent" button.
$(ele).each(function(i,ele){
if($(ele).data('level') !== 0) {
$(ele).children().first().before('<li class="parent-btn">Parent</li>');
var pbtn = $(ele).children().first();
$(pbtn).click(function(e){
$(root).animate({'left':'+=100%'});
$(pbtn).parent().css({'z-index': '1'});
e.stopPropagation();
});
}
});
var that = this;
//for each children of an ul, means the li
$(ele).children().each(function(i,ele){
$(ele).click(function(e){
if($(ele).children().children().length > 0 && !$(ele).is('.parent-btn') ){
var value = that.getTextInNode(ele);
$("#listview-selected-hidden-field").val(value);
}
e.stopPropagation();
});
if( !$(ele).is('.parent-btn') ){
//use the arrow button to navigate
if($(ele).children().length > 0 ){
$(ele).append("<i class='icon-chevron-right icon-white'></i>");
}
$(ele).children('i').click(function(){
if($(ele).children().children().length > 0 ){
$(root).animate({'left':'-=100%'});
$(ele).children().css({'z-index':'1'});
$(ele).siblings().children().css({'z-index':'-1'});
}
});
}
});
// recursively bind click event.
this.bindClickEvents($(ele).children().children());
}else{
return false;
}
},
showNode: function(nodes){
},
findNode: function(value){
var that = this;
// var items = $(this).find("li:contains('"+value+"')").not(".parent-btn").not('ul');
var items = $(this).find("li").not(".parent-btn").not('ul');
var isMatch = function(value, node) {
value = value.toLowerCase();
return that.getTextInNode(node).toLowerCase().indexOf(value) != -1;
// return node.innerHTML.toLowerCase().indexOf(value) > 0;
};
// filter with regular expression
var filtered = new Array(0);
for (var i = items.length - 1; i >= 0; i--) {
if(isMatch(value, items[i])) {
filtered.push(items[i]);
}
}
items = filtered;
var nodes_value = $.map($(items), function(item){
return that.getTextInNode(item);
});
return nodes_value;
},
getTextInNode : function(elem) {
return $.trim(elem.firstChild.nodeValue);
}
});
})($);