Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make the transition to a backbone view
- Loading branch information
Showing
11 changed files
with
274 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,88 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'), | ||
extendMixin = require('./extend'), | ||
$ = require(typeof window !== 'undefined' ? 'jquery' : 'cheerio'), | ||
Backbone = require('backbone'), | ||
|
||
Handlebars = require('./handlebars/handlebars'), | ||
View; | ||
|
||
View = function (module) { | ||
this.module = module; | ||
if (this.template) { | ||
this.template = this.getCompiledTemplate(this.template); | ||
this.noDataTemplate = this.getCompiledTemplate('noData'); | ||
Backbone.$ = $; | ||
|
||
View = Backbone.View.extend({ | ||
tagName: 'section', | ||
|
||
constructor: function (module, options) { | ||
this.module = module; | ||
if (this.template) { | ||
this.compiledTemplate = this.getCompiledTemplate(this.template); | ||
this.noDataTemplate = this.getCompiledTemplate('noData'); | ||
} | ||
|
||
Backbone.View.prototype.constructor.call(this, options); | ||
}, | ||
|
||
className: function () { | ||
var classNames = [ 'panel' ]; | ||
|
||
if (_.isEmpty(this.module.attributes)) { | ||
classNames.push('panel-warning'); | ||
classNames.push('noData'); | ||
} else { | ||
if (this.module.has('connected')) { | ||
classNames.push(this.module.get('connected') ? 'panel-success' : 'panel-danger'); | ||
} else { | ||
classNames.push('panel-success'); | ||
} | ||
classNames.push(this.module.viewId); | ||
} | ||
|
||
return classNames.join(' '); | ||
}, | ||
|
||
attributes: function () { | ||
return { | ||
id: this.module.id | ||
}; | ||
}, | ||
|
||
getCompiledTemplate: function (templateName) { | ||
return require('../build/compiledTemplates')(Handlebars)[templateName]; | ||
}, | ||
|
||
render: function () { | ||
this.updateHtml(); | ||
}, | ||
|
||
updateHtml: function () { | ||
var attrs = _.extend({}, _.result(this, 'attributes'), { 'class': _.result(this, 'className') }), | ||
html = !_.isEmpty(this.module.attributes) ? this.renderViewWithData() : this.renderViewWithoutData(); | ||
this.$el.attr(attrs).html(html); | ||
}, | ||
|
||
getFullHtml: function () { | ||
this.updateHtml(); | ||
return $('<div />').append(this.$el).html(); | ||
}, | ||
|
||
getViewData: function () { | ||
return { json: JSON.stringify(this.module.attributes) }; | ||
}, | ||
|
||
renderViewWithData: function () { | ||
return this.compiledTemplate(_.extend({ | ||
id: this.module.id, | ||
title: this.module.title, | ||
lastUpdated: new Date() | ||
}, this.module.attributes, this.getViewData())); | ||
}, | ||
|
||
renderViewWithoutData: function () { | ||
return this.noDataTemplate({ | ||
id: this.module.id, | ||
title: this.module.title | ||
}); | ||
} | ||
}; | ||
extendMixin(View); | ||
|
||
View.prototype.getCompiledTemplate = function (templateName) { | ||
return require('../build/compiledTemplates')(Handlebars)[templateName]; | ||
}; | ||
|
||
View.prototype.render = function () { | ||
return !_.isEmpty(this.module.attributes) ? this.renderViewWithData() : this.renderViewWithoutData(); | ||
}; | ||
|
||
View.prototype.getViewData = function () { | ||
return { json: JSON.stringify(this.module.attributes) }; | ||
}; | ||
|
||
View.prototype.renderViewWithData = function () { | ||
return this.template(_.extend({ | ||
id: this.module.id, | ||
title: this.module.title, | ||
lastUpdated: new Date() | ||
}, this.module.attributes, this.getViewData())); | ||
}; | ||
|
||
View.prototype.renderViewWithoutData = function () { | ||
return this.noDataTemplate({ | ||
id: this.module.id, | ||
title: this.module.title | ||
}); | ||
}; | ||
}); | ||
|
||
module.exports = View; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
<section class="json panel panel-success" id="{{ id }}"> | ||
<header class="panel-heading"> | ||
<h2 class="panel-title">{{ title }}</h2> | ||
</header> | ||
<div class="panel-body"> | ||
{{ json }} | ||
</div> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> | ||
</section> | ||
<header class="panel-heading"> | ||
<h2 class="panel-title">{{ title }}</h2> | ||
</header> | ||
<div class="panel-body"> | ||
{{ json }} | ||
</div> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
<section class="market panel panel-success" id="{{ id }}"> | ||
<header class="panel-heading"> | ||
<h2 class="panel-title">{{ title }}</h2> | ||
</header> | ||
<dl class="panel-body dl-horizontal"> | ||
<dt class="big-label">Latest Trade:</dt> | ||
<dd class="big">{{number close}} {{currency}}</dd> | ||
<dt>Best Bid:</dt> | ||
<dd>{{number bid}} {{currency}}</dd> | ||
<dt>Best Ask:</dt> | ||
<dd>{{number ask}} {{currency}}</dd> | ||
</dl> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> | ||
</section> | ||
<header class="panel-heading"> | ||
<h2 class="panel-title">{{ title }}</h2> | ||
</header> | ||
<dl class="panel-body dl-horizontal"> | ||
<dt class="big-label">Latest Trade:</dt> | ||
<dd class="big">{{number close}} {{currency}}</dd> | ||
<dt>Best Bid:</dt> | ||
<dd>{{number bid}} {{currency}}</dd> | ||
<dt>Best Ask:</dt> | ||
<dd>{{number ask}} {{currency}}</dd> | ||
</dl> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,88 @@ | ||
<section class="miner panel {{#if connected}}panel-success{{else}}panel-danger{{/if}}" id="{{ id }}"> | ||
<header class="panel-heading"> | ||
<header class="panel-heading"> | ||
<div class="row"> | ||
<h2 class="panel-title col-xs-6">{{ title }}</h2> | ||
<div class="col-xs-6"><span class="pull-right">{{#if connected}}Connected{{else}}Disconnected{{/if}}</span></div> | ||
</div> | ||
</header> | ||
{{#if connected}} | ||
<div class="panel-body"> | ||
<h4>Overview</h4> | ||
<div class="row"> | ||
<h2 class="panel-title col-xs-6">{{ title }}</h2> | ||
<div class="col-xs-6"><span class="pull-right">{{#if connected}}Connected{{else}}Disconnected{{/if}}</span></div> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt class="big-label">Hashrate:</dt> | ||
<dd class="big">{{hashrate avgHashrate}}</dd> | ||
<dt></dt> | ||
<dd></dd> | ||
<dt>Hardware Errors:</dt> | ||
<dd>{{number hardwareErrors}}</dd> | ||
<dt>Hardware Error Rate:</dt> | ||
<dd>{{number hardwareErrorRate precision="2"}}%</dd> | ||
<dt>Uptime:</dt> | ||
<dd>{{timespan elapsed}}</dd> | ||
<dt>Software:</dt> | ||
<dd>{{description}}</dd> | ||
</dl> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt>Accepted Shares:</dt> | ||
<dd>{{number shares.accepted}}</dd> | ||
<dt>Rejected Shares:</dt> | ||
<dd>{{number shares.rejected}}</dd> | ||
<dt>Stale Shares:</dt> | ||
<dd>{{number shares.stale}}</dd> | ||
<dt>Discarded Shares:</dt> | ||
<dd>{{number shares.discarded}}</dd> | ||
</dl> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt>Difficulty Accepted:</dt> | ||
<dd>{{number difficulty.accepted precision="2"}}</dd> | ||
<dt>Difficulty Rejected:</dt> | ||
<dd>{{number difficulty.rejected precision="2"}}</dd> | ||
<dt>Difficulty Stale:</dt> | ||
<dd>{{number difficulty.stale precision="2"}}</dd> | ||
</dl> | ||
</div> | ||
</header> | ||
{{#if connected}} | ||
<div class="panel-body"> | ||
<h4>Overview</h4> | ||
<div class="row"> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt class="big-label">Hashrate:</dt> | ||
<dd class="big">{{hashrate avgHashrate}}</dd> | ||
<dt></dt> | ||
<dd></dd> | ||
<dt>Hardware Errors:</dt> | ||
<dd>{{number hardwareErrors}}</dd> | ||
<dt>Hardware Error Rate:</dt> | ||
<dd>{{number hardwareErrorRate precision="2"}}%</dd> | ||
<dt>Uptime:</dt> | ||
<dd>{{timespan elapsed}}</dd> | ||
<dt>Software:</dt> | ||
<dd>{{description}}</dd> | ||
</dl> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt>Accepted Shares:</dt> | ||
<dd>{{number shares.accepted}}</dd> | ||
<dt>Rejected Shares:</dt> | ||
<dd>{{number shares.rejected}}</dd> | ||
<dt>Stale Shares:</dt> | ||
<dd>{{number shares.stale}}</dd> | ||
<dt>Discarded Shares:</dt> | ||
<dd>{{number shares.discarded}}</dd> | ||
</dl> | ||
<dl class="dl-horizontal col-xs-12 col-md-4"> | ||
<dt>Difficulty Accepted:</dt> | ||
<dd>{{number difficulty.accepted precision="2"}}</dd> | ||
<dt>Difficulty Rejected:</dt> | ||
<dd>{{number difficulty.rejected precision="2"}}</dd> | ||
<dt>Difficulty Stale:</dt> | ||
<dd>{{number difficulty.stale precision="2"}}</dd> | ||
</dl> | ||
{{#if pools}} | ||
<h4>Pools</h4> | ||
<div class="pools table-responsive"> | ||
<table class="table table-bordered"> | ||
<tr> | ||
<th></th> | ||
<th>Priority</th> | ||
<th>Url</th> | ||
<th>Alive</th> | ||
<th>Active</th> | ||
<th>Last Share Submitted</th> | ||
</tr> | ||
{{#each pools}} | ||
<tr> | ||
<td class="{{#if alive}}success{{else}}danger{{/if}}">Pool {{id}}</td> | ||
<td>{{priority}}</td> | ||
<td>{{url}}</td> | ||
<td>{{#if alive}}Yes{{else}}No{{/if}}</td> | ||
<td>{{#if active}}Yes{{else}}No{{/if}}</td> | ||
<td>{{#if lastShareTime}}{{time lastShareTime}}{{else}}Never{{/if}}</td> | ||
</tr> | ||
{{/each}} | ||
</table> | ||
</div> | ||
{{#if pools}} | ||
<h4>Pools</h4> | ||
<div class="pools table-responsive"> | ||
<table class="table table-bordered"> | ||
<tr> | ||
<th></th> | ||
<th>Priority</th> | ||
<th>Url</th> | ||
<th>Alive</th> | ||
<th>Active</th> | ||
<th>Last Share Submitted</th> | ||
</tr> | ||
{{#each pools}} | ||
<tr> | ||
<td class="{{#if alive}}success{{else}}danger{{/if}}">Pool {{id}}</td> | ||
<td>{{priority}}</td> | ||
<td>{{url}}</td> | ||
<td>{{#if alive}}Yes{{else}}No{{/if}}</td> | ||
<td>{{#if active}}Yes{{else}}No{{/if}}</td> | ||
<td>{{#if lastShareTime}}{{time lastShareTime}}{{else}}Never{{/if}}</td> | ||
</tr> | ||
{{/each}} | ||
</table> | ||
</div> | ||
{{/if}} | ||
{{#if devices}} | ||
<h4>Devices</h4> | ||
<div class="row devices"> | ||
{{#each devices}} | ||
<div class="col-xs-12 col-md-4"> | ||
<table class="table table-bordered"> | ||
<tr> | ||
<td class="{{#if connected}}success{{else}}danger{{/if}}">Device {{id}}</td> | ||
<td>{{description}}</td> | ||
<td>{{hashrate avgHashrate}}</td> | ||
<td>{{number hardwareErrorRate precision="2"}}% Hardware Error Rate</td> | ||
</tr> | ||
</table> | ||
</div> | ||
{{/each}} | ||
</div> | ||
{{/if}} | ||
</div> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> | ||
{{/if}} | ||
</section> | ||
{{/if}} | ||
{{#if devices}} | ||
<h4>Devices</h4> | ||
<div class="row devices"> | ||
{{#each devices}} | ||
<div class="col-xs-12 col-md-4"> | ||
<table class="table table-bordered"> | ||
<tr> | ||
<td class="{{#if connected}}success{{else}}danger{{/if}}">Device {{id}}</td> | ||
<td>{{description}}</td> | ||
<td>{{hashrate avgHashrate}}</td> | ||
<td>{{number hardwareErrorRate precision="2"}}% Hardware Error Rate</td> | ||
</tr> | ||
</table> | ||
</div> | ||
{{/each}} | ||
</div> | ||
{{/if}} | ||
</div> | ||
<footer class="text-muted">Updated: {{time lastUpdated}}</footer> | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<section class="no-data panel panel-warning" id="{{ id }}"> | ||
<header class="panel-heading"> | ||
<div class="row"> | ||
<h2 class="panel-title col-xs-6">{{ title }}</h2> | ||
<div class="col-xs-6"><span class="pull-right">No Data Yet.</span></div> | ||
</div> | ||
</header> | ||
</section> | ||
<header class="panel-heading"> | ||
<div class="row"> | ||
<h2 class="panel-title col-xs-6">{{ title }}</h2> | ||
<div class="col-xs-6"><span class="pull-right">No Data Yet.</span></div> | ||
</div> | ||
</header> |
Oops, something went wrong.