1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary DataGridPagerFilter  
  5  */
  6 
  7 goog.provide('Banana.Controls.DataGridSearchFilter');
  8 
  9 /** @namespace Banana.Controls.DataGridSearchFilter */
 10 namespace('Banana.Controls').DataGridSearchFilter = Banana.Controls.TextBox.extend(
 11 /** @lends Banana.Controls.DataGridSearchFilter.prototype */
 12 {	
 13 	/**
 14 	 * Creates a search box for usage in a datagrid. 
 15 	 * @constructs
 16 	 * @extends Banana.Controls.TextBox
 17 	 */
 18 	init : function()
 19 	{
 20 		this.untouched = true; //first time we have an untouched searchbox
 21 		this._super();
 22 		this.addCssClass("BDataGridFilter");
 23 		this.addCssClass("BDataGridSearchFilter");
 24 		this.promptText = 'Enter search term';
 25 		this.allKey = '%';
 26 
 27 		this.bind('dataChanged',this.getProxy(this.handleChange));
 28 	},
 29 	
 30 	getData : function()
 31 	{
 32 		if(this.data == this.promptText)
 33 		{
 34 			return this.allKey;
 35 		}
 36 		
 37 		return this.data || this.allKey;
 38 	},
 39 	
 40 	/**
 41 	 * when user types something in the search input this function is called.
 42 	 * only after 400ms the control will trigger an event. When user presses a key before 400ms
 43 	 * seconds passed will reset the timer to 0; so fast typing will not result in many search calls.
 44 	 */
 45 	handleChange : function(e)
 46 	{
 47 		if (this.getData() == this.previousSearch) {return;}
 48 
 49 		if (this.timer)
 50 		{
 51 			clearTimeout(this.timer);
 52 		}
 53 
 54 		this.timer = setTimeout(this.getProxy(function(){
 55 			
 56 			if (!this.untouched)
 57 			{
 58 				this.previousSearch = this.getData();
 59 				this.triggerEvent('filterDataChanged');
 60 
 61 				clearTimeout(this.timer);
 62 			}				
 63 
 64 		}),400);
 65 	},
 66 	
 67 	setFilterField : function(f)
 68 	{
 69 		this.filterField = f;
 70 		return this;
 71 	},
 72 	
 73 	getFilterField : function()
 74 	{
 75 		return this.filterField;
 76 	}
 77 });
 78 
 79 Banana.Controls.DataGridSearchFilter.prototype.getAllKey = function()
 80 {
 81 	return null;
 82 };
 83 
 84 Banana.Controls.DataGridSearchFilter.prototype.createComponents = function()
 85 {
 86 	//we bind a click event. when the control is untouched we remove the contents of the control
 87 	this.bind('mousedown',this.getProxy(this.searchClicked));
 88 
 89 	if (this.getData())
 90 	{
 91 		return;
 92 	}
 93 	var search = Banana.Util.UrlManager.getModule('search'); //we get the search string
 94 
 95 	if (search) //if we have one we make it visible on the control
 96 	{
 97 		this.setData(search);
 98 	}
 99 	else
100 	{
101 		this.setData(this.promptText);
102 	}
103 };
104 
105 Banana.Controls.DataGridSearchFilter.prototype.searchClicked = function()
106 {
107 	if (this.untouched)
108 	{
109 		this.setData('',true);
110 	}
111 
112 	this.untouched = false;
113 };