Advertisement
avr39ripe

jsMVCExample

Jun 9th, 2021
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="utf-8">
  6.     <title>MVC</title>
  7.     <style>
  8.         html,
  9.         body {
  10.             display: flex;
  11.             width: 95%;
  12.             height: 95%;
  13.             min-width: 810px;
  14.             min-height: 410px;
  15.             align-items: center;
  16.             justify-content: center;
  17.             margin: 8px 15px;
  18.         }
  19.  
  20.         .centered {
  21.             display: flex;
  22.             flex-direction: row;
  23.             align-items: center;
  24.             justify-content: center;
  25.             padding: 50px;
  26.         }
  27.         .calcElement {
  28.             display: flex;
  29.             justify-content: center;
  30.             align-items: center;
  31.             min-width: 50px;
  32.             min-height: 50px;
  33.             border: 1px solid black;
  34.             margin: 5px 5px;
  35.             padding: 2px 2px;
  36.             font: bold 30px arial;
  37.         }
  38.     </style>
  39. </head>
  40. <body>
  41.     <div id="calcView" class="centered">
  42.         <div id="calcValA" class="calcElement"></div>
  43.         <div id="calcOp" class="calcElement"></div>
  44.         <div id="calcValB" class="calcElement"></div>
  45.         <div id="calcEqual" class="calcElement">=</div>
  46.         <div id="calcRes" class="calcElement"></div>
  47.     </div>
  48.     <div id="calcController" class="centered">
  49.         <input id="calcInputAll" type="text" placeholder="valA oper valB">
  50.     </div>
  51.     <script>
  52.         `use strict`
  53.  
  54.         class CalcModel {
  55.             constructor() {
  56.                 this._valA = 0;
  57.                 this._valB = 0;
  58.                 this._op = '+'
  59.                 this._res = 0;
  60.                 this._views = [];
  61.             }
  62.             static operations = {
  63.                 '+': (a, b) => +a + +b,
  64.                 '-': (a, b) => a - b,
  65.                 '*': (a, b) => a * b,
  66.                 '/': (a, b) => a / b,
  67.             }
  68.  
  69.             set valA(val) {
  70.                 if (this._valA != val) { this._valA = val; }
  71.             }
  72.             set valB(val) {
  73.                 if (this._valB != val) { this._valB = val; }                
  74.             }
  75.             set op(val) {
  76.                 if (this._op != val) { this._op = val; }                
  77.             }
  78.  
  79.             get valA() { return this._valA; }
  80.             get valB() { return this._valB; }
  81.             get op() { return this._op; }
  82.  
  83.             evaluate() {
  84.                 if (Object.keys(CalcModel.operations).includes(this._op)) {
  85.                     this._res = CalcModel.operations[this._op](this._valA, this._valB);
  86.                 }
  87.                 else {
  88.                     this._res = 'ERROR';
  89.                 }
  90.                 this.notify();
  91.             }
  92.  
  93.             addView(view) {
  94.                 this._views.push(view);
  95.             }
  96.  
  97.             removeView(view) {
  98.                 this._views.splice(this._views.indexOf(view), 1);
  99.             }
  100.  
  101.             notify() {
  102.                 for (let view of this._views) {
  103.                     view.render(this._valA, this._valB, this._op, this._res);
  104.                 }
  105.             }
  106.         }
  107.  
  108.         class CalcView {
  109.             constructor(calcModel){
  110.                 this._calcModel = calcModel;
  111.                 this.attach();
  112.             }
  113.  
  114.             attach() {
  115.                 this._calcModel.addView(this);
  116.             }
  117.  
  118.             detach() {
  119.                 this._calcModel.removeView(this);
  120.             }
  121.  
  122.             render(valA, valB, op, res) {
  123.                 // empty
  124.             }
  125.         }
  126.  
  127.         class CalcViewConsole extends CalcView {
  128.             render(valA, valB, op, res) {
  129.                 console.log(`${valA} ${op} ${valB} = ${res}`);
  130.             }
  131.         }
  132.  
  133.         class CalcViewHtmlDiv extends CalcView {
  134.             constructor(calcModel, div) {
  135.                 super(calcModel);
  136.                 this._mainDiv = document.querySelector(`#${div}`);
  137.                 this._valA = this._mainDiv.querySelector('#calcValA');
  138.                 this._valB = this._mainDiv.querySelector('#calcValB');
  139.                 this._op = this._mainDiv.querySelector('#calcOp');
  140.                 this._res = this._mainDiv.querySelector('#calcRes');
  141.             }
  142.  
  143.             render(valA, valB, op, res) {
  144.                 this._valA.textContent = valA;
  145.                 this._op.textContent = op;
  146.                 this._valB.textContent = valB;
  147.                 this._res.textContent = res;
  148.             }
  149.         }
  150.  
  151.         class CalcController {
  152.             constructor(calcModel) {
  153.                 this._calcModel = calcModel;
  154.             }
  155.  
  156.             setValA(val, evaluate = false) {
  157.                 this._calcModel.valA = val;
  158.                 if (evaluate) { this._calcModel.evaluate(); }
  159.             }
  160.  
  161.             setValB(val, evaluate = false) {
  162.                 this._calcModel.valB = val;
  163.                 if (evaluate) { this._calcModel.evaluate(); }
  164.             }
  165.  
  166.             setOp(op, evaluate = false) {
  167.                 this._calcModel.op = op;
  168.                 if (evaluate) { this._calcModel.evaluate(); }
  169.             }
  170.  
  171.         }
  172.  
  173.         class CalcControllerText extends CalcController {
  174.             constructor(calcModel) {
  175.                 super(calcModel);
  176.             }
  177.  
  178.             evaluateText(text) {
  179.                 let data = [...text.matchAll(/\s*(\d+)\s*([+-/*]){1}\s*(\d+)/g)][0];
  180.                 if (data) {
  181.                     let [, valA, op, valB] = data;
  182.  
  183.                     this.setValA(valA);
  184.                     this.setValB(valB);
  185.                     this.setOp(op, true);
  186.                     return true;
  187.                 }
  188.                 return false;
  189.             }
  190.         }
  191.  
  192.         class CalcControllerHtmlInputText extends CalcControllerText {
  193.             constructor(calcModel, input) {
  194.                 super(calcModel);
  195.                 this._input = document.querySelector(`#${input}`);
  196.  
  197.                 this._input.addEventListener('change', (event) => { this.onChange(event); });
  198.             }
  199.  
  200.             onChange(event) {
  201.                 if (!this.evaluateText(event.target.value)) {
  202.                     alert('Invalid input for evalution!');
  203.                 }
  204.             }
  205.         }
  206.  
  207.         {
  208.             let calcModel = new CalcModel();
  209.             let calcViewConsole = new CalcViewConsole(calcModel);
  210.             let calcViewHtmlDiv = new CalcViewHtmlDiv(calcModel, 'calcView');
  211.             let calcController = new CalcController(calcModel);
  212.             let calcControllerText = new CalcControllerText(calcModel);
  213.             let calcControllerHtmlInputText = new CalcControllerHtmlInputText(calcModel, 'calcInputAll');
  214.  
  215.             calcController.setValA(1500, true);
  216.             calcController.setValB(1400, true);
  217.             calcController.setOp('+', true);
  218.  
  219.             //calcViewConsole.detach();
  220.  
  221.             calcController.setValA(3);
  222.             calcController.setValB(4);
  223.             calcController.setOp('*', true);
  224.  
  225.             console.log(calcControllerText.evaluateText('   7  +   8   '));
  226.         }
  227.     </script>
  228.  
  229. </body>
  230. </html>
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement