SidePanelButton.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // UI::hAttributes($params);
  3. // UI::hChildrens($childrens);
  4. // - [x] mode = 'static'
  5. // - [ ] mode = 'dynamic'
  6. class UI_SidePanelButton implements UITagInterface {
  7. /**
  8. * @param string $tagName = 'p5:SidePanelButton'
  9. * @param array $props
  10. * @param array $childrens
  11. *
  12. * @return string html code
  13. */
  14. static function h($tagName, $props = [], $childrens = []) {
  15. $jsFuncName = "p5UI__openSideBar"; // TODO: generate name based on $props['name']
  16. if (empty($props['label'])) throw new Exception("Missing 'label'");
  17. $mode = V::get('mode', 'static', $props);
  18. $name = (!empty($props['name'])) ? $props['name'] : self::generateUniqueName();
  19. $idHtmlNode = "p5__js-p5-side_panel-{$name}";
  20. switch ($mode) {
  21. case 'static': {
  22. if (empty($props['title'])) throw new Exception("Missing 'title'");
  23. if (empty($childrens)) throw new Exception("Missing childrens");
  24. } break;
  25. case 'dynamic': {
  26. if (empty($props['url'])) throw new Exception("Missing 'url'");
  27. } break;
  28. default: throw new Exception("Not implemented mode '{$mode}'");
  29. }
  30. $btnProps = [];
  31. if (!empty($props['style'])) $btnProps['style'] = $props['style'];
  32. if (!empty($props['class'])) $btnProps['class'] = $props['class'];
  33. if (!empty($props['id'])) $btnProps['id'] = $props['id'];
  34. // TODO: more attributes, custom, etc.
  35. return UI::h(null, [], [
  36. UI::h('button', array_merge($btnProps, [
  37. 'onClick' => "return {$jsFuncName}(event, this);",
  38. 'data-name' => $name,
  39. // 'data-url' => $props['url'], // TODO: if dynamic content
  40. ]), $props['label']),
  41. UI::hStyle($cssFile = __FILE__ . ".style.css"),
  42. ('static' === $mode)
  43. ?
  44. UI::h('div', [ 'style' => "display:none" ], [
  45. UI::h('div', [ 'class' => "p5-side_panel p5-side_panel--from-right", 'id' => $idHtmlNode ], [
  46. UI::h('header', [ 'class' => "p5-side_panel__header" ], [
  47. UI::h('h1', [], $props['title']),
  48. UI::h('button', [ 'class' => "p5-side_panel__close p5-side_panel--js-close" ], "Close"),
  49. ]),
  50. UI::h('div', [ 'class' => "p5-side_panel__container" ], [
  51. UI::h('div', [ 'class' => "p5-side_panel__content" ], $childrens),
  52. ]),
  53. ]),
  54. ])
  55. : null // created by JS on demand
  56. ,
  57. ('dynamic' === $mode)
  58. ?
  59. // TODO: create in JS
  60. // like UI_AjaxContent inside `p5-side_panel__content`
  61. UI::h('div', [ 'style' => "display:none" ], [
  62. UI::h('div', [ 'class' => "p5-side_panel p5-side_panel--from-right", 'id' => $idHtmlNode ], [
  63. UI::h('header', [ 'class' => "p5-side_panel__header" ], [
  64. UI::h('h1', [], $props['title']),
  65. UI::h('button', [ 'class' => "p5-side_panel__close p5-side_panel--js-close" ], "Close"),
  66. ]),
  67. UI::h('div', [ 'class' => "p5-side_panel__container" ], [
  68. UI::h('div', [ 'class' => "p5-side_panel__content" ], $childrens),
  69. ]),
  70. ]),
  71. ])
  72. : null // created by JS on demand
  73. ,
  74. UI::hScript($jsFile = __FILE__ . '.script.js', $jsonVars = [
  75. // 'FUNCTION_NAME' => $jsFuncName,
  76. // 'ID_HTML_NODE' => $idHtmlNode,
  77. ]),
  78. ]);
  79. }
  80. static function generateUniqueName() {
  81. static $_counter = 0;
  82. $_counter += 1;
  83. return "p5_side_panel_{$_counter}";
  84. }
  85. }