Skip to content

Commit

Permalink
Dev: Add new JSON output type to AjaxHelper: HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Sep 21, 2017
1 parent 533e222 commit 9fd0c42
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
65 changes: 59 additions & 6 deletions application/helpers/admin/ajax_helper.php
Expand Up @@ -81,6 +81,15 @@ public static function outputNotLoggedIn()
self::echoString($output);
}

/**
* @return void
*/
public static function outputHtml($html, $target)
{
$output = new JsonOutputHtml($html, $target);
self::echoString($output);
}

/**
* Echo $str with json header
* @param string str
Expand Down Expand Up @@ -157,12 +166,13 @@ public function __construct($result)
public function __toString()
{
return json_encode(array(
'ajaxHelper' => true, // To help JS parse in some cases.
'success' => $this->success,
'result' => $this->result,
'error' => $this->error,
'loggedIn' => $this->loggedIn,
'hasPermission' => $this->hasPermission,
'ajaxHelper' => true, // To help JS parse in some cases.
'success' => $this->success,
'result' => $this->result,
'error' => $this->error,
'type' => $this->type,
'loggedIn' => $this->loggedIn,
'hasPermission' => $this->hasPermission,
'noPermissionText' => gT('No permission')
));
}
Expand Down Expand Up @@ -292,3 +302,46 @@ public function __construct()
$this->loggedIn = false;
}
}

/**
* Echo HTML and put it in a <div> with id $target.
*/
class JsonOutputHtml extends JsonOutput
{

/**
* Content.
* @var string
*/
public $html;

/**
* ID of element to put HTML in.
* @var string
*/
public $target;

/**
* @param string $html
* @param string $target ID of element to put HTML in.
*/
public function __construct($html, $target)
{
$this->html = $html;
$this->target = $target;
}

public function __toString()
{
return json_encode(
array(
'loggedIn' => true,
'hasPermission' => true,
'success' => true,
'html' => $this->html,
'outputType' => 'jsonoutputhtml',
'target' => $this->target
)
);
}
}
4 changes: 4 additions & 0 deletions scripts/admin/admin_core.js
Expand Up @@ -969,6 +969,10 @@ LS.ajaxHelperOnSuccess = function(response) {
else if (response.error) {
notifyFader(response.error.message, 'well-lg bg-danger text-center');
}
// Put HTML into element.
else if (response.outputType == 'jsonoutputhtml') {
$('#' + response.target).html(response.html);
}
// Success popup
else if (response.success) {
notifyFader(response.success, 'well-lg bg-primary text-center');
Expand Down

10 comments on commit 9fd0c42

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do it in 3.0 ?

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean it's already done in 3.0?

@Shnoulle
Copy link
Collaborator

@Shnoulle Shnoulle commented on 9fd0c42 Sep 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :), i think we create a global ajaxHelper for all ajax request for 3.0 . I start a broken draft (and never find time to continue) but for an old develop (2 month ago) :)

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link?

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No link :) just discussion here (on github commit). About onerror and a lot of different system for 401/404/403 errors

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I remember!

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, Markus is using pjax for the new system, so I'm not sure anything of this from 2.67 will be relevant.

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rather look at how pjax and Yii (and Yii 2) is bound together.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well : pjax is used only for "update content via ajax" no ? We need a system too for json return for example :)

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes maybe. I don't know anything about pjax. @lacrioque ?

Please sign in to comment.