Skip to content

Instantly share code, notes, and snippets.

@olliemaitland
Created July 18, 2013 07:52
Show Gist options
  • Save olliemaitland/6027530 to your computer and use it in GitHub Desktop.
Save olliemaitland/6027530 to your computer and use it in GitHub Desktop.
Tile directive on the Oracle and Push demo
define(['App', 'jquery', 'ValueDirective','DiffusionDirective', 'EventsService'], function(App, $) {
App.directive('orcTile', ['DiffusionService', 'Events', function(DiffusionService, Events){
var linker = function(scope, iElement, iAttr) {
scope.showMode= 'normal';
scope.tryCount = 0;
scope.spread = '';
scope.skew = '';
scope.msg ={bid:{},ask:{}};
scope.isLocked = false;
scope.lockTryCount = 0;
scope.lastStatus = '';
scope.tryToUnlock = false;
scope.currency = iAttr.currency;
scope.topic = iAttr.topic;
scope.tryCount = DiffusionService.subscribe(iAttr.topic, scope);
scope.lockTryCount = DiffusionService.initLockSubscriptions(scope);
scope.update = function(msg) {
var field = JSON.parse(msg.nextField());
if(!scope.isLocked) {
var bidValue = field.bid,
offerValue = field.offer,
message = {
bid: {
value: bidValue,
show: bidValue.toFixed(4)
},
ask : {
value: offerValue,
show: offerValue.toFixed(4)
}
};
scope.msg = message;
}
if(!scope.$$phase){
scope.$digest(scope);
}
}
scope.switchToOptions = function () {
scope.showMode = 'options';
$(iElement).find('[data-jcarousel]').each(function () {
var el = $(this);
el.jcarousel(el.data());
});
$(iElement).find('[data-jcarousel-control]').each(function () {
var el = $(this);
el.jcarouselControl(el.data());
});
$(iElement).find('.carousel-vertical').jcarousel({
vertical:true
});
}
scope.watchBid = function() {
scope.$watch("msg", function(newValue, oldValue) {
var nv = newValue, ov = oldValue;
if (!nv.bid || !ov.bid || nv.bid.value == ov.bid.value)
{
return false;
}
if (!nv.ask || !ov.ask || nv.ask.value == ov.ask.value)
{
return false;
}
if ((nv.bid.value > ov.bid.value))
{
scope.msg.bid.delta = 'up';
}
if ((nv.bid.value < ov.bid.value))
{
scope.msg.bid.delta = 'down';
}
if ((nv.ask.value > ov.ask.value))
{
scope.msg.ask.delta = 'up';
}
if ((nv.ask.value < ov.ask.value))
{
scope.msg.ask.delta = 'down';
}
});
}
scope.watchLockTryCount = function () {
scope.$watch("lockTryCount", function(newCount, oldCount) {
// console.log(newCount);
if(newCount !== false) {
window.setTimeout(function(){
scope.lockTryCount = DiffusionService.initLockSubscriptions(scope);
if(!scope.$$phase){
scope.$digest(scope);
}
},10);
}else{
}
});
}
scope.watchTryCount = function () {
scope.$watch("tryCount", function(newCount, oldCount) {
if(newCount !== false) {
window.setTimeout(function(){
scope.tryCount = DiffusionService.subscribe(iAttr.topic, scope);
if(!scope.$$phase){
scope.$digest(scope);
}
},10);
}else{
}
});
}
scope.selectSpread = function (value) {
scope.spread = (value/10);
scope.activeSpreadSelection = value;
}
scope.selectSkew = function (value) {
scope.skew = (value/10);
scope.activeSkewSelection = value;
}
scope.initEvents = function () {
scope.watchBid();
scope.watchTryCount();
scope.watchLockTryCount();
scope.activateTile = function () {
DiffusionService.sendUnlockCommand(scope);
};
scope.onSubmitSpreadSkew = function () {
scope.showMode = 'normal';
if(scope.spread != "" && scope.skew != "") {
DiffusionService.sendPriceCommand(scope, " "+scope.spread + " " + scope.skew);
}
};
scope.enterLockMode = function () {
scope.showMode = 'normal';
DiffusionService.sendLockCommand(scope);
};
}
scope.initTile = function () {
scope.showBid = 5.46;
scope.initEvents();
}
scope.initTile();
};
return {
restrict: 'E',
priority:50,
scope: {
theTopic: '@topic'
},
templateUrl: 'scripts/js/app/tiles/tile_template.html',
link: linker
}
}]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment