v1.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace zin;
  3. class modalDialog extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'title?: string',
  10. 'titleClass?: string',
  11. 'size?: string|number',
  12. 'condensed?: bool',
  13. 'itemID?: int',
  14. 'headerClass?: string',
  15. 'headerProps?: array',
  16. 'bodyClass?: string',
  17. 'bodyProps?: array',
  18. 'actions?: array',
  19. 'closeBtn?: bool|array=true',
  20. 'footerActions?: array',
  21. 'footerClass?: string',
  22. 'footerProps?: array',
  23. 'rawContent?: bool',
  24. 'hookContent?: bool',
  25. 'autoLoad?: string'
  26. );
  27. /**
  28. * @var mixed[]
  29. */
  30. protected static $defineBlocks = array(
  31. 'header' => array('map' => 'modalHeader'),
  32. 'actions' => array(),
  33. 'footer' => array('map' => 'toolbar')
  34. );
  35. protected function buildHeader()
  36. {
  37. $title = $this->prop('title');
  38. $titleClass = $this->prop('titleClass');
  39. $itemID = $this->prop('itemID');
  40. $headerBlock = $this->block('header');
  41. if(empty($title) && empty($headerBlock)) return null;
  42. return div
  43. (
  44. setClass('modal-header', $this->prop('headerClass')),
  45. set($this->prop('headerProps')),
  46. empty($itemID) ? null : label($itemID, setStyle(['min-width' => '30px']), setClass('justify-center')),
  47. empty($title) ? null : div(setClass('modal-title', $titleClass), $title),
  48. $headerBlock
  49. );
  50. }
  51. protected function buildActions()
  52. {
  53. list($actions, $closeBtn) = $this->prop(array('actions', 'closeBtn'));
  54. $actionsBlock = $this->block('actions');
  55. if(empty($actions) && empty($actionsBlock) && !$closeBtn) return;
  56. return div
  57. (
  58. setClass('modal-actions'),
  59. empty($actions) ? null : toolbar(set::items($actions)),
  60. $actionsBlock,
  61. $closeBtn ? btn
  62. (
  63. set('data-dismiss', 'modal'),
  64. set::square(true),
  65. is_array($closeBtn) ? set($closeBtn) : setClass('ghost'),
  66. span(setClass('close'))
  67. ) : null
  68. );
  69. }
  70. protected function buildFooter()
  71. {
  72. $footerActions = $this->prop('footerActions');
  73. $footerBlock = $this->block('footer');
  74. if(empty($footerActions) && empty($footerBlock)) return;
  75. return div
  76. (
  77. setClass('modal-footer', $this->prop('footerClass')),
  78. set($this->prop('footerProps')),
  79. $footerBlock,
  80. empty($footerActions) ? null : toolbar(set::items($footerActions))
  81. );
  82. }
  83. protected function buildBody()
  84. {
  85. $rawContent = $this->prop('rawContent', !context()->rawContentCalled);
  86. $hookContent = $this->prop('hookContent', !context()->hookContentCalled);
  87. return div
  88. (
  89. setClass('modal-body scrollbar-hover', $this->prop('bodyClass')),
  90. set($this->prop('bodyProps')),
  91. $this->children(),
  92. $rawContent ? rawContent() : null,
  93. $hookContent ? hookContent() : null
  94. );
  95. }
  96. protected function setSize()
  97. {
  98. $size = $this->prop('size');
  99. if(!$size) return null;
  100. if(is_string($size)) return set('data-size', $size);
  101. return setStyle('width', "{$size}px");
  102. }
  103. protected function getAutoLoad()
  104. {
  105. global $app;
  106. $autoLoad = $this->prop('autoLoad');
  107. if(!is_null($autoLoad)) return $autoLoad;
  108. if($app->rawMethod !== 'view') return null;
  109. $objectType = $app->rawModule;
  110. $objectID = data($objectType . 'ID');
  111. if(empty($objectID))
  112. {
  113. $object = data($objectType);
  114. if(is_object($object) && isset($object->id)) $objectID = $object->id;
  115. elseif(is_array($object) && isset($object['id'])) $objectID = $object['id'];
  116. }
  117. if(empty($objectID)) return null;
  118. return "$objectType:$objectID";
  119. }
  120. protected function build()
  121. {
  122. return div
  123. (
  124. setClass('modal-dialog', $this->prop('condensed') ? 'modal-condensed' : ''),
  125. set($this->getRestProps()),
  126. setData('auto-load', $this->getAutoLoad()),
  127. $this->setSize(),
  128. div
  129. (
  130. setClass('modal-content'),
  131. $this->buildHeader(),
  132. $this->buildActions(),
  133. $this->buildBody(),
  134. $this->buildFooter()
  135. )
  136. );
  137. }
  138. }