Skip to content

Commit

Permalink
render display in the fixed position style
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyknown committed Dec 21, 2015
1 parent 1745830 commit 193aed2
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 33 deletions.
61 changes: 61 additions & 0 deletions app/components/render-display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Ember from 'ember';
const { isPresent } = Ember;

export default Ember.Component.extend({
tagName: 'div',

classNames: ['display'],

termOne: null,

termTwo: null,

answer: null,

level: null,

operator: null,

isEqualVisible: false,

_pad(value, length) {
return ` ${String(string)}`.slice(-length).split('').map((s) => {
return isPresent(s.trim()) ? s : '';
});
},

characters: Ember.computed('termOne', 'termTwo', 'level', 'operator', function() {
let characters = [];

if (isPresent(this.get('termOne'))) {
characters = characters.concat(` ${this.get('termOne').toString()}`.slice(-2).split(''));
} else {
characters = characters.concat(['','']);
}
if (isPresent(this.get('operator'))) {
characters.pushObject(this.get('operator'));
}
if (isPresent(this.get('termTwo'))) {
characters = characters.concat(this._pad(this.get('termTwo')));
} else {
characters = characters.concat(['','']);
}
if (this.get('displayEquals')) {
characters.pushObject('=');
} else {
characters.pushObject(' ')
}
if (isPresent(this.get('level'))) {
characters = characters.concat(['L','',this.get('level')]);
} else {
if (isPresent(this.get('answer'))) {
characters = characters.concat(` ${this.get('answer').toString()}`.slice(-2).split(''));
} else {
characters = characters.concat(['','']);
}
}

return characters;
})

});
11 changes: 6 additions & 5 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember';
import ProblemGenerator from 'little-professor/models/problem-generator';
const { isPresent } = Ember;

export default Ember.Controller.extend({
levels: [1,2,3,4],
Expand All @@ -9,19 +10,19 @@ export default Ember.Controller.extend({
isRetrying: Ember.computed.gt('tryCount', 0),

problem: Ember.computed('problems', 'problemIndex', function() {
if (Ember.isPresent(this.get('problems')) && Ember.isPresent(this.get('problemIndex'))) {
if (isPresent(this.get('problems')) && isPresent(this.get('problemIndex'))) {
return this.get('problems').objectAt(this.get('problemIndex'));
}
}),

isComplete: Ember.computed('problems', 'problemIndex', function() {
if (Ember.isPresent(this.get('problemIndex')) && Ember.isPresent(this.get('problems'))) {
if (isPresent(this.get('problemIndex')) && isPresent(this.get('problems'))) {
return this.get('problemIndex') >= this.get('problems').length;
}
}),

isAnswerComplete: Ember.computed('answer', 'problem.answer', function() {
if ((this.get('answer') || this.get('answer') === 0) && (this.get('problem.answer') || this.get('problem.answer') === 0)) {
if ((isPresent(this.get('answer'))) && (isPresent(this.get('problem.answer')))) {
return this.get('answer').toString().length === this.get('problem.answer').toString().length;
} else {
return false;
Expand Down Expand Up @@ -77,7 +78,7 @@ export default Ember.Controller.extend({
this.set('isAnswerLocked', false);
this.set('isIncorrect', false);
this.set('tryCount', 0);
if (Ember.isPresent(this.get('problemIndex'))) {
if (isPresent(this.get('problemIndex'))) {
this.incrementProperty('problemIndex');
} else {
this.set('problemIndex', 0);
Expand Down Expand Up @@ -181,8 +182,8 @@ export default Ember.Controller.extend({
this.toggleProperty('isDisplayingAnswer');
this.set('isAnswerLocked', true);
} else {
this.set('isAnswerLocked', false);
this.set('answer', null);
this.set('isAnswerLocked', false);
}
}, 1000);
}, 500);
Expand Down
21 changes: 21 additions & 0 deletions app/helpers/format-term.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Ember from 'ember';
const { isPresent } = Ember;

export function formatTerm(params) {
const term = params[0];
let formatted;
const characters = ['',''];

if (isPresent(term)) {
const termCharacters = String(term).split('');
termCharacters.reverse().forEach((character, index) => {
characters[characters.length - index - 1] = character;
});
}

return new Ember.Handlebars.SafeString(characters.map((c) => {
return `<span class='display-character'>${c}</span>`;
}).join(''));
}

export default Ember.Helper.helper(formatTerm);
7 changes: 6 additions & 1 deletion app/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ button {
height: 1.5em;
}

#display {
.display {
height: 15%;
background-color: #ba0c18;
color: #fcd9e9;
Expand Down Expand Up @@ -120,3 +120,8 @@ button {
margin-bottom: 5px;
font-size: 1.2em;
}

.display-character {
width: 20px;
display: inline-block;
}
48 changes: 21 additions & 27 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
<div id="little-professor">
<div id="display">
<div class="display-characters">
{{#if isOn}}
{{#if (eq mode 'settings')}}
{{format-operator operator}}
L
{{level}}
{{else if (eq mode 'game')}}
{{#if isComplete}}
<span class="blink">
{{correctAnswerCount}}
</span>
{{else}}
{{#if isIncorrect}}
EEE
{{else}}
{{problem.termOne}}
{{format-operator problem.operator}}
{{problem.termTwo}}
=
{{answer}}
{{/if}}
{{/if}}
{{/if}}
{{#if isOn}}
{{#if (eq mode 'settings')}}
{{render-display operator=operator level=level}}
{{else if (eq mode 'game')}}
{{#if isComplete}}
{{render-display answer=correctAnswerCount blink=true}}
{{else}}
&nbsp;
{{#if isIncorrect}}
{{render-display isError=true}}
{{else}}
{{render-display
operator=problem.operator
termOne=problem.termOne
termTwo=problem.termTwo
isEqualVisible=true
answer=answer
}}
{{/if}}
{{/if}}
</div>
</div>
{{/if}}
{{else}}
{{render-display}}
{{/if}}
<div id="main">
<div id="face">
&nbsp;
Expand Down
35 changes: 35 additions & 0 deletions app/templates/components/render-display.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div class="display-characters">
{{format-term termOne}}

<span class="display-character">
{{#if operator}}
{{format-operator operator}}
{{/if}}
</span>

{{format-term termTwo}}

<span class="display-character">
{{#if isEqualVisible}}
=
{{/if}}
</span>

{{#if level}}
<span class="display-character">L</span>
<span class="display-character"></span>
<span class="display-character">{{level}}</span>
{{else if isError}}
<span class="display-character">E</span>
<span class="display-character">E</span>
<span class="display-character">E</span>
{{else}}
{{#if blink}}
<span class="blink">
{{format-term answer}}
</span>
{{else}}
{{format-term answer}}
{{/if}}
{{/if}}
</div>
25 changes: 25 additions & 0 deletions tests/integration/components/display-characters-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('display-characters', 'Integration | Component | display characters', {
integration: true
});

test('it renders', function(assert) {

// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });" + EOL + EOL +

this.render(hbs`{{display-characters}}`);

assert.equal(this.$().text().trim(), '');

// Template block usage:" + EOL +
this.render(hbs`
{{#display-characters}}
template block text
{{/display-characters}}
`);

assert.equal(this.$().text().trim(), 'template block text');
});
10 changes: 10 additions & 0 deletions tests/unit/helpers/format-term-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { formatTerm } from '../../../helpers/format-term';
import { module, test } from 'qunit';

module('Unit | Helper | format term');

// Replace this with your real tests.
test('it works', function(assert) {
let result = formatTerm(42);
assert.ok(result);
});

0 comments on commit 193aed2

Please sign in to comment.