Skip to content

Commit

Permalink
Queue refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Oct 18, 2014
1 parent 78a7970 commit b8bc644
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Queue/Components/IJobControlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ interface IJobControlFactory
{

/**
* @param integer $id
* @return \Venne\Queue\Components\JobControl
*/
public function create();
public function create($id);

}
24 changes: 16 additions & 8 deletions src/Queue/Components/JobControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
class JobControl extends \Venne\System\UI\Control
{

/** @var integer */
private $id;

/** @var \Nette\Security\User */
private $user;

Expand All @@ -33,24 +36,29 @@ class JobControl extends \Venne\System\UI\Control
/** @var \Venne\DataTransfer\DataTransferManager */
private $dataTransferManager;

/**
* @param integer $id
* @param \Doctrine\ORM\EntityManager $entityManager
* @param \Nette\Security\User $user
* @param \Venne\DataTransfer\DataTransferManager $dataTransferManager
*/
public function __construct(
$id,
EntityManager $entityManager,
User $user,
DataTransferManager $dataTransferManager
) {
parent::__construct();

$this->id = $id;
$this->jobRepository = $entityManager->getRepository(Job::class);
$this->user = $user;
$this->dataTransferManager = $dataTransferManager;
}

/**
* @param int $id
*/
public function handleRemove($id)
public function handleRemove()
{
if (($entity = $this->jobRepository->find($id)) === null) {
if (($entity = $this->jobRepository->find($this->id)) === null) {
throw new BadRequestException;
}

Expand All @@ -65,11 +73,11 @@ public function handleRemove($id)
}
}

public function render($id)
public function render()
{
$this->template->job = $this->dataTransferManager
->createQuery(JobDto::class, function () use ($id) {
return $this->jobRepository->find($id);
->createQuery(JobDto::class, function () {
return $this->jobRepository->find($this->id);
})
->enableCache()
->fetch();
Expand Down
23 changes: 19 additions & 4 deletions src/Queue/Components/JobsControl.latte
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{var $count = count($jobs)}

<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-tasks"></i> <span class="badge">{$count}</span>
<i class="fa fa-tasks"></i> <span class="badge">{$jobCount}</span>
</a>

<div id="jobs-{!$control->name}" class="dropdown-menu list-group notification-container">
<div role="presentation" class="dropdown-header">
<i class="fa fa-tasks"></i>
{_'%count% jobs', $count}
{_'%count% jobs', $jobCount}
</div>

<div class="list-group notification-list-group">
<div class="list-group notification-list-group" n:snippet="content" data-ajax-append>
<div n:foreach="$jobs->toArray() as $job" class="list-group-item">
{control job $job->id}
{control job-$job->id}
</div>
</div>

Expand All @@ -21,6 +21,7 @@
</a>
</div>

{snippet js}
<script type="text/javascript">
$(function(){
$('#jobs-{!$control->name}').on('click', function(e) {
Expand All @@ -31,5 +32,19 @@
$(this).remove();
});
});
var $list = $('#jobs-{!$control->name} .notification-list-group');
$list.on('scroll.notifications', function () {
var $this = $(this);
if ($this.scrollTop() + $this.height() >= $this[0].scrollHeight - 20) {
$list.off('scroll.notifications');
$.nette.ajax({
url: {link load! offset => $offset},
off: ['spinner', 'unique']
});
}
});
});
</script>
{/snippet}
30 changes: 27 additions & 3 deletions src/Queue/Components/JobsControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Venne\Queue\Components;

use Doctrine\ORM\EntityManager;
use Nette\Application\UI\Multiplier;
use Nette\Security\User;
use Venne\DataTransfer\DataTransferManager;
use Venne\Queue\JobDto;
Expand All @@ -23,6 +24,9 @@
class JobsControl extends \Venne\System\UI\Control
{

/** @var integer */
private $offset = 0;

/** @var \Nette\Security\User */
private $user;

Expand Down Expand Up @@ -50,11 +54,23 @@ public function __construct(
}

/**
* @return \Venne\Queue\Components\JobControl
* @param integer $offset
*/
public function handleLoad($offset)
{
$this->offset = (integer) $offset;
$this->redrawControl('content');
$this->redrawControl('js');
}

/**
* @return \Nette\Application\UI\Multiplier
*/
protected function createComponentJob()
{
return $this->jobControlFactory->create();
return new Multiplier(function ($id) {
return $this->jobControlFactory->create($id);
});
}

public function render()
Expand All @@ -64,10 +80,18 @@ public function render()
return $this->jobRepository->createQueryBuilder('a')
->andWhere('a.user = :user')->setParameter('user', $this->user->getIdentity()->getId())
->orderBy('a.date', 'ASC')
->setMaxResults(5)
->setFirstResult($this->offset)
->getQuery()->getResult();
})
->enableCache()
->enableCache($this->offset)
->fetchAll();
$this->template->jobCount = $this->jobRepository->createQueryBuilder('a')
->select('COUNT(a.id)')
->andWhere('a.user = :user')->setParameter('user', $this->user->getIdentity()->getId())
->getQuery()->getSingleScalarResult();
$this->template->offset = $this->offset + 5;

$this->template->render();
}

Expand Down
4 changes: 0 additions & 4 deletions src/Queue/DI/QueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public function loadConfiguration()
->setInject(true)
->addTag(SystemExtension::TAG_TRAY_COMPONENT)
->addTag(WidgetsExtension::TAG_WIDGET, 'jobs');

$container->addDefinition($this->prefix('jobControlFactory'))
->setImplement('Venne\Queue\Components\IJobControlFactory')
->setInject(true);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Queue/DI/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ services:

- Venne\Queue\AdminModule\JobFormFactory (@system.admin.ajaxFormFactory)
- Venne\Queue\AdminModule\JobFormService

Venne\Queue\Components\NotificationControl:
arguments: [%id%]
implement: Venne\Queue\Components\IJobControlFactory
parameters: [id]
inject: true

0 comments on commit b8bc644

Please sign in to comment.