updated FML to improve Page chunking

This commit is contained in:
kremsy 2017-06-21 18:24:12 +02:00
parent 56ae4a8525
commit 652f46703b

View File

@ -19,428 +19,429 @@ use FML\Script\ScriptLabel;
class Paging extends ScriptFeature class Paging extends ScriptFeature
{ {
/* /*
* Constants * Constants
*/ */
const VAR_CURRENT_PAGE = "FML_Paging_CurrentPage"; const VAR_CURRENT_PAGE = "FML_Paging_CurrentPage";
const FUNCTION_UPDATE_CURRENT_PAGE = "FML_UpdateCurrentPage"; const FUNCTION_UPDATE_CURRENT_PAGE = "FML_UpdateCurrentPage";
/** /**
* @var Label $label Page number Label * @var Label $label Page number Label
*/ */
protected $label = null; protected $label = null;
/** /**
* @var PagingPage[] $pages Pages * @var PagingPage[] $pages Pages
*/ */
protected $pages = array(); protected $pages = array();
/** /**
* @var PagingButton[] $buttons Paging Buttons * @var PagingButton[] $buttons Paging Buttons
*/ */
protected $buttons = array(); protected $buttons = array();
/** /**
* @var int $startPageNumber Start Page number * @var int $startPageNumber Start Page number
*/ */
protected $startPageNumber = null; protected $startPageNumber = null;
/** /**
* @var int $customMaxPageNumber Custom maximum page number * @var int $customMaxPageNumber Custom maximum page number
*/ */
protected $customMaxPageNumber = null; protected $customMaxPageNumber = null;
/** /**
* @var string $previousChunkAction Previous chunk action name * @var string $previousChunkAction Previous chunk action name
*/ */
protected $previousChunkAction = null; protected $previousChunkAction = null;
/** /**
* @var string $nextChunkAction Next chunk action name * @var string $nextChunkAction Next chunk action name
*/ */
protected $nextChunkAction = null; protected $nextChunkAction = null;
/** /**
* @var bool $chunkActionAppendsPageNumber Chunk action appended with Page number * @var bool $chunkActionAppendsPageNumber Chunk action appended with Page number
*/ */
protected $chunkActionAppendsPageNumber = null; protected $chunkActionAppendsPageNumber = null;
/** /**
* Construct a new Paging * Construct a new Paging
* *
* @api * @api
* @param Label $label (optional) Page number Label * @param Label $label (optional) Page number Label
* @param PagingPage[] $pages (optional) Pages * @param PagingPage[] $pages (optional) Pages
* @param PagingButton[] $buttons (optional) Pageing Buttons * @param PagingButton[] $buttons (optional) Pageing Buttons
*/ */
public function __construct(Label $label = null, array $pages = null, array $buttons = null) public function __construct(Label $label = null, array $pages = null, array $buttons = null)
{ {
if ($label) { if ($label) {
$this->setLabel($label); $this->setLabel($label);
} }
if ($pages) { if ($pages) {
$this->setPages($pages); $this->setPages($pages);
} }
if ($buttons) { if ($buttons) {
$this->setButtons($buttons); $this->setButtons($buttons);
} }
} }
/** /**
* Get the Label showing the Page number * Get the Label showing the Page number
* *
* @api * @api
* @return Label * @return Label
*/ */
public function getLabel() public function getLabel()
{ {
return $this->label; return $this->label;
} }
/** /**
* Set the Label showing the Page number * Set the Label showing the Page number
* *
* @api * @api
* @param Label $label Page number Label * @param Label $label Page number Label
* @return static * @return static
*/ */
public function setLabel(Label $label) public function setLabel(Label $label)
{ {
$label->checkId(); $label->checkId();
$this->label = $label; $this->label = $label;
return $this; return $this;
} }
/** /**
* Get the Pages * Get the Pages
* *
* @api * @api
* @return PagingPage[] * @return PagingPage[]
*/ */
public function getPages() public function getPages()
{ {
return $this->pages; return $this->pages;
} }
/** /**
* Add a new Page Control * Add a new Page Control
* *
* @api * @api
* @param Control $pageControl Page Control * @param Control $pageControl Page Control
* @param string $pageNumber (optional) Page number * @param string $pageNumber (optional) Page number
* @return static * @return static
*/ */
public function addPageControl(Control $pageControl, $pageNumber = null) public function addPageControl(Control $pageControl, $pageNumber = null)
{ {
if ($pageNumber === null) { if ($pageNumber === null) {
$pageNumber = count($this->pages) + 1; $pageNumber = count($this->pages) + 1;
} }
$page = new PagingPage($pageControl, $pageNumber); $page = new PagingPage($pageControl, $pageNumber);
return $this->addPage($page); return $this->addPage($page);
} }
/** /**
* Add a new Page * Add a new Page
* *
* @api * @api
* @param PagingPage $page Page * @param PagingPage $page Page
* @return static * @return static
*/ */
public function addPage(PagingPage $page) public function addPage(PagingPage $page)
{ {
if (!in_array($page, $this->pages, true)) { if (!in_array($page, $this->pages, true)) {
array_push($this->pages, $page); array_push($this->pages, $page);
} }
return $this; return $this;
} }
/** /**
* Add new Pages * Add new Pages
* *
* @api * @api
* @param PagingPage[] $pages Pages * @param PagingPage[] $pages Pages
* @return static * @return static
*/ */
public function setPages(array $pages) public function setPages(array $pages)
{ {
$this->pages = array(); $this->pages = array();
foreach ($pages as $page) { foreach ($pages as $page) {
$this->addPage($page); $this->addPage($page);
} }
return $this; return $this;
} }
/** /**
* Get the Buttons * Get the Buttons
* *
* @api * @api
* @return PagingButton[] * @return PagingButton[]
*/ */
public function getButtons() public function getButtons()
{ {
return $this->buttons; return $this->buttons;
} }
/** /**
* Add a new Button Control to browse through the Pages * Add a new Button Control to browse through the Pages
* *
* @api * @api
* @param Control $buttonControl Button used for browsing * @param Control $buttonControl Button used for browsing
* @param int $browseAction (optional) Number of browsed Pages per click * @param int $browseAction (optional) Number of browsed Pages per click
* @return static * @return static
*/ */
public function addButtonControl(Control $buttonControl, $browseAction = null) public function addButtonControl(Control $buttonControl, $browseAction = null)
{ {
if ($browseAction === null) { if ($browseAction === null) {
$buttonCount = count($this->buttons); $buttonCount = count($this->buttons);
if ($buttonCount % 2 === 0) { if ($buttonCount % 2 === 0) {
$browseAction = $buttonCount / 2 + 1; $browseAction = $buttonCount / 2 + 1;
} else { } else {
$browseAction = $buttonCount / -2 - 1; $browseAction = $buttonCount / -2 - 1;
} }
} }
$button = new PagingButton($buttonControl, $browseAction); $button = new PagingButton($buttonControl, $browseAction);
return $this->addButton($button); return $this->addButton($button);
} }
/** /**
* Add a new Button to browse through Pages * Add a new Button to browse through Pages
* *
* @api * @api
* @param PagingButton $button Paging Button * @param PagingButton $button Paging Button
* @return static * @return static
*/ */
public function addButton(PagingButton $button) public function addButton(PagingButton $button)
{ {
if (!in_array($button, $this->buttons, true)) { if (!in_array($button, $this->buttons, true)) {
array_push($this->buttons, $button); array_push($this->buttons, $button);
} }
return $this; return $this;
} }
/** /**
* Set the Paging Buttons * Set the Paging Buttons
* *
* @api * @api
* @param PagingButton[] $buttons Paging Buttons * @param PagingButton[] $buttons Paging Buttons
* @return static * @return static
*/ */
public function setButtons(array $buttons) public function setButtons(array $buttons)
{ {
$this->buttons = array(); $this->buttons = array();
foreach ($buttons as $button) { foreach ($buttons as $button) {
$this->addButton($button); $this->addButton($button);
} }
return $this; return $this;
} }
/** /**
* Get the start Page number * Get the start Page number
* *
* @api * @api
* @return int * @return int
*/ */
public function getStartPageNumber() public function getStartPageNumber()
{ {
return $this->startPageNumber; return $this->startPageNumber;
} }
/** /**
* Set the start Page number * Set the start Page number
* *
* @api * @api
* @param int $startPageNumber Page number to start with * @param int $startPageNumber Page number to start with
* @return static * @return static
*/ */
public function setStartPageNumber($startPageNumber) public function setStartPageNumber($startPageNumber)
{ {
$this->startPageNumber = (int)$startPageNumber; $this->startPageNumber = (int)$startPageNumber;
return $this; return $this;
} }
/** /**
* Get a custom maximum Page number for using chunks * Get a custom maximum Page number for using chunks
* *
* @api * @api
* @return int * @return int
*/ */
public function getCustomMaxPageNumber() public function getCustomMaxPageNumber()
{ {
return $this->customMaxPageNumber; return $this->customMaxPageNumber;
} }
/** /**
* Set a custom maximum Page number for using chunks * Set a custom maximum Page number for using chunks
* *
* @api * @api
* @param int $maxPageNumber Custom maximum Page number * @param int $maxPageNumber Custom maximum Page number
* @return static * @return static
*/ */
public function setCustomMaxPageNumber($maxPageNumber) public function setCustomMaxPageNumber($maxPageNumber)
{ {
$this->customMaxPageNumber = (int)$maxPageNumber; $this->customMaxPageNumber = (int)$maxPageNumber;
return $this; return $this;
} }
/** /**
* Get the action triggered when the previous chunk is needed * Get the action triggered when the previous chunk is needed
* *
* @api * @api
* @return string * @return string
*/ */
public function getPreviousChunkAction() public function getPreviousChunkAction()
{ {
return $this->previousChunkAction; return $this->previousChunkAction;
} }
/** /**
* Set the action triggered when the previous chunk is needed * Set the action triggered when the previous chunk is needed
* *
* @api * @api
* @param string $previousChunkAction Triggered action * @param string $previousChunkAction Triggered action
* @return static * @return static
*/ */
public function setPreviousChunkAction($previousChunkAction) public function setPreviousChunkAction($previousChunkAction)
{ {
$this->previousChunkAction = (string)$previousChunkAction; $this->previousChunkAction = (string)$previousChunkAction;
return $this; return $this;
} }
/** /**
* Get the action triggered when the next chunk is needed * Get the action triggered when the next chunk is needed
* *
* @api * @api
* @return string * @return string
*/ */
public function getNextChunkAction() public function getNextChunkAction()
{ {
return $this->nextChunkAction; return $this->nextChunkAction;
} }
/** /**
* Set the action triggered when the next chunk is needed * Set the action triggered when the next chunk is needed
* *
* @api * @api
* @param string $nextChunkAction Triggered action * @param string $nextChunkAction Triggered action
* @return static * @return static
*/ */
public function setNextChunkAction($nextChunkAction) public function setNextChunkAction($nextChunkAction)
{ {
$this->nextChunkAction = (string)$nextChunkAction; $this->nextChunkAction = (string)$nextChunkAction;
return $this; return $this;
} }
/** /**
* Set the actions triggered when another chunk is needed * Set the actions triggered when another chunk is needed
* *
* @api * @api
* @param string $chunkAction Triggered action * @param string $chunkAction Triggered action
* @return static * @return static
*/ */
public function setChunkActions($chunkAction) public function setChunkActions($chunkAction)
{ {
return $this->setNextChunkAction($chunkAction) return $this->setNextChunkAction($chunkAction)
->setPreviousChunkAction($chunkAction); ->setPreviousChunkAction($chunkAction);
} }
/** /**
* Get if the chunk action should append the needed Page number * Get if the chunk action should append the needed Page number
* *
* @api * @api
* @return bool * @return bool
*/ */
public function getChunkActionAppendsPageNumber() public function getChunkActionAppendsPageNumber()
{ {
return $this->chunkActionAppendsPageNumber; return $this->chunkActionAppendsPageNumber;
} }
/** /**
* Set if the chunk action should append the needed Page number * Set if the chunk action should append the needed Page number
* *
* @api * @api
* @param bool $appendPageNumber Append the needed Page number * @param bool $appendPageNumber Append the needed Page number
* @return static * @return static
*/ */
public function setChunkActionAppendsPageNumber($appendPageNumber) public function setChunkActionAppendsPageNumber($appendPageNumber)
{ {
$this->chunkActionAppendsPageNumber = (bool)$appendPageNumber; $this->chunkActionAppendsPageNumber = (bool)$appendPageNumber;
return $this; return $this;
} }
/** /**
* @see ScriptFeature::prepare() * @see ScriptFeature::prepare()
*/ */
public function prepare(Script $script) public function prepare(Script $script)
{ {
if (empty($this->pages)) { if (empty($this->pages)) {
return $this; return $this;
} }
$script->setScriptInclude(ScriptInclude::TEXTLIB); $script->setScriptInclude(ScriptInclude::TEXTLIB);
$currentPageVariable = self::VAR_CURRENT_PAGE; $currentPageVariable = self::VAR_CURRENT_PAGE;
$updatePageFunction = self::FUNCTION_UPDATE_CURRENT_PAGE; $updatePageFunction = self::FUNCTION_UPDATE_CURRENT_PAGE;
$minPageNumber = 1; $minPageNumber = 1;
$startPageNumber = (is_int($this->startPageNumber) ? $this->startPageNumber : $minPageNumber); $startPageNumber = (is_int($this->startPageNumber) ? $this->startPageNumber : $minPageNumber);
$maxPage = $this->getMaxPage(); $maxPage = $this->getMaxPage();
$maxPageNumber = $this->customMaxPageNumber; $maxPageNumber = $this->customMaxPageNumber;
if (!is_int($maxPageNumber)) { if (!is_int($maxPageNumber)) {
$maxPageNumber = $maxPage->getPageNumber(); $maxPageNumber = $maxPage->getPageNumber();
} }
$pagingId = $maxPage->getControl() $pagingId = $maxPage->getControl()
->getId(true, true); ->getId(true, true);
$pagingId = Builder::escapeText($maxPage->getControl() $pagingId = Builder::escapeText($maxPage->getControl()
->getId()); ->getId());
$pageLabelId = Builder::EMPTY_STRING; $pageLabelId = Builder::EMPTY_STRING;
if ($this->label) { if ($this->label) {
$pageLabelId = Builder::escapeText($this->label->getId()); $pageLabelId = Builder::escapeText($this->label->getId());
} }
$pagesArrayText = $this->getPagesArrayText(); $pagesArrayText = $this->getPagesArrayText();
$pageButtonsArrayText = $this->getPageButtonsArrayText(); $pageButtonsArrayText = $this->getPageButtonsArrayText();
$previousChunkAction = Builder::escapeText($this->previousChunkAction); $previousChunkAction = Builder::escapeText($this->previousChunkAction);
$nextChunkAction = Builder::escapeText($this->nextChunkAction); $nextChunkAction = Builder::escapeText($this->nextChunkAction);
$chunkActionAppendsPageNumber = Builder::getBoolean($this->chunkActionAppendsPageNumber); $chunkActionAppendsPageNumber = Builder::getBoolean($this->chunkActionAppendsPageNumber);
// Init // Init
$initScriptText = " $initScriptText = "
declare {$currentPageVariable} for This = Integer[Text]; declare {$currentPageVariable} for This = Integer[Text];
{$currentPageVariable}[{$pagingId}] = {$startPageNumber}; {$currentPageVariable}[{$pagingId}] = {$startPageNumber};
{$updatePageFunction}({$pagingId}, {$pageLabelId}, 0, {$minPageNumber}, {$maxPageNumber}, {$pagesArrayText}, {$previousChunkAction}, {$nextChunkAction}, {$chunkActionAppendsPageNumber});"; {$updatePageFunction}({$pagingId}, {$pageLabelId}, 0, {$minPageNumber}, {$maxPageNumber}, {$pagesArrayText}, {$previousChunkAction}, {$nextChunkAction}, {$chunkActionAppendsPageNumber});";
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $initScriptText, true); $script->appendGenericScriptLabel(ScriptLabel::ONINIT, $initScriptText, true);
// MouseClick // MouseClick
$clickScriptText = " $clickScriptText = "
declare PageButtons = {$pageButtonsArrayText}; declare PageButtons = {$pageButtonsArrayText};
if (PageButtons.existskey(Event.Control.ControlId)) { if (PageButtons.existskey(Event.Control.ControlId)) {
declare BrowseAction = PageButtons[Event.Control.ControlId]; declare BrowseAction = PageButtons[Event.Control.ControlId];
{$updatePageFunction}({$pagingId}, {$pageLabelId}, BrowseAction, {$minPageNumber}, {$maxPageNumber}, {$pagesArrayText}, {$previousChunkAction}, {$nextChunkAction}, {$chunkActionAppendsPageNumber}); {$updatePageFunction}({$pagingId}, {$pageLabelId}, BrowseAction, {$minPageNumber}, {$maxPageNumber}, {$pagesArrayText}, {$previousChunkAction}, {$nextChunkAction}, {$chunkActionAppendsPageNumber});
}"; }";
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $clickScriptText, true); $script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $clickScriptText, true);
// Update function // Update function
$functionText = " $functionText = "
Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAction, Integer _MinPageNumber, Integer _MaxPageNumber, Text[Integer] _Pages, Text _PreviousChunkAction, Text _NextChunkAction, Boolean _ChunkActionAppendPageNumber) { Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAction, Integer _MinPageNumber, Integer _MaxPageNumber, Text[Integer] _Pages, Text _PreviousChunkAction, Text _NextChunkAction, Boolean _ChunkActionAppendPageNumber) {
declare {$currentPageVariable} for This = Integer[Text]; declare {$currentPageVariable} for This = Integer[Text];
if (!{$currentPageVariable}.existskey(_PagingId)) return; if (!{$currentPageVariable}.existskey(_PagingId)) return;
declare CurrentPage = {$currentPageVariable}[_PagingId] + _BrowseAction; declare NewPageNumber = {$currentPageVariable}[_PagingId] + _BrowseAction;
if (CurrentPage < _MinPageNumber) { if (NewPageNumber < _MinPageNumber) {
CurrentPage = _MinPageNumber; NewPageNumber = _MinPageNumber;
} else if (CurrentPage > _MaxPageNumber) { } else if (NewPageNumber > _MaxPageNumber) {
CurrentPage = _MaxPageNumber; NewPageNumber = _MaxPageNumber;
} }
{$currentPageVariable}[_PagingId] = CurrentPage; {$currentPageVariable}[_PagingId] = NewPageNumber;
declare PageFound = False; if (_Pages.existskey(NewPageNumber)) {
foreach (PageNumber => PageId in _Pages) { foreach (PageNumber => PageId in _Pages) {
declare PageControl <=> Page.GetFirstChild(PageId); declare PageControl <=> Page.GetFirstChild(PageId);
PageControl.Visible = (CurrentPage == PageNumber); PageControl.Visible = (PageNumber == NewPageNumber);
if (PageControl.Visible) { }
PageFound = True; if (_PageLabelId != \"\") {
} declare PageLabel <=> (Page.GetFirstChild(_PageLabelId) as CMlLabel);
} PageLabel.Value = NewPageNumber^\"/\"^_MaxPageNumber;
if (!PageFound && _BrowseAction != 0) { }
} else {
declare Text ChunkAction; declare Text ChunkAction;
if (_BrowseAction < 0) { if (_BrowseAction < 0) {
ChunkAction = _PreviousChunkAction; ChunkAction = _PreviousChunkAction;
@ -448,91 +449,87 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
ChunkAction = _NextChunkAction; ChunkAction = _NextChunkAction;
} }
if (_ChunkActionAppendPageNumber) { if (_ChunkActionAppendPageNumber) {
ChunkAction ^= CurrentPage; ChunkAction ^= NewPageNumber;
} }
TriggerPageAction(ChunkAction); TriggerPageAction(ChunkAction);
} }
if (_PageLabelId == " . Builder::EMPTY_STRING . ") return;
declare PageLabel <=> (Page.GetFirstChild(_PageLabelId) as CMlLabel);
if (PageLabel == Null) return;
PageLabel.Value = CurrentPage^\"/\"^_MaxPageNumber;
}"; }";
$script->addScriptFunction($updatePageFunction, $functionText); $script->addScriptFunction($updatePageFunction, $functionText);
return $this; return $this;
} }
/** /**
* Get the minimum Page * Get the minimum Page
* *
* @return PagingPage * @return PagingPage
*/ */
protected function getMinPage() protected function getMinPage()
{ {
$minPageNumber = null; $minPageNumber = null;
$minPage = null; $minPage = null;
foreach ($this->pages as $page) { foreach ($this->pages as $page) {
$pageNumber = $page->getPageNumber(); $pageNumber = $page->getPageNumber();
if ($minPageNumber === null || $pageNumber < $minPageNumber) { if ($minPageNumber === null || $pageNumber < $minPageNumber) {
$minPageNumber = $pageNumber; $minPageNumber = $pageNumber;
$minPage = $page; $minPage = $page;
} }
} }
return $minPage; return $minPage;
} }
/** /**
* Get the maximum Page * Get the maximum Page
* *
* @return PagingPage * @return PagingPage
*/ */
protected function getMaxPage() protected function getMaxPage()
{ {
$maxPageNumber = null; $maxPageNumber = null;
$maxPage = null; $maxPage = null;
foreach ($this->pages as $page) { foreach ($this->pages as $page) {
$pageNumber = $page->getPageNumber(); $pageNumber = $page->getPageNumber();
if ($maxPageNumber === null || $pageNumber > $maxPageNumber) { if ($maxPageNumber === null || $pageNumber > $maxPageNumber) {
$maxPageNumber = $pageNumber; $maxPageNumber = $pageNumber;
$maxPage = $page; $maxPage = $page;
} }
} }
return $maxPage; return $maxPage;
} }
/** /**
* Build the array text for the Pages * Build the array text for the Pages
* *
* @return string * @return string
*/ */
protected function getPagesArrayText() protected function getPagesArrayText()
{ {
if (empty($this->pages)) { if (empty($this->pages)) {
return Builder::getArray(array(0 => ''), true); return Builder::getArray(array(0 => ''), true);
} }
$pages = array(); $pages = array();
foreach ($this->pages as $page) { foreach ($this->pages as $page) {
$pages[$page->getPageNumber()] = $page->getControl() $pages[$page->getPageNumber()] = $page->getControl()
->getId(); ->getId();
} }
return Builder::getArray($pages, true); return Builder::getArray($pages, true);
} }
/** /**
* Build the array text for the Page Buttons * Build the array text for the Page Buttons
* *
* @return string * @return string
*/ */
protected function getPageButtonsArrayText() protected function getPageButtonsArrayText()
{ {
if (empty($this->buttons)) { if (empty($this->buttons)) {
return Builder::getArray(array('' => 0), true); return Builder::getArray(array('' => 0), true);
} }
$pageButtons = array(); $pageButtons = array();
foreach ($this->buttons as $pageButton) { foreach ($this->buttons as $pageButton) {
$pageButtons[$pageButton->getControl() $pageButtons[$pageButton->getControl()
->getId()] = $pageButton->getPagingCount(); ->getId()] = $pageButton->getPagingCount();
} }
return Builder::getArray($pageButtons, true); return Builder::getArray($pageButtons, true);
} }
} }