v1.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace zin;
  3. class floatToolbar extends wg
  4. {
  5. /**
  6. * @var object|null
  7. */
  8. protected $object;
  9. /**
  10. * @var mixed[]
  11. */
  12. protected static $defineProps = array(
  13. 'prefix?:array',
  14. 'main?:array',
  15. 'suffix?:array',
  16. 'object?:object'
  17. );
  18. /**
  19. * @var mixed[]
  20. */
  21. protected static $defineBlocks = array(
  22. 'prefix' => array(),
  23. 'main' => array(),
  24. 'suffix' => array()
  25. );
  26. /**
  27. * @param \zin\node|mixed[]|null $wg1
  28. * @param \zin\node|mixed[]|null $wg2
  29. */
  30. private function buildDivider($wg1, $wg2)
  31. {
  32. if(empty($wg1) || empty($wg2)) return null;
  33. return div(setClass('divider w-px self-center h-6 mx-2'));
  34. }
  35. /**
  36. * @param mixed[]|null $items
  37. */
  38. private function buildBtns($items)
  39. {
  40. if(empty($items)) return null;
  41. $btns = array();
  42. foreach ($items as &$item)
  43. {
  44. if(!$item) continue;
  45. if(isset($item['url']) && is_array($item['url'])) $item['url'] = createLink($item['url']['module'], $item['url']['method'], $item['url']['params']);
  46. if(!empty($item['url'])) $item['url'] = preg_replace_callback('/\{(\w+)\}/', array($this, 'getObjectValue'), $item['url']);
  47. if(!empty($item['data-url'])) $item['data-url'] = preg_replace_callback('/\{(\w+)\}/', array($this, 'getObjectValue'), $item['data-url']);
  48. $className = 'ghost';
  49. if(!empty($item['className'])) $className .= ' ' . $item['className'];
  50. /* Close other modals on open modal from float toolbar. */
  51. if(isset($item['data-toggle']) && $item['data-toggle'] === 'modal' && (!empty($item['url']) || !empty($item['data-url'])) && !isset($item['data-close-others'])) $item['data-close-others'] = true;
  52. $btns[] = btn(set($item), setClass($className));
  53. }
  54. return $btns;
  55. }
  56. public function getObjectValue($matches)
  57. {
  58. if(!isset($this->object)) $this->object = $this->prop('object');
  59. return zget($this->object, $matches[1]);
  60. }
  61. /**
  62. * @param mixed[]|\zin\node|null $block
  63. * @return mixed[]|\zin\node|null
  64. * @param mixed[]|null $btns
  65. */
  66. private function mergeBtns($btns, $block)
  67. {
  68. if(empty($btns) && empty($block)) return null;
  69. if(empty($block)) return $btns;
  70. if($block[0] instanceof btn)
  71. {
  72. $block[0]->add(setClass('ghost'));
  73. }
  74. else
  75. {
  76. foreach($block[0]->children() as $blockBtn) $blockBtn->add(setClass('ghost'));
  77. }
  78. if(empty($btns)) return $block;
  79. if(!is_array($block)) $block = array($block);
  80. return array_merge($btns, $block);
  81. }
  82. protected function build()
  83. {
  84. $prefixBtns = $this->buildBtns($this->prop('prefix'));
  85. $mainBtns = $this->buildBtns($this->prop('main'));
  86. $suffixBtns = $this->buildBtns($this->prop('suffix'));
  87. $prefixBlock = $this->block('prefix');
  88. $mainBlock = $this->block('main');
  89. $suffixBlock = $this->block('suffix');
  90. $prefixBtns = $this->mergeBtns($prefixBtns, $prefixBlock);
  91. $mainBtns = $this->mergeBtns($mainBtns, $mainBlock);
  92. $suffixBtns = $this->mergeBtns($suffixBtns, $suffixBlock);
  93. return ($prefixBtns || $mainBtns || $suffixBtns) ? div
  94. (
  95. setClass('toolbar bg-black text-fore-in-dark backdrop-blur bg-opacity-60 text-canvas float-toolbar rounded p-1.5'),
  96. $prefixBtns,
  97. $this->buildDivider($prefixBtns, $mainBtns),
  98. $mainBtns,
  99. $this->buildDivider($mainBtns, $suffixBtns),
  100. $suffixBtns
  101. ) : div();
  102. }
  103. }