From 295f4bbfc76ccc94ff5654e3106c0072bd402a1d Mon Sep 17 00:00:00 2001
From: Marcel Mernitz <marcel.mernitz@informatik.uni-halle.de>
Date: Mon, 14 Dec 2020 19:54:44 +0100
Subject: [PATCH] initial push

---
 week03/app/controllers/application.js         | 70 +++++++++++++++++++
 week03/app/templates/application.hbs          | 38 +++++++++-
 week03/debug.log                              |  3 +
 week03/jsconfig.json                          | 10 +++
 .../unit/controllers/application-test.js      | 12 ++++
 5 files changed, 130 insertions(+), 3 deletions(-)
 create mode 100644 week03/app/controllers/application.js
 create mode 100644 week03/debug.log
 create mode 100644 week03/jsconfig.json
 create mode 100644 week03/tests/unit/controllers/application-test.js

diff --git a/week03/app/controllers/application.js b/week03/app/controllers/application.js
new file mode 100644
index 0000000..75034cd
--- /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 7b1b104..ad575a2 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 0000000..098b381
--- /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 0000000..5d20ed4
--- /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 0000000..e4b814b
--- /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);
+  });
+});
-- 
GitLab