forked from mbrevoort/jquery-facebook-multi-friend-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.facebook.multifriend.select.js
216 lines (180 loc) · 9.1 KB
/
jquery.facebook.multifriend.select.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2010 Mike Brevoort http://mike.brevoort.com @mbrevoort
//
// v1.0 jquery-facebook-multi-friend-selector
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
(function($) {
var JFMFS = function(element, options) {
var elem = $(element);
var obj = this;
var settings = $.extend({
max_selected: -1,
max_selected_message: "{0} of {1} selected"
}, options || {});
var lastSelected; // used when shift-click is performed to know where to start from to select multiple elements
// ----------+----------+----------+----------+----------+----------+----------+
// Initialization of container
// ----------+----------+----------+----------+----------+----------+----------+
elem.html(
"<div id='jfmfs-friend-selector'>" +
" <div id='jfmfs-inner-header'>" +
" <span class='jfmfs-title'>Find Friends: </span><input type='text' id='jfmfs-friend-filter-text'/>" +
" <a class='filter-link selected' id='jfmfs-filter-all' href='#'>All</a>" +
" <a class='filter-link' id='jfmfs-filter-selected' href='#'>Selected (<span id='jfmfs-selected-count'>0</span>)</a>" +
((settings.max_selected > 0) ? "<div id='jfmfs-max-selected-wrapper'></div>" : "") +
" </div>" +
" <div id='jfmfs-friend-container'></div>" +
"</div>"
);
FB.api({method: 'fql.query',
query: 'SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name'},
function(response) {
var html=[];
$.each(response, function(i, friend) {
html.push("<div class='jfmfs-friend' data-id=\"" + friend.uid +
"\"><img width=\"50\" height=\"50\" src='" + friend.pic_square +
"' /><div class='friend-name'>" + friend.name + "</div></div>");
});
$("#jfmfs-friend-container").append(html.join(''));
init();
});
// ----------+----------+----------+----------+----------+----------+----------+
// Public functions
// ----------+----------+----------+----------+----------+----------+----------+
this.getSelectedIds = function() {
var ids = [];
$.each(elem.find(".jfmfs-friend.selected"), function(i, friend) {
ids.push($(friend).attr("data-id"));
});
return ids;
};
// ----------+----------+----------+----------+----------+----------+----------+
// Private functions
// ----------+----------+----------+----------+----------+----------+----------+
var init = function() {
// handle when a friend is clicked for selection
elem.delegate(".jfmfs-friend", 'click', function(event) {
// if the element is being selected, test if the max number of items have
// already been selected, if so, just return
if(!$(this).hasClass("selected") &&
maxSelectedEnabled() &&
$(".jfmfs-friend.selected").size() >= settings.max_selected)
return;
$(this).toggleClass("selected");
$(this).removeClass("hover");
// support shift-click operations to select multiple items at a time
if( $(this).hasClass("selected")) {
if ( !lastSelected ) {
lastSelected = $(this);
}
else {
if( event.shiftKey ) {
var selIndex = $(this).index();
var lastIndex = lastSelected.index();
var end = Math.max(selIndex,lastIndex);
var start = Math.min(selIndex,lastIndex);
for(var i=start; i<=end; i++) {
var aFriend = $( $(".jfmfs-friend")[i] );
if(!aFriend.hasClass("hide-non-selected") && !aFriend.hasClass("hide-filtered"))
if( maxSelectedEnabled() && $(".jfmfs-friend.selected").size() < settings.max_selected )
$( $(".jfmfs-friend")[i] ).addClass("selected");
}
}
}
}
// keep track of last selected, this is used for the shift-select functionality
lastSelected = $(this);
// update the count of the total number selected
$("#jfmfs-selected-count").html( selectedCount() );
if( maxSelectedEnabled() ) {
updateMaxSelectedMessage();
}
});
// filter by selected, hide all non-selected
$("#jfmfs-filter-selected").click(function() {
$(".jfmfs-friend").not(".selected").addClass("hide-non-selected");
$(".filter-link").removeClass("selected");
$(this).addClass("selected");
return false;
});
// remove filter, show all
$("#jfmfs-filter-all").click(function() {
$(".jfmfs-friend").removeClass("hide-non-selected");
$(".filter-link").removeClass("selected");
$(this).addClass("selected");
return false;
});
// hover effect on friends
elem.delegate(".jfmfs-friend", 'mouseenter mouseleave', function (ev) {
if($(this).hasClass('selected')) return;
if (ev.type == 'mouseenter') {
$(this).addClass("hover");
}
if (ev.type == 'mouseleave') {
$(this).removeClass("hover");
}
});
// filter as you type
elem.find("#jfmfs-friend-filter-text")
.keyup( function() {
var filter = $(this).val();
if(filter == '') {
$(".jfmfs-friend").removeClass("hide-filtered");
}
else {
$("#jfmfs-friend-selector").find(".friend-name:not(:Contains(" + filter +"))").parent().addClass("hide-filtered");
$("#jfmfs-friend-selector").find(".friend-name:Contains(" + filter +")").parent().removeClass("hide-filtered");
}
})
.focus( function() {
if($.trim($(this).val()) == 'Start typing a name')
$(this).val('');
})
.blur(function() {
if($.trim($(this).val()) == '')
$(this).val('Start typing a name');
});
// hover states on the buttons
elem.find(".jfmfs-button").hover(
function(){ $(this).addClass("jfmfs-button-hover");} ,
function(){ $(this).removeClass("jfmfs-button-hover");}
);
updateMaxSelectedMessage();
};
var selectedCount = function() {
return $(".jfmfs-friend.selected").size();
}
var maxSelectedEnabled = function () {
return settings.max_selected > 0;
}
var updateMaxSelectedMessage = function() {
var message = settings.max_selected_message.replace("{0}", selectedCount()).replace("{1}", settings.max_selected)
$("#jfmfs-max-selected-wrapper").html( message );
}
};
$.fn.jfmfs = function(options) {
return this.each(function() {
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('jfmfs')) return;
// pass options to plugin constructor
var jfmfs = new JFMFS(this, options);
// Store plugin object in this element's data
element.data('jfmfs', jfmfs);
});
};
// todo, make this more ambiguous
jQuery.expr[':'].Contains = function(a, i, m) {
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};
})(jQuery);