view 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
line wrap: on
line source
1 // Cards = (function (){
4 // (function (info paper){
5 // info.name
6 // info.occupation
7 // info.email
8 // info.phone
15 // });
23 Buy = (function (){
25 //Cards drawing functions:
27 var Cards =
28 {"bold" : {
29 "name" :(function (text, display){
30 return display.print(10,10,text,helvetica, 25).attr(
31 {"fill" : "#0F0"});}),
32 "email" : (function (text, paper){
33 return paper.print(50,50,text,helvetica_I,40).attr(
34 {"fill" : "red"});})}
36 };
43 // Utility Functinos
49 var switchText = (function (){
50 if ($(this).val() == $(this).attr('title'))
51 $(this).val('').removeClass('exampleText');
52 else if ($.trim($(this).val()) == '')
53 $(this).addClass('exampleText').val($(this).attr('title'));});
55 //what an asinine function... referential transparency blah blah...
56 var deref = (function (var_name){return var_name;});
61 // Initilization Functions.
62 var card;
64 var card_init = (function () {
66 card = {
67 name : {text: "Robert McIntyre", ref: display.set()},
68 company : {text: "", ref: display.set()},
69 occupation : {text: "", ref: display.set()},
70 phone : {text: "", ref: display.set()},
71 email : {text: "rlm@mit.edu", ref: display.set()},
72 website : {text: "", ref: display.set()},
73 decoration : {text: "", ref: display.set()}};
74 });
77 var order =
78 {color : "blue",
79 style : "bold",
80 quantity : 30,
81 content : undefined,
82 info : undefined,
83 name : "Robert McIntyre",
84 occupation : "AI Researcher",
85 phone : "617.821.9129",
86 email : "rlm@mit.edu",
87 website : "www.rlmcintyre.com",
88 company : "LaserKard LLC."
89 };
91 var display;
92 var color;
93 var style;
94 var helvetica;
95 var helvetica_I;
97 var state_map = {
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, 338, 213, 20).attr(state_map["display"].state);
131 });
133 var text_entry_init = (function (){
134 $('input[type=text][title!=""]').each(function() {
135 if ($.trim($(this).val()) == ''){
136 $(this).val($(this).attr('title'));}
137 if ($(this).val() == $(this).attr('title')){
138 $(this).addClass('exampleText');}
139 }).focus(switchText).blur(switchText);
141 $('form').submit(function() {
142 $(this).find('input[type=text][title!=""]').each(function() {
143 if ($(this).val() == $(this).attr('title')){
144 $(this).val('');}})})});
151 var init = (function () {
152 display = Raphael("card-display", 340 ,215);
153 color = Raphael("color-select", 300, 100);
154 style = Raphael("style-select", 200, 70);
155 helvetica = display.getFont('HelveticaNeue', 700);
156 helvetica_I = display.getFont('HelveticaNeue', 800);
158 color_select_init();
159 display_init();
160 card_init();
161 text_entry_init();
162 key_handling();
163 update();
164 });
167 //Update Functions
169 var toggle_on = (function (button){
170 button.ref.animate(button.onState, 2000);
171 });
173 var toggle_off = (function (button){
174 button.ref.animate(button.offState, 2000);
175 });
177 var color_select_update = (function (){
178 var color = order.color;
179 return (function (){
180 if (order.color === color){}
181 else {
182 toggle_off(state_map[color]);
183 toggle_on(state_map[(order.color)]);
184 color = order.color;}
185 });})();
187 var display_color_update = (function (){
188 var color = state_map.display.state.fill
190 return (function (){
191 if (order.color === color){}
192 else {
193 state_map["display"].ref.animate(
194 {"fill" : state_map[order.color].onState.fill}, 2000);
195 color = order.color;}})})();
197 var display_style_update = (function (){
198 var style = null;
199 return (function (){
200 if (style !== order.style){
201 ""; }});
202 });
204 var key_handling = (function (){
205 var assoc = (function (target, field) {
206 $(target).keyup(function() {
207 field.text = $(target).val();
208 update();})});
209 assoc('#user-name', card.name);
210 assoc('#user-phone', card.phone);
211 assoc('#user-email', card.email);
212 assoc('#user-company', card.company);
213 assoc('#user-occupation', card.occupation);
214 assoc('#user-website', card.website);
215 });
217 var display_style_update = (function (){
218 var style = null;
219 return (function () {
220 if (style !== order.style){
221 for ( var property in card ){
222 if (!Cards[order.style][(deref(property))]){
223 $("#user" + "-" + property).toggle(400);}}
224 style = order.style;}
225 });})();
227 var display_text_update = (function (){
228 var state = {name : "nothing",
229 phone: "nothing",
230 email: "nothing",
231 occupation: "nothing",
232 company : "nothing",
233 website : "nothing" };
235 var check_text = (function (var_name) {
236 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
237 if (Cards[order.style][deref(var_name)]) {
238 card[deref(var_name)].ref.remove();
239 card[var_name].ref =
240 Cards[order.style][deref(var_name)](
241 card[deref(var_name)].text,display);
242 state[deref(var_name)] = card[deref(var_name)].text;
243 }}});
245 return (function (){
246 check_text("name");
247 check_text("phone");
248 check_text("email");
249 check_text("occupation");
250 check_text("company");
251 check_text("website");
253 $("#debug").html(JSON.stringify(state));
254 });
256 })();
258 var update = (function (){
259 color_select_update();
260 display_color_update();
261 display_text_update();
262 display_style_update();
263 });
265 // return closure over state
266 return {init : init,
267 update : update};})();
271 $(document).ready(function() {
272 Buy.init();
273 Buy.update();
274 });