annotate temp342423/buy3.js @ 102:8ed3394e6b73 laserkard

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