VueComponent.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits;
  3. /**
  4. * Vue Components related functions
  5. *
  6. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  7. */
  8. trait VueComponent
  9. {
  10. protected $vueComponent = false;
  11. protected $defaultVueComponentName = null;
  12. protected static $vueComponentBody = '';
  13. protected static $listIdElements = [];
  14. protected static $vueComponentBodyJs = '';
  15. protected static $listIdElementsJs = [];
  16. protected static $vueComponentRegistrationsBody = null;
  17. protected static $vueComponentRegistrations = [];
  18. protected static $vueComponentRegistredIds = [];
  19. public function isVueComponent()
  20. {
  21. return $this->vueComponent;
  22. }
  23. public function getDefaultVueComponentName()
  24. {
  25. return $this->defaultVueComponentName;
  26. }
  27. public function getVueComponents()
  28. {
  29. if(self::$vueComponentBody === '')
  30. {
  31. $this->html = self::generate($this);
  32. }
  33. return self::$vueComponentBody;
  34. }
  35. protected function addVueComponentTemplate($componentBody, $id)
  36. {
  37. if ($id === $this->getRequestValue('loadData', false) && $this->getRequestValue('ajax') == '1')
  38. {
  39. return $this;
  40. }
  41. if (!in_array($id, self::$listIdElements))
  42. {
  43. self::$vueComponentBody .= $componentBody;
  44. self::$listIdElements[] = $id;
  45. }
  46. return $this;
  47. }
  48. public function getVueComponentsJs()
  49. {
  50. return self::$vueComponentBodyJs;
  51. }
  52. protected function addVueComponentJs($componentBodyJs, $id)
  53. {
  54. if (!in_array($id, self::$listIdElementsJs))
  55. {
  56. self::$vueComponentBodyJs .= $componentBodyJs;
  57. self::$listIdElementsJs[] = $id;
  58. }
  59. return $this;
  60. }
  61. protected function registerVueComponent($componentId, $componentTemplateId)
  62. {
  63. if ($componentId === $this->getRequestValue('loadData', false) && $this->getRequestValue('ajax') == '1')
  64. {
  65. return $this;
  66. }
  67. if (!in_array($componentId, self::$vueComponentRegistredIds))
  68. {
  69. self::$vueComponentRegistrations[$componentId] = $componentTemplateId;
  70. }
  71. return $this;
  72. }
  73. public function getVueComponentsRegistrationsJs()
  74. {
  75. self::$vueComponentRegistrationsBody = '';
  76. foreach (self::$vueComponentRegistrations as $componentId => $componentTemplateId)
  77. {
  78. self::$vueComponentRegistrationsBody .= ' mgJsComponentHandler.addComponentByDefaultTemplate(\''. strtolower($componentId) .'\', \''. $componentTemplateId .'\'); ';
  79. }
  80. return self::$vueComponentRegistrationsBody;
  81. }
  82. public function getVueComponentsRegistrations()
  83. {
  84. return self::$vueComponentRegistrations;
  85. }
  86. /**
  87. * A default method to be overwritten in order to manage vue components registration process.
  88. */
  89. public function isVueRegistrationAllowed()
  90. {
  91. return true;
  92. }
  93. }