Mercurial > laserkard
view js-lib/buy3.js @ 94:0f19af92260e laserkard
saving progress.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 26 Jul 2010 20:33:17 -0400 |
parents | 69b4defd835d |
children | 5fb202915c11 |
line wrap: on
line source
1 // Cards = (function (){4 // (function (info paper){5 // info.name6 // info.occupation7 // info.email8 // info.phone15 // });23 Buy = (function (){25 //Cards drawing functions:27 var Cards =28 {"bold" : {29 "name" :(function (text, display){30 return display.print(10,10,text,helvetica, 25).attr(31 {"fill" : "#FFFFFF"});32 })}};37 // Utility Functinos43 var switchText = (function (){44 if ($(this).val() == $(this).attr('title'))45 $(this).val('').removeClass('exampleText');46 else if ($.trim($(this).val()) == '')47 $(this).addClass('exampleText').val($(this).attr('title'));});53 // Initilization Functions.54 var order =55 {color : "green",56 style : "bold",57 quantity : 30,58 content : undefined,59 info : undefined,60 name : "Robert McIntyre",61 occupation : "AI Researcher",62 phone : "617.821.9129",63 email : "rlm@mit.edu",64 website : "www.rlmcintyre.com",65 company : "LaserKard LLC."66 };68 var display;69 var color;70 var style;71 var helvetica;72 var helvetica_I;74 var state_map = {75 green : {ref : null,76 offState : {"fill" : "#030", "scale": 1} ,77 onState : {"fill" : "#0F0", "scale": 1}},78 red : {ref : null,79 offState: {"fill" : "#300", "scale" : 1},80 onState : {"fill" : "#F00" ,"scale" : 1}},81 blue : {ref : null,82 offState : {"fill" : "#003", "scale": 1},83 onState : {"fill" : "#00F", "scale": 1},84 ref : null},85 black : {offState : null, onState : null, ref : null},86 display : {ref : null, state : {"fill" : "black"}}};88 var color_select_init = (function (){89 state_map["red"].ref =90 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);91 state_map["red"].ref.node.onclick =92 (function (){order.color= "red"; update();});93 state_map["green"].ref =94 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);95 state_map["green"].ref.node.onclick =96 (function (){order.color= "green"; update();});97 state_map["blue"].ref =98 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);99 state_map["blue"].ref.node.onclick =100 (function (){order.color= "blue"; update();});102 toggle_on(state_map[order.color]);103 });105 var display_init = (function (){106 state_map["display"].ref =107 display.rect(1, 1, 338, 213, 20).attr(state_map["display"].state);108 });110 var text_entry_init = (function (){111 $('input[type=text][title!=""]').each(function() {112 if ($.trim($(this).val()) == ''){113 $(this).val($(this).attr('title'));}114 if ($(this).val() == $(this).attr('title')){115 $(this).addClass('exampleText');}116 }).focus(switchText).blur(switchText);118 $('form').submit(function() {119 $(this).find('input[type=text][title!=""]').each(function() {120 if ($(this).val() == $(this).attr('title')){121 $(this).val('');}})})});128 var init = (function () {129 display = Raphael("card-display", 340 ,215);130 color = Raphael("color-select", 300, 100);131 style = Raphael("style-select", 200, 70);132 helvetica = display.getFont('HelveticaNeue', 700);133 helvetica_I = display.getFont('HelveticaNeue', 800);135 color_select_init();136 display_init();137 card_init();138 text_entry_init();139 key_handling();140 update();141 });144 //Update Functions146 var toggle_on = (function (button){147 button.ref.animate(button.onState, 2000);148 });150 var toggle_off = (function (button){151 button.ref.animate(button.offState, 2000);152 });154 var color_select_update = (function (){155 var color = order.color;156 return (function (){157 if (order.color === color){}158 else {159 toggle_off(state_map[color]);160 toggle_on(state_map[(order.color)]);161 color = order.color;}162 });})();164 var display_color_update = (function (){165 var color = state_map.display.state.fill167 return (function (){168 if (order.color === color){}169 else {170 state_map["display"].ref.animate(171 {"fill" : state_map[order.color].onState.fill}, 2000);172 color = order.color;}})})();176 var display_style_update = (function (){177 var style = null;178 return (function (){179 if (style !== order.style){180 ""; }});181 });184 var card =185 {name : {text: "Robert McIntyre", ref: ""},186 company : {text: "", ref: ""},187 occupation : {text: "", ref: ""},188 phone : {text: "", ref: ""},189 email : {text: "", ref: ""},190 website : {text: "", ref: ""},191 decoration : {text: "", ref: ""}};193 var card_init = (function () {194 card.name.ref = display.rect(10,10,10,10,5);195 });197 var key_handling = (function (){198 var assoc = (function (target, field) {199 $(target).keyup(function() {200 field.text = $(target).val();201 update();})});202 assoc('#user-name', card.name);203 assoc('#user-phone', card.phone);204 assoc('#user-email', card.email);205 assoc('#user-company', card.company);206 assoc('#user-occupation', card.occupation);207 assoc('#user-website', card.website);208 });211 var display_text_update = (function (){212 var name = null;214 var check_text = (function () {215 if (name !== card.name.text){216 card.name.ref.remove();217 card.name.ref = Cards[order.style].name(card.name.text, display);218 name = card.name.text;219 }});220 return check_text;223 })();227 var update = (function (){228 color_select_update();229 display_color_update();230 display_text_update();231 $("#debug").html(JSON.stringify({name : card.name.text, email : card.email.text}));232 });236 // return closure over state237 return {init : init,238 update : update};})();248 $(document).ready(function() {249 Buy.init();250 Buy.update();251 });