Skip to content

Instantly share code, notes, and snippets.

@javan
Created December 21, 2010 16:24
Show Gist options
  • Select an option

  • Save javan/750150 to your computer and use it in GitHub Desktop.

Select an option

Save javan/750150 to your computer and use it in GitHub Desktop.
(function($) {
Ink.TradingInterface = function(options) {
if (!(this instanceof arguments.callee)) {
return new arguments.callee(arguments);
}
var self = this;
self.initialize = function() {
self.colors = {
buyFill: "#98AA62",
buyStroke:"#7A8743",
sellFill: "#60909F",
sellStroke: "#3E6A75",
hoverStroke: "#333",
selectedStroke: "#FEBC16"
};
self.stockID = options.stockID;
self.ajaxSubmit = options.ajaxSubmit;
self.replace = options.replace;
self.tradingInterface = $('#stock_'+self.stockID+'_interface');
self.advanced = options.advanced;
self.reasonRequired = options.reasonRequired;
if (self.replace) {
self.insertHiddenReplaceField();
}
if (options.opinionMarket) {
self.bindings();
return true;
}
self.sellQuantities = options.sellQuantities;
self.buyQuantities = options.buyQuantities;
self.simpleInterface = self.tradingInterface.find('.simple_interface:first');
self.advancedInterface = self.tradingInterface.find('.advanced_interface:first');
self.simpleInterfaceDrawn = false;
self.backgroundPaths = [];
self.copyTradeReasonToAdvanced();
self.bindings();
if (self.advanced) {
self.useAdvancedInterface();
} else {
self.useSimpleInterface();
}
};
self.bindings = function() {
self.tradingInterface.find('form:first').submit(function(){
return self.submitForm(this);
});
self.advancedInterface.find('.mode :radio').change(function(){
self.customQuantityChanged();
});
self.advancedInterface.find('.custom_quantity').bind('keyup', function(){
self.customQuantityChanged();
}).bind('keydown', 'up', function(){
self.changeCustomQuantity('up');
return false;
}).bind('keydown', 'down', function(){
self.changeCustomQuantity('down');
return false;
}).bind('keydown', 's', function(){
self.selectRadio(-1);
return false;
}).bind('keydown', 'b', function(){
self.selectRadio(1);
return false;
});
self.tradingInterface.find('.trade_reason :input').charCounter(140).focus(function(){
$(this).next('.charcounter').fadeIn();
});
};
self.useAdvancedInterface = function() {
self.setAdvanced(true);
self.tradingInterface.removeClass('simple_mode');
self.simpleInterface.hide().find(':input').attr('disabled', true);
self.advancedInterface.show().find(':input:not(.trade_submit)').attr('disabled', false);
var customQuantity = parseInt(self.tradingInterface.find('.custom_quantity').val());
var quantity = customQuantity;
if (customQuantity == 0 || isNaN(customQuantity)) {
var simpleQuantity = self.simpleInterface.find('input.hidden_quantity').val();
simpleQuantity = parseInt(simpleQuantity);
if (simpleQuantity == 0 || isNaN(simpleQuantity)) {
if (self.buyQuantities.length > 0) {
simpleQuantity = self.buyQuantities[0];
} else if (self.sellQuantities.length > 0) {
simpleQuantity = self.sellQuantities[0];
} else {
simpleQuantity = 1;
}
}
self.tradingInterface.find('.custom_quantity').val(Math.abs(simpleQuantity));
quantity = simpleQuantity;
}
setTimeout(function(){
self.tradingInterface.find('.custom_quantity').get(0).focus();
}, 100);
self.selectRadio(quantity);
self.customQuantityChanged();
};
self.useSimpleInterface = function() {
self.setAdvanced(false);
self.tradingInterface.addClass('simple_mode');
self.advancedInterface.hide().find(':input').attr('disabled', true);
self.simpleInterface.show().find(':input:not(.trade_submit)').attr('disabled', false);
self.drawSimpleInterface();
var val = parseInt(self.simpleInterface.find('input.hidden_quantity').val());
if (val < 0 || val > 0) {
self.quote(val);
} else {
self.quote('awaiting');
}
};
</snip>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment