view js-lib/buy3.js @ 95:5fb202915c11 laserkard

created card template multifunction
author Robert McIntyre <rlm@mit.edu>
date Mon, 26 Jul 2010 21:42:20 -0400
parents 0f19af92260e
children a0b768d3494a
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"});})
37 }};
44 // Utility Functinos
50 var switchText = (function (){
51 if ($(this).val() == $(this).attr('title'))
52 $(this).val('').removeClass('exampleText');
53 else if ($.trim($(this).val()) == '')
54 $(this).addClass('exampleText').val($(this).attr('title'));});
60 // Initilization Functions.
61 var card =
62 {name : {text: "Robert McIntyre", ref: ""},
63 company : {text: "", ref: ""},
64 occupation : {text: "", ref: ""},
65 phone : {text: "", ref: ""},
66 email : {text: "rlm@mit.edu", ref: ""},
67 website : {text: "", ref: ""},
68 decoration : {text: "", ref: ""}};
70 var card_init = (function () {
71 card.name.ref = display.set();
72 card.company.ref = display.set();
73 card.occupation.ref = display.set();
74 card.phone.ref = display.set();
75 card.email.ref = display.set();
76 card.website.ref = display.set();
77 card.decoration.ref = display.set();
78 });
81 var order =
82 {color : "green",
83 style : "bold",
84 quantity : 30,
85 content : undefined,
86 info : undefined,
87 name : "Robert McIntyre",
88 occupation : "AI Researcher",
89 phone : "617.821.9129",
90 email : "rlm@mit.edu",
91 website : "www.rlmcintyre.com",
92 company : "LaserKard LLC."
93 };
95 var display;
96 var color;
97 var style;
98 var helvetica;
99 var helvetica_I;
101 var state_map = {
102 green : {ref : null,
103 offState : {"fill" : "#030", "scale": 1} ,
104 onState : {"fill" : "#0F0", "scale": 1}},
105 red : {ref : null,
106 offState: {"fill" : "#300", "scale" : 1},
107 onState : {"fill" : "#F00" ,"scale" : 1}},
108 blue : {ref : null,
109 offState : {"fill" : "#003", "scale": 1},
110 onState : {"fill" : "#00F", "scale": 1},
111 ref : null},
112 black : {offState : null, onState : null, ref : null},
113 display : {ref : null, state : {"fill" : "black"}}};
115 var color_select_init = (function (){
116 state_map["red"].ref =
117 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);
118 state_map["red"].ref.node.onclick =
119 (function (){order.color= "red"; update();});
120 state_map["green"].ref =
121 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);
122 state_map["green"].ref.node.onclick =
123 (function (){order.color= "green"; update();});
124 state_map["blue"].ref =
125 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);
126 state_map["blue"].ref.node.onclick =
127 (function (){order.color= "blue"; update();});
129 toggle_on(state_map[order.color]);
130 });
132 var display_init = (function (){
133 state_map["display"].ref =
134 display.rect(1, 1, 338, 213, 20).attr(state_map["display"].state);
135 });
137 var text_entry_init = (function (){
138 $('input[type=text][title!=""]').each(function() {
139 if ($.trim($(this).val()) == ''){
140 $(this).val($(this).attr('title'));}
141 if ($(this).val() == $(this).attr('title')){
142 $(this).addClass('exampleText');}
143 }).focus(switchText).blur(switchText);
145 $('form').submit(function() {
146 $(this).find('input[type=text][title!=""]').each(function() {
147 if ($(this).val() == $(this).attr('title')){
148 $(this).val('');}})})});
155 var init = (function () {
156 display = Raphael("card-display", 340 ,215);
157 color = Raphael("color-select", 300, 100);
158 style = Raphael("style-select", 200, 70);
159 helvetica = display.getFont('HelveticaNeue', 700);
160 helvetica_I = display.getFont('HelveticaNeue', 800);
162 color_select_init();
163 display_init();
164 card_init();
165 text_entry_init();
166 key_handling();
167 update();
168 });
171 //Update Functions
173 var toggle_on = (function (button){
174 button.ref.animate(button.onState, 2000);
175 });
177 var toggle_off = (function (button){
178 button.ref.animate(button.offState, 2000);
179 });
181 var color_select_update = (function (){
182 var color = order.color;
183 return (function (){
184 if (order.color === color){}
185 else {
186 toggle_off(state_map[color]);
187 toggle_on(state_map[(order.color)]);
188 color = order.color;}
189 });})();
191 var display_color_update = (function (){
192 var color = state_map.display.state.fill
194 return (function (){
195 if (order.color === color){}
196 else {
197 state_map["display"].ref.animate(
198 {"fill" : state_map[order.color].onState.fill}, 2000);
199 color = order.color;}})})();
203 var display_style_update = (function (){
204 var style = null;
205 return (function (){
206 if (style !== order.style){
207 ""; }});
208 });
212 var key_handling = (function (){
213 var assoc = (function (target, field) {
214 $(target).keyup(function() {
215 field.text = $(target).val();
216 update();})});
217 assoc('#user-name', card.name);
218 assoc('#user-phone', card.phone);
219 assoc('#user-email', card.email);
220 assoc('#user-company', card.company);
221 assoc('#user-occupation', card.occupation);
222 assoc('#user-website', card.website);
223 });
226 var display_text_update = (function (){
227 var state = {name : "nothing",
228 phone: "nothing",
229 email: "nothing"};
230 //what an asinine function... referential transparency blah blah...
231 var deref = (function (var_name){return var_name;});
233 var check_text = (function (var_name) {
234 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
235 if (Cards[order.style][deref(var_name)]) {
236 card[deref(var_name)].ref.remove();
237 card[var_name].ref =
238 Cards[order.style][deref(var_name)](
239 card[deref(var_name)].text,display);
240 state[deref(var_name)] = card[deref(var_name)].text;
241 }}});
243 return (function (){
244 check_text("name");
245 check_text("phone");
246 check_text("email");
247 $("#debug").html(JSON.stringify(state));
248 });
250 })();
254 var update = (function (){
255 color_select_update();
256 display_color_update();
257 display_text_update();
258 // $("#debug").html(JSON.stringify({name : card.name.text, email : card.email.text, phone : card.phone.text}));
259 });
263 // return closure over state
264 return {init : init,
265 update : update};})();
275 $(document).ready(function() {
276 Buy.init();
277 Buy.update();
278 });