diff --git a/week03/app/controllers/application.js b/week03/app/controllers/application.js new file mode 100644 index 0000000000000000000000000000000000000000..75034cdba4c27c10b5893e6f4cb07c6d1d1f7fcb --- /dev/null +++ b/week03/app/controllers/application.js @@ -0,0 +1,70 @@ +import Controller from '@ember/controller'; +import { tracked } from '@glimmer/tracking'; +import { action } from '@ember/object'; + +export default class ApplicationController extends Controller { + @tracked a; + @tracked b; + @tracked operator = "+"; + @tracked result; + @tracked calculated = false; + @tracked historyAvailable = false; + @tracked calculationsList = []; + + @action + calculate(event) { + event.preventDefault(); + if (this.operator === "+") { + this.result = parseInt(this.a) + parseInt(this.b); + this.calculated = true; + this.calculationsList.pushObject({calc: `${this.a} ${this.operator} ${this.b} = ${this.result}`}); + console.log(`list: ${this.calculationsList}`) + this.historyAvailable = true; + console.log(`a: ${this.a}, b: ${this.b}, operator: ${this.operator}, result: ${this.result}, calculated: ${this.calculated}`) + } else if (this.operator === "-") { + this.result = parseInt(this.a) - parseInt(this.b); + this.calculated = true; + this.calculationsList.pushObject({calc: `${this.a} ${this.operator} ${this.b} = ${this.result}`}); + console.log(`list: ${this.calculationsList}`) + this.historyAvailable = true; + console.log(`a: ${this.a}, b: ${this.b}, operator: ${this.operator}, result: ${this.result}, calculated: ${this.calculated}`) + } else if (this.operator === "*") { + this.result = parseInt(this.a) * parseInt(this.b); + this.calculated = true; + this.calculationsList.pushObject({calc: `${this.a} ${this.operator} ${this.b} = ${this.result}`}); + console.log(`list: ${this.calculationsList}`) + this.historyAvailable = true; + console.log(`a: ${this.a}, b: ${this.b}, operator: ${this.operator}, result: ${this.result}, calculated: ${this.calculated}`) + }else if (this.operator === "%") { + this.result = parseInt(this.a) % parseInt(this.b); + this.calculated = true; + this.calculationsList.pushObject({calc: `${this.a} ${this.operator} ${this.b} = ${this.result}`}); + console.log(`list: ${this.calculationsList}`) + this.historyAvailable = true; + console.log(`a: ${this.a}, b: ${this.b}, operator: ${this.operator}, result: ${this.result}, calculated: ${this.calculated}`) + } else if (this.operator === "/" && parseInt(this.b) !== 0) { + this.result = parseInt(this.a) / parseInt(this.b); + this.calculated = true; + this.calculationsList.pushObject({calc: `${this.a} ${this.operator} ${this.b} = ${this.result}`}); + console.log(`list: ${this.calculationsList}`) + this.historyAvailable = true; + console.log(`a: ${this.a}, b: ${this.b}, operator: ${this.operator}, result: ${this.result}, calculated: ${this.calculated}`) + } + + } + + @action + setNewOperator(value) { + this.operator = value; + } + + @action + recalculate(params) { + let splitted = params.split(" "), arr = []; + console.log(splitted) + this.a = splitted[0]; + this.operator = splitted[1]; + this.b = splitted[2]; + this.calculate(event); + } +} diff --git a/week03/app/templates/application.hbs b/week03/app/templates/application.hbs index 7b1b1040fe707db00d874cb2695aa509cfb3a93f..ad575a238dbde266b8f6f5fd04deda8597fc1c37 100644 --- a/week03/app/templates/application.hbs +++ b/week03/app/templates/application.hbs @@ -1,5 +1,37 @@ -{{!-- The following component displays Ember's default welcome message. --}} -<WelcomePage /> -{{!-- Feel free to remove this! --}} +<h1>Calculator</h1> +<form {{on "submit" this.calculate}}> + <label> + A + <Input @value={{this.a}} name="a" type="number" /> + </label> + <label> + B + <Input @value={{this.b}} name="b" type="number" /> + </label> + <select onchange={{action "setNewOperator" value="target.value"}}> + <option value="+">+</option> + <option value="-">-</option> + <option value="*">*</option> + <option value="/">/</option> + <option value="%">%</option> + </select> + <button>Calculate</button> +</form> +{{#if calculated}} + <h2>Result</h2> + <div id="result"> + <p>{{this.a}} {{this.operator}} {{this.b}} = {{this.result}}</p> + </div> +{{/if}} +{{#if historyAvailable}} + <h2>History</h2> + <div id="history"> + <ul> + {{#each this.calculationsList as | calculation |}} + <li><a {{on "click" (fn this.recalculate calculation.calc)}}>{{calculation.calc}}</a></li> + {{/each}} + </ul> + </div> +{{/if}} {{outlet}} \ No newline at end of file diff --git a/week03/debug.log b/week03/debug.log new file mode 100644 index 0000000000000000000000000000000000000000..098b38158c8d44768ca531508f53f6fe34b5a803 --- /dev/null +++ b/week03/debug.log @@ -0,0 +1,3 @@ +[1214/170119.440:ERROR:directory_reader_win.cc(43)] FindFirstFile: Das System kann den angegebenen Pfad nicht finden. (0x3) +[1214/175008.630:ERROR:directory_reader_win.cc(43)] FindFirstFile: Das System kann den angegebenen Pfad nicht finden. (0x3) +[1214/191059.372:ERROR:directory_reader_win.cc(43)] FindFirstFile: Das System kann den angegebenen Pfad nicht finden. (0x3) diff --git a/week03/jsconfig.json b/week03/jsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..5d20ed48af47d81b5c315fadd9a5b8106ff6cd12 --- /dev/null +++ b/week03/jsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "es2015", + "experimentalDecorators": true + }, + "exclude": [ + "node_modules", + "dist" + ] +} \ No newline at end of file diff --git a/week03/tests/unit/controllers/application-test.js b/week03/tests/unit/controllers/application-test.js new file mode 100644 index 0000000000000000000000000000000000000000..e4b814b2829f4c33cda8703305f578d4fd7940ca --- /dev/null +++ b/week03/tests/unit/controllers/application-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Controller | application', function(hooks) { + setupTest(hooks); + + // TODO: Replace this with your real tests. + test('it exists', function(assert) { + let controller = this.owner.lookup('controller:application'); + assert.ok(controller); + }); +});