BuildDom.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Response');
  4. class Route_Test_BuildDom extends RouteBase {
  5. function defaultAction() {
  6. UI::gora();
  7. try {
  8. echo UI::h('script', ['src'=>"static/vendor.js"]);
  9. echo UI::h('script', [ 'src' => "static/p5UI/buildDom.js" ]);
  10. $listExampleProp = [];
  11. $listExampleProp['main'] = '
  12. [ "div", { "class": "container" }, [
  13. [ "h1", [], "Test H1"],
  14. [ "h2", [], "Test H2"],
  15. [ "div", { "class": "alert alert-info" }, "Test alert info" ],
  16. [ "button", { "class": "btn btn-primary" }, "button" ]
  17. ] ]
  18. ';
  19. $listExampleProp['alerts'] = '
  20. [ "div", { "class": "container" }, [
  21. [ "div", { "class": "alert alert-info" }, "Test alert info" ],
  22. [ "div", { "class": "alert alert-warning" }, "Test alert warning" ],
  23. [ "div", { "class": "alert alert-danger" }, "Test alert danger" ],
  24. [ "div", { "class": "alert alert-success" }, "Test alert success" ]
  25. ] ]
  26. ';
  27. $listExampleProp['proces'] = '
  28. [ "div", { "class": "container" }, [
  29. [ "P5UI__ProcessStep", { "id": "996" }, [
  30. [ "div", { "class": "alert alert-info" }, "Test 996 custom children" ]
  31. ] ]
  32. ] ]
  33. ';
  34. $examplePreload = $listExampleProp['proces'];
  35. echo '<div class="container-fluid">';
  36. echo UI::h('details', [ 'style' => "padding:6px; background-color:#333; color:#fff", 'open' => "open" ], [
  37. UI::h('summary', [ 'style' => "padding:0 3px; outline:none; cursor:pointer" ], "buildDom input"),
  38. UI::h('textarea', [ 'id' => 'exampleBuildDomInput', 'style' => "width:100%; height:300px; margin:0; font-size:small; font-family:monospace; border-radius:0; color:#000" ], $examplePreload),
  39. ]);
  40. echo UI::h('button', [ 'class' => "btn btn-primary", 'onClick' => "return runBuildDomExample()" ], "Uruchom (Ctrl + Enter)");
  41. echo UI::h('div', [ 'style' => "display:inline", 'id' => "example-btns" ]);
  42. echo UI::h('details', [ 'style' => "padding:6px; background-color:#333; color:#fff", 'open' => "open" ], [
  43. UI::h('summary', [ 'style' => "padding:0 3px; outline:none; cursor:pointer" ], "buildDom output"),
  44. UI::h('div', [ 'id' => 'exampleBuildDomOutput', 'style' => "margin:0; padding:12px 0; font-size:normal; border-radius:0; background-color:#fff; color:#000" ], 'loading...'),
  45. ]);
  46. UI::endContainer();
  47. $jsExamples = json_encode($listExampleProp);
  48. $js = [];
  49. $js['URL_GET_PROCES_STEP_INFO'] = $this->getLink('getProcesStepGuiAjax');
  50. echo UI::h('script', [], "
  51. (function (global, p5VendorJs) {
  52. var URL_GET_PROCES_STEP_INFO = '{$js['URL_GET_PROCES_STEP_INFO']}';
  53. if (!p5VendorJs.React) throw 'Missing React'
  54. if (!p5VendorJs.ReactDOM) throw 'Missing ReactDOM'
  55. if (!p5VendorJs.createReactClass) throw 'Missing createReactClass'
  56. var React = p5VendorJs.React
  57. var ReactDOM = p5VendorJs.ReactDOM
  58. var createReactClass = p5VendorJs.createReactClass
  59. var h = React.createElement
  60. global.p5VendorJs['P5UI__ProcessStep'] = createReactClass({
  61. componentWillMount: function () {
  62. this.setState({ // default values
  63. zasobyRaw: []
  64. })
  65. var updateZasoby = function (zasobyRaw) {
  66. this.setState({
  67. zasobyRaw: zasobyRaw
  68. })
  69. }.bind(this)
  70. console.log('ProcessStep::componentWillMount...');
  71. global.fetch(URL_GET_PROCES_STEP_INFO + '&id=' + this.props.id, {
  72. credentials: 'same-origin',
  73. }).then(function (response) {
  74. return response.json()
  75. }).then(function (resp) {
  76. p5UI__notifyAjaxCallback(resp)
  77. updateZasoby(resp.body.zasobyRaw)
  78. }).catch(function (e) {
  79. p5UI__notifyAjaxCallback({ type: 'error', msg: '' + e })
  80. })
  81. },
  82. render: function () {
  83. console.log('ProcessStep::render... state, props', this.state, this.props);
  84. var formProps = {}
  85. return h('div', Object.assign(formProps, {
  86. ref: function (node) { this.rootNode = node; }.bind(this),
  87. // onSubmit: this.submit.bind(this)
  88. }), [].concat(
  89. [ h('h1', {}, 'Test P5UI__ProcessStep...') ],
  90. this.props.children,
  91. this.state.zasobyRaw.map(function (zasob) {
  92. return h('pre', {}, JSON.stringify(zasob));
  93. })
  94. ))
  95. },
  96. // componentDidMount: function () { console.log('ProcessStep::componentDidMount...'); },
  97. // componentWillReceiveProps: function (nextProps) { console.log('ProcessStep::componentWillReceiveProps(nextProps)...', nextProps); },
  98. // shouldComponentUpdate: function (nextProps, nextState) { console.log('ProcessStep::shouldComponentUpdate(nextProps, nextState)...', nextProps, nextState); },
  99. // componentWillUpdate: function (nextProps, nextState) { console.log('ProcessStep::componentWillUpdate(nextProps, nextState)...', nextProps, nextState); },
  100. // componentDidUpdate: function (prevProps, prevState) { console.log('ProcessStep::componentDidUpdate(prevProps, prevState)...', prevProps, prevState); },
  101. // componentWillUnmount: function () { console.log('ProcessStep::componentWillUnmount...'); },
  102. })
  103. })(window, window.p5VendorJs)
  104. ");
  105. echo UI::h('script', [], "
  106. var examples = {$jsExamples};
  107. console.log('examples', examples)
  108. function setBuildDomExample(i) {
  109. document.getElementById('exampleBuildDomInput').value = examples[i]
  110. runBuildDomExample()
  111. }
  112. for (var i in examples) {
  113. jQuery(document.getElementById('example-btns')).append('<button class=\"btn btn-default\" style=\"margin-left:12px\" onClick=\"setBuildDomExample(\''+i+'\')\">'+i+'</button>')
  114. }
  115. ");
  116. echo UI::h('script', [], "
  117. function runBuildDomExample() {
  118. document.getElementById('exampleBuildDomOutput').innerHTML = 'loading...'
  119. var exampleBody = document.getElementById('exampleBuildDomInput').value.replace(/&lt;/g, '<').replace(/&gt;/g, '>')
  120. console.log('exampleBuildDomInput:', exampleBody)
  121. try {
  122. exampleBody = JSON.parse(exampleBody)
  123. } catch (e) {
  124. p5UI__buildDom(['div', {'class': 'alert alert-danger'}, '' + e], document.getElementById('exampleBuildDomOutput'))
  125. return false
  126. }
  127. p5UI__buildDom(exampleBody, document.getElementById('exampleBuildDomOutput'))
  128. return false
  129. }
  130. (function () {
  131. runBuildDomExample()
  132. })()
  133. ");
  134. echo UI::h('script', [], "
  135. document.getElementById('exampleBuildDomInput').onkeydown = function(e) {
  136. if ( e.keyCode == 9 || e.which == 9 ) {
  137. e.preventDefault();
  138. var s = this.selectionStart;
  139. this.value = this.value.substring(0, this.selectionStart) + '\t' + this.value.substring(this.selectionEnd);
  140. this.selectionEnd = s + 1;
  141. }
  142. }
  143. window.onkeyup = function(e) {
  144. if (!e.ctrlKey) return;
  145. if (13 !== e.keyCode) return; // 13: Enter
  146. runBuildDomExample()
  147. }
  148. // window.onkeyup = function(e) {
  149. // var pressed = '';
  150. // if (e.shiftKey) {
  151. // pressed += ' + Shift';
  152. // } else if (e.ctrlKey) {
  153. // pressed += ' + Ctrl';
  154. // } else if (e.cmdKey) {
  155. // pressed += ' + Command';
  156. // }
  157. // pressed = e.keyCode + pressed;
  158. // console.log(pressed, 'metaKey', e.metaKey);
  159. // }
  160. ");
  161. } catch (Exception $e) {
  162. DBG::log($e);
  163. UI::alert('danger', $e->getMessage());
  164. }
  165. UI::dol();
  166. }
  167. function getProcesStepGuiAjaxAction() {
  168. Response::sendTryCatchJson( [ $this, 'getProcesStepGuiAjax' ], $args = $_GET );
  169. }
  170. function getProcesStepGuiAjax($args) {
  171. $id = V::get('id', 0, $args);
  172. if (!$id) throw new Exception("Missing id");
  173. return [
  174. 'type' => "success",
  175. 'msg' => "OK",
  176. 'body' => [
  177. 'zasobyRaw' => DB::getPDO()->fetchAll("
  178. select w.*, z.*
  179. from CRM_PROCES p
  180. left join CRM_WSKAZNIK w on ( w.ID_PROCES = p.ID )
  181. left join CRM_LISTA_ZASOBOW z on ( z.ID = w.ID_ZASOB )
  182. where p.ID = :id
  183. ", [ ':id' => $id ]),
  184. ]
  185. ];
  186. }
  187. }