(function(){u8.Base.Cursor=function(t){$.extend(this,u8.Base.Cursor.defaults,t);if(!this.resultSet){$error("A cursor requires property result set to be set!")}this.resultSet.bind("Update",this._onResultSetUpdate.bind(this));this.bind(t);this._raiseEvents=false;try{if(this.position>=0){this.moveTo(this.position,null,{force:true})}}finally{this._raiseEvents=true}};u8.Base.Cursor.makeClass({name:"Cursor",events:["ValueChange","SizeChange"]});$.extend(u8.Base.Cursor,{defaults:{position:-1,pageSize:10,chunkSize:10}});$.extend(u8.Base.Cursor.prototype,{dispose:function(){if(this._isDisposed){return}this.unbind();this._isDisposed=true},getPage:function(t){if(typeof t=="undefined"){t=this.position}return t<0?-1:Math.floor(t/this.pageSize)},moveFirst:function(t){return this.moveTo(0,t)},moveLast:function(t){var e=this.resultSet.getLastRow();var i;if(!this.isSizeFinal()&&this.position>=e){i=e+1}else{i=e}return this.moveTo(i,t,{moveType:"last"})},moveNext:function(t){return this.moveTo(this.position+1,t)},movePrev:function(t){return this.moveTo(this.position-1,t)},moveNextPage:function(t){return this.moveTo(this.position+this.pageSize,t)},movePrevPage:function(t){return this.moveTo(this.position-this.pageSize,t)},isSizeFinal:function(){return!this.resultSet.canNext()},isEmpty:function(){return this.isSizeFinal()&&this.getSize()<=0},canMovePrev:function(){return this.position>0},canMoveNext:function(){if(!this.isSizeFinal()){return true}return this.position+1<this.getSize()},getSize:function(){return this.resultSet.getSize()},getCurrentSize:function(){return this.isSizeFinal()?this.resultSet.getSize():this.resultSet.getFirstRow()+this.resultSet.getNumberOfRows()},getValue:function(){return this.resultSet.getRowAt(this.position)},moveTo:function(t,e,i){t=this._clip(t);if(t==this.position&&(!i||!i.force)){return}var s=this;this._load(t,n);function n(n,o){var r={};if(o.error){r.error=o.error}else{if(i&&i.moveType=="last"){s.position=s.getCurrentSize()-1}else{s.position=s._clip(t)}if(s._raiseEvents){s.onValueChange.raise(s,r)}}if(typeof e=="function"){e(s,r)}}},_load:function(t,e){var i=this;var s=this.resultSet.getFirstRow();var n=this.resultSet.getLastRow();if(t<s){s=t;n=s-1}else if(t>n){s=n+1;n=t}else{return o()}s-=s%this.chunkSize;n+=this.chunkSize-(n+1)%this.chunkSize;s=this._clip(s);n=this._clip(n);if(n<s){return o()}this.resultSet.load({from:s,to:n},function(t,e){o(e)});function o(t){if(typeof e=="function"){e(i,t||{})}}},_clip:function(t){if(this.isEmpty()){return-1}if(t<0){return 0}var e=this.getSize();if(typeof e!="undefined"&&t>=e){return e-1}return t},_onResultSetUpdate:function(t,e){if(e.sizeChanged){this.onSizeChange.raise(this,{})}}})})();