class Locmare.ListGroupModule.LibModule.Pager @factory: (page_status, params) -> pagers = { default: Locmare.ListGroupModule.LibModule.PagerModule.Default, offset: Locmare.ListGroupModule.LibModule.PagerModule.Offset, unlimited: Locmare.ListGroupModule.LibModule.PagerModule.Unlimited, more: Locmare.ListGroupModule.LibModule.PagerModule.More } c = pagers[page_status.type] return null if not page_status.total_page new c({page_status: page_status, params: params}) class Locmare.ListGroupModule.LibModule.PagerModule class Locmare.ListGroupModule.LibModule.PagerModule.Default extends Backbone.View tagName: 'ul' className: 'pagination' initialize: (options) -> @page_status = options.page_status @params = options.params @total = @page_status.total @window_size = 3 @controller = Manifest.manifest().controllers[@params['controller']] @action = @controller.actions[@params['action']] @action = @action.original if @action.original @current_page = @page_status.page @per_page = @page_status.page_size @total_page = @page_status.total_page render: () -> this.$el.html('') if @hasPreviousPage() view = new Locmare.ListGroupModule.LibModule.PagerModule.FirstPage({ parent: this, page: 1, class_name: 'first' }) @listenTo(view, 'navigate', @navigate) @listenTo(view, 'page', @click) this.$el.append(view.render().el) if @hasPreviousPage() view = new Locmare.ListGroupModule.LibModule.PagerModule.PrevPage({ parent: this, page: @current_page - 1, class_name: 'prev' }) @listenTo(view, 'navigate', @navigate) @listenTo(view, 'page', @click) this.$el.append(view.render().el) if @hasPreviousPageGap() this.$el.append( (new Locmare.ListGroupModule.LibModule.PagerModule.PageGap()).render().el ) _.each @range(), (page) => view = new Locmare.ListGroupModule.LibModule.PagerModule.Page({ parent: this, page: page, class_name: 'page' }) @listenTo(view, 'navigate', @navigate) @listenTo(view, 'page', @click) this.$el.append(view.render().el) if @hasNextPageGap() this.$el.append( (new Locmare.ListGroupModule.LibModule.PagerModule.PageGap()).render().el ) if @hasNextPage() view = new Locmare.ListGroupModule.LibModule.PagerModule.NextPage({ parent: this, page: @current_page + 1, class_name: 'next' }) @listenTo(view, 'navigate', @navigate) @listenTo(view, 'page', @click) this.$el.append(view.render().el) if @hasNextPage() view = new Locmare.ListGroupModule.LibModule.PagerModule.LastPage({ parent: this, page: @total_page, class_name: 'last' }) @listenTo(view, 'navigate', @navigate) @listenTo(view, 'page', @click) this.$el.append(view.render().el) rb = new Tag.RowBreak() this.$el.append(rb.render().el) this range: () -> f = if @hasPreviousPageGap() @current_page - @window_size else 1 t = if @hasNextPageGap() @current_page + @window_size else @total_page _.range(f, t) hasPreviousPage: () -> @current_page > 1 hasNextPage: () -> @current_page < @total_page hasPreviousPageGap: () -> @current_page > @window_size + 1 hasNextPageGap: () -> @total_page - @current_page > @window_size url: (page) -> params = {} _.each @params, (v, k) -> # deep copy params[k] = v params['page'] = page params['page_size'] = @per_page @action.url params click: (page) -> @trigger('page', page) navigate: (url) -> @trigger('http_get', url) class Locmare.ListGroupModule.LibModule.PagerModule.Part extends Backbone.View tagName: 'li' initialize: (options) -> @parent = options.parent @page = options.page @el.className = options.class_name render: () -> if @page == @parent.current_page this.$el.html(@content()) else linked_caption = new Tag.A({ attr: {href: '/' + @url()}, content: @content() }) @listenTo(linked_caption, 'click', @click) this.$el.html(linked_caption.render().el) this content: () -> '' url: () -> @parent.url(@page) click: () -> @trigger('page', @page) @trigger('navigate', @url()) class Locmare.ListGroupModule.LibModule.PagerModule.FirstPage extends Locmare.ListGroupModule.LibModule.PagerModule.Part content: () -> '<<' click: () -> @trigger('page', @page) @trigger('navigate', @url()) class Locmare.ListGroupModule.LibModule.PagerModule.PrevPage extends Locmare.ListGroupModule.LibModule.PagerModule.Part content: () -> '<' class Locmare.ListGroupModule.LibModule.PagerModule.PageGap extends Backbone.View tagName: 'li' className: 'page-gap' render: () -> this.$el.html('...') this class Locmare.ListGroupModule.LibModule.PagerModule.Page extends Locmare.ListGroupModule.LibModule.PagerModule.Part content: () -> Pettanr.to_s(@page) class Locmare.ListGroupModule.LibModule.PagerModule.NextPage extends Locmare.ListGroupModule.LibModule.PagerModule.Part content: () -> '>' class Locmare.ListGroupModule.LibModule.PagerModule.LastPage extends Locmare.ListGroupModule.LibModule.PagerModule.Part content: () -> '>>' class Locmare.ListGroupModule.LibModule.PagerModule.More extends Backbone.View tagName: 'div' className: 'pagination' initialize: (options) -> @params = options.params @controller = Manifest.manifest().controllers[@params['controller']] @action = @controller.actions[@params['action']] @action = @action.original if @action.original render: () -> this.$el.html('') linked_caption = new Tag.A({ attr: {href: '/' + @url()}, content: 'More...' }) @listenTo(linked_caption, 'click', @click) this.$el.html(linked_caption.render().el) this url: () -> @action.url @params click: () -> @trigger('http_get', @url())