BuildDom.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_Test_BuildDom extends RouteBase {
  4. function defaultAction() {
  5. UI::gora();
  6. try {
  7. echo UI::h('script', ['src'=>"static/vendor.js"]);
  8. echo UI::h('script', [ 'src' => "static/p5UI/buildDom.js" ]);
  9. $listExampleProp = [];
  10. $listExampleProp['main'] = '
  11. [ "div", { "class": "container" }, [
  12. [ "h1", [], "Test H1"],
  13. [ "h2", [], "Test H2"],
  14. [ "div", { "class": "alert alert-info" }, "Test alert info" ],
  15. [ "button", { "class": "btn btn-primary" }, "button" ]
  16. ] ]
  17. ';
  18. $listExampleProp['alerts'] = '
  19. [ "div", { "class": "container" }, [
  20. [ "div", { "class": "alert alert-info" }, "Test alert info" ],
  21. [ "div", { "class": "alert alert-warning" }, "Test alert warning" ],
  22. [ "div", { "class": "alert alert-danger" }, "Test alert danger" ],
  23. [ "div", { "class": "alert alert-success" }, "Test alert success" ]
  24. ] ]
  25. ';
  26. $examplePreload = $listExampleProp['main'];
  27. echo '<div class="container-fluid">';
  28. echo UI::h('details', [ 'style' => "padding:6px; background-color:#333; color:#fff", 'open' => "open" ], [
  29. UI::h('summary', [ 'style' => "padding:0 3px; outline:none; cursor:pointer" ], "buildDom input"),
  30. UI::h('textarea', [ 'id' => 'exampleBuildDomInput', 'style' => "width:100%; height:300px; margin:0; font-size:small; font-family:monospace; border-radius:0; color:#000" ], $examplePreload),
  31. ]);
  32. echo UI::h('button', [ 'class' => "btn btn-primary", 'onClick' => "return runBuildDomExample()" ], "Uruchom (Ctrl + Enter)");
  33. echo UI::h('div', [ 'style' => "display:inline", 'id' => "example-btns" ]);
  34. echo UI::h('details', [ 'style' => "padding:6px; background-color:#333; color:#fff", 'open' => "open" ], [
  35. UI::h('summary', [ 'style' => "padding:0 3px; outline:none; cursor:pointer" ], "buildDom output"),
  36. UI::h('pre', [ 'id' => 'exampleBuildDomOutput', 'style' => "margin:0; font-size:x-small; border-radius:0" ], 'loading...'),
  37. ]);
  38. UI::endContainer();
  39. $jsExamples = json_encode($listExampleProp);
  40. echo UI::h('script', [], "
  41. var examples = {$jsExamples};
  42. console.log('examples', examples)
  43. function setBuildDomExample(i) {
  44. document.getElementById('exampleBuildDomInput').value = examples[i]
  45. runBuildDomExample()
  46. }
  47. for (var i in examples) {
  48. jQuery(document.getElementById('example-btns')).append('<button class=\"btn btn-default\" style=\"margin-left:12px\" onClick=\"setBuildDomExample(\''+i+'\')\">'+i+'</button>')
  49. }
  50. ");
  51. echo UI::h('script', [], "
  52. function runBuildDomExample() {
  53. document.getElementById('exampleBuildDomOutput').innerHTML = 'loading...'
  54. var exampleBody = document.getElementById('exampleBuildDomInput').value.replace(/&lt;/g, '<').replace(/&gt;/g, '>')
  55. console.log('exampleBuildDomInput:', exampleBody)
  56. try {
  57. exampleBody = JSON.parse(exampleBody)
  58. } catch (e) {
  59. p5UI__buildDom(['div', {'class': 'alert alert-danger'}, '' + e], document.getElementById('exampleBuildDomOutput'))
  60. return false
  61. }
  62. p5UI__buildDom(exampleBody, document.getElementById('exampleBuildDomOutput'))
  63. return false
  64. }
  65. (function () {
  66. runBuildDomExample()
  67. })()
  68. ");
  69. echo UI::h('script', [], "
  70. document.getElementById('exampleBuildDomInput').onkeydown = function(e) {
  71. if ( e.keyCode == 9 || e.which == 9 ) {
  72. e.preventDefault();
  73. var s = this.selectionStart;
  74. this.value = this.value.substring(0, this.selectionStart) + '\t' + this.value.substring(this.selectionEnd);
  75. this.selectionEnd = s + 1;
  76. }
  77. }
  78. window.onkeyup = function(e) {
  79. if (!e.ctrlKey) return;
  80. if (13 !== e.keyCode) return; // 13: Enter
  81. runBuildDomExample()
  82. }
  83. // window.onkeyup = function(e) {
  84. // var pressed = '';
  85. // if (e.shiftKey) {
  86. // pressed += ' + Shift';
  87. // } else if (e.ctrlKey) {
  88. // pressed += ' + Ctrl';
  89. // } else if (e.cmdKey) {
  90. // pressed += ' + Command';
  91. // }
  92. // pressed = e.keyCode + pressed;
  93. // console.log(pressed, 'metaKey', e.metaKey);
  94. // }
  95. ");
  96. } catch (Exception $e) {
  97. DBG::log($e);
  98. UI::alert('danger', $e->getMessage());
  99. }
  100. UI::dol();
  101. }
  102. }