Skip to content

Commit

Permalink
Merge pull request #38 from creative-commoners/pulls/2.0/ux-improvement
Browse files Browse the repository at this point in the history
NEW Add loading animation to Create Report button, fix bug in CurlLinkChecker
  • Loading branch information
NightJar authored Nov 29, 2017
2 parents a119add + 44fbc02 commit e21c0ad
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 92 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
[*.{yml,js,scss,css,json}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
}
},
"extra": {
"expose": ["javascript"],
"expose": [
"css",
"javascript"
],
"branch-alias": {
"dev-master": "2.x-dev"
}
Expand Down
4 changes: 4 additions & 0 deletions css/BrokenExternalLinksReport.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.external-links-report__create-report,
.external-links-report__report-progress {
margin-top: 20px;
}
200 changes: 133 additions & 67 deletions javascript/BrokenExternalLinksReport.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,135 @@
(function($) {
$.entwine('ss', function($) {
$('#externalLinksReport').entwine({
PollTimeout: null,
onclick: function() {
this.start();
},
onmatch: function() {
// poll the current job and update the front end status
$('#externalLinksReport').hide();
this.poll();
},
start: function() {
// initiate a new job
$('#ReportHolder').empty();
$('#ReportHolder').text('Running report 0%');
$('#ReportHolder').append('<span class="ss-ui-loading-icon"></span>');
$('#externalLinksReport').hide();
$.ajax({url: "admin/externallinks/start", async: false, timeout: 3000 });
this.poll();
},
poll: function() {
var self = this;

$.ajax({
url: "admin/externallinks/getJobStatus",
async: true,
success: function(data) {
// No report, so let user create one
if (!data) {
$('#externalLinksReport').show();
return;
}

// Parse data
var completed = data.Completed ? data.Completed : 0;
var total = data.Total ? data.Total : 0;

// If complete status
if (data.Status === 'Completed') {
$('#ReportHolder').text('Report Finished ' + completed + '/' + total);
$('#externalLinksReport').show();
return;
}

// If incomplete update status
if (completed < total) {
var percent = (completed / total) * 100;
$('#ReportHolder')
.text('Running report ' + completed + '/' + total + ' (' + percent.toFixed(2) + '%)')
.append('<span class="ss-ui-loading-icon"></span>');
}

// Ensure the regular poll method is run
// kill any existing timeout
if(self.getPollTimeout() !== null) {
clearTimeout(self.getPollTimeout());
}

self.setPollTimeout(setTimeout(function() { $('#externalLinksReport').poll(); }, 1000));
},
error: function(e) {
if(typeof console !== 'undefined') console.log(e);
}
});
}
});
});
$.entwine('ss', function($) {
$('.external-links-report__create-report').entwine({
PollTimeout: null,
ButtonIsLoading: false,

onclick: function(e) {
e.preventDefault();

this.buttonLoading();
this.start();
},

onmatch: function() {
// poll the current job and update the front end status
this.poll();
},

start: function() {
// initiate a new job
$('.external-links-report__report-progress')
.empty()
.text('Running report 0%');

$.ajax({
url: "admin/externallinks/start",
async: true,
timeout: 3000
});

this.poll();
},

/**
* Get the "create report" button selector
*
* @return {Object}
*/
getButton: function() {
return $('.external-links-report__create-report');
},

/**
* Sets the button into a loading state. See LeftAndMain.js.
*/
buttonLoading: function() {
if (this.getButtonIsLoading()) {
return;
}
this.setButtonIsLoading(true);

var $button = this.getButton();

// set button to "submitting" state
$button.addClass('btn--loading loading');

if ($button.is('button')) {
$button.append($(
'<div class="btn__loading-icon">'+
'<span class="btn__circle btn__circle--1" />'+
'<span class="btn__circle btn__circle--2" />'+
'<span class="btn__circle btn__circle--3" />'+
'</div>'));

$button.css($button.outerWidth() + 'px');
}
},

/**
* Reset the button back to its original state after loading. See LeftAndMain.js.
*/
buttonReset: function() {
this.setButtonIsLoading(false);

var $button = this.getButton();

$button.removeClass('btn--loading loading');
$button.find('.btn__loading-icon').remove();
$button.css('width', 'auto');
},

poll: function() {
var self = this;
this.buttonLoading();

$.ajax({
url: "admin/externallinks/getJobStatus",
async: true,
success: function(data) {
// No report, so let user create one
if (!data) {
this.buttonReset();
return;
}

// Parse data
var completed = data.Completed ? data.Completed : 0;
var total = data.Total ? data.Total : 0;

// If complete status
if (data.Status === 'Completed') {
$('.external-links-report__report-progress')
.text('Report finished ' + completed + '/' + total);

self.buttonReset();
return;
}

// If incomplete update status
if (completed < total) {
var percent = (completed / total) * 100;
$('.external-links-report__report-progress')
.text('Running report ' + completed + '/' + total + ' (' + percent.toFixed(2) + '%)');
}

// Ensure the regular poll method is run
// kill any existing timeout
if (self.getPollTimeout() !== null) {
clearTimeout(self.getPollTimeout());
}

self.setPollTimeout(setTimeout(function() {
$('.external-links-report__create-report').poll();
}, 1000));
},
error: function(e) {
if (typeof console !== 'undefined') {
console.log(e);
}
}
});
}
});
});
}(jQuery));
3 changes: 2 additions & 1 deletion src/Controllers/CMSExternalLinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\ExternalLinks\Controllers;

use SilverStripe\Control\HTTP;
use SilverStripe\Core\Convert;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function getJobStatus()
// Format status
$track = BrokenExternalPageTrackStatus::get_latest();
if ($track) {
return json_encode([
return Convert::array2json([
'TrackID' => $track->ID,
'Status' => $track->Status,
'Completed' => $track->getCompletedPages(),
Expand Down
27 changes: 11 additions & 16 deletions src/Reports/BrokenExternalLinksReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace SilverStripe\ExternalLinks\Reports;

use SilverStripe\ORM\ArrayList;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\Core\Convert;
use SilverStripe\View\HTML;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Reports\Report;
use SilverStripe\View\Requirements;

Expand Down Expand Up @@ -78,25 +78,20 @@ public function sourceRecords()

public function getCMSFields()
{
Requirements::css('silverstripe/externallinks: css/BrokenExternalLinksReport.css');
Requirements::javascript('silverstripe/externallinks: javascript/BrokenExternalLinksReport.js');

$fields = parent::getCMSFields();

$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
$runReportButton = FormAction::create('createReport', _t(__CLASS__ . '.RUNREPORT', 'Create new report'))
->addExtraClass('btn-primary external-links-report__create-report')
->setUseButtonTag(true);
$fields->push($runReportButton);

$reportResultSpan = '<p class="external-links-report__report-progress"></p>';
$reportResult = LiteralField::create('ResultTitle', $reportResultSpan);
$fields->push($reportResult);

$button = HTML::createTag(
'button',
[
'id' => 'externalLinksReport',
'type' => 'button',
'class' => 'btn btn-primary'
],
_t(__CLASS__ . '.RUNREPORT', 'Create new report')
);
$runReportButton = LiteralField::create('runReport', $button);
$fields->push($runReportButton);

return $fields;
}
}
6 changes: 3 additions & 3 deletions src/Tasks/CurlLinkChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CurlLinkChecker implements LinkChecker
/**
* Return cache
*
* @return Zend_Cache_Frontend
* @return CacheInterface
*/
protected function getCache()
{
Expand All @@ -36,7 +36,7 @@ public function checkLink($href)

// Check if we have a cached result
$cacheKey = md5($href);
$result = $this->getCache()->get($cacheKey);
$result = $this->getCache()->get($cacheKey, false);
if ($result !== false) {
return $result;
}
Expand All @@ -51,7 +51,7 @@ public function checkLink($href)
curl_close($handle);

// Cache result
$this->getCache()->set($httpCode, $cacheKey);
$this->getCache()->set($cacheKey, $httpCode);
return $httpCode;
}
}

0 comments on commit e21c0ad

Please sign in to comment.