view js-lib/buy3.js @ 98:388344355ebf laserkard

figured out how to scale down main display
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Jul 2010 05:14:22 -0400
parents 8a3615df4c18
children 9649b14f3b38
line wrap: on
line source

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