// Business process dedicated search query handler
// (used to handle business processes table in official roles)

'use strict';

var isNaturalNumber   = require('es5-ext/number/is-natural')
  , findKey           = require('es5-ext/object/find-key')
  , appLocation       = require('mano/lib/client/location')
  , setupQueryHandler = require('../../../utils/setup-client-query-handler')

  , stringify = JSON.stringify;

module.exports = exports = function (listManager/*, pathname*/) {
	var queryHandler = setupQueryHandler(exports.conf, appLocation, arguments[2] || '/')
	  , statusMap = listManager._statusMap;
	queryHandler._statusMap = statusMap;
	queryHandler._statusMapDefaultKey = findKey(statusMap, function (data) { return data.default; });
	queryHandler._listManager = listManager;
	queryHandler.on('query', function (query) { listManager.update(query); });
};
exports.conf = [
	{
		name: 'status',
		ensure: function (value) {
			if (!value) return this._statusMapDefaultKey;
			if (!this._statusMap[value]) throw new Error("Unreconized status value " + stringify(value));
			return (value === 'all') ? null : value;
		}
	},
	{ name: 'search' },
	{
		name: 'page',
		ensure: function (value) {
			var num;
			if (value == null) return '1';
			if (isNaN(value)) throw new Error("Unrecognized page value " + stringify(value));
			num = Number(value);
			if (!isNaturalNumber(num)) throw new Error("Unreconized page value " + stringify(value));
			if (num <= 1) throw new Error("Unexpected page value " + stringify(value));
			if (num > this._listManager.pageCount) throw new Error("Page value overflow");
			return value;
		}
	}
];

@@ -384,16 +384,34 @@
 r')%0A%0A  ,
+ ceil = Math.ceil,
  stringi
@@ -431,16 +431,16 @@
 ingify;%0A
-
 %0Amodule.
@@ -655,32 +655,87 @@
 ap = statusMap;%0A
+%09queryHandler._statusViews = listManager._statusViews;%0A
 %09queryHandler._s
@@ -1264,24 +1264,31 @@
 ction (value
+, query
 ) %7B%0A%09%09%09var n
@@ -1289,16 +1289,27 @@
 %09var num
+, pageCount
 ;%0A%09%09%09if 
@@ -1624,36 +1624,96 @@
 %0A%09%09%09
-if (num %3E this._listManager.
+pageCount = ceil(this._statusViews%5Bquery.status %7C%7C 'all'%5D.totalSize / 50);%0A%09%09%09if (num %3E 
 page

Fix 'page' value validation