view js-lib/buy3.js @ 99:9649b14f3b38 laserkard

made main drawing function capable of rendering style selection boxes. no more pics needed
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Jul 2010 07:56:22 -0400
parents 388344355ebf
children 595866daadbe
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 return paper.print(w*0.1,h*0.2,text,helvetica).attr(
22 {"fill" : "#0F0"}).scale(0.0002*h,0.00025*w,w*0.1,h*0.2);}),
23 "email" : (function (text){
24 return paper.print(w*0.1,h*0.4,text,helveticaI).attr(
25 {"fill" : "red"}).scale(0.0002*h,0.00025*w,w*0.1,h*0.4);})}
26 };
28 });
38 // Utility Functinos
44 var switchText = (function (){
45 if ($(this).val() == $(this).attr('title'))
46 $(this).val('').removeClass('exampleText');
47 else if ($.trim($(this).val()) == '')
48 $(this).addClass('exampleText').val($(this).attr('title'));});
50 //what an asinine function... referential transparency blah blah...
51 var deref = (function (var_name){return var_name;});
56 // Initilization Functions.
57 var card;
59 var card_init = (function () {
61 card = {
62 name : {text: "Robert McIntyre", ref: display.set()},
63 company : {text: "", ref: display.set()},
64 occupation : {text: "", ref: display.set()},
65 phone : {text: "", ref: display.set()},
66 email : {text: "rlm@mit.edu", ref: display.set()},
67 website : {text: "", ref: display.set()},
68 decoration : {text: "", ref: display.set()}};
69 });
72 var order =
73 {color : "blue",
74 style : "bold",
75 quantity : 30,
76 content : undefined,
77 info : undefined,
78 name : "Robert McIntyre",
79 occupation : "AI Researcher",
80 phone : "617.821.9129",
81 email : "rlm@mit.edu",
82 website : "www.rlmcintyre.com",
83 company : "LaserKard LLC."
84 };
86 var display;
87 var color;
88 var style = {};
89 var helvetica;
90 var helvetica_I;
92 var state_map = {
93 bold : null,
94 classic : null,
96 green : {ref : null,
97 offState : {"fill" : "#030", "scale": 1} ,
98 onState : {"fill" : "#0F0", "scale": 1}},
99 red : {ref : null,
100 offState: {"fill" : "#300", "scale" : 1},
101 onState : {"fill" : "#F00" ,"scale" : 1}},
102 blue : {ref : null,
103 offState : {"fill" : "#003", "scale": 1},
104 onState : {"fill" : "#00F", "scale": 1},
105 ref : null},
106 black : {offState : null, onState : null, ref : null},
107 display : {ref : null, state : {"fill" : "black"}}};
109 var color_select_init = (function (){
110 state_map["red"].ref =
111 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);
112 state_map["red"].ref.node.onclick =
113 (function (){order.color= "red"; update();});
114 state_map["green"].ref =
115 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);
116 state_map["green"].ref.node.onclick =
117 (function (){order.color= "green"; update();});
118 state_map["blue"].ref =
119 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);
120 state_map["blue"].ref.node.onclick =
121 (function (){order.color= "blue"; update();});
123 toggle_on(state_map[order.color]);
124 });
126 var display_init = (function (){
127 state_map["display"].ref =
128 display.rect(1, 1 , display.width - 1, display.height - 2, 20).attr(
129 state_map["display"].state);
130 });
132 var text_entry_init = (function (){
133 $('input[type=text][title!=""]').each(function() {
134 if ($.trim($(this).val()) == ''){
135 $(this).val($(this).attr('title'));}
136 if ($(this).val() == $(this).attr('title')){
137 $(this).addClass('exampleText');}
138 }).focus(switchText).blur(switchText);
140 $('form').submit(function() {
141 $(this).find('input[type=text][title!=""]').each(function() {
142 if ($(this).val() == $(this).attr('title')){
143 $(this).val('');}})})});
145 var style_text = {
146 phone : "617.821.9129",
147 email : "rlm@mit.edu",
148 website : "www.rlmcintyre.com",
149 name : "Robert McIntyre",
150 company : "LaserKard LLC.",
151 occupation : "AI Researcher"}
154 var style_init = (function (){
155 var draw = draw_card(style.bold);
156 draw.bold.name(style_text.name);
157 style.bold.rect(1, 1 ,style.bold.width - 1, style.bold.height - 2, 10);
158 draw.bold.email(style_text.email);
159 // state_map["bold"] = style.bold.set();
160 // state_map["bold"].push(Cards.bold.name(style_text.name, style.bold));
161 // state_map["bold"].push(Cards.bold.email(style_text.email, style.bold));
162 //
163 // state_map["bold"].attr({"fill" : "red"});
164 // state_map["bold"].scale(0.03, 0.03, 0 , 0);
166 });
171 var init = (function () {
172 display = Raphael("card-display", 340 ,215);
173 color = Raphael("color-select", 300, 100);
175 style.bold = Raphael("bold", 170, 107);
177 style.classic = Raphael("classic", 170 , 107);
179 helvetica = display.getFont('HelveticaNeue', 700);
180 helvetica_I = display.getFont('HelveticaNeue', 800);
181 color_select_init();
182 display_init();
183 card_init();
184 text_entry_init();
185 key_handling();
186 style_init();
187 update();
188 });
191 //Update Functions
193 var toggle_on = (function (button){
194 button.ref.animate(button.onState, 2000);
195 });
197 var toggle_off = (function (button){
198 button.ref.animate(button.offState, 2000);
199 });
201 var color_select_update = (function (){
202 var color = order.color;
203 return (function (){
204 if (order.color === color){}
205 else {
206 toggle_off(state_map[color]);
207 toggle_on(state_map[(order.color)]);
208 color = order.color;}
209 });})();
211 var display_color_update = (function (){
212 var color = state_map.display.state.fill
214 return (function (){
215 if (order.color === color){}
216 else {
217 state_map["display"].ref.animate(
218 {"fill" : state_map[order.color].onState.fill}, 2000);
219 color = order.color;}})})();
221 var display_style_update = (function (){
222 var style = null;
223 return (function (){
224 if (style !== order.style){
225 ""; }});
226 });
228 var key_handling = (function (){
229 var assoc = (function (target, field) {
230 $(target).keyup(function() {
231 field.text = $(target).val();
232 update();})});
233 assoc('#user-name', card.name);
234 assoc('#user-phone', card.phone);
235 assoc('#user-email', card.email);
236 assoc('#user-company', card.company);
237 assoc('#user-occupation', card.occupation);
238 assoc('#user-website', card.website);
239 });
241 var display_style_update = (function (){
242 var style = null;
244 return (function () {
245 var draw = draw_card(display);
246 if (style !== order.style){
247 for ( var property in card ){
248 if (!draw[order.style][(deref(property))]){
249 $("#user" + "-" + property).toggle(400);}}
250 style = order.style;}
251 });})();
253 var display_text_update = (function (){
254 var state = {name : "nothing",
255 phone: "nothing",
256 email: "nothing",
257 occupation: "nothing",
258 company : "nothing",
259 website : "nothing" };
261 var check_text = (function (var_name) {
262 var draw = draw_card(display);
263 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
264 if (draw[order.style][deref(var_name)]) {
265 card[deref(var_name)].ref.remove();
266 card[var_name].ref =
267 draw[order.style][deref(var_name)](
268 card[deref(var_name)].text,display);
269 state[deref(var_name)] = card[deref(var_name)].text;
270 }}});
272 return (function (){
273 check_text("name");
274 check_text("phone");
275 check_text("email");
276 check_text("occupation");
277 check_text("company");
278 check_text("website");
280 $("#debug").html(JSON.stringify(state));
281 });
283 })();
285 var update = (function (){
286 color_select_update();
287 display_color_update();
288 display_text_update();
289 display_style_update();
290 });
292 // return closure over state
293 return {init : init,
294 update : update};})();
298 $(document).ready(function() {
299 Buy.init();
300 Buy.update();
301 });