<commit_before>/**
*	JQGrid state managment
* 
*	@requires json2.js - //cdn.jsdelivr.net/json2/0.1/json2.min.js (JSON serializer)
*	@requires jstorage.js - //cdn.jsdelivr.net/jstorage/0.1/jstorage.min.js (jStorage plugin)
* 
*	Changelog:
*	- Added toolbar search filter persistance (9-dec-14)
*	- It will persist various settings of jqGrid on page refresh
*	- It will persist various settings of jqGrid on page refresh
*	- It includes: column chooser display, search filters, row selection, subgrid expansion, pager, column order
*	- To enable, make set property to true when initializing object
*	- Revamped selection persistance, removed old version
*
*	Useful links:
*
*	http://stackoverflow.com/questions/19119122/trigger-search-for-jqgrid-filter-on-every-revisit-of-page
*	http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_single.htm
*	http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_.htm
*	http://stackoverflow.com/questions/8545953/how-to-persist-current-row-in-jqgrid
*	http://stackoverflow.com/questions/9308583/jqgrid-how-to-define-filter-presets-templates-inside-a-combo-box
*	http://www.google.com.pk/search?q=site:www.ok-soft-gmbh.com/jqGrid/+localstorage
*
*/

var nonModelColumns = ["cb", "subgrid", "rn"];

//************************** Utils functions *************************************************
function swap(key1, key2, arr) {
	if (typeof (arr[key1]) !== 'undefined' && typeof (arr[key2]) !== 'undefined') {
		var tmp = arr[key1];
		arr[key1] = arr[key2]
		arr[key2] = tmp;
	}
};

//*************************** Grid state object ************************************
function GridState(options) {
	this.stateOpts = {
		storageKey: null,
		columns: false,
		filters: false,
		selection: false,
		expansion: false,
		pager: false,
		order: false
	};
	this.colsData = null;
	this.filtersData = null;
	this.pagerData = null;
	this.orderData = null;
	this.selRows = [];
	this.expRows = [];

	this.save = function (grid) {
		if (grid) {
			this.refreshOptions(grid);
			this.refresh(grid);
		}

		var dataToSave = {};
		if (this.stateOpts.columns)
			dataToSave.colsData = this.colsData;
		if (this.stateOpts.filters)
			dataToSave.filtersData = this.filtersData;
		if (this.stateOpts.selection)
			dataToSave.selRows = this.selRows;
		if (this.stateOpts.expansion)
			dataToSave.expRows = this.expRows;
		if (this.stateOpts.pager)
			dataToSave.pagerData = this.pagerData;
		if (this.stateOpts.order)
			dataToSave.orderData = this.orderData;

		$.jStorage.set(this.stateOpts.storageKey, dataToSave);
		return this;
	};
	this.load = function (storageKey) {
		if (storageKey)
			this.stateOpts.storageKey = storageKey;

		if (this.stateOpts.storageKey) {
			var savedState = $.jStorage.get(this.stateOpts.storageKey);
			if (savedState) {
				this.colsData = savedState.colsData;
				this.filtersData = savedState.filtersData;
				this.selRows = savedState.selRows || [];
				this.expRows = savedState.expRows || [];
				this.pagerData = savedState.pagerData;
				this.orderData = savedState.orderData;
			}
		}

		return this;
	};
	this.remove = function (storageKey) {
		$.jStorage.deleteKey(storageKey || this.stateOpts.storageKey);

		this.colsData = 
		this.filtersData = 
		this.pagerData = 
		this.orderData = null
		this.selRows = [];
		this.expRows = [];
	};
	this.apply = function (grid) {
		grid.gridState(this);

		//TODO:Aplicar a un grid ya creado el estado guardado.
	};
	this.refresh = function (grid) {
		this.refreshOptions(grid);

		if (this.stateOpts.columns)
			this.refreshColumns(grid);

		if (this.stateOpts.filters)
			this.refreshFilters(grid);

		if (this.stateOpts.pager)
			this.refreshPagerData(grid);

		if (this.stateOpts.order)
			this.refreshOrderData(grid);

		return this;
	};
	this.refreshOptions = function (gridOrOpts) {
		gridOrOpts = gridOrOpts || '';

		var opts = typeof (gridOrOpts) === 'string'
				   ? gridOrOpts
				   : jQuery.isFunction(gridOrOpts.getGridParams)
					 ? gridOrOpts.getGridParams('stateOptions')
					 : gridOrOpts;

		if (jQuery.isFunction(opts))
			opts = opts.call(gridOrOpts);

		if (typeof (opts) === 'string') {
			this.stateOpts.storageKey = opts;
			this.stateOpts.columns = this.stateOpts.filters =
			this.stateOpts.selection = this.stateOpts.expansion =
			this.stateOpts.pager = this.stateOpts.order = true;
		}
		else if (jQuery.isPlainObject(opts))
			jQuery.extend(this.stateOpts, opts);

		return this;
	}
	this.refreshColumns = function (grid) {
		this.colsData = { count: 0, cols: {} };

		var nonModelColCount = 0;
		var colModel = grid.getGridParam('colModel');
		for (i = 0; i < colModel.length; i++) {
			if (jQuery.inArray(colModel[i].name, nonModelColumns) != -1) {
				nonModelColCount++;
				continue;
			}

			this.colsData.count++;
			this.colsData.cols[colModel[i].name] = {
				hidden: colModel[i].hidden,
				width: colModel[i].width,
				uiIndex: i - nonModelColCount
			};
		}

		return this;
	};
	this.refreshFilters = function (grid) {
		var prmNames = grid.getGridParam('prmNames');
		var postData = grid.getGridParam('postData');
		var sFilter = grid.searchGrid('sFilter');
		var sFilter = 'filters';
		if (sFilter) {
			var fltrData = postData[sFilter];
			var searchBtn = jQuery('div.ui-pg-div span.ui-icon.ui-icon-search');
			if (prmNames.search && postData[prmNames.search]) {
				searchBtn.parent().addClass('ui-state-default ui-corner-all');
				this.filtersData = JSON.parse(fltrData);
				alert(JSON.stringify(this.filtersData));<commit_msg>Update jqGrid.state.js<commit_after>/**
*	JQGrid state managment
* 
*	@requires json2.js - //cdn.jsdelivr.net/json2/0.1/json2.min.js (JSON serializer)
*	@requires jstorage.js - //cdn.jsdelivr.net/jstorage/0.1/jstorage.min.js (jStorage plugin)
* 
*	Changelog:
*	- Added toolbar search filter persistance (9-dec-14)
*	- It will persist various settings of jqGrid on page refresh
*	- It will persist various settings of jqGrid on page refresh
*	- It includes: column chooser display, search filters, row selection, subgrid expansion, pager, column order
*	- To enable, make set property to true when initializing object
*	- Revamped selection persistance, removed old version
*
*	Useful links:
*
*	http://stackoverflow.com/questions/19119122/trigger-search-for-jqgrid-filter-on-every-revisit-of-page
*	http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_single.htm
*	http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_.htm
*	http://stackoverflow.com/questions/8545953/how-to-persist-current-row-in-jqgrid
*	http://stackoverflow.com/questions/9308583/jqgrid-how-to-define-filter-presets-templates-inside-a-combo-box
*	http://www.google.com.pk/search?q=site:www.ok-soft-gmbh.com/jqGrid/+localstorage
*
*/

var nonModelColumns = ["cb", "subgrid", "rn"];

//************************** Utils functions *************************************************
function swap(key1, key2, arr) {
	if (typeof (arr[key1]) !== 'undefined' && typeof (arr[key2]) !== 'undefined') {
		var tmp = arr[key1];
		arr[key1] = arr[key2]
		arr[key2] = tmp;
	}
};

//*************************** Grid state object ************************************
function GridState(options) {
	this.stateOpts = {
		storageKey: null,
		columns: false,
		filters: false,
		selection: false,
		expansion: false,
		pager: false,
		order: false
	};
	this.colsData = null;
	this.filtersData = null;
	this.pagerData = null;
	this.orderData = null;
	this.selRows = [];
	this.expRows = [];

	this.save = function (grid) {
		if (grid) {
			this.refreshOptions(grid);
			this.refresh(grid);
		}

		var dataToSave = {};
		if (this.stateOpts.columns)
			dataToSave.colsData = this.colsData;
		if (this.stateOpts.filters)
			dataToSave.filtersData = this.filtersData;
		if (this.stateOpts.selection)
			dataToSave.selRows = this.selRows;
		if (this.stateOpts.expansion)
			dataToSave.expRows = this.expRows;
		if (this.stateOpts.pager)
			dataToSave.pagerData = this.pagerData;
		if (this.stateOpts.order)
			dataToSave.orderData = this.orderData;

		$.jStorage.set(this.stateOpts.storageKey, dataToSave);
		return this;
	};
	this.load = function (storageKey) {
		if (storageKey)
			this.stateOpts.storageKey = storageKey;

		if (this.stateOpts.storageKey) {
			var savedState = $.jStorage.get(this.stateOpts.storageKey);
			if (savedState) {
				this.colsData = savedState.colsData;
				this.filtersData = savedState.filtersData;
				this.selRows = savedState.selRows || [];
				this.expRows = savedState.expRows || [];
				this.pagerData = savedState.pagerData;
				this.orderData = savedState.orderData;
			}
		}

		return this;
	};
	this.remove = function (storageKey) {
		$.jStorage.deleteKey(storageKey || this.stateOpts.storageKey);

		this.colsData = 
		this.filtersData = 
		this.pagerData = 
		this.orderData = null
		this.selRows = [];
		this.expRows = [];
	};
	this.apply = function (grid) {
		grid.gridState(this);

		//TODO:Aplicar a un grid ya creado el estado guardado.
	};
	this.refresh = function (grid) {
		this.refreshOptions(grid);

		if (this.stateOpts.columns)
			this.refreshColumns(grid);

		if (this.stateOpts.filters)
			this.refreshFilters(grid);

		if (this.stateOpts.pager)
			this.refreshPagerData(grid);

		if (this.stateOpts.order)
			this.refreshOrderData(grid);

		return this;
	};
	this.refreshOptions = function (gridOrOpts) {
		gridOrOpts = gridOrOpts || '';

		var opts = typeof (gridOrOpts) === 'string'
				   ? gridOrOpts
				   : jQuery.isFunction(gridOrOpts.getGridParams)
					 ? gridOrOpts.getGridParams('stateOptions')
					 : gridOrOpts;

		if (jQuery.isFunction(opts))
			opts = opts.call(gridOrOpts);

		if (typeof (opts) === 'string') {
			this.stateOpts.storageKey = opts;
			this.stateOpts.columns = this.stateOpts.filters =
			this.stateOpts.selection = this.stateOpts.expansion =
			this.stateOpts.pager = this.stateOpts.order = true;
		}
		else if (jQuery.isPlainObject(opts))
			jQuery.extend(this.stateOpts, opts);

		return this;
	}
	this.refreshColumns = function (grid) {
		this.colsData = { count: 0, cols: {} };

		var nonModelColCount = 0;
		var colModel = grid.getGridParam('colModel');
		for (i = 0; i < colModel.length; i++) {
			if (jQuery.inArray(colModel[i].name, nonModelColumns) != -1) {
				nonModelColCount++;
				continue;
			}

			this.colsData.count++;
			this.colsData.cols[colModel[i].name] = {
				hidden: colModel[i].hidden,
				width: colModel[i].width,
				uiIndex: i - nonModelColCount
			};
		}

		return this;
	};
	this.refreshFilters = function (grid) {
		var prmNames = grid.getGridParam('prmNames');
		var postData = grid.getGridParam('postData');
		var sFilter = grid.searchGrid('sFilter');
		var sFilter = 'filters';
		if (sFilter) {
			var fltrData = postData[sFilter];
			var searchBtn = jQuery('div.ui-pg-div span.ui-icon.ui-icon-search');
			if (prmNames.search && postData[prmNames.search]) {
				searchBtn.parent().addClass('ui-state-default ui-corner-all');
				this.filtersData = JSON.parse(fltrData);
{'fraction_non_alphanumeric': 0.16438730853391684, 'fraction_numerical': 0.008388037928519328, 'mean_word_length': 3.938766321476812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3167012', 'n_tokens_mistral': 4256, 'n_tokens_neox': 3838, 'n_words': 695}