Created
September 16, 2009 07:23
-
-
Save mtthwkfmn/187917 to your computer and use it in GitHub Desktop.
bats trading ext Book function in Bats Ext.ns namespace ... I guess. :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ext.ns('Bats'); | |
Bats.Book = function (config) { | |
config = config || {}; | |
config.symbol = config.symbol || ""; | |
config.ttl = typeof(config.ttl) != 'undefined' ? config.ttl : 3500; | |
config.count = config.count || ''; | |
Ext.apply(this, config); | |
}; | |
Bats.Book.prototype = { | |
failureCnt: 0, | |
deferID: 0, | |
symbol: '', | |
count: '', | |
ctype: "Bats.Book", | |
ttl: 0, | |
loadFailed: function () { | |
var div = document.getElementById('bookViewerForm2' + this.count); | |
div.className = "bookViewerForm2"; | |
this.failureCnt++; | |
if (this.failureCnt == 4) { | |
return; | |
} | |
this.ttl = this.ttl * 2; | |
this.deferID = this.load.defer(this.ttl, this, [this.symbol]); | |
}, | |
updateRowData: function (data) { | |
var divData = document.getElementById('bkData' + this.count); | |
if (data.asks.length == 0 && data.bids.length == 0 && data.trades.length == 0) { | |
divData.style.opacity = 0.4; | |
divData.style.zoom = 1; | |
} else { | |
divData.style.opacity = 1.0; | |
divData.style.zoom = 1; | |
} | |
var nbbo = false; | |
if ((data.asks.length > 0) && (data.bids.length > 0)) { | |
if (Math.round((data.asks[0][1] / 1 - data.bids[0][1]) * 100) == 1) { | |
nbbo = true; | |
} | |
} | |
var idx = 5; | |
var rows = document.getElementById('asks' + this.count).getElementsByTagName('tbody')[0].rows; | |
for (var ri = 0; ri < 5; ri++) { | |
idx--; | |
if (idx < data.asks.length) { | |
rows[ri].cells[0].innerHTML = data.asks[idx][0]; | |
if ((ri == 4) && nbbo) { | |
rows[ri].cells[1].innerHTML = '<b>' + data.asks[idx][1] + '</b>'; | |
} else { | |
rows[ri].cells[1].innerHTML = data.asks[idx][1]; | |
} | |
} else { | |
rows[ri].cells[0].innerHTML = ' '; | |
rows[ri].cells[1].innerHTML = ' '; | |
} | |
} | |
var rows = document.getElementById('bids' + this.count).getElementsByTagName('tbody')[0].rows; | |
for (var ri = 0; ri < 5; ri++) { | |
if (ri < data.bids.length) { | |
rows[ri].cells[0].innerHTML = data.bids[ri][0]; | |
if ((ri == 0) && nbbo) { | |
rows[ri].cells[1].innerHTML = '<b>' + data.bids[ri][1] + '</b>'; | |
} else { | |
rows[ri].cells[1].innerHTML = data.bids[ri][1]; | |
} | |
} else { | |
rows[ri].cells[0].innerHTML = ' '; | |
rows[ri].cells[1].innerHTML = ' '; | |
} | |
} | |
var rows = document.getElementById('trades' + this.count).getElementsByTagName('tbody')[0].rows; | |
for (var ri = 0; ri < 10; ri++) { | |
if (data.trades[ri]) { | |
rows[ri].cells[0].innerHTML = data.trades[ri][0]; | |
rows[ri].cells[1].innerHTML = data.trades[ri][2]; | |
rows[ri].cells[2].innerHTML = data.trades[ri][1]; | |
} else { | |
rows[ri].cells[0].innerHTML = ' '; | |
rows[ri].cells[1].innerHTML = ' '; | |
rows[ri].cells[2].innerHTML = ' '; | |
} | |
} | |
}, | |
show: function (data, params) { | |
var div = document.getElementById('bookViewerForm2' + this.count); | |
div.className = "bookViewerForm2"; | |
res = Ext.decode(data.responseText); | |
if (res.success) { | |
this.ttl = res.reload; | |
this.symbol = res.data.symbol; | |
Ext.get('bkCompany' + this.count).dom.innerHTML = res.data.company; | |
Ext.get('bkOrdersAccepted' + this.count).dom.innerHTML = res.data.orders; | |
Ext.get('bkTotalShares' + this.count).dom.innerHTML = res.data.volume; | |
this.updateRowData(res.data); | |
if (this.ttl > 0) { | |
this.deferID = this.load.defer(this.ttl, this, [this.symbol]); | |
} | |
Ext.get('bkTimestamp' + this.count).dom.innerHTML = res.data.timestamp; | |
} else { | |
Ext.get('bkCompany' + this.count).dom.innerHTML = res.statusText; | |
Ext.get('bkOrdersAccepted' + this.count).dom.innerHTML = ' '; | |
Ext.get('bkTotalShares' + this.count).dom.innerHTML = ' '; | |
this.updateRowData({ | |
asks: [], | |
bids: [], | |
trades: [] | |
}); | |
} | |
}, | |
load: function (s) { | |
clearTimeout(this.deferID); | |
if (s == "") return; | |
var div = document.getElementById('bookViewerForm2' + this.count); | |
div.className = "bookViewerForm2 loading"; | |
Ext.Ajax.request({ | |
url: '/book/' + s + '/data/', | |
disableCaching: false, | |
success: this.show, | |
failure: this.loadFailed, | |
scope: this | |
}); | |
}, | |
refresh: function (s) { | |
clearTimeout(this.deferID); | |
if (s != "") s += "/"; | |
document.location = '/book/' + s; | |
}, | |
allcaps: function (v) { | |
v = v.trim(); | |
if (v.length == 0) { | |
v = this.symbol; | |
} | |
return v.toUpperCase(); | |
}, | |
postlowercap: function (v) { | |
v = v.trim(); | |
if (v.length == 0) { | |
v = this.symbol; | |
} | |
return v.substring(0, v.length - 1).toUpperCase() + v.substring(v.length - 1).toLowerCase(); | |
}, | |
spawn: function () { | |
var s = this.symbol; | |
if (s != "") s += "/"; | |
this.openWin({ | |
src: '/book/' + s, | |
width: 375, | |
height: 360 | |
}); | |
}, | |
openWin: function (config) { | |
config = typeof config == "object" ? config : {}; | |
defaults = { | |
name: "_blank", | |
src: "", | |
scrollbars: 0, | |
menubar: 0, | |
toolbar: 0, | |
status: 0, | |
resizable: 0, | |
location: 0 | |
}; | |
for (key in defaults) { | |
if (!config[key]) { | |
config[key] = defaults[key]; | |
} | |
} | |
if (config.center) { | |
x = window.screenX ? window.screenX : window.screenLeft; | |
y = window.screenY ? window.screenY : window.screenTop; | |
x = Math.floor(x / screen.width) * screen.width; | |
y = Math.floor(y / screen.height) * screen.height; | |
config.left = (screen.width - config.width) / 2 + x; | |
config.top = (screen.height - config.height) / 2 + y; | |
} | |
props = ((config.width > 0) ? ",width=" + config.width : "") + ((config.height > 0) ? ",height=" + config.height : "") + ((config.top > 0) ? ",top=" + config.top : "") + ((config.left > 0) ? ",left=" + config.left : "") + ((config.scrollbars == 0) ? ",scrollbars=0" : ",scrollbars=1") + ((config.menubar == 0) ? ",menubar=0" : ",menubar=1") + ((config.toolbar == 0) ? ",toolbar=0" : ",toolbar=1") + ((config.status == 0) ? ",status=0" : ",status=1") + ((config.resizable == 0) ? ",resizable=0" : ",resizable=1") + ((config.location == 0) ? ",location=0" : ",location=1"); | |
props = props.substring(1, props.length); | |
return window.open(config.src, config.name, props); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment