[Borges-users] Extension of BatchedList

Kaspar Schiess eule at space.ch
Thu Mar 25 17:25:59 EST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all,

I have developed an extension of the Borges::BatchedList. I would like
to submit it to you for free usage; maybe we could even add this to
Borges itself ?

The problem addressed here is that often you get batches counting
thousands of pages. The current Borges::BatchedList is going to nicely
display all of them as links; unless you choose to use fonts at sizes
smaller than 5pt, nobody will use these links.

So I propose the concept of 'environment': My ExtendedBatchedList will
only display 5 (by default) pages of each sort around your current page.
Let's say you are on page 344 of 1000; given an environment of 3 you
will see:

<<< .. 100 .. 200 .. 300 .. 310 .. 320 .. 330 .. 341 .. 342 .. 343 ..
344 .. 345 .. 346 .. 347 .. 350 .. 360 .. 370 .. 400 .. 500 .. 600 >>>

Everything except 344 will be a link. (I took an environment of 3
because 5 gives you a lot of links here...)

I will gladly listen to all suggestions. Note that this is tested code
and seems to work, but as always, if it blows, it's not the fault of the
friendly guy giving away stuff (ie: me).

kaspar - code philosopher

- -- stolen off the net --
It is odd, but on the infrequent occasions when I have been called upon
in a formal place to play the bongo drums, the introducer never seems to
find it necessary to mention that I also do theoretical physics.
		-- Richard Feynman
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAYwgXh6tlx0BWOuARAhwCAJ9Fi7QC7sbvHjDz99NZzpQ5JwWRdQCbBCdh
fcozKn3LJulFXc8761xsOsk=
=/xXF
-----END PGP SIGNATURE-----
-------------- next part --------------
##
# A ExtendedBatchedList paginates a list of items; as opposed to the
# simple BatchedList, this class only displays an environment of N pages
# to be accessed directly around the current one. 
#
# The user must render the currently displayed list and the page bar.
#
# Example:
#
# class MyList < Borges::Component
# 
#   def initialize(items)
#     @batcher = Borges::ExtendedBatchedList.new(items, 5)
#   end
#
#   def choose(item)
#     call(MyView.new(item)) # MyView is a component that views the item.
#   end
#
#   def renderContentOn(r)
#     r.list_do(@batcher.batch) do |item|
#       r.anchor(item.title do choose(item) end
#     end
#
#     r.render(@batcher)
#   end
# end

class Borges::ExtendedBatchedList < Borges::BatchedList

  DEFAULT_ENV = 5

  attr_accessor :environment

  ##
  # Create a new BatchedList from +items+, with +size+ items per page.

  def initialize(items = [], size = DEFAULT_SIZE, environment = DEFAULT_ENV)
    super(items, size)
    @environment = environment
  end
  
  # Decides if the page number n is displayed, given current page and 
  # environment size
  def should_display?(n)
    f = 1
    while f<max_pages
      if (n+1)%f==0 && (current_page-n).abs < environment*f
	return true
      end
      
      f*=10
    end
    return false
  end

  ##
  # Render the page selector for the batch.
  #
  # You must supply your own code to render the contents of the batch.  See
  # StoreItemList for an example.

  def render_content_on(r)
    return if max_pages == 0
    
    r.div_named('batch') do
      unless on_first_page? then
        r.anchor('<<') do previous_page end
      else
        r.text('<<')
      end

      r.space

      dots = false
      0.upto(max_pages - 1) do |i|
	unless should_display?(i) 
	  unless dots
	    r.space
	    r.text('...')
	  end
	  dots = true
	  next
	end
	
	dots = false
	
        r.space

        unless @current_page == i then
          r.anchor(i + 1) do @current_page = i end
        else
          r.bold(i + 1)
        end

      end

      r.space
      r.space

      unless on_last_page? then
        r.anchor('>>') do next_page end

      else
        r.text('>>')

      end
    end
  end

end



More information about the Borges-users mailing list