
/**
 * @class Voyeur.Tool.DocumentTypeCollocateFrequenciesGrid
 * @description A panel that provides per-document collocate data in a tabular format.
 * @extends Ext.grid.GridPanel
 * @extends Voyeur.Tool
 * @author Stéfan Sinclair
 * @since 0.1.1
 */
Voyeur.Tool.DocumentTypeCollocateFrequenciesGrid = Ext.extend(Ext.grid.GridPanel, {
	constructor: function(config){
		Ext.apply(this, new Voyeur.Tool(config, this))
		var reader = new Ext.data.JsonReader({
			root: 'documentTypeCollocateFrequencies.types',
			totalProperty: 'documentTypeCollocateFrequencies["@totalTypes"]'
		}, Voyeur.data.DocumentTypeCollocates.fields);
		var store = new Ext.data.GroupingStore({
			reader: reader
			,fields: Voyeur.data.DocumentTypeCollocates.fields
			,groupField: this.getApiParamValue('groupBy')
			,proxy: new Ext.data.HttpProxy({url: this.getTromboneUrl()})
			,listeners : {
				'beforeload' : {
					fn : function(store, options) {
						if (this.hidden || this.getCorpus().isEmpty() || (!this.getApiParamValue('docIdType') && !this.getApiParamValue('type'))) {return false;}
						Ext.applyIf(options.params, this.getApiParams());
						Ext.apply(options.params, {tool: 'DocumentTypeCollocateFrequencies'});
					},
					scope : this
				},
				'loadexception' : {
					fn: function(conn, proxy, response, error){
						var alert = Ext.MessageBox.alert("Error", "An error occurred while loading data:<pre style='overflow: auto; clear: both'>\n" + response.responseText+"</pre>");
						alert.setIcon(Ext.MessageBox.ERROR);
					}
					,scope : this
				}
			}
			,remoteSort: true
			,sortInfo : {field : this.getApiParamValue('sortBy'), direction : this.getApiParamValue('sortDirection')}
		});
		store.paramNames.sort='sortBy';
		store.paramNames.dir='sortDirection'
		
		var xtypePrefix = this.getXType() + '.';
		
		config.viewConfig = config.viewConfig ? config.viewConfig : {};
		Ext.applyIf(config.viewConfig, {
			forceFit: true,
			emptyText : this.localize('noResults','tool'),
			deferEmptyText: false
			,hideGroupedColumn: true
		})
		
		if (!config.plugins) {config.plugins=[]}
		config.plugins.push(new Ext.ux.grid.Favs())
		
		var context = this.getApiParamValue('context');
		var visibleColumn = this.getApiParamValue('visibleColumn');
		
		var panel = this;
		Ext.applyIf(config, {
			view: new Ext.grid.GroupingView(config.viewConfig)
			,iconCls : 'table'
			,loadMask: true,
			sm: new Ext.grid.RowSelectionModel(),
			stripeRows: true,
			colModel: new Ext.grid.ColumnModel({
				columns: [
					{
						header: this.localize('docLabel'), tooltip : this.localize('docLabelTip'), dataIndex: 'docIndex', hidden: visibleColumn.indexOf('docIndex')==-1,
						renderer : function(val) {return panel.getCorpus().getDocument(val).getShortLabel()}
					}
					,{
						header: this.localize('keywordDocument'), tooltip : this.localize('keywordDocumentTip'), dataIndex: 'keyword_docId', sortable: true, hidden: visibleColumn.indexOf('keyword_docId')==-1,
						renderer : function(val, meta, record) {return '<span class="keyword">'+record.get('keyword')+'</span> in '+panel.getCorpus().getDocument(record.get('docIndex')).getShortTitle()}
					},{
						header: this.localize('keyword'), tooltip : this.localize('keywordTip'), dataIndex: 'keyword', sortable: true, hidden: visibleColumn.indexOf('keyword')==-1,
						renderer : function(val) {return '<span class="keyword">'+val+'</span>'}
					},{
						header: this.localize('type'), tooltip : this.localize('typeTip'), dataIndex: 'type', sortable: true, hidden: visibleColumn.indexOf('type')==-1,
						renderer : function(val) {return '<span class="keyword">'+val+'</span>'}
					}
					,{
						header: this.localize('rawCollocateFreq'), tooltip : this.localize('rawCollocateFreqTip'), dataIndex: 'rawCollocateFreq', sortable: true, hidden: visibleColumn.indexOf('rawCollocateFreq')==-1,
						renderer : function (val, meta, record) {return val + ' ('+ record.get('rawFreq') + ')'}
					}
					,{
						header: this.localize('relativeCollocateFreq'), tooltip : this.localize('relativeCollocateFreqTip'), sortable: true, dataIndex: 'relativeCollocateFreq', hidden: visibleColumn.indexOf('relativeCollocateFreq')==-1,
						renderer : function (val, meta, record) {return Ext.util.Format.number(val*10000,'0,000.0') + ' ('+Ext.util.Format.number(record.get('relativeFreq')*10000,'0,000.0')+')'}
					}
					,{
						header: this.localize('relativeRatio'), tooltip : this.localize('relativeRatioTip'), sortable: true,  dataIndex: 'relativeRatio', hidden: visibleColumn.indexOf('relativeRatio')==-1,
						renderer : function(val) {return "<span class='" + (val < 0 ? 'negative' : 'positive') + "'>" + Ext.util.Format.number(val*100,'0,000.0') + '</span>'}
					}
				],
				listeners: {
					hiddenchange: {
						fn: this.handleColumnModelHidden
						,scope: this
					}
				}
			}),
			store: store,
			bbar: new Ext.PagingToolbar({
				store: store,
				pageSize: this.getApiParamValue('limit'),
				displayInfo: true
				,items : [
					'-',
					{
						text : this.localize('context')
						,tooltip : this.localize('contextTip')
						,menu : new Ext.menu.Menu({
							items : [
								new Ext.menu.CheckItem({text : '1',checked:context==1,group:'context'})
								,new Ext.menu.CheckItem({text : '2',checked:context==2,group:'context'})
								,new Ext.menu.CheckItem({text : '3',checked:context==3,group:'context'})
								,new Ext.menu.CheckItem({text : '4',checked:context==4,group:'context'})
								,new Ext.menu.CheckItem({text : '5',checked:context==5,group:'context'})
								,new Ext.menu.CheckItem({text : '7',checked:context==7,group:'context'})
								,new Ext.menu.CheckItem({text : '10',checked:context==10,group:'context'})
								,new Ext.menu.CheckItem({text : '15',checked:context==15,group:'context'})
								,new Ext.menu.CheckItem({text : '20',checked:context==20,group:'context'})
								,new Ext.menu.CheckItem({text : '25',checked:context==25,group:'context'})
							]
							,listeners : {
								'itemclick' : {
									fn : function(item) {
										this.setApiParams({context: item.text})
										this.getStore().load();
									}
									,scope : this
								}
							}
						})
					}
				]
			
			})
		});
		Voyeur.Tool.DocumentTypeCollocateFrequenciesGrid.superclass.constructor.apply(this, arguments);
		
		this.addListener('CorpusSummaryResultLoaded', function(src, data) {
			this.getStore().load();
		}, this);

		this.addListener('documentTypeSelected', function(src, data) {
			this.setApiParams({docIdType: data.docIdType});
			this.getStore().load();
		}, this);

		this.addListener('documentTypesSelected', function(src, data) {
			this.setApiParams({docIdType: data.docIdType});
			this.getStore().load();
		}, this);

		this.addListener('voyeurDocumentTermFrequenciesGrid.selectionchange', function(src, selectionModel){
			var records = selectionModel.getSelections();
			docIdTerms = [];
			if (records.length > 0) {
				for (var i = 0; i < records.length; i++) {
					docIdTerms.push(records[i].get('docId') + ':' + records[i].get('term'));
				}
				this.setApiParams({'docIdType': docIdTerms})
				this.getStore().load();
			}
		}, this);
		
	}
	
	,api: {
		stopList: {'default': null ,'choices': ['stop.en.taporware.txt', 'stop.en.glasgow.txt']}
		,start: {'default': 0}
		,limit: {'default': 50}
		,context: {'default': 3}
		,sortBy: {'default': 'relativeRatio'}
		,sortDirection: {'default': 'desc'}
		,groupBy: {'default': 'keyword_docId'}
		,query: {'default': null}
		,docIdType: {'default': null}
		,visibleColumn: {'default': ['type', 'rawCollocateFreq', 'relativelCollocateFreq', 'relativeRatio']}
	}

	// private localization variables
	,i18n : {
		title : {en: "Collocates"}
		,help : {en: "This table shows the frequencies of words that appear near a specified term in a document."}
		,context : {en: "Context"}
		,contextTip : {en: "This value determines how many words to count on the left and the right of each keyword."}
		,term : {en: "Term"}
		,termTip : {en: "The term from the corpus for which data is being provided."}
		,docLabel: {en: 'Document'}
		,docLabelTip: {en: "The document in which the collocate is found."}
		,keywordDocument: {en: 'Keyword/Document'}
		,keywordDocumentTip: {en: "Indicates the keyword and document for the collocate."}
		,keyword: {en: 'Keyword'}
		,keywordTip: {en: 'This is the keyword for which collocates are shown.'}
		,type: {en: 'Collocate'}
		,typeTip: {en: 'This is the collocate that occurs near the keyword.'}
		,rawCollocateFreq: {en: 'Raw Frequency'}
		,rawCollocateFreqTip: {en: 'This shows the frequency of the collocate near the keyword, and then in parentheses the frequency of the collocate in the entire document.'}
		,relativeCollocateFreq: {en: 'Relative Frequency'}
		,relativeCollocateFreqTip: {en: 'This shows the relative frequency per 10,000 words of the collocate near the keyword, and then in parentheses the relative frequency of the collocate in the entire document.'}
		,relativeRatio: {en: 'Ratio'}
		,relativeRatioTip: {en: "This indicates the difference between the relative frequency of the collocates near the keywords and the relative frequency of the collocates in the entire document. Higher positive values mean that the collocate is more closely associated with the keyword than in the rest of the document."}
	}
});

Ext.reg('voyeurDocumentTypeCollocateFrequenciesGrid', Voyeur.Tool.DocumentTypeCollocateFrequenciesGrid);
