var Pager=require("./pager.js"),Table=require("./table.js"),Form=require("./form.js"),Dialog=require("./dialogPC.js"),Lister=function(a,e){var t=null;"table"in a&&(t=a.table);var r=$(t),i=null;"pager"in a&&(i=a.pager);var n=$(i),s=null;"search"in a&&(s=a.search);var o=$(s),c=this,d={page:1,field:[],order:[],search:{}},l=$.extend({server:"/path/to/server",entityCheckbox:!1,listRendered:null},e),h=new Pager(n,function(a){d.page=a,c.load(!1)}),f=new Table(r,{entityCheckbox:l.entityCheckbox});r.on("click","thead > tr > th",function(){if(!$(this).is("[data-orderAble]"))return!0;var a=$(this).attr("data-item"),e=$(this).attr("data-order");return e||(e="asc"),e="asc"==e?"desc":"asc",d.order[0]=[a,e],d.page=1,c.load(!0),!1}),o.on("click","[data-search-trigger]",function(){return c.setSearch(),c.load(!1),!1}),this.init=function(){var a=window.location.hash;0===a.indexOf("#")&&(a=a.substring(1));try{var e=JSON.parse(a);for(var t in e)t in d&&(d[t]=e[t])}catch(a){}},this.initSearch=function(){o.find("[data-search]").each(function(a,e){var t=$(e),r=t.attr("data-search");if(r in d.search){var i=t.data("widget");i?i.val(d.search[r]):t.val(d.search[r])}})},this.setSearch=function(){d.search={},o.find("[data-search]").each(function(a,e){var t=$(e),r=t.attr("data-search");if(r){var i=t.data("widget");d.search[r]=i?i.val():t.val()}})},this.setParam=function(a,e){a in d&&(d[a]=e)},this.load=function(a){a?f.loading():Dialog.loadingOn(),$.post(l.server,d).done(function(e){window.location.replace("#"+JSON.stringify(d)),a||Dialog.loadingOff(),Form.defaultCallback(e,{success:function(a){f.render(a.data),h.render(a.data.total,a.data.pageSize,a.data.page),l.listRendered&&l.listRendered()}})}).fail(function(e){a||Dialog.loadingOff(),Form.defaultCallback(e)})},this.init(),this.initSearch(),this.load(!0)};module.exports=Lister;