summaryrefslogtreecommitdiffstats
path: root/kjsembed/docs/examples/calc/calc.js
blob: 8ff7a4038ecfdbf15d6aa6b21ae5689f3afb9805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env kjscmd


function Calculator(ui)
{
  // Setup entry functions
  var display = ui.child('display');
  this.display = display;

  this.one = function() { display.intValue = display.intValue*10+1; }
  this.two = function() { display.intValue = display.intValue*10+2; }
  this.three = function() { display.intValue = display.intValue*10+3; }
  this.four = function() { display.intValue = display.intValue*10+4; }
  this.five = function() { display.intValue = display.intValue*10+5; }
  this.six = function() { display.intValue = display.intValue*10+6; }
  this.seven = function() { display.intValue = display.intValue*10+7; }
  this.eight = function() { display.intValue = display.intValue*10+8; }
  this.nine = function() { display.intValue = display.intValue*10+9; }
  this.zero = function() { display.intValue = display.intValue*10+0; }

  ui.connect( ui.child('one'), 'clicked()', this, 'one' );
  ui.connect( ui.child('two'), 'clicked()', this, 'two' );
  ui.connect( ui.child('three'), 'clicked()', this, 'three' );
  ui.connect( ui.child('four'), 'clicked()', this, 'four' );
  ui.connect( ui.child('five'), 'clicked()', this, 'five' );
  ui.connect( ui.child('six'), 'clicked()', this, 'six' );
  ui.connect( ui.child('seven'), 'clicked()', this, 'seven' );
  ui.connect( ui.child('eight'), 'clicked()', this, 'eight' );
  ui.connect( ui.child('nine'), 'clicked()', this, 'nine' );
  ui.connect( ui.child('zero'), 'clicked()', this, 'zero' );

  this.val = 0;
  this.lastop = function() {}

  this.plus = function()
              {
                 this.val = display.intValue+this.val;
                 display.intValue = 0;
                 this.lastop=this.plus
              }

  this.minus = function()
               {
                  this.val = display.intValue-this.val;
                  display.intValue = 0;
                  this.lastop=this.minus;
               }


  ui.connect( ui.child('plus'), 'clicked()', this, 'plus' );
  ui.connect( ui.child('minus'), 'clicked()', this, 'minus' );

  this.equals = function() { this.lastop(); display.intValue = this.val; }
  this.clear = function() { this.lastop=function(){}; display.intValue = 0; this.val = 0; }

  ui.connect( ui.child('equals'), 'clicked()', this, 'equals' );
  ui.connect( ui.child('clear'), 'clicked()', this, 'clear' );
}

var ui = Factory.loadui('calc.ui');
var calc = new Calculator(ui);

ui.show();
application.exec();