/lib/wiki-plugins/wikiplugin_shorten.php

https://gitlab.com/ElvisAns/tiki · PHP · 174 lines · 148 code · 21 blank · 5 comment · 8 complexity · 020058759f543dc6435f1d0e558c998b MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. function wikiplugin_shorten_info()
  8. {
  9. return [
  10. 'name' => tra('Shorten'),
  11. 'documentation' => 'Shorten',
  12. 'description' => tra('Show/hide a portion of text'),
  13. 'prefs' => ['wikiplugin_shorten', 'wikiplugin_button'],
  14. 'body' => tra('Code to be displayed'),
  15. 'iconname' => 'shorten',
  16. 'introduced' => 17,
  17. 'filter' => 'rawhtml_unsafe',
  18. 'format' => 'html',
  19. 'tags' => [ 'basic' ],
  20. 'params' => [
  21. 'length' => [
  22. 'required' => true,
  23. 'name' => tra('Length'),
  24. 'description' => tra('Char length of always visible portion of text.'),
  25. 'since' => '17',
  26. 'filter' => 'digits',
  27. ],
  28. 'moreText' => [
  29. 'required' => false,
  30. 'name' => tra('More text'),
  31. 'description' => tra('A label indicating the text is expandable.'),
  32. 'since' => '17',
  33. 'filter' => 'text',
  34. ],
  35. 'lessText' => [
  36. 'required' => false,
  37. 'name' => tra('Less text'),
  38. 'description' => tra('A label for collapse text action.'),
  39. 'since' => '17',
  40. 'filter' => 'text',
  41. ],
  42. 'show_speed' => [
  43. 'required' => false,
  44. 'name' => tra('Show Speed'),
  45. 'filter' => 'alnum',
  46. 'description' => tr('Speed of animation in milliseconds when showing content (%0200%1 is fast and
  47. %0600%1 is slow. %01000%1 equals 1 second).', '<code>', '</code>'),
  48. 'default' => 0,
  49. 'since' => '17',
  50. 'accepted' => tr(
  51. 'Integer greater than 0 and less than or equal to 1000, or %0 or %1',
  52. '<code>fast</code>',
  53. '<code>slow</code>'
  54. ),
  55. 'advanced' => true,
  56. ],
  57. 'hide_speed' => [
  58. 'required' => false,
  59. 'name' => tra('Hide Speed'),
  60. 'filter' => 'alnum',
  61. 'description' => tr('Speed of animation in milliseconds when hiding content (%0200%1 is fast and
  62. %0600%1 is slow. %01000%1 equals 1 second).', '<code>', '</code>'),
  63. 'default' => 0,
  64. 'since' => '17',
  65. 'accepted' => tr(
  66. 'Integer greater than 0 and less than or equal to 1000, or %0 or %1',
  67. '<code>fast</code>',
  68. '<code>slow</code>'
  69. ),
  70. 'advanced' => true,
  71. ],
  72. ],
  73. ];
  74. }
  75. function wikiplugin_shorten($data, $params)
  76. {
  77. static $shorten_count;
  78. $headerlib = TikiLib::lib('header');
  79. $parserlib = TikiLib::lib('parser');
  80. extract([
  81. 'length' => 20,
  82. 'moreText' => '[+]',
  83. 'lessText' => '[-]',
  84. 'show_speed' => 0,
  85. 'hide_speed' => 0,
  86. ]);
  87. if (isset($params['length'])) {
  88. $length = (int) sprintf('%d', $params['length']);
  89. $length = max(1, $length);
  90. }
  91. if (isset($params['show_speed'])) {
  92. $show_speed = str_replace(['slow', 'fast'], ['600', '200'], $params['show_speed']);
  93. $show_speed = sprintf(' data-show-speed="%d"', $show_speed);
  94. }
  95. if (isset($params['hide_speed'])) {
  96. $hide_speed = str_replace(['slow', 'fast'], ['600', '200'], $params['hide_speed']);
  97. $hide_speed = sprintf(' data-hide-speed="%d"', $hide_speed);
  98. }
  99. $match = null;
  100. if (preg_match('/^\s*.{' . $length . '}[^\s]*/', $data, $match)) {
  101. if (! $shorten_count) {
  102. $headerlib->add_css(
  103. ".wikiplugin-shorten .content { display: none; }"
  104. . ".wikiplugin-shorten .btn_less, .wikiplugin-shorten .btn_more { cursor: pointer; margin-left: 0.5em; }"
  105. . ".wikiplugin-shorten .btn_less { display: none; }"
  106. );
  107. $headerlib->add_jq_onready(
  108. '$(".wikiplugin-shorten").each(function(){'
  109. . 'var $this = $(this);'
  110. . 'var $sample = $this.find("> .sample");'
  111. . 'var $content = $this.find("> .content");'
  112. . 'var show_speed = $this.data("show-speed") || 0;'
  113. . 'var hide_speed = $this.data("hide-speed") || 0;'
  114. . 'var $btn_more = $sample.find(".btn_more:first");'
  115. . 'var $btn_less = $content.find(".btn_less:last");'
  116. . '$btn_more.click(function(){'
  117. . '$sample.hide();'
  118. . '$btn_more.hide();'
  119. . '$btn_less.show();'
  120. . '$content.show(show_speed);'
  121. . 'return false;'
  122. . '});'
  123. . '$btn_less.click(function(){'
  124. . '$sample.show();'
  125. . '$btn_less.hide();'
  126. . '$btn_more.show();'
  127. . '$content.hide(hide_speed);'
  128. . 'return false;'
  129. . '});'
  130. . '});'
  131. );
  132. }
  133. $shorten_count += 1;
  134. if (isset($params['moreText'])) {
  135. $moreText = strip_tags($params['moreText']);
  136. }
  137. if (isset($params['lessText'])) {
  138. $lessText = strip_tags($params['lessText']);
  139. }
  140. $html = '<div class="wikiplugin-shorten"' . $show_speed . $hide_speed . '>';
  141. $html .= '<div class="sample" data-btn-more="' . $moreText . '">%s</div>';
  142. $html .= '<div class="content" data-btn-less="' . $lessText . '">%s</div>';
  143. $html .= '</div>';
  144. $index = strlen($match[0]);
  145. $sample = $parserlib->parse_data_plugin(substr($data, 0, $index));
  146. $sample .= '<a href="#" class="btn_more btn btn-primary">' . $moreText . '</a>';
  147. $content = $parserlib->parse_data_plugin($data) . '<a href="#" class="btn_less btn btn-primary">' . $lessText . '</a>';
  148. $out = sprintf($html, $sample, $content);
  149. return $out;
  150. } else { // short enough already
  151. return $parserlib->parse_data_plugin($data);
  152. }
  153. }