Skip to content

Commit c1bc017

Browse files
committed
Use HTML5 bar.
1 parent 5f0b93b commit c1bc017

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

docs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,24 @@ echo h($status); // Outputs "Doing the last thing" for example
368368

369369
#### Progress Bar
370370
Using Tools plugin 1.9.6+ you can also use the more visual progress bar (or any custom one of yours):
371+
371372
```php
372373
echo $this->QueueProgress->progressBar($queuedJob, 18);
373374
```
375+
![HTML5](bar_text.png)
376+
374377
The length refers to the amount of chars to display.
375378

376379
Using Tools plugin 1.9.7+ you can even use HTML5 progress bar (easier to style using CSS).
380+
377381
For this it is recommended to add the textual one from above as fallback, though:
378382
```php
379383
$textProgressBar = $this->QueueProgress->progressBar($queuedJob, 18);
380384
echo $this->QueueProgress->htmlProgressBar($queuedJob, $textProgressBar);
381385
```
386+
387+
![HTML5](bar_html.png)
388+
382389
The text one will only be visible for older browsers that do not support the HTML5 tag.
383390

384391
Make sure you loaded the helper in your AppView class.

docs/bar_html.png

4.41 KB
Loading

docs/bar_text.png

5.21 KB
Loading

src/View/Helper/QueueProgressHelper.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,31 @@ public function htmlProgressBar(QueuedJob $queuedJob, $fallbackHtml = null) {
110110
*/
111111
public function timeoutProgressBar(QueuedJob $queuedJob, $length) {
112112
$progress = $this->calculateTimeoutProgress($queuedJob);
113-
if (!$progress) {
113+
if ($progress === null) {
114114
return null;
115115
}
116116

117117
return $this->Progress->progressBar($progress, $length);
118118
}
119119

120+
/**
121+
* @param \Queue\Model\Entity\QueuedJob $queuedJob
122+
* @param string|null $fallbackHtml
123+
*
124+
* @return string|null
125+
*/
126+
public function htmlTimeoutProgressBar(QueuedJob $queuedJob, $fallbackHtml = null) {
127+
$progress = $this->calculateTimeoutProgress($queuedJob);
128+
if ($progress === null) {
129+
return null;
130+
}
131+
132+
$progress = $this->Progress->roundPercentage($progress);
133+
$title = Number::toPercentage($progress, 0, ['multiply' => true]);
134+
135+
return '<progress value="' . number_format($progress * 100, 0) . '" max="100" title="' . $title . '">' . $fallbackHtml . '</progress>';
136+
}
137+
120138
/**
121139
* Calculates the timeout progress rate.
122140
*
@@ -139,8 +157,8 @@ protected function calculateTimeoutProgress(QueuedJob $queuedJob) {
139157
return null;
140158
}
141159

142-
if ($progressed <= 0) {
143-
$progressed = $total;
160+
if ($progressed < 0) {
161+
$progressed = 0;
144162
}
145163

146164
$progress = min($progressed / $total, 1.0);

tests/TestCase/View/Helper/QueueProgressHelperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,39 @@ public function testTimeoutProgressBar() {
134134
$this->assertTextContains('<span title="50%">', $result);
135135
}
136136

137+
/**
138+
* @return void
139+
*/
140+
public function testHtmlTimeoutProgressBar() {
141+
$queuedJob = new QueuedJob([
142+
'created' => (new FrozenTime())->subMinute(),
143+
'notbefore' => (new FrozenTime())->addMinute(),
144+
]);
145+
$result = $this->QueueProgressHelper->htmlTimeoutProgressBar($queuedJob);
146+
$expected = '<progress value="50" max="100" title="50%"></progress>';
147+
$this->assertSame($expected, $result);
148+
149+
$queuedJob = new QueuedJob([
150+
'created' => (new FrozenTime()),
151+
'notbefore' => (new FrozenTime())->addHour(),
152+
]);
153+
// For IE9 and below
154+
$fallback = $this->QueueProgressHelper->timeoutProgressBar($queuedJob, 10);
155+
$result = $this->QueueProgressHelper->htmlTimeoutProgressBar($queuedJob, $fallback);
156+
$expected = '<progress value="0" max="100" title="0%"><span title="0%">░░░░░░░░░░</span></progress>';
157+
$this->assertSame($expected, $result);
158+
159+
$queuedJob = new QueuedJob([
160+
'created' => (new FrozenTime())->subMinute(),
161+
'notbefore' => (new FrozenTime())->subSecond(),
162+
]);
163+
// For IE9 and below
164+
$fallback = $this->QueueProgressHelper->timeoutProgressBar($queuedJob, 10);
165+
$result = $this->QueueProgressHelper->htmlTimeoutProgressBar($queuedJob, $fallback);
166+
$expected = '<progress value="100" max="100" title="100%"><span title="100%">██████████</span></progress>';
167+
$this->assertSame($expected, $result);
168+
}
169+
137170
/**
138171
* Helper method for skipping tests that need a real connection.
139172
*

0 commit comments

Comments
 (0)