moremenu.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * A menu with a More... button to show more elements
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Widget
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * A menu with a More... element to show more items
  37. *
  38. * @category General
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class MoreMenu extends Menu
  46. {
  47. const SOFT_MAX = 5;
  48. const HARD_MAX = 15;
  49. /**
  50. * Show a menu with a limited number of elements
  51. *
  52. * @param
  53. *
  54. * @return
  55. */
  56. function show()
  57. {
  58. $items = $this->getItems();
  59. $tag = $this->tag();
  60. $menuID = null;
  61. $attrs = array('class' => 'nav');
  62. if (!is_null($tag)) {
  63. $menuID = 'nav_' . $tag;
  64. $attrs['id'] = $menuID;
  65. }
  66. if (Event::handle('StartNav', array($this, &$tag, &$items))) {
  67. $this->out->elementStart('ul', $attrs);
  68. $total = count($items);
  69. if ($total <= self::SOFT_MAX + 1) {
  70. $toShow = $items;
  71. } else {
  72. $toShow = array_slice($items, 0, self::SOFT_MAX);
  73. }
  74. foreach ($toShow as $item) {
  75. if (count($item) == 5) {
  76. list($actionName, $args, $label, $description, $id) = $item;
  77. } else {
  78. list($actionName, $args, $label, $description) = $item;
  79. $id = null;
  80. }
  81. $this->item($actionName, $args, $label, $description, $id);
  82. }
  83. if ($total > self::SOFT_MAX + 1) {
  84. $this->out->elementStart('li', array('class' => 'more_link'));
  85. $this->out->element('a', array('href' => '#',
  86. 'onclick' => 'SN.U.showMoreMenuItems("'.$menuID.'"); return false;'),
  87. // TRANS: Link description to show more items in a list.
  88. _('More ▼'));
  89. $this->out->elementEnd('li');
  90. $extended = array_slice($items, self::SOFT_MAX, self::HARD_MAX - self::SOFT_MAX);
  91. foreach ($extended as $item) {
  92. if (count($item) == 5) {
  93. list($actionName, $args, $label, $description, $id) = $item;
  94. } else {
  95. list($actionName, $args, $label, $description) = $item;
  96. $id = null;
  97. }
  98. $this->item($actionName, $args, $label, $description, $id, 'extended_menu');
  99. }
  100. if ($total > self::HARD_MAX) {
  101. $seeAll = $this->seeAllItem();
  102. if (!empty($seeAll)) {
  103. if (count($seeAll) == 5) {
  104. list($actionName, $args, $label, $description, $id) = $seeAll;
  105. } else {
  106. list($actionName, $args, $label, $description) = $seeAll;
  107. $id = null;
  108. }
  109. $this->item($actionName, $args, $label, $description, $id, 'extended_menu see_all');
  110. }
  111. }
  112. }
  113. $this->out->elementEnd('ul');
  114. Event::handle('EndNav', array($this, $tag, $items));
  115. }
  116. }
  117. function seeAllItem()
  118. {
  119. return null;
  120. }
  121. }