Mercurial > summit-idp
comparison static-files/js/index.js @ 58:422fcf9774b1
added js, css, and img subdirectories to static-files.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Mon, 28 Jun 2010 16:51:13 -0700 |
parents | static-files/index.js@bd71f612d3b1 |
children | a63f69858b68 |
comparison
equal
deleted
inserted
replaced
57:83c3f19d7770 | 58:422fcf9774b1 |
---|---|
1 // ----------------------------------------------------------------------- | |
2 // UserInterface object | |
3 // ----------------------------------------------------------------------- | |
4 // | |
5 // This manages the user interface logic. | |
6 | |
7 ( | |
8 function(window) { | |
9 var UserInterface = window.UserInterface = {}; | |
10 | |
11 function updateUI() { | |
12 $(".screen").hide(); | |
13 $("#" + Config.value.state).show(); | |
14 switch (Config.value.state) { | |
15 case "login": | |
16 break; | |
17 case "wait-for-verify": | |
18 break; | |
19 case "logged-in": | |
20 $(".login-email").text(Config.value.email); | |
21 break; | |
22 } | |
23 } | |
24 | |
25 function normalizeUserInfo(userInfo) { | |
26 if (!(userInfo.interests && jQuery.isArray(userInfo.interests))) | |
27 userInfo.interests = []; | |
28 } | |
29 | |
30 function fillUserInfo() { | |
31 var userInfo = Attendees.currentUser; | |
32 if (!userInfo) | |
33 userInfo = {}; | |
34 normalizeUserInfo(userInfo); | |
35 | |
36 // Fill out the form. | |
37 $(".usr").each( | |
38 function() { | |
39 var prop = this.id.match(/usr_(.+)/)[1]; | |
40 $(this).val(userInfo[prop] || ""); | |
41 }); | |
42 $("#usr_interests input").each( | |
43 function() { | |
44 var label = $.trim($(this.parentNode).text()); | |
45 this.checked = (userInfo.interests.indexOf(label) != -1); | |
46 }); | |
47 | |
48 // Build list of all attendees. | |
49 var everyone = $("#everyone-info .attendees"); | |
50 everyone.empty(); | |
51 for (userID in Attendees.all) { | |
52 var person = Attendees.all[userID]; | |
53 normalizeUserInfo(person); | |
54 | |
55 var elem = $("#templates .attendee").clone(); | |
56 elem.find(".name").text(person.name || "Anonymous Human"); | |
57 var profileImageURL = person.profileImageURL; | |
58 if (!profileImageURL && person.twitterScreenName) { | |
59 // This used to be: | |
60 // "http://api.twitter.com/1/users/profile_image/" + | |
61 // screenName + ".xml?size=normal" | |
62 // But Twitter's API is rate-limited and/or slow, so we're | |
63 // using Joe Stump's awesome service instead. | |
64 | |
65 profileImageURL = ("http://img.tweetimag.es/i/" + | |
66 person.twitterScreenName + "_n"); | |
67 } | |
68 | |
69 if (profileImageURL) | |
70 elem.find(".headshot img").attr("src", profileImageURL); | |
71 | |
72 if (person.websiteURL) { | |
73 var websiteLink = document.createElement("a"); | |
74 websiteLink.href = person.websiteURL; | |
75 websiteLink.target = "_blank"; | |
76 elem.find(".name").wrapInner(websiteLink); | |
77 } | |
78 | |
79 if (person.twitterScreenName) { | |
80 var twitterLink = document.createElement("a"); | |
81 twitterLink.href = "http://twitter.com/" + person.twitterScreenName; | |
82 twitterLink.target = "_blank"; | |
83 twitterLink.textContent = "@" + person.twitterScreenName; | |
84 elem.find(".twitter").append(twitterLink); | |
85 } else | |
86 elem.find(".twitter").remove(); | |
87 | |
88 if (person.bugzillaEmail) { | |
89 var bugzillaLink = document.createElement("a"); | |
90 bugzillaLink.href = ("https://hg.mozilla.org/users/" + | |
91 "avarma_mozilla.com/" + | |
92 "bugzilla-dashboard/raw-file/v2/" + | |
93 "index.html#username=" + | |
94 encodeURI(person.bugzillaEmail)); | |
95 bugzillaLink.target = "_blank"; | |
96 elem.find(".bugzilla").wrapInner(bugzillaLink); | |
97 } else | |
98 elem.find(".bugzilla").remove(); | |
99 | |
100 if (person.interests.length > 0) { | |
101 var interests = elem.find(".interests ul"); | |
102 person.interests.forEach( | |
103 function(interest) { | |
104 var item = document.createElement("li"); | |
105 item.textContent = interest; | |
106 interests.append(item); | |
107 }); | |
108 } else | |
109 elem.find(".interests").remove(); | |
110 | |
111 if (person.bio) { | |
112 var converter = new Showdown.converter(); | |
113 var text = converter.makeHtml(person.bio); | |
114 elem.find(".bio").html(text); | |
115 } else | |
116 elem.find(".bio").remove(); | |
117 | |
118 everyone.append(elem); | |
119 } | |
120 } | |
121 | |
122 Attendees.observers.push(fillUserInfo); | |
123 Config.observers.push(updateUI); | |
124 | |
125 function initUI() { | |
126 $("#logged-in").tabs(); | |
127 | |
128 $(".start-over").submit( | |
129 function(event) { | |
130 event.preventDefault(); | |
131 Config.wipe(); | |
132 }); | |
133 | |
134 $("#login form").submit( | |
135 function(event) { | |
136 event.preventDefault(); | |
137 $("#login .error").hide(); | |
138 Ajax.postJSON( | |
139 "api/challenge/request", | |
140 {email: $(this).find("#email").val() }, | |
141 function(success, data) { | |
142 if (success) { | |
143 Config.setValue({state: "wait-for-verify"}); | |
144 } else { | |
145 $("#login .error").slideDown(); | |
146 } | |
147 }); | |
148 }); | |
149 | |
150 $("#logged-in form").submit( | |
151 function(event) { | |
152 event.preventDefault(); | |
153 $("#logged-in .success").hide(); | |
154 $("#logged-in .error").hide(); | |
155 | |
156 var contents = {}; | |
157 | |
158 $(".usr").each( | |
159 function() { | |
160 var prop = this.id.match(/usr_(.+)/)[1]; | |
161 contents[prop] = this.value; | |
162 }); | |
163 | |
164 contents.interests = []; | |
165 | |
166 $("#usr_interests input").each( | |
167 function() { | |
168 if (this.checked) | |
169 contents.interests.push($.trim($(this.parentNode).text())); | |
170 }); | |
171 | |
172 Ajax.postJSON( | |
173 "api/profile", | |
174 {token: Config.value.token, | |
175 contents: contents}, | |
176 function(success, data) { | |
177 if (success) { | |
178 $("#logged-in .success").slideDown(); | |
179 // Do a full round-trip to refresh our cached information | |
180 // about ourselves. Not efficient at all, but it'll work | |
181 // for now. | |
182 Attendees.refresh(); | |
183 } else { | |
184 $("#logged-in .error").slideDown(); | |
185 } | |
186 }); | |
187 }); | |
188 | |
189 var verify = window.location.hash.match(/#verify=(.+)/); | |
190 if (verify && Config.value.state != "logged-in") { | |
191 verify = verify[1]; | |
192 Config.setValue({state: "wait-for-verify"}); | |
193 Ajax.postJSON( | |
194 "api/challenge/respond", | |
195 {token: verify}, | |
196 function(success, data) { | |
197 window.location.hash = ""; | |
198 updateUI(); | |
199 if (success) { | |
200 Config.setValue({state: "logged-in", | |
201 token: data.token, | |
202 userID: data.user_id, | |
203 email: data.email}); | |
204 } else { | |
205 $("#wait-for-verify .error").slideDown(); | |
206 } | |
207 }); | |
208 } else | |
209 updateUI(); | |
210 } | |
211 | |
212 $(window).ready(initUI); | |
213 } | |
214 )(window); |