annotate js-lib/buy3.js @ 97:8a3615df4c18 laserkard

saving progress, going to add styles
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Jul 2010 03:51:01 -0400
parents a0b768d3494a
children 388344355ebf
rev   line source
rlm@92 1 // Cards = (function (){
rlm@92 2
rlm@92 3
rlm@92 4 // (function (info paper){
rlm@92 5 // info.name
rlm@92 6 // info.occupation
rlm@92 7 // info.email
rlm@92 8 // info.phone
rlm@92 9
rlm@92 10
rlm@92 11
rlm@92 12
rlm@92 13
rlm@92 14
rlm@92 15 // });
rlm@92 16
rlm@92 17
rlm@92 18
rlm@94 19
rlm@94 20
rlm@94 21
rlm@92 22
rlm@85 23 Buy = (function (){
rlm@94 24
rlm@94 25 //Cards drawing functions:
rlm@94 26
rlm@94 27 var Cards =
rlm@94 28 {"bold" : {
rlm@94 29 "name" :(function (text, display){
rlm@94 30 return display.print(10,10,text,helvetica, 25).attr(
rlm@95 31 {"fill" : "#0F0"});}),
rlm@95 32 "email" : (function (text, paper){
rlm@95 33 return paper.print(50,50,text,helvetica_I,40).attr(
rlm@97 34 {"fill" : "red"});})}
rlm@97 35
rlm@97 36 };
rlm@95 37
rlm@95 38
rlm@94 39
rlm@94 40
rlm@94 41
rlm@94 42
rlm@92 43 // Utility Functinos
rlm@90 44
rlm@94 45
rlm@94 46
rlm@94 47
rlm@94 48
rlm@92 49 var switchText = (function (){
rlm@92 50 if ($(this).val() == $(this).attr('title'))
rlm@92 51 $(this).val('').removeClass('exampleText');
rlm@92 52 else if ($.trim($(this).val()) == '')
rlm@92 53 $(this).addClass('exampleText').val($(this).attr('title'));});
rlm@90 54
rlm@96 55 //what an asinine function... referential transparency blah blah...
rlm@96 56 var deref = (function (var_name){return var_name;});
rlm@92 57
rlm@92 58
rlm@92 59
rlm@92 60
rlm@92 61 // Initilization Functions.
rlm@96 62 var card;
rlm@96 63
rlm@95 64 var card_init = (function () {
rlm@96 65
rlm@96 66 card = {
rlm@96 67 name : {text: "Robert McIntyre", ref: display.set()},
rlm@96 68 company : {text: "", ref: display.set()},
rlm@96 69 occupation : {text: "", ref: display.set()},
rlm@96 70 phone : {text: "", ref: display.set()},
rlm@96 71 email : {text: "rlm@mit.edu", ref: display.set()},
rlm@96 72 website : {text: "", ref: display.set()},
rlm@96 73 decoration : {text: "", ref: display.set()}};
rlm@95 74 });
rlm@95 75
rlm@95 76
rlm@85 77 var order =
rlm@96 78 {color : "blue",
rlm@92 79 style : "bold",
rlm@92 80 quantity : 30,
rlm@92 81 content : undefined,
rlm@92 82 info : undefined,
rlm@92 83 name : "Robert McIntyre",
rlm@92 84 occupation : "AI Researcher",
rlm@92 85 phone : "617.821.9129",
rlm@92 86 email : "rlm@mit.edu",
rlm@92 87 website : "www.rlmcintyre.com",
rlm@92 88 company : "LaserKard LLC."
rlm@92 89 };
rlm@85 90
rlm@85 91 var display;
rlm@85 92 var color;
rlm@85 93 var style;
rlm@94 94 var helvetica;
rlm@94 95 var helvetica_I;
rlm@90 96
rlm@91 97 var state_map = {
rlm@91 98 green : {ref : null,
rlm@91 99 offState : {"fill" : "#030", "scale": 1} ,
rlm@91 100 onState : {"fill" : "#0F0", "scale": 1}},
rlm@91 101 red : {ref : null,
rlm@91 102 offState: {"fill" : "#300", "scale" : 1},
rlm@91 103 onState : {"fill" : "#F00" ,"scale" : 1}},
rlm@91 104 blue : {ref : null,
rlm@91 105 offState : {"fill" : "#003", "scale": 1},
rlm@91 106 onState : {"fill" : "#00F", "scale": 1},
rlm@91 107 ref : null},
rlm@91 108 black : {offState : null, onState : null, ref : null},
rlm@91 109 display : {ref : null, state : {"fill" : "black"}}};
rlm@90 110
rlm@90 111 var color_select_init = (function (){
rlm@90 112 state_map["red"].ref =
rlm@92 113 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);
rlm@90 114 state_map["red"].ref.node.onclick =
rlm@90 115 (function (){order.color= "red"; update();});
rlm@90 116 state_map["green"].ref =
rlm@92 117 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);
rlm@90 118 state_map["green"].ref.node.onclick =
rlm@90 119 (function (){order.color= "green"; update();});
rlm@90 120 state_map["blue"].ref =
rlm@92 121 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);
rlm@90 122 state_map["blue"].ref.node.onclick =
rlm@90 123 (function (){order.color= "blue"; update();});
rlm@90 124
rlm@90 125 toggle_on(state_map[order.color]);
rlm@90 126 });
rlm@86 127
rlm@90 128 var display_init = (function (){
rlm@90 129 state_map["display"].ref =
rlm@90 130 display.rect(1, 1, 338, 213, 20).attr(state_map["display"].state);
rlm@90 131 });
rlm@92 132
rlm@92 133 var text_entry_init = (function (){
rlm@92 134 $('input[type=text][title!=""]').each(function() {
rlm@92 135 if ($.trim($(this).val()) == ''){
rlm@92 136 $(this).val($(this).attr('title'));}
rlm@92 137 if ($(this).val() == $(this).attr('title')){
rlm@92 138 $(this).addClass('exampleText');}
rlm@92 139 }).focus(switchText).blur(switchText);
rlm@92 140
rlm@92 141 $('form').submit(function() {
rlm@92 142 $(this).find('input[type=text][title!=""]').each(function() {
rlm@92 143 if ($(this).val() == $(this).attr('title')){
rlm@92 144 $(this).val('');}})})});
rlm@92 145
rlm@92 146
rlm@92 147
rlm@92 148
rlm@92 149
rlm@90 150
rlm@90 151 var init = (function () {
rlm@90 152 display = Raphael("card-display", 340 ,215);
rlm@90 153 color = Raphael("color-select", 300, 100);
rlm@90 154 style = Raphael("style-select", 200, 70);
rlm@94 155 helvetica = display.getFont('HelveticaNeue', 700);
rlm@94 156 helvetica_I = display.getFont('HelveticaNeue', 800);
rlm@94 157
rlm@90 158 color_select_init();
rlm@90 159 display_init();
rlm@92 160 card_init();
rlm@92 161 text_entry_init();
rlm@92 162 key_handling();
rlm@94 163 update();
rlm@90 164 });
rlm@90 165
rlm@90 166
rlm@90 167 //Update Functions
rlm@86 168
rlm@86 169 var toggle_on = (function (button){
rlm@90 170 button.ref.animate(button.onState, 2000);
rlm@86 171 });
rlm@86 172
rlm@86 173 var toggle_off = (function (button){
rlm@90 174 button.ref.animate(button.offState, 2000);
rlm@86 175 });
rlm@86 176
rlm@90 177 var color_select_update = (function (){
rlm@86 178 var color = order.color;
rlm@86 179 return (function (){
rlm@86 180 if (order.color === color){}
rlm@86 181 else {
rlm@90 182 toggle_off(state_map[color]);
rlm@90 183 toggle_on(state_map[(order.color)]);
rlm@86 184 color = order.color;}
rlm@86 185 });})();
rlm@86 186
rlm@92 187 var display_color_update = (function (){
rlm@90 188 var color = state_map.display.state.fill
rlm@90 189
rlm@90 190 return (function (){
rlm@90 191 if (order.color === color){}
rlm@90 192 else {
rlm@91 193 state_map["display"].ref.animate(
rlm@91 194 {"fill" : state_map[order.color].onState.fill}, 2000);
rlm@90 195 color = order.color;}})})();
rlm@92 196
rlm@92 197 var display_style_update = (function (){
rlm@92 198 var style = null;
rlm@92 199 return (function (){
rlm@92 200 if (style !== order.style){
rlm@92 201 ""; }});
rlm@92 202 });
rlm@92 203
rlm@92 204 var key_handling = (function (){
rlm@92 205 var assoc = (function (target, field) {
rlm@92 206 $(target).keyup(function() {
rlm@92 207 field.text = $(target).val();
rlm@92 208 update();})});
rlm@92 209 assoc('#user-name', card.name);
rlm@92 210 assoc('#user-phone', card.phone);
rlm@92 211 assoc('#user-email', card.email);
rlm@92 212 assoc('#user-company', card.company);
rlm@92 213 assoc('#user-occupation', card.occupation);
rlm@92 214 assoc('#user-website', card.website);
rlm@94 215 });
rlm@92 216
rlm@96 217 var display_style_update = (function (){
rlm@96 218 var style = null;
rlm@96 219 return (function () {
rlm@96 220 if (style !== order.style){
rlm@97 221 for ( var property in card ){
rlm@96 222 if (!Cards[order.style][(deref(property))]){
rlm@97 223 $("#user" + "-" + property).toggle(400);}}
rlm@97 224 style = order.style;}
rlm@96 225 });})();
rlm@92 226
rlm@92 227 var display_text_update = (function (){
rlm@95 228 var state = {name : "nothing",
rlm@95 229 phone: "nothing",
rlm@96 230 email: "nothing",
rlm@96 231 occupation: "nothing",
rlm@96 232 company : "nothing",
rlm@96 233 website : "nothing" };
rlm@92 234
rlm@95 235 var check_text = (function (var_name) {
rlm@95 236 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
rlm@95 237 if (Cards[order.style][deref(var_name)]) {
rlm@95 238 card[deref(var_name)].ref.remove();
rlm@95 239 card[var_name].ref =
rlm@95 240 Cards[order.style][deref(var_name)](
rlm@95 241 card[deref(var_name)].text,display);
rlm@95 242 state[deref(var_name)] = card[deref(var_name)].text;
rlm@95 243 }}});
rlm@95 244
rlm@95 245 return (function (){
rlm@95 246 check_text("name");
rlm@95 247 check_text("phone");
rlm@95 248 check_text("email");
rlm@96 249 check_text("occupation");
rlm@96 250 check_text("company");
rlm@96 251 check_text("website");
rlm@96 252
rlm@95 253 $("#debug").html(JSON.stringify(state));
rlm@95 254 });
rlm@96 255
rlm@92 256 })();
rlm@92 257
rlm@86 258 var update = (function (){
rlm@90 259 color_select_update();
rlm@92 260 display_color_update();
rlm@92 261 display_text_update();
rlm@96 262 display_style_update();
rlm@90 263 });
rlm@86 264
rlm@90 265 // return closure over state
rlm@86 266 return {init : init,
rlm@90 267 update : update};})();
rlm@85 268
rlm@85 269
rlm@92 270
rlm@85 271 $(document).ready(function() {
rlm@90 272 Buy.init();
rlm@90 273 Buy.update();
rlm@85 274 });