1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | var priceHandler = |
---|
19 | { |
---|
20 | _price: function(message) |
---|
21 | { |
---|
22 | if (message != null) { |
---|
23 | |
---|
24 | var price = parseFloat(message.getAttribute('bid')) |
---|
25 | var symbol = message.getAttribute('stock') |
---|
26 | var movement = message.getAttribute('movement') |
---|
27 | if (movement == null) { |
---|
28 | movement = 'up' |
---|
29 | } |
---|
30 | |
---|
31 | var row = document.getElementById(symbol) |
---|
32 | if (row) { |
---|
33 | // perform portfolio calculations |
---|
34 | var value = asFloat(find(row, 'amount')) * price |
---|
35 | var pl = value - asFloat(find(row, 'cost')) |
---|
36 | |
---|
37 | // now lets update the HTML DOM |
---|
38 | find(row, 'price').innerHTML = fixedDigits(price, 2) |
---|
39 | find(row, 'value').innerHTML = fixedDigits(value, 2) |
---|
40 | find(row, 'pl').innerHTML = fixedDigits(pl, 2) |
---|
41 | find(row, 'price').className = movement |
---|
42 | find(row, 'pl').className = pl >= 0 ? 'up' : 'down' |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | function portfolioPoll(first) |
---|
50 | { |
---|
51 | if (first) |
---|
52 | { |
---|
53 | amq.addListener('stocks','topic://STOCKS.*',priceHandler._price); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | amq.addPollHandler(portfolioPoll); |
---|
58 | |
---|
59 | |
---|
60 | /// ----------------- |
---|
61 | // Original code by Joe Walnes |
---|
62 | // ----------------- |
---|
63 | |
---|
64 | /*** Convenience methods, added as mixins to standard classes (object prototypes) ***/ |
---|
65 | |
---|
66 | /** |
---|
67 | * Return number as fixed number of digits. |
---|
68 | */ |
---|
69 | function fixedDigits(t, digits) { |
---|
70 | return (t.toFixed) ? t.toFixed(digits) : this |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Find direct child of an element, by id. |
---|
75 | */ |
---|
76 | function find(t, id) { |
---|
77 | for (i = 0; i < t.childNodes.length; i++) { |
---|
78 | var child = t.childNodes[i] |
---|
79 | if (child.id == id) { |
---|
80 | return child |
---|
81 | } |
---|
82 | } |
---|
83 | return null |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Return the text contents of an element as a floating point number. |
---|
88 | */ |
---|
89 | function asFloat(t) { |
---|
90 | return parseFloat(t.innerHTML) |
---|
91 | } |
---|