added realtime search to clients table

pull/567/head
Justin Richer 2014-03-17 22:48:40 -04:00
parent 762fe11ab1
commit 0e343b936d
2 changed files with 60 additions and 20 deletions

View File

@ -93,7 +93,22 @@ var ClientModel = Backbone.Model.extend({
requireClientSecret: true, requireClientSecret: true,
}, },
urlRoot:"api/clients" urlRoot:"api/clients",
matches:function(term) {
if (term) {
return (this.get('clientId').toLowerCase().indexOf(term.toLowerCase()) != -1)
|| (this.get('clientName') != null && this.get('clientName').toLowerCase().indexOf(term.toLowerCase()) != -1)
|| (this.get('clientDescription') != null && this.get('clientDescription').toLowerCase().indexOf(term.toLowerCase()) != -1)
|| (this.get('clientUri') != null && this.get('clientUri').toLowerCase().indexOf(term.toLowerCase()) != -1)
|| (this.get('contacts') != null && _.filter(this.get('contacts'), function(item) {
return item.toLowerCase().indexOf(term.toLowerCase()) != -1;
}));
} else {
return true;
}
}
}); });
@ -236,6 +251,7 @@ var ClientListView = Backbone.View.extend({
tagName: 'span', tagName: 'span',
initialize:function () { initialize:function () {
this.filteredModel = this.model;
}, },
load:function(callback) { load:function(callback) {
@ -267,7 +283,8 @@ var ClientListView = Backbone.View.extend({
events:{ events:{
"click .new-client":"newClient", "click .new-client":"newClient",
"click .refresh-table":"refreshTable" "click .refresh-table":"refreshTable",
'keyup .search-query':'searchTable'
}, },
newClient:function (e) { newClient:function (e) {
@ -280,10 +297,15 @@ var ClientListView = Backbone.View.extend({
// append and render table structure // append and render table structure
$(this.el).html($('#tmpl-client-table').html()); $(this.el).html($('#tmpl-client-table').html());
this.renderInner();
var whiteList = this.options.whiteListList.getByClientId(this.model.get('clientId')); return this;
},
_.each(this.model.models, function (client) {
renderInner:function(eventName) {
_.each(this.filteredModel.models, function (client) {
$("#client-table",this.el).append( $("#client-table",this.el).append(
new ClientView({ new ClientView({
model:client, model:client,
@ -293,13 +315,11 @@ var ClientListView = Backbone.View.extend({
}).render().el); }).render().el);
}, this); }, this);
this.togglePlaceholder(); this.togglePlaceholder();
return this;
}, },
togglePlaceholder:function() { togglePlaceholder:function() {
if (this.model.length > 0) { if (this.filteredModel.length > 0) {
$('#client-table', this.el).show(); $('#client-table', this.el).show();
$('#client-table-empty', this.el).hide(); $('#client-table-empty', this.el).hide();
} else { } else {
@ -326,6 +346,30 @@ var ClientListView = Backbone.View.extend({
$('#loadingbox').sheet('hide'); $('#loadingbox').sheet('hide');
_self.render(); _self.render();
}); });
},
searchTable:function(e) {
var term = $('.search-query', this.el).val();
if (term) {
this.filteredModel = new ClientCollection(this.model.filter(function(client) {
return client.matches(term);
}));
} else {
this.filteredModel = this.model;
}
console.log(this.filteredModel);
// clear out the table
$('tbody', this.el).html('');
// re-render the table
this.renderInner();
//$('.search-query', this.el).val(term);
//$('.search-query', this.el).focus();
} }
}); });
@ -371,13 +415,11 @@ var ClientFormView = Backbone.View.extend({
"change #logoUri input":"previewLogo" "change #logoUri input":"previewLogo"
}, },
toggleRefreshTokenTimeout:function (e) { toggleRefreshTokenTimeout:function () {
e.preventDefault();
$("#refreshTokenValidityTime", this.$el).toggle(); $("#refreshTokenValidityTime", this.$el).toggle();
}, },
previewLogo:function(e) { previewLogo:function() {
e.preventDefault();
if ($('#logoUri input', this.el).val()) { if ($('#logoUri input', this.el).val()) {
$('#logoPreview', this.el).empty(); $('#logoPreview', this.el).empty();
$('#logoPreview', this.el).attr('src', $('#logoUri input').val()); $('#logoPreview', this.el).attr('src', $('#logoUri input').val());
@ -390,8 +432,7 @@ var ClientFormView = Backbone.View.extend({
* Set up the form based on the current state of the requireClientSecret checkbox parameter * Set up the form based on the current state of the requireClientSecret checkbox parameter
* @param event * @param event
*/ */
toggleRequireClientSecret:function(e) { toggleRequireClientSecret:function() {
e.preventDefault();
if ($('#requireClientSecret input', this.el).is(':checked')) { if ($('#requireClientSecret input', this.el).is(':checked')) {
// client secret is required, show all the bits // client secret is required, show all the bits
@ -408,8 +449,7 @@ var ClientFormView = Backbone.View.extend({
* Set up the form based on the "Generate" checkbox * Set up the form based on the "Generate" checkbox
* @param event * @param event
*/ */
toggleGenerateClientSecret:function(e) { toggleGenerateClientSecret:function() {
e.preventDefault();
if ($('#generateClientSecret input', this.el).is(':checked')) { if ($('#generateClientSecret input', this.el).is(':checked')) {
// show the "generated" block, hide the "display" checkbox // show the "generated" block, hide the "display" checkbox
@ -420,7 +460,7 @@ var ClientFormView = Backbone.View.extend({
} else { } else {
// show the display checkbox, fall back to the "display" logic // show the display checkbox, fall back to the "display" logic
$('#displayClientSecret', this.el).show(); $('#displayClientSecret', this.el).show();
this.toggleDisplayClientSecret(e); this.toggleDisplayClientSecret();
} }
}, },
@ -428,8 +468,7 @@ var ClientFormView = Backbone.View.extend({
* Handle whether or not to display the client secret * Handle whether or not to display the client secret
* @param event * @param event
*/ */
toggleDisplayClientSecret:function(e) { toggleDisplayClientSecret:function() {
e.preventDefault();
if ($('#displayClientSecret input').is(':checked')) { if ($('#displayClientSecret input').is(':checked')) {
// want to display it // want to display it

View File

@ -84,6 +84,7 @@
<div class="well well-small"> <div class="well well-small">
<button class="btn btn-small refresh-table"><i class="icon-refresh"></i> Refresh</button> &nbsp; <button class="btn btn-small refresh-table"><i class="icon-refresh"></i> Refresh</button> &nbsp;
<button class="btn btn-small btn-primary new-client"><i class="icon-plus icon-white"></i> New Client</button> <button class="btn btn-small btn-primary new-client"><i class="icon-plus icon-white"></i> New Client</button>
<input type="text" class="search-query pull-right" placeholder="Search...">
</div> </div>
<div id="client-table-empty" class="alert alert-info"> <div id="client-table-empty" class="alert alert-info">