web/views/navigation.py
branchstable
changeset 5615 cfa9a776d99a
parent 5424 8ecbcbff9777
child 5616 859225caf375
equal deleted inserted replaced
5614:37b31a7b8e74 5615:cfa9a776d99a
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """navigation components definition for CubicWeb web client
    18 """navigation components definition for CubicWeb web client"""
    19 
    19 
    20 """
       
    21 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    22 _ = unicode
    21 _ = unicode
    23 
    22 
    24 from rql.nodes import VariableRef, Constant
    23 from rql.nodes import VariableRef, Constant
    25 
    24 
    36 
    35 
    37 class PageNavigation(NavigationComponent):
    36 class PageNavigation(NavigationComponent):
    38 
    37 
    39     def call(self):
    38     def call(self):
    40         """displays a resultset by page"""
    39         """displays a resultset by page"""
    41         w = self.w
    40         params = dict(self._cw.form)
    42         req = self._cw
    41         self.clean_params(params)
       
    42         basepath = self._cw.relative_path(includeparams=False)
       
    43         self.w(u'<div class="pagination">')
       
    44         self.w(u'%s&#160;' % self.previous_link(basepath, params))
       
    45         self.w(u'[&#160;%s&#160;]' %
       
    46                u'&#160;| '.join(self.iter_page_links(basepath, params)))
       
    47         self.w(u'&#160;%s' % self.next_link(basepath, params))
       
    48         self.w(u'</div>')
       
    49 
       
    50     def index_display(self, start, stop):
       
    51         return u'%s - %s' % (start+1, stop+1)
       
    52 
       
    53     def iter_page_links(self, basepath, params):
    43         rset = self.cw_rset
    54         rset = self.cw_rset
    44         page_size = self.page_size
    55         page_size = self.page_size
    45         start = 0
    56         start = 0
    46         blocklist = []
       
    47         params = dict(req.form)
       
    48         self.clean_params(params)
       
    49         basepath = req.relative_path(includeparams=False)
       
    50         while start < rset.rowcount:
    57         while start < rset.rowcount:
    51             stop = min(start + page_size - 1, rset.rowcount - 1)
    58             stop = min(start + page_size - 1, rset.rowcount - 1)
    52             blocklist.append(self.page_link(basepath, params, start, stop,
    59             yield self.page_link(basepath, params, start, stop,
    53                                             self.index_display(start, stop)))
    60                                  self.index_display(start, stop))
    54             start = stop + 1
    61             start = stop + 1
       
    62 
       
    63 
       
    64 class PageNavigationSelect(PageNavigation):
       
    65     """displays a resultset by page as PageNavigationSelect but in a <select>,
       
    66     better when there are a lot of results.
       
    67     """
       
    68     __select__ = paginated_rset(4)
       
    69 
       
    70     page_link_templ = u'<option value="%s" title="%s">%s</option>'
       
    71     selected_page_link_templ = u'<option value="%s" selected="selected" title="%s">%s</option>'
       
    72     def call(self):
       
    73         params = dict(self._cw.form)
       
    74         self.clean_params(params)
       
    75         basepath = self._cw.relative_path(includeparams=False)
       
    76         w = self.w
    55         w(u'<div class="pagination">')
    77         w(u'<div class="pagination">')
    56         w(u'%s&#160;' % self.previous_link(basepath, params))
    78         w(u'%s&#160;' % self.previous_link(basepath, params))
    57         w(u'[&#160;%s&#160;]' % u'&#160;| '.join(blocklist))
    79         w(u'<select onchange="javascript: document.location=this.options[this.selectedIndex].value">')
       
    80         for option in self.iter_page_links(basepath, params):
       
    81             w(option)
       
    82         w(u'</select>')
    58         w(u'&#160;%s' % self.next_link(basepath, params))
    83         w(u'&#160;%s' % self.next_link(basepath, params))
    59         w(u'</div>')
    84         w(u'</div>')
    60 
       
    61     def index_display(self, start, stop):
       
    62         return u'%s - %s' % (start+1, stop+1)
       
    63 
    85 
    64 
    86 
    65 class SortedNavigation(NavigationComponent):
    87 class SortedNavigation(NavigationComponent):
    66     """sorted navigation apply if navigation is needed (according to page size)
    88     """sorted navigation apply if navigation is needed (according to page size)
    67     and if the result set is sorted
    89     and if the result set is sorted