view js-lib/buy3.js @ 101:2bec541ef58f laserkard

made it HTML 4.1 strict
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Jul 2010 08:39:43 -0400
parents 595866daadbe
children 8ed3394e6b73
line wrap: on
line source

2 Buy = (function (){
4 //Cards drawing functions:
6 var magic_font_ratio = 318; //the font size for identity under .scale(1,1)
7 var mfg = magic_font_ratio;
11 var draw_card = (function (paper){
13 var helvetica = paper.getFont('HelveticaNeue', 700);
14 var helveticaI = paper.getFont('HelveticaNeue', 800);
15 var h = paper.height;
16 var w = paper.width;
18 return {
19 "bold" : {
20 "name" :(function (text){
21 var x = w*0.1; var y = h*0.2;
22 return paper.print(x,y,text,helvetica).attr(
23 {"fill" : "red"}).scale(0.0002*h,0.00025*w,x,y);}),
24 "email" : (function (text){
25 var x = w*0.1; var y = h*0.4;
26 return paper.print(x,y,text,helveticaI).attr(
27 {"fill" : "red"}).scale(0.0002*h,0.00025*w,x,y);})}
28 };
30 });
40 // Utility Functinos
46 var switchText = (function (){
47 if ($(this).val() == $(this).attr('title'))
48 $(this).val('').removeClass('exampleText');
49 else if ($.trim($(this).val()) == '')
50 $(this).addClass('exampleText').val($(this).attr('title'));});
52 //what an asinine function... referential transparency blah blah...
53 var deref = (function (var_name){return var_name;});
58 // Initilization Functions.
59 var card;
61 var card_init = (function () {
63 card = {
64 name : {text: "Robert McIntyre", ref: display.set()},
65 company : {text: "", ref: display.set()},
66 occupation : {text: "", ref: display.set()},
67 phone : {text: "", ref: display.set()},
68 email : {text: "rlm@mit.edu", ref: display.set()},
69 website : {text: "", ref: display.set()},
70 decoration : {text: "", ref: display.set()}};
71 });
74 var order =
75 {color : "blue",
76 style : "bold",
77 quantity : 30,
78 content : undefined,
79 info : undefined,
80 name : "Robert McIntyre",
81 occupation : "AI Researcher",
82 phone : "617.821.9129",
83 email : "rlm@mit.edu",
84 website : "www.rlmcintyre.com",
85 company : "LaserKard LLC."
86 };
88 var display;
89 var color;
90 var style = {};
91 var helvetica;
92 var helvetica_I;
94 var state_map = {
95 bold : null,
96 classic : null,
98 green : {ref : null,
99 offState : {"fill" : "#030", "scale": 1} ,
100 onState : {"fill" : "#0F0", "scale": 1}},
101 red : {ref : null,
102 offState: {"fill" : "#300", "scale" : 1},
103 onState : {"fill" : "#F00" ,"scale" : 1}},
104 blue : {ref : null,
105 offState : {"fill" : "#003", "scale": 1},
106 onState : {"fill" : "#00F", "scale": 1},
107 ref : null},
108 black : {offState : null, onState : null, ref : null},
109 display : {ref : null, state : {"fill" : "black"}}};
111 var color_select_init = (function (){
112 state_map["red"].ref =
113 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);
114 state_map["red"].ref.node.onclick =
115 (function (){order.color= "red"; update();});
116 state_map["green"].ref =
117 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);
118 state_map["green"].ref.node.onclick =
119 (function (){order.color= "green"; update();});
120 state_map["blue"].ref =
121 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);
122 state_map["blue"].ref.node.onclick =
123 (function (){order.color= "blue"; update();});
125 toggle_on(state_map[order.color]);
126 });
128 var display_init = (function (){
129 state_map["display"].ref =
130 display.rect(1, 1 , display.width - 1, display.height - 2, 20).attr(
131 state_map["display"].state);
132 });
134 var text_entry_init = (function (){
135 $('input[type=text][title!=""]').each(function() {
136 if ($.trim($(this).val()) == ''){
137 $(this).val($(this).attr('title'));}
138 if ($(this).val() == $(this).attr('title')){
139 $(this).addClass('exampleText');}
140 }).focus(switchText).blur(switchText);
142 $('form').submit(function() {
143 $(this).find('input[type=text][title!=""]').each(function() {
144 if ($(this).val() == $(this).attr('title')){
145 $(this).val('');}})})});
147 var style_text = {
148 phone : "617.821.9129",
149 email : "rlm@mit.edu",
150 website : "www.rlmcintyre.com",
151 name : "Robert McIntyre",
152 company : "LaserKard LLC.",
153 occupation : "AI Researcher"}
156 var style_init = (function (){
157 var draw = draw_card(style.bold);
158 draw.bold.name(style_text.name);
159 style.bold.rect(1, 1 ,style.bold.width - 1, style.bold.height - 2, 10);
160 draw.bold.email(style_text.email);
161 // state_map["bold"] = style.bold.set();
162 // state_map["bold"].push(Cards.bold.name(style_text.name, style.bold));
163 // state_map["bold"].push(Cards.bold.email(style_text.email, style.bold));
164 //
165 // state_map["bold"].attr({"fill" : "red"});
166 // state_map["bold"].scale(0.03, 0.03, 0 , 0);
168 });
173 var init = (function () {
174 display = Raphael("card-display", 340 ,215);
175 color = Raphael("color-select", 300, 100);
177 style.bold = Raphael("bold", 170, 107);
179 style.classic = Raphael("classic", 170 , 107);
181 helvetica = display.getFont('HelveticaNeue', 700);
182 helvetica_I = display.getFont('HelveticaNeue', 800);
183 color_select_init();
184 display_init();
185 card_init();
186 text_entry_init();
187 key_handling();
188 style_init();
189 update();
190 });
193 //Update Functions
195 var toggle_on = (function (button){
196 button.ref.animate(button.onState, 2000);
197 });
199 var toggle_off = (function (button){
200 button.ref.animate(button.offState, 2000);
201 });
203 var color_select_update = (function (){
204 var color = order.color;
205 return (function (){
206 if (order.color === color){}
207 else {
208 toggle_off(state_map[color]);
209 toggle_on(state_map[(order.color)]);
210 color = order.color;}
211 });})();
213 var display_color_update = (function (){
214 var color = state_map.display.state.fill
216 return (function (){
217 if (order.color === color){}
218 else {
219 state_map["display"].ref.animate(
220 {"fill" : state_map[order.color].onState.fill}, 2000);
221 color = order.color;}})})();
223 var display_style_update = (function (){
224 var style = null;
225 return (function (){
226 if (style !== order.style){
227 ""; }});
228 });
230 var key_handling = (function (){
231 var assoc = (function (target, field) {
232 $(target).keyup(function() {
233 field.text = $(target).val();
234 update();})});
235 assoc('#user-name', card.name);
236 assoc('#user-phone', card.phone);
237 assoc('#user-email', card.email);
238 assoc('#user-company', card.company);
239 assoc('#user-occupation', card.occupation);
240 assoc('#user-website', card.website);
241 });
243 var display_style_update = (function (){
244 var style = null;
246 return (function () {
247 var draw = draw_card(display);
248 if (style !== order.style){
249 for ( var property in card ){
250 if (!draw[order.style][(deref(property))]){
251 $("#user" + "-" + property).toggle(400);}}
252 style = order.style;}
253 });})();
255 var display_text_update = (function (){
256 var state = {name : "nothing",
257 phone: "nothing",
258 email: "nothing",
259 occupation: "nothing",
260 company : "nothing",
261 website : "nothing" };
263 var check_text = (function (var_name) {
264 var draw = draw_card(display);
265 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
266 if (draw[order.style][deref(var_name)]) {
267 card[deref(var_name)].ref.remove();
268 card[var_name].ref =
269 draw[order.style][deref(var_name)](
270 card[deref(var_name)].text,display);
271 state[deref(var_name)] = card[deref(var_name)].text;
272 }}});
274 return (function (){
275 check_text("name");
276 check_text("phone");
277 check_text("email");
278 check_text("occupation");
279 check_text("company");
280 check_text("website");
282 $("#debug").html(JSON.stringify(state));
283 });
285 })();
287 var update = (function (){
288 color_select_update();
289 display_color_update();
290 display_text_update();
291 display_style_update();
292 });
294 // return closure over state
295 return {init : init,
296 update : update};})();
300 $(document).ready(function() {
301 Buy.init();
302 Buy.update();
303 });