view js-lib/buy3.js @ 96:a0b768d3494a laserkard

created text-entry-restriction function
author Robert McIntyre <rlm@mit.edu>
date Mon, 26 Jul 2010 23:39:03 -0400
parents 5fb202915c11
children 8a3615df4c18
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'));});
56 //what an asinine function... referential transparency blah blah...
57 var deref = (function (var_name){return var_name;});
62 // Initilization Functions.
63 var card;
65 var card_init = (function () {
67 card = {
68 name : {text: "Robert McIntyre", ref: display.set()},
69 company : {text: "", ref: display.set()},
70 occupation : {text: "", ref: display.set()},
71 phone : {text: "", ref: display.set()},
72 email : {text: "rlm@mit.edu", ref: display.set()},
73 website : {text: "", ref: display.set()},
74 decoration : {text: "", ref: display.set()}};
75 });
78 var order =
79 {color : "blue",
80 style : "bold",
81 quantity : 30,
82 content : undefined,
83 info : undefined,
84 name : "Robert McIntyre",
85 occupation : "AI Researcher",
86 phone : "617.821.9129",
87 email : "rlm@mit.edu",
88 website : "www.rlmcintyre.com",
89 company : "LaserKard LLC."
90 };
92 var display;
93 var color;
94 var style;
95 var helvetica;
96 var helvetica_I;
98 var state_map = {
99 green : {ref : null,
100 offState : {"fill" : "#030", "scale": 1} ,
101 onState : {"fill" : "#0F0", "scale": 1}},
102 red : {ref : null,
103 offState: {"fill" : "#300", "scale" : 1},
104 onState : {"fill" : "#F00" ,"scale" : 1}},
105 blue : {ref : null,
106 offState : {"fill" : "#003", "scale": 1},
107 onState : {"fill" : "#00F", "scale": 1},
108 ref : null},
109 black : {offState : null, onState : null, ref : null},
110 display : {ref : null, state : {"fill" : "black"}}};
112 var color_select_init = (function (){
113 state_map["red"].ref =
114 color.rect(160, 1, 70, 50, 10).attr(state_map["red"].offState);
115 state_map["red"].ref.node.onclick =
116 (function (){order.color= "red"; update();});
117 state_map["green"].ref =
118 color.rect(1, 1, 70, 50, 10).attr(state_map["green"].offState);
119 state_map["green"].ref.node.onclick =
120 (function (){order.color= "green"; update();});
121 state_map["blue"].ref =
122 color.rect(80, 1, 70, 50, 10).attr(state_map["blue"].offState);
123 state_map["blue"].ref.node.onclick =
124 (function (){order.color= "blue"; update();});
126 toggle_on(state_map[order.color]);
127 });
129 var display_init = (function (){
130 state_map["display"].ref =
131 display.rect(1, 1, 338, 213, 20).attr(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('');}})})});
152 var init = (function () {
153 display = Raphael("card-display", 340 ,215);
154 color = Raphael("color-select", 300, 100);
155 style = Raphael("style-select", 200, 70);
156 helvetica = display.getFont('HelveticaNeue', 700);
157 helvetica_I = display.getFont('HelveticaNeue', 800);
159 color_select_init();
160 display_init();
161 card_init();
162 text_entry_init();
163 key_handling();
164 update();
165 });
168 //Update Functions
170 var toggle_on = (function (button){
171 button.ref.animate(button.onState, 2000);
172 });
174 var toggle_off = (function (button){
175 button.ref.animate(button.offState, 2000);
176 });
178 var color_select_update = (function (){
179 var color = order.color;
180 return (function (){
181 if (order.color === color){}
182 else {
183 toggle_off(state_map[color]);
184 toggle_on(state_map[(order.color)]);
185 color = order.color;}
186 });})();
188 var display_color_update = (function (){
189 var color = state_map.display.state.fill
191 return (function (){
192 if (order.color === color){}
193 else {
194 state_map["display"].ref.animate(
195 {"fill" : state_map[order.color].onState.fill}, 2000);
196 color = order.color;}})})();
198 var display_style_update = (function (){
199 var style = null;
200 return (function (){
201 if (style !== order.style){
202 ""; }});
203 });
205 var key_handling = (function (){
206 var assoc = (function (target, field) {
207 $(target).keyup(function() {
208 field.text = $(target).val();
209 update();})});
210 assoc('#user-name', card.name);
211 assoc('#user-phone', card.phone);
212 assoc('#user-email', card.email);
213 assoc('#user-company', card.company);
214 assoc('#user-occupation', card.occupation);
215 assoc('#user-website', card.website);
216 });
218 var display_style_update = (function (){
219 var style = null;
220 return (function () {
221 if (style !== order.style){
223 for ( var property in card )
224 {
225 alert( property );
226 if (!Cards[order.style][(deref(property))]){
227 $("#user" + "-" + property).toggle(400);}
228 }
231 style = order.style; }
233 });})();
235 var display_text_update = (function (){
236 var state = {name : "nothing",
237 phone: "nothing",
238 email: "nothing",
239 occupation: "nothing",
240 company : "nothing",
241 website : "nothing" };
243 var check_text = (function (var_name) {
244 if (state[deref(var_name)] !== (card[deref(var_name)]).text){
245 if (Cards[order.style][deref(var_name)]) {
246 card[deref(var_name)].ref.remove();
247 card[var_name].ref =
248 Cards[order.style][deref(var_name)](
249 card[deref(var_name)].text,display);
250 state[deref(var_name)] = card[deref(var_name)].text;
251 }}});
253 return (function (){
254 check_text("name");
255 check_text("phone");
256 check_text("email");
257 check_text("occupation");
258 check_text("company");
259 check_text("website");
261 $("#debug").html(JSON.stringify(state));
262 });
264 })();
266 var update = (function (){
267 color_select_update();
268 display_color_update();
269 display_text_update();
270 display_style_update();
271 });
273 // return closure over state
274 return {init : init,
275 update : update};})();
285 $(document).ready(function() {
286 Buy.init();
287 Buy.update();
288 });