rlm@102: rlm@102: Buy = (function (){ rlm@102: rlm@102: rlm@102: rlm@102: var magic_font_ratio = 318; rlm@102: var mfg = magic_font_ratio; rlm@102: rlm@102: rlm@102: rlm@102: var draw_card = (function (paper){ rlm@102: rlm@102: var helvetica = paper.getFont('HelveticaNeue', 700); rlm@102: var helveticaI = paper.getFont('HelveticaNeue', 800); rlm@102: var h = paper.height; rlm@102: var w = paper.width; rlm@102: rlm@102: var standard = (function (text, x, y, pt, font){ rlm@102: if (!font) {font = helvetica;} rlm@102: var scale_x = 0.00030*h*pt/20; rlm@102: var scale_y = 0.00025*w*pt/20; rlm@102: return paper.print(h*x, w*y,text, font).scale( rlm@102: scale_x,scale_y,h*x,w*y);}); rlm@102: rlm@102: var bold = { rlm@102: "name" :(function (text){ rlm@102: return standard(text, 0.2,0.1,20).attr({"fill" : "red"});}), rlm@102: "email" : (function (text){ rlm@102: return standard(text, 0.2,0.4,20,helveticaI).attr( rlm@102: {"fill" : "red"});})}; rlm@102: rlm@102: rlm@102: return { rlm@102: "bold" : bold rlm@102: }; rlm@102: rlm@102: }); rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: var switchText = (function (){ rlm@102: if ($(this).val() == $(this).attr('title')) rlm@102: $(this).val('').removeClass('exampleText'); rlm@102: else if ($.trim($(this).val()) == '') rlm@102: $(this).addClass('exampleText').val($(this).attr('title'));}); rlm@102: rlm@102: rlm@102: var deref = (function (var_name){return var_name;}); rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: var card; rlm@102: rlm@102: var card_init = (function () { rlm@102: rlm@102: card = { rlm@102: name : {text: "Robert McIntyre", ref: display.set()}, rlm@102: company : {text: "", ref: display.set()}, rlm@102: occupation : {text: "", ref: display.set()}, rlm@102: phone : {text: "", ref: display.set()}, rlm@102: email : {text: "rlm@mit.edu", ref: display.set()}, rlm@102: website : {text: "", ref: display.set()}, rlm@102: decoration : {text: "", ref: display.set()}}; rlm@102: }); rlm@102: rlm@102: rlm@102: var order = rlm@102: {color : "blue", rlm@102: style : "bold", rlm@102: quantity : 30, rlm@102: content : undefined, rlm@102: info : undefined, rlm@102: name : "Robert McIntyre", rlm@102: occupation : "AI Researcher", rlm@102: phone : "617.821.9129", rlm@102: email : "rlm@mit.edu", rlm@102: website : "www.rlmcintyre.com", rlm@102: company : "LaserKard LLC." rlm@102: }; rlm@102: rlm@102: var display; rlm@102: var color; rlm@102: var style = {}; rlm@102: var helvetica; rlm@102: var helvetica_I; rlm@102: rlm@102: var state_map = { rlm@102: bold : null, rlm@102: classic : null, rlm@102: rlm@102: green : {ref : null, rlm@102: offState : {"fill" : "#030", "scale": 1} , rlm@102: onState : {"fill" : "#0F0", "scale": 1}}, rlm@102: red : {ref : null, rlm@102: offState: {"fill" : "#300", "scale" : 1}, rlm@102: onState : {"fill" : "#F00" ,"scale" : 1}}, rlm@102: blue : {ref : null, rlm@102: offState : {"fill" : "#003", "scale": 1}, rlm@102: onState : {"fill" : "#00F", "scale": 1}, rlm@102: ref : null}, rlm@102: black : {offState : null, onState : null, ref : null}, rlm@102: display : {ref : null, state : {"fill" : "black"}}}; rlm@102: rlm@102: var color_select_init = (function (){ rlm@102: state_map["red"].ref = rlm@102: color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState); rlm@102: state_map["red"].ref.node.onclick = rlm@102: (function (){order.color= "red"; update();}); rlm@102: state_map["green"].ref = rlm@102: color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState); rlm@102: state_map["green"].ref.node.onclick = rlm@102: (function (){order.color= "green"; update();}); rlm@102: state_map["blue"].ref = rlm@102: color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState); rlm@102: state_map["blue"].ref.node.onclick = rlm@102: (function (){order.color= "blue"; update();}); rlm@102: rlm@102: toggle_on(state_map[order.color]); rlm@102: }); rlm@102: rlm@102: var display_init = (function (){ rlm@102: state_map["display"].ref = rlm@102: display.rect(1, 1 , display.width - 1, display.height - 2, 20).attr( rlm@102: state_map["display"].state); rlm@102: }); rlm@102: rlm@102: var text_entry_init = (function (){ rlm@102: $('input[type=text][title!=""]').each(function() { rlm@102: if ($.trim($(this).val()) == ''){ rlm@102: $(this).val($(this).attr('title'));} rlm@102: if ($(this).val() == $(this).attr('title')){ rlm@102: $(this).addClass('exampleText');} rlm@102: }).focus(switchText).blur(switchText); rlm@102: rlm@102: $('form').submit(function() { rlm@102: $(this).find('input[type=text][title!=""]').each(function() { rlm@102: if ($(this).val() == $(this).attr('title')){ rlm@102: $(this).val('');}})})}); rlm@102: rlm@102: var style_text = { rlm@102: phone : "617.821.9129", rlm@102: email : "rlm@mit.edu", rlm@102: website : "www.rlmcintyre.com", rlm@102: name : "Robert McIntyre", rlm@102: company : "LaserKard LLC.", rlm@102: occupation : "AI Researcher"} rlm@102: rlm@102: rlm@102: var style_init = (function (){ rlm@102: var draw = draw_card(style.bold); rlm@102: draw.bold.name(style_text.name); rlm@102: style.bold.rect(1, 1 ,style.bold.width - 1, style.bold.height - 2, 10); rlm@102: draw.bold.email(style_text.email); rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: }); rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: var init = (function () { rlm@102: display = Raphael("card-display", 340 ,215); rlm@102: color = Raphael("color-select", 300, 100); rlm@102: rlm@102: style.bold = Raphael("bold", 170, 107); rlm@102: rlm@102: style.classic = Raphael("classic", 170 , 107); rlm@102: rlm@102: helvetica = display.getFont('HelveticaNeue', 700); rlm@102: helvetica_I = display.getFont('HelveticaNeue', 800); rlm@102: color_select_init(); rlm@102: display_init(); rlm@102: card_init(); rlm@102: text_entry_init(); rlm@102: key_handling(); rlm@102: style_init(); rlm@102: update(); rlm@102: }); rlm@102: rlm@102: rlm@102: rlm@102: rlm@102: var toggle_on = (function (button){ rlm@102: button.ref.animate(button.onState, 2000); rlm@102: }); rlm@102: rlm@102: var toggle_off = (function (button){ rlm@102: button.ref.animate(button.offState, 2000); rlm@102: }); rlm@102: rlm@102: var color_select_update = (function (){ rlm@102: var color = order.color; rlm@102: return (function (){ rlm@102: if (order.color === color){} rlm@102: else { rlm@102: toggle_off(state_map[color]); rlm@102: toggle_on(state_map[(order.color)]); rlm@102: color = order.color;} rlm@102: });})(); rlm@102: rlm@102: var display_color_update = (function (){ rlm@102: var color = state_map.display.state.fill rlm@102: rlm@102: return (function (){ rlm@102: if (order.color === color){} rlm@102: else { rlm@102: state_map["display"].ref.animate( rlm@102: {"fill" : state_map[order.color].onState.fill}, 2000); rlm@102: color = order.color;}})})(); rlm@102: rlm@102: var display_style_update = (function (){ rlm@102: var style = null; rlm@102: return (function (){ rlm@102: if (style !== order.style){ rlm@102: ""; }}); rlm@102: }); rlm@102: rlm@102: var key_handling = (function (){ rlm@102: var assoc = (function (target, field) { rlm@102: $(target).keyup(function() { rlm@102: field.text = $(target).val(); rlm@102: update();})}); rlm@102: assoc('#user-name', card.name); rlm@102: assoc('#user-phone', card.phone); rlm@102: assoc('#user-email', card.email); rlm@102: assoc('#user-company', card.company); rlm@102: assoc('#user-occupation', card.occupation); rlm@102: assoc('#user-website', card.website); rlm@102: }); rlm@102: rlm@102: var display_style_update = (function (){ rlm@102: var style = null; rlm@102: rlm@102: return (function () { rlm@102: var draw = draw_card(display); rlm@102: if (style !== order.style){ rlm@102: for ( var property in card ){ rlm@102: if (!draw[order.style][(deref(property))]){ rlm@102: $("#user" + "-" + property).toggle(400);}} rlm@102: style = order.style;} rlm@102: });})(); rlm@102: rlm@102: var display_text_update = (function (){ rlm@102: var state = {name : "nothing", rlm@102: phone: "nothing", rlm@102: email: "nothing", rlm@102: occupation: "nothing", rlm@102: company : "nothing", rlm@102: website : "nothing" }; rlm@102: rlm@102: var check_text = (function (var_name) { rlm@102: var draw = draw_card(display); rlm@102: if (state[deref(var_name)] !== (card[deref(var_name)]).text){ rlm@102: if (draw[order.style][deref(var_name)]) { rlm@102: card[deref(var_name)].ref.remove(); rlm@102: card[var_name].ref = rlm@102: draw[order.style][deref(var_name)]( rlm@102: card[deref(var_name)].text,display); rlm@102: state[deref(var_name)] = card[deref(var_name)].text; rlm@102: }}}); rlm@102: rlm@102: return (function (){ rlm@102: check_text("name"); rlm@102: check_text("phone"); rlm@102: check_text("email"); rlm@102: check_text("occupation"); rlm@102: check_text("company"); rlm@102: check_text("website"); rlm@102: rlm@102: $("#debug").html(JSON.stringify(state)); rlm@102: }); rlm@102: rlm@102: })(); rlm@102: rlm@102: var update = (function (){ rlm@102: color_select_update(); rlm@102: display_color_update(); rlm@102: display_text_update(); rlm@102: display_style_update(); rlm@102: }); rlm@102: rlm@102: rlm@102: return {init : init, rlm@102: update : update};})(); rlm@102: rlm@102: rlm@102: rlm@102: $(document).ready(function() { rlm@102: Buy.init(); rlm@102: Buy.update(); rlm@102: });