Enh #4407: Reworked TimeAgo widget

This commit is contained in:
buddh4 2020-10-12 18:46:21 +02:00
parent 4793578308
commit 9bbf13f837
3 changed files with 194 additions and 18 deletions

View File

@ -0,0 +1,111 @@
<?php
namespace humhub\tests\codeception\unit\models;
use DateTime;
use humhub\widgets\TimeAgo;
use tests\codeception\_support\HumHubDbTestCase;
use Yii;
class TimeAgoWidgetTest extends HumHubDbTestCase
{
public function _before()
{
Yii::$app->params['formatter']['timeAgoStatic'] = false;
Yii::$app->params['formatter']['timeAgoBefore'] = 172800;
Yii::$app->params['formatter']['timeAgoHideTimeAfter'] = 259200;
}
//DateTime::createFromFormat("Y-m-d H:i:s", $timestamp)
public function testDefaultSettingsTimeAgoBeforeActiveInIntveral()
{
// TS within default timAgoBefore interval (172800 s)
$ts = time() - 60;
$result = TimeAgo::widget(['timestamp' => $ts]);
$this->assertTimeAgoActive($result);
}
public function testOverwriteDefaultTimeAgoBefore()
{
// TS outside of overwritten ts (50 s)
$ts = time() - 60;
$result = TimeAgo::widget(['timestamp' => $ts, 'timeAgoBefore' => 50]);
$this->assertTimeAgoNotActive($result);
}
public function testDeactivateTimeAgoBeforeInWidget()
{
// TS outside of default but overwritten
$ts = time() - (172800 + 10);
$result = TimeAgo::widget(['timestamp' => $ts, 'timeAgoBefore' => false]);
$this->assertTimeAgoActive($result);
}
public function testActivateDefaultTimeAgoStatic()
{
Yii::$app->params['formatter']['timeAgoStatic'] = true;
$ts = time() - (172800 + 10);
$result = TimeAgo::widget(['timestamp' => $ts]);
$this->assertTimeAgoNotActive($result);
}
public function testActivateTimeAgoStaticInWidget()
{
Yii::$app->params['formatter']['timeAgoStatic'] = false;
$ts = time() - (172800 + 10);
$result = TimeAgo::widget(['timestamp' => $ts, 'staticTimeAgo' => true]);
$this->assertTimeAgoNotActive($result);
}
public function testDefaultSettingsTimeAgoBeforeActiveOutOfInterval()
{
// TS outside of default timAgoBefore (172800 s)
$ts = time() - (172800 + 10);
$result = TimeAgo::widget(['timestamp' => $ts]);
$this->assertTimeAgoNotActive($result);
}
public function testDefaultSettingsTimeAgoBeforeNotActiveOutOfInterval()
{
// TS outside of default 172800 but default deactivated
Yii::$app->params['formatter']['timeAgoBefore'] = false;
$ts = time() - (172800 + 10);
$result = TimeAgo::widget(['timestamp' => $ts]);
$this->assertTimeAgoActive($result);
}
public function testHideTimeAfterMatches()
{
// TS outside of default 172800 but default deactivated
$ts = DateTime::createFromFormat("Y-m-d H:i:s", "2018-10-12 12:00:00")->getTimestamp();
$result = TimeAgo::widget(['timestamp' => $ts]);
$this->assertContains('<span title="Oct 12, 2018 - 12:00 PM">Oct 12, 2018</span></span>', $result);
}
public function testHideTimeAfterNotMatches()
{
// TS outside of default 172800 but default deactivated
$ts = (new DateTime())->setTime(12,00,00)->getTimestamp();
$result = TimeAgo::widget(['timestamp' => $ts, 'timeAgoBefore' => 1]);
$this->assertContains('<span title="Oct 12, 2020 - 12:00 PM">Oct 12, 2020 - 12:00 PM</span></span>', $result);
}
public function testHideTimeAfterDeactivated()
{
// TS outside of default 172800 but default deactivated
$ts = DateTime::createFromFormat("Y-m-d H:i:s", "2018-10-12 12:00:00")->getTimestamp();
$result = TimeAgo::widget(['timestamp' => $ts, 'hideTimeAfter' => false]);
$this->assertContains('<span title="Oct 12, 2018 - 12:00 PM">Oct 12, 2018 - 12:00 PM</span>', $result);
}
private function assertTimeAgoActive($result)
{
$this->assertContains('data-ui-addition="timeago"', $result);
}
private function assertTimeAgoNotActive($result)
{
$this->assertNotContains('data-ui-addition="timeago"', $result);
}
}

View File

@ -23,6 +23,23 @@ class TimeAgo extends \yii\base\Widget
*/
public $timestamp;
/**
* @var int|bool defines if the timeAgo calculation shold only be used within a certain time interval in seconds.
* (default Yii::$app->params['formatter']['timeAgoBefore'])
*/
public $timeAgoBefore;
/**
* @var int|bool defines if the time information should only be added within a certain time interval in seconds this
* is only used if the timeAgo calculation is not active. (default Yii::$app->params['formatter']['timeAgoHideTimeAfter'])
*/
public $hideTimeAfter;
/**
* @var bool defines if a static render method should be used (default Yii::$app->params['formatter']['timeAgoStatic'])
*/
public $staticTimeAgo;
/**
* @inheritdoc
*/
@ -42,9 +59,12 @@ class TimeAgo extends \yii\base\Widget
*/
public function run()
{
$elapsed = time() - $this->timestamp;
if($this->isRenderStatic()) {
return $this->renderStatic();
}
if (Yii::$app->params['formatter']['timeAgoBefore'] !== false && $elapsed >= Yii::$app->params['formatter']['timeAgoBefore']) {
$elapsed = time() - $this->timestamp;
if ($this->isTimeAgoElapsed($elapsed)) {
return $this->renderDateTime($elapsed);
}
@ -52,46 +72,87 @@ class TimeAgo extends \yii\base\Widget
}
/**
* Render TimeAgo Javascript
*
* @return string timeago span
* @return bool
*/
public function renderTimeAgo()
private function isRenderStatic()
{
// Use static timeago
if (Yii::$app->params['formatter']['timeAgoStatic']) {
return '<span class="time"><span title="' . $this->getFullDateTime() . '">' . Yii::$app->formatter->asRelativeTime($this->timestamp) . '</span></span>';
}
$timeAgoStatic = $this->staticTimeAgo !== null ? $this->staticTimeAgo : Yii::$app->params['formatter']['timeAgoStatic'];
return $timeAgoStatic;
}
// Convert timestamp to ISO 8601
$this->timestamp = date("c", $this->timestamp);
/**
* @return string
* @throws \yii\base\InvalidConfigException
*/
private function renderStatic()
{
return '<span class="time"><span title="tt ' . $this->getFullDateTime() . '">' . Yii::$app->formatter->asRelativeTime($this->timestamp) . '</span></span>';
}
$this->getView()->registerJs('$(".time").timeago();', \yii\web\View::POS_END, 'timeago');
return '<span class="time" title="' . $this->timestamp . '">' . $this->getFullDateTime() . '</span>';
/**
* @param $elapsed
* @return bool
*/
private function isTimeAgoElapsed($elapsed)
{
$timeAgoBefore = $this->timeAgoBefore !== null ? $this->timeAgoBefore : Yii::$app->params['formatter']['timeAgoBefore'];
return $timeAgoBefore !== false && $elapsed >= $timeAgoBefore;
}
/**
* @param $elapsed
* @return bool
*/
private function isHideTimeAfter($elapsed)
{
$timeAgoHideTimeAfter = $this->hideTimeAfter !== null ? $this->hideTimeAfter : Yii::$app->params['formatter']['timeAgoHideTimeAfter'];
return $timeAgoHideTimeAfter === false || $elapsed >= $timeAgoHideTimeAfter;
}
/**
* Show full date
*
*
* @param int $elasped time in seconds
* @return string output of full date and time
* @throws \yii\base\InvalidConfigException
*/
public function renderDateTime($elapsed)
{
// Show time when within specified range
if (Yii::$app->params['formatter']['timeAgoHideTimeAfter'] === false || $elapsed <= Yii::$app->params['formatter']['timeAgoHideTimeAfter']) {
if (!$this->isHideTimeAfter($elapsed)) {
$date = $this->getFullDateTime();
} else {
$date = Yii::$app->formatter->asDate($this->timestamp, 'medium');
}
return '<span class="time"><span title="' . $this->getFullDateTime() . '">' . $date . '</span></span>';
return '<span class="tt time"><span title="' . $this->getFullDateTime() . '">' . $date . '</span></span>';
}
/**
* Render TimeAgo Javascript
*
* @return string timeago span
* @throws \yii\base\InvalidConfigException
*/
public function renderTimeAgo()
{
// $this->getView()->registerJs('$(".time").timeago();', \yii\web\View::POS_END, 'timeago');
// return '<span class="tt timeago time" data-ui-addition="timeago" title="' . date("c", $this->timestamp) . '">' . $this->getFullDateTime() . '</span>';
// Convert timestamp to ISO 8601
$date = date("c", $this->timestamp);
return '<time class="tt time timeago" data-ui-addition="timeago" datetime="'.$date.'" title="' .$this->getFullDateTime() . '">' . $this->getFullDateTime() . '</time>';
}
/**
* Returns full date as text
*
*
* @return string
* @throws \yii\base\InvalidConfigException
*/
protected function getFullDateTime()
{

View File

@ -180,6 +180,10 @@ humhub.module('ui.additions', function (module, require, $) {
$match.autosize();
});
module.register('timeago', function($match) {
$match.timeago();
});
module.register('select2', '[data-ui-select2]', function ($match) {
$match.select2({theme: "humhub"});
});