Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / apps / calculator / app / controller.js
1 /**
2  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  **/
6
7 // Checking for "chrome.app.runtime" availability allows this Chrome app code to
8 // be tested in a regular web page (like tests/manual.html). Checking for
9 // "chrome" and "chrome.app" availability further allows this code to be tested
10 // in non-Chrome browsers, which is useful for example to test touch support
11 // with a non-Chrome touch device.
12 if (typeof chrome !== 'undefined' && chrome.app && chrome.app.runtime) {
13   var showCalculatorWindow = function () {
14     chrome.app.window.create('calculator.html', {
15       defaultWidth: 243, minWidth: 243, maxWidth: 243,
16       defaultHeight: 380, minHeight: 380, maxHeight: 380,
17       id: 'calculator'
18     }, function(appWindow) {
19       appWindow.contentWindow.onload = function() {
20         new Controller(new Model(9), new View(appWindow.contentWindow));
21       };
22     });
23   }
24
25   chrome.app.runtime.onLaunched.addListener(showCalculatorWindow);
26 }
27
28 function Controller(model, view) {
29   this.inputs = this.defineInputs_();
30   this.model = model;
31   this.view = view;
32   this.view.onButton = function(button) {
33     this.handleInput_(this.inputs.byButton[button]);
34   }.bind(this);
35   this.view.onKey = function(key) {
36     this.handleInput_(this.inputs.byKey[key]);
37   }.bind(this);
38 }
39
40 /** @private */
41 Controller.prototype.defineInputs_ = function() {
42   var inputs = {byButton: {}, byKey: {}};
43   inputs.byButton['zero'] = inputs.byKey['48'] = inputs.byKey['96'] = '0';
44   inputs.byButton['one'] = inputs.byKey['49'] = inputs.byKey['97'] = '1';
45   inputs.byButton['two'] = inputs.byKey['50'] = inputs.byKey['98'] = '2';
46   inputs.byButton['three'] = inputs.byKey['51'] = inputs.byKey['99'] = '3';
47   inputs.byButton['four'] = inputs.byKey['52'] = inputs.byKey['100'] = '4';
48   inputs.byButton['five'] = inputs.byKey['53'] = inputs.byKey['101'] = '5';
49   inputs.byButton['six'] = inputs.byKey['54'] = inputs.byKey['102'] = '6';
50   inputs.byButton['seven'] = inputs.byKey['55'] = inputs.byKey['103'] = '7';
51   inputs.byButton['eight'] = inputs.byKey['56'] = inputs.byKey['104'] = '8';
52   inputs.byButton['nine'] = inputs.byKey['57'] = inputs.byKey['105'] = '9';
53   inputs.byButton['point'] = inputs.byKey['190'] = inputs.byKey['110'] = '.';
54   inputs.byButton['add'] = inputs.byKey['^187'] = inputs.byKey['107'] = '+';
55   inputs.byButton['subtract'] = inputs.byKey['189'] = inputs.byKey['109'] = '-';
56   inputs.byButton['multiply'] = inputs.byKey['^56'] = inputs.byKey['106'] = '*';
57   inputs.byButton['divide'] = inputs.byKey['191'] = inputs.byKey['111'] = '/';
58   inputs.byButton['equals'] = inputs.byKey['187'] = inputs.byKey['13'] = '=';
59   inputs.byButton['negate'] = inputs.byKey['32'] = '+ / -';
60   inputs.byButton['clear'] = inputs.byKey['67'] = 'AC';
61   inputs.byButton['back'] = inputs.byKey['8'] = 'back';
62   return inputs;
63 };
64
65 /** @private */
66 Controller.prototype.handleInput_ = function(input) {
67   var values, accumulator, operator, operand;
68   if (input) {
69     values = this.model.handle(input);
70     accumulator = values.accumulator;
71     operator = values.operator;
72     operand = values.operand;
73     if (input === 'AC') {
74       this.view.clearDisplay({operand: '0'});
75     } else if (input === '=') {
76       this.view.addResults({accumulator: accumulator, operand: accumulator});
77     } else if (input.match(/^[+*/-]$/)) {
78       this.updateValues_({accumulator: accumulator});
79       this.view.addValues({operator: values.operator});
80     } else if (!this.updateValues_({operator: operator, operand: operand})) {
81       this.view.addValues({operator: operator, operand: operand});
82     }
83   }
84 };
85
86 /** @private */
87 Controller.prototype.updateValues_ = function(values) {
88   // Values which are "finalized" (which have an accumulator value) shouldn't
89   // and won't be updated, and this method will return false for them.
90   var before = this.view.getValues();
91   var after = !before.accumulator ? values : {};
92   this.view.setValues({
93     accumulator: this.getUpdatedValue_(before, after, 'accumulator'),
94     operator: this.getUpdatedValue_(before, after, 'operator'),
95     operand: this.getUpdatedValue_(before, after, 'operand', !before.operator)
96   });
97   return !before.accumulator;
98 }
99
100 /** @private */
101 Controller.prototype.getUpdatedValue_ = function(before, after, key, zero) {
102   var value = (typeof after[key] !== 'undefined') ? after[key] : before[key];
103   return zero ? (value || '0') : value;
104 }