Ext.namespace("Ext.ux.grid.plugins");

Ext.ux.grid.plugins.GroupCheckboxSelection = {

	init: function(grid){
		this.grid = grid;

		grid.view.groupTextTpl =
			['<dl style="height:18px; border:0px !important">',
			'<dd class="x-grid3-hd-checker" style="width:18px; float:left;" x-grid-group-hd-text="{text}">&nbsp;</dd>',
			'<dd style="float:left; padding:3px 0px 0px 3px;">',
			grid.view.groupTextTpl,
			'</dd>',
			'</dl>'
			].join('');

		grid.on('render', this.initBehaviors, this);
	},
	initBehaviors: function() {
		var view = this.grid.getView();
		view.mainBody.on('mousedown', this.onGroupMouseDown, this);
	},
	onGroupMouseDown: function(e, t) {
		var grid = this.grid;
		if(e.button === 0 && t.className == 'x-grid3-hd-checker'){ // Only fire if left-click

			var hd = Ext.fly(t.parentNode)
			t.checked = !hd.hasClass('x-grid3-hd-checker-on');
			if (t.checked)
				hd.addClass('x-grid3-hd-checker-on');
			else hd.removeClass('x-grid3-hd-checker-on');

			var ds = grid.getStore();
			var sm = grid.getSelectionModel();
			var cm = grid.getColumnModel();
			var text = t.getAttribute("x-grid-group-hd-text");

			var parts = text.split(":")

			var value = parts[1].trim();
			var header = parts[0].trim();
			var field = cm.getColumnsBy(function(columnConfig, index){
				return (columnConfig.header == header);
			})[0].dataIndex;

			var records = ds.query(field, value).items;

			for(var i = 0, len = records.length; i < len; i++){
				var row = ds.indexOf(records[i]);
				if (t.checked) {
					sm.selectRow(row, true);
				}
				else {
					sm.deselectRow(row);
				}
			}
			// expand group
			var view = grid.getView();
			view.toggleGroup(view.getGroupId(value), Ext.isIE ? false : true);
		}
	}
}



/*

Ext.namespace("Ext.ux.grid.plugins");

Ext.ux.grid.plugins.GroupCheckboxSelection = {

	init: function(grid){

		grid.view.groupTextTpl =	
			'<input type="checkbox" ' +
			'class="x-grid-group-checkbox" x-grid-group-hd-text="{text}" /> ' +
			grid.view.groupTextTpl;
	
		grid.on('render', function() {
			Ext.ux.grid.plugins.GroupCheckboxSelection.initBehaviors(grid);
		});
	
		grid.view.on('refresh', function() {
			Ext.ux.grid.plugins.GroupCheckboxSelection.initBehaviors(grid);
		});
	},
	
	initBehaviors: function(grid) {
		var id = "#" + grid.id;
		var behaviors = {};

		// Check/Uncheck all items in group
		behaviors[id + ' .x-grid-group-hd .x-grid-group-checkbox@click'] =
			function(e, target){

				var ds = grid.getStore();
				var sm = grid.getSelectionModel();
				var cm = grid.getColumnModel();
				
				var text = target.getAttribute("x-grid-group-hd-text");
				var parts = text.split(":")
				
				var value = parts[1].trim();
				var header = parts[0].trim();
				var field = cm.getColumnsBy(function(columnConfig, index){
					return (columnConfig.header == header);
				})[0].dataIndex;
				
				var records = ds.query(field, value).items;
				
				for(var i = 0, len = records.length; i < len; i++){
					var row = ds.indexOf(records[i]);
					if (target.checked) {
						sm.selectRow(row, true);
					}
					else {
						sm.deselectRow(row);
					}
				}
			};

		// Avoid group expand/collapse clicking on checkbox
		behaviors[id + ' .x-grid-group-hd .x-grid-group-checkbox@mousedown'] =
			function(e, target){
				e.stopPropagation();
			};
		
		Ext.addBehaviors(behaviors);
	}

}

*/