Skip to content

Instantly share code, notes, and snippets.

@edelabar
Created March 24, 2011 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edelabar/885507 to your computer and use it in GitHub Desktop.
Save edelabar/885507 to your computer and use it in GitHub Desktop.
var PhotoSelector = Class.create();
PhotoSelector.prototype = {
initialize: function( name ) {
var init = new Benchmark();
init.start();
console.debug( "Beginning Initialization!" );
this.old = 0;
var totals = {
fourby: $("fourby"),
fiveby: $("fiveby"),
eightby: $("eightby"),
wallet: $("wallet")
};
totals.fourby.value = 0;
totals.fiveby.value = 0;
totals.eightby.value = 0;
totals.wallet.value = 0;
$$(".qtyInput").each(
function( inp, index ) {
inp.onfocus = this.enter( inp, this ).bindAsEventListener( this );
inp.onblur = this.recalculate( inp, totals, this ).bindAsEventListener( this );
}, this
);
init.end();
console.debug( "Initialization Complete in " + init.inMillis() + " milli(s)." );
},
enter: function( inp, me ) {
return function(e) {
me.old = parseInt(inp.value);
}
},
recalculate: function( inp, totals, me ) {
var type = inp.id.match(/([a-z]+)[0-9]+/)[1];
var total = totals[type];
inp.value = 0;
return function(e) {
var calc = new Benchmark();
calc.start();
var newVal = parseInt(inp.value);
if( me.old > newVal ) {
newVal = ( me.old - newVal ) * -1;
}
total.value = parseInt(total.value) + newVal;
calc.end();
console.debug( "Recalculation Complete in " + calc.inMillis() + " milli(s)." );
}
}
}
var ps;
Event.observe(window,"load",function(e){ps = new PhotoSelector()});
var PhotoSelector = Class.create();
PhotoSelector.prototype = {
initialize: function( name ) {
var init = new Benchmark();
init.start();
console.debug( "Beginning Initialization!" );
$A(document.getElementsByTagName("input")).each(
function( inp ) {
inp.value = 0;
if( Element.hasClassName(inp,"qtyInput") ) {
inp.onkeyup = this.recalculate.bindAsEventListener( this );
}
}, this
);
init.end();
console.debug( "Initialization Complete in " + init.inMillis() + " milli(s)." );
},
recalculate: function(e) {
var calc = new Benchmark();
calc.start();
$("fourby").value = 0;
$("fiveby").value = 0;
$("eightby").value = 0;
$("wallet").value = 0;
var inputs = $("pictures").getElementsByTagName("input");
for( var i = 0; i < inputs.length; i++ ) {
var totalId = inputs[i].id.match(/([a-z]+)[0-9]+/)[1];
var total = $(totalId);
total.value = parseInt(total.value) + parseInt($(inputs[i]).value);
}
calc.end();
console.debug( "Recalculation Complete in " + calc.inMillis() + " milli(s)." );
}
}
var ps;
Event.observe(window,"load",function(e){ps = new PhotoSelector()});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment