Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #279 - Persistent DataTable properties attribute. #281

Merged
merged 13 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ src-gen
/target-grunt/
/gradleResources/.nb-gradle/
.DS_Store
.gradle
.gradle
.idea/
*.iml
87 changes: 75 additions & 12 deletions src/main/java/net/bootsfaces/component/dataTable/DataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

package net.bootsfaces.component.dataTable;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import net.bootsfaces.component.AttributeMapWrapper;
import net.bootsfaces.component.ajax.IAJAXComponent;
import net.bootsfaces.render.Tooltip;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIData;
import javax.faces.component.behavior.ClientBehaviorHolder;

import net.bootsfaces.component.AttributeMapWrapper;
import net.bootsfaces.component.ajax.IAJAXComponent;
import net.bootsfaces.render.Tooltip;
import javax.faces.context.FacesContext;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/** This class holds the attributes of <b:dataTable />. */
@ResourceDependencies({ @ResourceDependency(library = "bsf", name = "css/core.css", target = "head"),
Expand All @@ -55,7 +54,54 @@ public class DataTable extends UIData implements IAJAXComponent, ClientBehaviorH
"dblclick", "dragstart", "dragover", "drop", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"));

private Map<String, Object> attributes;


public enum DataTablePropertyType
{
pageLength, searchTerm, currentPage
}

@Override
public void decode( FacesContext context )
{
super.decode( context );
Map<DataTablePropertyType, Object> dataTableProperties = getDataTableProperties();
if ( dataTableProperties != null )
{
String params = context.getExternalContext().getRequestParameterMap().get( "params" );
if ( params != null )
{
params = params.replace( "BsFEvent=", "" );
String[] paramArray = params.split( "," );
for ( String keyValuePair : paramArray )
{
String[] pair = keyValuePair.split( ":", 2 );
String key = pair[ 0 ];
Object value = null;
if ( pair.length == 2 )
{
value = pair[ 1 ];
}
if ( value != null )
{
switch ( DataTablePropertyType.valueOf( key ) )
{
case pageLength:
dataTableProperties.put( DataTablePropertyType.currentPage, 0 );
case currentPage:
value = Integer.parseInt( value.toString() );
break;
case searchTerm:
dataTableProperties.put( DataTablePropertyType.currentPage, 0 );
value = value.toString();
break;
}
}
dataTableProperties.put( DataTablePropertyType.valueOf( key ), value );
}
}
}
}

@Override
public Map<String, Object> getAttributes() {
if (attributes == null)
Expand Down Expand Up @@ -118,7 +164,8 @@ protected enum PropertyKeys {
onmouseover,
onmouseup,
process,
update
update,
dataTableProperties
;

String toString;
Expand Down Expand Up @@ -513,6 +560,22 @@ public String getUpdate() {
public void setUpdate(String _update) {
getStateHelper().put(PropertyKeys.update, _update);
}


/**
* Get the property map for the DataTable state. <p>
* @return The property map for the DataTable state managed by a backing bean.
*/
public Map<DataTablePropertyType, Object> getDataTableProperties() {
Map<DataTablePropertyType, Object> value = (Map<DataTablePropertyType, Object>)getStateHelper().eval( PropertyKeys.dataTableProperties );
return value;
}

/**
* Set the map cntaining the DataTable properties for this instance.
* @param _dataTableProperties The map
*/
public void setDataTableProperties(Map<DataTablePropertyType, Object> _dataTableProperties){
getStateHelper().put( PropertyKeys.dataTableProperties, _dataTableProperties );
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@

package net.bootsfaces.component.dataTable;

import java.io.IOException;
import java.util.List;

import net.bootsfaces.component.ajax.AJAXRenderer;
import net.bootsfaces.component.dataTable.DataTable.DataTablePropertyType;
import net.bootsfaces.render.CoreRenderer;
import net.bootsfaces.render.Tooltip;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;

import net.bootsfaces.component.ajax.AJAXRenderer;
import net.bootsfaces.render.CoreRenderer;
import net.bootsfaces.render.Tooltip;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/** This class generates the HTML code of &lt;b:dataTable /&gt;. */
@FacesRenderer(componentFamily = "net.bootsfaces.component", rendererType = "net.bootsfaces.component.dataTable.DataTable")
Expand Down Expand Up @@ -173,13 +173,55 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
return;
}
DataTable dataTable = (DataTable) component;
Map<DataTablePropertyType, Object> dataTableProperties = dataTable.getDataTableProperties();
Integer page = 0;
Integer pageLength = 10;
String searchTerm = "''";
if(dataTableProperties != null){
Object currentPage = dataTableProperties.get( DataTablePropertyType.currentPage );
Object currentPageLength = dataTableProperties.get( DataTablePropertyType.pageLength );
Object currentSearchTerm = dataTableProperties.get( DataTablePropertyType.searchTerm );
if(currentPage != null){
page = (Integer)currentPage;
}
if(currentPageLength != null){
pageLength = (Integer)currentPageLength;
}
if(currentSearchTerm != null){
searchTerm = String.format("'%s'", (String)currentSearchTerm);
}
}
ResponseWriter rw = context.getResponseWriter();
String clientId = dataTable.getClientId().replace(":", "");
rw.endElement("table");
Tooltip.activateTooltips(context, dataTable);
rw.startElement("script", component);
rw.writeText("$(document).ready(function() {$('." + clientId + "Table" + "').DataTable();} );",
null);
//# Start JS
rw.writeText("$(document).ready(function() {", null);
//# Initialize table at nth page
rw.writeText("var element = $('." + clientId + "Table" + "');" +
"var table = element.DataTable();" +
"table.page("+page+");" +
"table.search("+searchTerm+");" +
"table.page.len("+pageLength+").draw('page');", null);
//# Event setup: http://datatables.net/reference/event/page
rw.writeText( "element.on('page.dt', function(){" +
"var info = table.page.info();" +
"BsF.ajax.callAjax(this, event, null, null, null, " +
"'" + DataTablePropertyType.currentPage + ":'+info.page);" +
"});", null );
//# Event setup: https://datatables.net/reference/event/length
rw.writeText( "element.on('length.dt', function(e, settings, len) {" +
"BsF.ajax.callAjax(this, event, null, null, null, " +
"'" + DataTablePropertyType.pageLength + ":'+len);" +
"});", null );
//# Event setup: https://datatables.net/reference/event/search
rw.writeText( "element.on('search.dt', function() {" +
"BsF.ajax.callAjax(this, event, null, null, null, " +
"'" + DataTablePropertyType.searchTerm + ":'+table.search());" +
"});", null );
//# End JS
rw.writeText("} );",null );
rw.endElement("script");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package net.bootsfaces.component.dataTable;

import net.bootsfaces.component.dataTable.DataTable.DataTablePropertyType;
import org.junit.Test;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import java.util.HashMap;
import java.util.Map;
import static net.bootsfaces.component.dataTable.DataTable.DataTablePropertyType.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
* Created by XaeroDegreaz on 1/29/2016.
*/
public class DataTableTest
{
@Test
public void testDecodeCurrentPage() throws Exception
{
DataTable dataTable = mock( DataTable.class );
when( dataTable.getDataTableProperties() ).thenReturn( new HashMap<DataTablePropertyType, Object>() );
Map<String, String> parameterMap = new HashMap<String, String>();
FacesContext facesContext = mock( FacesContext.class );
ExternalContext externalContext = mock( ExternalContext.class );
when( facesContext.getExternalContext() ).thenReturn( externalContext );
when( externalContext.getRequestParameterMap() ).thenReturn( parameterMap );
parameterMap.put( "params", "BsFEvent=currentPage:2" );
doCallRealMethod().when( dataTable ).decode( facesContext );
dataTable.decode( facesContext );
assertEquals( 2, dataTable.getDataTableProperties().get( currentPage ) );
}

@Test
public void testDecodePageLength() throws Exception
{
DataTable dataTable = mock( DataTable.class );
HashMap<DataTablePropertyType, Object> dataTableProperties = new HashMap<DataTablePropertyType, Object>();
dataTableProperties.put( currentPage, 5 );
when( dataTable.getDataTableProperties() ).thenReturn( dataTableProperties );
Map<String, String> parameterMap = new HashMap<String, String>();
FacesContext facesContext = mock( FacesContext.class );
ExternalContext externalContext = mock( ExternalContext.class );
when( facesContext.getExternalContext() ).thenReturn( externalContext );
when( externalContext.getRequestParameterMap() ).thenReturn( parameterMap );
parameterMap.put( "params", "BsFEvent=pageLength:100" );
doCallRealMethod().when( dataTable ).decode( facesContext );
dataTable.decode( facesContext );
assertEquals( 100, dataTable.getDataTableProperties().get( pageLength ) );
assertEquals( 0, dataTable.getDataTableProperties().get( currentPage ) );
}

@Test
public void testDecodeSearchTerm() throws Exception
{
DataTable dataTable = mock( DataTable.class );
HashMap<DataTablePropertyType, Object> dataTableProperties = new HashMap<DataTablePropertyType, Object>();
dataTableProperties.put( currentPage, 5 );
when( dataTable.getDataTableProperties() ).thenReturn( dataTableProperties );
Map<String, String> parameterMap = new HashMap<String, String>();
FacesContext facesContext = mock( FacesContext.class );
ExternalContext externalContext = mock( ExternalContext.class );
when( facesContext.getExternalContext() ).thenReturn( externalContext );
when( externalContext.getRequestParameterMap() ).thenReturn( parameterMap );
parameterMap.put( "params", "BsFEvent=searchTerm:SomeSearchTerm" );
doCallRealMethod().when( dataTable ).decode( facesContext );
dataTable.decode( facesContext );
assertEquals( "SomeSearchTerm", dataTable.getDataTableProperties().get( searchTerm ) );
assertEquals( 0, dataTable.getDataTableProperties().get( currentPage ) );
}

@Test
public void testDecodeSearchTerm2() throws Exception
{
DataTable dataTable = mock( DataTable.class );
HashMap<DataTablePropertyType, Object> dataTableProperties = new HashMap<DataTablePropertyType, Object>();
dataTableProperties.put( currentPage, 5 );
when( dataTable.getDataTableProperties() ).thenReturn( dataTableProperties );
Map<String, String> parameterMap = new HashMap<String, String>();
FacesContext facesContext = mock( FacesContext.class );
ExternalContext externalContext = mock( ExternalContext.class );
when( facesContext.getExternalContext() ).thenReturn( externalContext );
when( externalContext.getRequestParameterMap() ).thenReturn( parameterMap );
parameterMap.put( "params", "BsFEvent=searchTerm:Some:Search: Term" );
doCallRealMethod().when( dataTable ).decode( facesContext );
dataTable.decode( facesContext );
assertEquals( "Some:Search: Term", dataTable.getDataTableProperties().get( searchTerm ) );
assertEquals( 0, dataTable.getDataTableProperties().get( currentPage ) );
}
}