📄 pager.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: pager.cpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 19:15:56 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.39 * PRODUCTION * =========================================================================== *//* $Id: pager.cpp,v 1000.2 2004/06/01 19:15:56 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Eugene Vasilchenko, Anton Golikov * */#include <ncbi_pch.hpp>#include <corelib/ncbistd.hpp>#include <cgi/ncbicgi.hpp>#include <html/pager.hpp>BEGIN_NCBI_SCOPEconst string CPager::KParam_PageSize = "dispmax";const string CPager::KParam_ShownPageSize = "showndispmax";const string CPager::KParam_DisplayPage = "page";const string CPager::KParam_Page = "page ";const string CPager::KParam_PreviousPages = "previous pages";const string CPager::KParam_NextPages = "next pages";const string CPager::KParam_InputPage = "inputpage";CPager::CPager(const CCgiRequest& request, int pageBlockSize, int defaultPageSize, EPagerView view /* = eImage */) : m_PageSize(GetPageSize(request, defaultPageSize)), m_PageBlockSize(max(1, pageBlockSize)), m_PageChanged(false), m_view(view){ const TCgiEntries& entries = request.GetEntries(); if ( IsPagerCommand(request) ) { // look in preprocessed IMAGE values with empty string key TCgiEntriesCI i = entries.find(NcbiEmptyString); if (i != entries.end()) { const string& value = i->second; if (value == KParam_PreviousPages) { // previous pages // round to previous page block m_PageChanged = true; int page = GetDisplayedPage(request); m_DisplayPage = page - page % m_PageBlockSize - 1; } else if (value == KParam_NextPages) { // next pages // round to next page block m_PageChanged = true; int page = GetDisplayedPage(request); m_DisplayPage = page - page % m_PageBlockSize + m_PageBlockSize; } else if ( NStr::StartsWith(value, KParam_Page) ) { // look for params like: "page 2" string page = value.substr(KParam_Page.size()); try { m_DisplayPage = NStr::StringToInt(page) - 1; m_PageChanged = true; } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::CPager: " << e.what() ); m_PageChanged = false; } } } i = entries.find(KParam_InputPage); if (i != entries.end()) { try { m_DisplayPage = NStr::StringToInt(i->second) - 1; m_DisplayPage = max(m_DisplayPage, 0); m_PageChanged = true; } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::IsPagerCommand: " << e.what() ); m_PageChanged = false; } } } else { try { m_PageChanged = true; int page = GetDisplayedPage(request); TCgiEntriesCI oldPageSize = entries.find(KParam_ShownPageSize); if( !page || oldPageSize == entries.end() ) throw runtime_error("Error getting page params"); //number of the first element in old pagination int oldFirstItem = page * NStr::StringToInt( oldPageSize->second ); m_DisplayPage = oldFirstItem / m_PageSize; } catch(exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::CPager: " << e.what() ); m_DisplayPage = 0; m_PageChanged = false; } } if( !m_PageChanged ) m_DisplayPage = GetDisplayedPage(request); m_PageBlockStart = m_DisplayPage - m_DisplayPage % m_PageBlockSize;}bool CPager::IsPagerCommand(const CCgiRequest& request){ TCgiEntries& entries = const_cast<TCgiEntries&>(request.GetEntries()); // look in preprocessed IMAGE values with empty string key TCgiEntriesI i = entries.find(NcbiEmptyString); if (i != entries.end()) { const string& value = i->second.GetValue(); if (value == KParam_PreviousPages) { // previous pages return true; } else if (value == KParam_NextPages) { // next pages return true; } else if ( NStr::StartsWith(value, KParam_Page) ) { // look for params like: "page 2" string page = value.substr(KParam_Page.size()); try { NStr::StringToInt(page); return true; } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::IsPagerCommand: " << e.what() ); } } } i = entries.find(KParam_InputPage); if (i != entries.end()) { try { NStr::StringToInt(i->second.GetValue()); return true; } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::IsPagerCommand: " << e.what() ); } } return false;}int CPager::GetDisplayedPage(const CCgiRequest& request){ const TCgiEntries& entries = request.GetEntries(); TCgiEntriesCI entry = entries.find(KParam_DisplayPage); if (entry != entries.end()) { try { int displayPage = NStr::StringToInt(entry->second); if ( displayPage >= 0 ) return displayPage; _TRACE( "Negative page start in CPager::GetDisplayedPage: " << displayPage ); } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::GetDisplayedPage " << e.what() ); } } // use default page start return 0;}int CPager::GetPageSize(const CCgiRequest& request, int defaultPageSize){ TCgiEntries& entries = const_cast<TCgiEntries&>(request.GetEntries()); TCgiEntriesCI entry; if( IsPagerCommand(request) ) { entry = entries.find(KParam_ShownPageSize); } else { entry = entries.find(KParam_PageSize); } if (entry != entries.end()) { try { string dispMax = entry->second; int pageSize = NStr::StringToInt(dispMax); if( pageSize > 0 ) { //replace dispmax for current page size entries.erase(KParam_PageSize); entries.insert(TCgiEntries::value_type(KParam_PageSize, dispMax)); return pageSize; } _TRACE( "Nonpositive page size in CPager::GetPageSize: " << pageSize ); } catch (exception& _DEBUG_ARG(e)) { _TRACE( "Exception in CPager::GetPageSize " << e.what() ); } } // use default page size return defaultPageSize;}void CPager::SetItemCount(int itemCount){ m_ItemCount = itemCount; if (m_DisplayPage * m_PageSize >= itemCount) { m_DisplayPage = 0; }}pair<int, int> CPager::GetRange(void) const{ int firstItem = m_DisplayPage * m_PageSize; return pair<int, int>(firstItem, min(firstItem + m_PageSize, m_ItemCount));}void CPager::CreateSubNodes(void){ AppendChild(new CHTML_hidden(KParam_ShownPageSize, m_PageSize)); AppendChild(new CHTML_hidden(KParam_DisplayPage, m_DisplayPage));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -