From 23ae6c061f680c9ba006c60ecc8e7b9cbda1560e Mon Sep 17 00:00:00 2001 From: Alain Belair Date: Fri, 23 Jun 2017 16:53:16 -0400 Subject: [PATCH 1/8] Enhance js handling - init commit Add function renderJSON when json response is need for ajax request. --- js/src/plugins/reloadView.js | 10 +++++--- src/TableColumn/Delete.php | 3 +-- src/View.php | 43 +++++++++++++++++++++++----------- src/VirtualPage.php | 3 +++ src/jsModal.php | 14 ++++++++--- src/jsReload.php | 2 +- template/semantic-ui/html.html | 20 +++++++++------- template/semantic-ui/html.pug | 22 +++++++++-------- 8 files changed, 75 insertions(+), 42 deletions(-) diff --git a/js/src/plugins/reloadView.js b/js/src/plugins/reloadView.js index a1ce6a6805..76266b9b6f 100644 --- a/js/src/plugins/reloadView.js +++ b/js/src/plugins/reloadView.js @@ -9,11 +9,15 @@ export default class reloadView { 'active': true, 'inline': true, 'centered': true, - 'replace': true}); + 'replace': false}); if(options.uri) { - $.get(options.uri, options.uri_options, (data) => { - $element.replaceWith(data); + $element.api({ + on: 'now', + url: options.uri, + data: options.uri_options, + method: 'GET', + obj: $element }); } } diff --git a/src/TableColumn/Delete.php b/src/TableColumn/Delete.php index 07c0aa5929..c4ef9709a5 100644 --- a/src/TableColumn/Delete.php +++ b/src/TableColumn/Delete.php @@ -17,8 +17,7 @@ public function init() $reload = $this->table->reload ?: $this->table; - $ajaxec = (new \atk4\ui\jQuery($reload))->replaceWith($reload->render())->jsRender(); - $this->table->app->terminate(json_encode(['success'=>true, 'message'=>'Success', 'eval'=>$ajaxec])); + $this->table->app->terminate($reload->renderJSON()); }); $this->table->on('click', 'a.'.$this->short_name)->ajaxec([ diff --git a/src/View.php b/src/View.php index 9585b48d46..352589762c 100644 --- a/src/View.php +++ b/src/View.php @@ -686,21 +686,36 @@ public function renderAll() } } - /** - * This method is for those cases when developer want to simply render his - * view and grab HTML himself. - * - * @param bool $force_echo - * - * @return string - */ - public function render($force_echo = true) + /** + * This method is for those cases when developer want to simply render his + * view and grab HTML himself. + * + * @param bool $force_echo + * + * @return string + */ + public function render($force_echo = true) + { + $this->renderAll(); + return + $this->getJS($force_echo). + $this->template->render(); + } + + /** + * Render View using json format. + * @param bool $force_echo + * + * @return string + */ + public function renderJSON($force_echo = true) { - $this->renderAll(); - - return - $this->getJS($force_echo). - $this->template->render(); + $this->renderAll(); + return json_encode(['success'=>true, + 'message'=>'Success', + 'eval'=>$this->getJS($force_echo), + 'html'=>$this->template->render(), + 'id'=>$this->name]); } /** diff --git a/src/VirtualPage.php b/src/VirtualPage.php index 555c10a72a..a240447340 100644 --- a/src/VirtualPage.php +++ b/src/VirtualPage.php @@ -33,6 +33,9 @@ public function init() } if ($this->cb->triggered == 'cut') { + if (isset($_GET['json'])) { + $this->app->terminate($this->renderJSON()); + } $this->app->terminate($this->render()); } diff --git a/src/jsModal.php b/src/jsModal.php index 9ba9e6e784..b3e3af03c7 100644 --- a/src/jsModal.php +++ b/src/jsModal.php @@ -27,14 +27,22 @@ public function __construct($title, $url, $args = []) '; - parent::__construct(' + parent::__construct(' + var param = [arg]; var m=$("
").appendTo("body").addClass("ui scrolling modal").html([content]); - m.modal({duration: 100, onHide: function() { m.children().remove(); return true; }}).modal("show").find(".content").load($.addParams([url], [arg]), function() { m.modal("refresh"); }); + m.modal({onHide: function() { m.children().remove(); return true; }, onShow: function(){ + let $el = $(this); + $.getJSON( [url], param, function(resp){ + $el.find(".atk-dialog-content").html(resp.html); + eval(resp.eval.replace(/<\/?script>/g, \'\')); + } + ); + }}).modal("show"); m.find(".atk-dialog-content").data("opener", this).on("close", function() { m.modal("hide"); m.remove(); }); ', - ['content'=>$content, 'url'=>$url, 'arg'=>$args]); + ['content'=>$content, 'url'=>$url, 'arg'=>array_merge($args,['json'=>true])]); } } diff --git a/src/jsReload.php b/src/jsReload.php index 89f770915f..1e25e8b314 100644 --- a/src/jsReload.php +++ b/src/jsReload.php @@ -25,7 +25,7 @@ public function __construct($view, $args = []) $this->cb = $this->view->_add(new CallbackLater()); $this->cb->set(function () { - $this->view->app->terminate($this->view->render()); + $this->view->app->terminate($this->view->renderJSON()); }); } diff --git a/template/semantic-ui/html.html b/template/semantic-ui/html.html index b85c3bccb9..6ec2d156a8 100644 --- a/template/semantic-ui/html.html +++ b/template/semantic-ui/html.html @@ -5,16 +5,18 @@ {$HEAD} {$Content} diff --git a/template/semantic-ui/html.pug b/template/semantic-ui/html.pug index 311858b5ea..3921abd3f6 100644 --- a/template/semantic-ui/html.pug +++ b/template/semantic-ui/html.pug @@ -5,36 +5,6 @@ html(lang="{lang}en{/}") | {$meta} meta(charset='utf-8') | {$HEAD} - script. - $.fn.api.settings.successTest = function(response) { - if(response.success){ - if (response && response.html && response.id) { - $('#'+response.id).replaceWith(response.html); - } - if (response && response.eval) { - try { - var result = function(){ eval(response.eval.replace(/<\/?script>/g, '')); }.call(this.obj); - } catch (e) { - - } - } - } else { - var m = $("
").appendTo("body").addClass("ui scrolling modal").html(response.message); - m.modal({duration: 100, onHide: function() { m.children().remove(); return true; }}).modal("show"); - } - this.data = new Object(); - return true; - } - $.fn.api.settings.onFailure = function(response) { - w=window.open(null, 'Error in JSON response', 'height=1000,width=1100,location=no,menubar=no,scrollbars=yes,status=no,titlebar=no,toolbar=no'); - if(w){ - w.document.write('
Error in JSON response
'); - w.document.write(response); - w.document.write('
'); - }else{ - alert("Error in ajaxec response"+response); - } - } style(type='text/css') | {$style} body(class="{$class}")