Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7080 from dscravag/nightly2
Browse files Browse the repository at this point in the history
Nightly 2012-12-19
  • Loading branch information
dscravag committed Dec 19, 2012
2 parents 2b77f0a + a0ecffc commit d38689a
Show file tree
Hide file tree
Showing 41 changed files with 910 additions and 641 deletions.
1 change: 1 addition & 0 deletions apps/calculator/index.html
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8"/>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" media="screen" href="style/calculator.css"/>
<script type="text/javascript" src="js/parser.js" defer></script>
<script type="text/javascript" src="js/calculator.js" defer></script>
</head>

Expand Down
54 changes: 23 additions & 31 deletions apps/calculator/js/calculator.js
Expand Up @@ -116,13 +116,6 @@ var Calculator = {
return key;
},

formatNumber: function calculator_formatNumber(n) {
if (n % 1 == 0) {
return n;
}
return n.toFixed(3);
},

calculate: function calculator_calculate() {
if (this.stack.length === 0)
return;
Expand All @@ -131,7 +124,7 @@ var Calculator = {
var postfix =
this.infix2postfix(this.stack.map(this.substitute).join(''));
var result = this.evaluatePostfix(postfix);
this.stack = [this.formatNumber(result).toString()];
this.stack = [String(result)];
this.updateDisplay();
this.toClear = true;
} catch (err) {
Expand Down Expand Up @@ -177,41 +170,31 @@ var Calculator = {
// Currently functions are unimplemented and only operators with
// left association are used
infix2postfix: function calculator_infix2postfix(infix) {
// We cant know up till this point whether - is for negation or subtraction
// at this point we modify negation operators into (0-N) so 4+-5 -> 4+(0-5)
infix = infix.replace(
/(([^0-9])-|^-)([0-9.]+)/g,
function(match, _, pre, num) {
pre = pre || '';
return pre + '(0-' + num + ')';
}
);

// basic tokenisation to ensure we group numbers with >1 digit together
var tokens = infix.match(/[0-9.]+|\*|\/|\+|\-|\(|\)/g);
var parser = new Parser(infix);
var tokens = parser.parse();
var output = [];
var stack = [];

tokens.forEach(function infix2postfix_inner(token) {
if (/[0-9.]+/.test(token)) {
output.push(parseFloat(token, 10));
if (token.number) {
output.push(parseFloat(token.value, 10));
}

var isOperator = this.isOperator;
if (isOperator(token)) {
if (isOperator(token.value)) {
var precedence = this.precedence;
while (isOperator(stack[stack.length - 1]) &&
precedence(token) <= precedence(stack[stack.length - 1])) {
while (stack.length && isOperator(stack[stack.length - 1]) &&
precedence(token.value) <= precedence(stack[stack.length - 1])) {
output.push(stack.pop());
}
stack.push(token);
stack.push(token.value);
}

if (token === '(') {
stack.push(token);
if (token.value === '(') {
stack.push(token.value);
}

if (token === ')') {
if (token.value === ')') {
while (stack.length && stack[stack.length - 1] !== '(') {
output.push(stack.pop());
}
Expand Down Expand Up @@ -316,7 +299,17 @@ Calculator.test = function() {
['-5.5*6', -33],
['-5.5*-6.4', 35.2],
['-6-6-6', -18],
['6-6-6', -6]
['6-6-6', -6],
['.001 /2', .0005],
['(0-.001)/2', -.0005],
['-.001/(0-2)', .0005],
['1000000000000000000000000+1', 1e+24],
['1000000000000000000000000-1', 1e+24],
['1e+30+10', 1e+30],
['1e+30*10', 1e+31],
['1e+30/100', 1e+28],
['10/1000000000000000000000000', 1e-23],
['10/-1000000000000000000000000', -1e-23]
];

var passed = formulas.every(run);
Expand All @@ -327,4 +320,3 @@ Calculator.test = function() {
return passed;
};


128 changes: 128 additions & 0 deletions apps/calculator/js/parser.js
@@ -0,0 +1,128 @@
'use strict';

function Parser(input) { this.init(input); }
Parser.prototype = {
init: function(input) {
// No spaces.
input = input.replace(/[ \t\v\n]/g, '');

// String to array:
this._chars = [];
for (var i = 0; i < input.length; ++i)
this._chars.push(input[i]);

this._tokens = [];
},

// This method returns an array of objects with these properties:
// - number: true/false
// - value: the token value
parse: function() {

// The input must be a 'block' without any digit left.
if (!this._tokenizeBlock() || this._chars.length)
throw ({ type: 'error', msg: 'Wrong input'});

return this._tokens;
},

_tokenizeBlock: function() {
if (!this._chars.length)
return false;

// '(' + something + ')'
if (this._chars[0] == '(') {
this._tokens.push({number: false, value: this._chars[0]});
this._chars.shift();

if (!this._tokenizeBlock())
return false;

if (!this._chars.length || this._chars[0] != ')')
return false;

this._chars.shift();

this._tokens.push({number: false, value: ')'});
} else {
// number + ...
if (!this._tokenizeNumber())
return false;
}

if (!this._chars.length || this._chars[0] == ')')
return true;

while(this._chars.length && this._chars[0] != ')') {
if (!this._tokenizeOther())
return false;

if (!this._tokenizeBlock())
return false;
}

return true;
},

// This is a simple float parser.
_tokenizeNumber: function() {
if (!this._chars.length)
return false;

// {+,-}something
var number = [];
if (/[+-]/.test(this._chars[0])) {
number.push(this._chars.shift());
}

var me = this;
function tokenizeNumberInternal() {
if (!me._chars.length || !/^[0-9.]/.test(me._chars[0]))
return false;

while (me._chars.length && /[0-9.]/.test(me._chars[0])) {
number.push(me._chars.shift());
}

return true;
}

if (!tokenizeNumberInternal())
return false;

// 123{e...}
if (!this._chars.length || this._chars[0] != 'e') {
this._tokens.push({number: true, value: number.join('')});
return true;
}

number.push(this._chars.shift());

// 123e{+,-}
if (/[+-]/.test(this._chars[0])) {
number.push(this._chars.shift());
}

if (!this._chars.length)
return false;

// the number
if (!tokenizeNumberInternal())
return false;

this._tokens.push({number: true, value: number.join('')});
return true;
},

_tokenizeOther: function() {
if (!this._chars.length)
return false;

if (['*', '/', '+', '-'].indexOf(this._chars[0]) != -1) {
this._tokens.push({number: false, value: this._chars.shift()});
return true;
}

return false;
}
};
5 changes: 3 additions & 2 deletions apps/camera/index.html
Expand Up @@ -20,10 +20,11 @@
</div>

<div id="controls">
<a id="switch-button" name="Switch source" disabled="disabled"><span></span></a>
<a id="switch-button" name="Switch source" class="hidden" disabled="disabled"><span></span></a>
<a id="capture-button" name="Capture" disabled="disabled"><span></span></a>
<div id="misc-button">
<a id="gallery-button" name="View Gallery"><span></span></a>
<a id="gallery-button" class="hidden" name="View Gallery"><span></span></a>
<a id="cancel-pick" class="hidden"><span></span></a>
<span id="video-timer">00:00</span>
</div>
</div>
Expand Down
68 changes: 34 additions & 34 deletions apps/camera/js/camera.js
@@ -1,9 +1,6 @@
'use strict';

var Camera = {

_started: false,

_cameras: null,
_camera: 0,
_captureMode: null,
Expand Down Expand Up @@ -152,6 +149,13 @@ var Camera = {
},

init: function camera_init() {
// If we don't have any pending messages, show the usual UI
// Otherwise, determine which buttons to show once we get our
// activity message
if (!navigator.mozHasPendingMessage('activity')) {
this.galleryButton.classList.remove('hidden');
this.switchButton.classList.remove('hidden');
}

// Dont let the phone go to sleep while the camera is
// active, user must manually close it
Expand Down Expand Up @@ -214,11 +218,18 @@ var Camera = {
this.setToggleCameraStyle();
this.setSource(this._camera);

this._started = true;

if (this._pendingPick) {
this.initActivity();
}
navigator.mozSetMessageHandler('activity', function(activity) {
var name = activity.source.name;
if (name === 'pick') {
Camera.initPick(activity);
}
else {
// We got another activity. Perhaps we were launched from gallery
// So show our usual buttons
Camera.galleryButton.classList.remove('hidden');
Camera.switchButton.classList.remove('hidden');
}
});
}).bind(this));
},

Expand All @@ -236,21 +247,24 @@ var Camera = {

// When inside an activity the user cannot switch between
// the gallery or video recording.
initActivity: function camera_initActivity() {
this.galleryButton.setAttribute('disabled', 'disabled');
this.switchButton.setAttribute('disabled', 'disabled');
initPick: function camera_initPick(activity) {
this._pendingPick = activity;

// Hide the gallery and switch buttons, leaving only the shutter
this.galleryButton.classList.add('hidden');
this.switchButton.classList.add('hidden');

// Display the cancel button and add an event listener for it
var cancelButton = document.getElementById('cancel-pick');
cancelButton.classList.remove('hidden');
cancelButton.onclick = this.cancelPick.bind(this);
},

cancelActivity: function camera_cancelActivity(error) {
if (error && this._pendingPick) {
cancelPick: function camera_cancelPick() {
if (this._pendingPick) {
this._pendingPick.postError('pick cancelled');
}
this._pendingPick = null;

if (!this._secureMode) {
this.galleryButton.removeAttribute('disabled');
}
this.switchButton.removeAttribute('disabled');
},

toggleModePressed: function camera_toggleCaptureMode(e) {
Expand Down Expand Up @@ -655,7 +669,7 @@ var Camera = {
type: 'image/jpeg',
blob: getreq.result
});
this.cancelActivity();
this.cancelPick();
}).bind(this);

return;
Expand Down Expand Up @@ -846,28 +860,14 @@ var Camera = {
}
};

function actHandle(activity) {
var name = activity.source.name;
if (name === 'pick') {
Camera._pendingPick = activity;
if (Camera._started) {
Camera.initActivity();
}
}
}

if (window.navigator.mozSetMessageHandler) {
window.navigator.mozSetMessageHandler('activity', actHandle);
}

window.addEventListener('DOMContentLoaded', function CameraInit() {
Camera.init();
});

document.addEventListener('mozvisibilitychange', function() {
if (document.mozHidden) {
Camera.stopPreview();
Camera.cancelActivity(true);
Camera.cancelPick();
if (this._secureMode) // If the lockscreen is locked
Filmstrip.clear(); // then forget everything when closing camera
} else {
Expand Down
3 changes: 2 additions & 1 deletion apps/camera/manifest.webapp
Expand Up @@ -29,7 +29,8 @@
"type": ["image/jpeg"]
},
"returnValue": true,
"disposition": "inline"
"disposition": "inline",
"href": "/index.html#pick"
}
},
"locales": {
Expand Down

0 comments on commit d38689a

Please sign in to comment.