diff options
Diffstat (limited to 'docroot/classes/sketchbook.class.js')
-rwxr-xr-x | docroot/classes/sketchbook.class.js | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/docroot/classes/sketchbook.class.js b/docroot/classes/sketchbook.class.js new file mode 100755 index 0000000..d62101e --- /dev/null +++ b/docroot/classes/sketchbook.class.js | |||
@@ -0,0 +1,288 @@ | |||
1 | /* | ||
2 | * Material Experience - Sketchbook Class | ||
3 | * | ||
4 | * EYEMG - Interactive Media Group | ||
5 | * Created by Mike Crute (mcrute@eyemg.com) | ||
6 | * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07 | ||
7 | */ | ||
8 | |||
9 | var Sketchbook = Class.create(); | ||
10 | |||
11 | Object.extend(Sketchbook.prototype, | ||
12 | { | ||
13 | /* | ||
14 | * Initializes a new sketchbook and table. | ||
15 | */ | ||
16 | initialize: function() | ||
17 | { | ||
18 | // Sketchbook chip data store | ||
19 | this.dataStore = []; | ||
20 | |||
21 | this.table = new CardTable( | ||
22 | { | ||
23 | color: SME.colors.grey, | ||
24 | name: Strings.sketchbook, | ||
25 | id: 'sketchbook', | ||
26 | |||
27 | onRender: function(table) | ||
28 | { | ||
29 | table.table.appendChild(Builder.node('img', | ||
30 | { | ||
31 | id: 'sbTrash', | ||
32 | src: 'images/trash_can.jpg' | ||
33 | })); | ||
34 | |||
35 | Droppables.add($('sbTrash'), | ||
36 | { | ||
37 | accept: 'chip', | ||
38 | |||
39 | onDrop: function(chip) | ||
40 | { | ||
41 | this.removeChip(chip); | ||
42 | }.bind(this) | ||
43 | }); | ||
44 | }.bind(this) | ||
45 | }); | ||
46 | |||
47 | this.hide(); | ||
48 | |||
49 | if (Sketchbook.loggedIn) | ||
50 | { | ||
51 | this.loadData(); | ||
52 | } | ||
53 | |||
54 | // Be sure to save everything when the window closes | ||
55 | Event.observe(window, 'unload', this.saveData.bindAsEventListener(this)); | ||
56 | }, | ||
57 | |||
58 | /* | ||
59 | * Loads sketchbook data from the server and calls a function to merge | ||
60 | * that data into the currently loaded sketchbook. | ||
61 | */ | ||
62 | loadData: function() | ||
63 | { | ||
64 | new Ajax.Request(SME.url.sketchBookIN, | ||
65 | { | ||
66 | method: 'get', | ||
67 | |||
68 | onSuccess: function(transport) | ||
69 | { | ||
70 | this.dataStore = transport.responseText.cleanJSON().evalJSON(); | ||
71 | this._mergeSketchbooks(); | ||
72 | }.bind(this) | ||
73 | }); | ||
74 | }, | ||
75 | |||
76 | /* | ||
77 | * Merges the currently active (presumably guest) sketchbook with the | ||
78 | * data loaded from the server. Prevents the user from losing data when | ||
79 | * they login after adding chips to a guest sketchbook. | ||
80 | */ | ||
81 | _mergeSketchbooks: function() | ||
82 | { | ||
83 | this.dataStore.each(function(chip) | ||
84 | { | ||
85 | if (!this.table.hasChip(chip.cid)) | ||
86 | { | ||
87 | this.table.addChip(chip, { animate: false }); | ||
88 | } | ||
89 | }.bind(this)); | ||
90 | }, | ||
91 | |||
92 | /* | ||
93 | * Add a chip to the sketchbook. This function does duplicate detection | ||
94 | * using methods provided by the CardTable class to avoid adding a chip | ||
95 | * more than once. The function also updates the sketchbook's data | ||
96 | * store. | ||
97 | */ | ||
98 | addChip: function(contID) | ||
99 | { | ||
100 | if (this.table.hasChip(contID)) | ||
101 | { | ||
102 | new Bezel().show(Strings.alreadyAddedSB); | ||
103 | return; | ||
104 | } | ||
105 | |||
106 | new Ajax.Request(SME.url.chipResolver.evaluate({card: contID}), | ||
107 | { | ||
108 | method: 'get', | ||
109 | |||
110 | onSuccess: function(transport) | ||
111 | { | ||
112 | var data = transport.responseText.cleanJSON().evalJSON(); | ||
113 | var windSize = window.getDimensions(); | ||
114 | |||
115 | this.table.addChip(data, | ||
116 | { | ||
117 | x: Math.floor(1 + (windSize.width - 1) * Math.random()), | ||
118 | y: Math.floor(1 + ((windSize.height - 120) - 1) * Math.random()), | ||
119 | animate: false | ||
120 | }); | ||
121 | |||
122 | // Per BS: Would like different messages if logged in or not | ||
123 | if (Sketchbook.loggedIn) | ||
124 | { | ||
125 | new Bezel().show(Strings.addedSketchbook); | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | new Bezel({ fadeTime: 5 }).show(Strings.addedGuestSB); | ||
130 | } | ||
131 | |||
132 | this.saveData(); | ||
133 | }.bind(this) | ||
134 | }); | ||
135 | }, | ||
136 | |||
137 | /* | ||
138 | * Remove a chip from the table by DOM node. | ||
139 | */ | ||
140 | removeChip: function(chip) | ||
141 | { | ||
142 | this.table.removeChip(chip.classLink.options.contID); | ||
143 | this.saveData(); | ||
144 | }, | ||
145 | |||
146 | /* | ||
147 | * Serialize the datastore and post it back to the server for safe | ||
148 | * keeping. | ||
149 | */ | ||
150 | saveData: function() | ||
151 | { | ||
152 | new Ajax.Request(SME.url.sketchBook, | ||
153 | { | ||
154 | method: 'post', | ||
155 | asynchronous: false, | ||
156 | parameters: { 'sketchbook_data': this.table.getChipData().toJSON() } | ||
157 | }); | ||
158 | }, | ||
159 | |||
160 | /* | ||
161 | * Show the sketchbook table or if not logged in show a login screen. | ||
162 | */ | ||
163 | show: function() | ||
164 | { | ||
165 | if (!Sketchbook.loggedIn) | ||
166 | { | ||
167 | Sketchbook.showLoginScreen(); | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | this.table.show(); | ||
172 | } | ||
173 | }, | ||
174 | |||
175 | /* | ||
176 | * Hide the sketchbook table. | ||
177 | */ | ||
178 | hide: function() | ||
179 | { | ||
180 | this.table.hide(); | ||
181 | } | ||
182 | }); | ||
183 | |||
184 | Object.extend(Sketchbook, | ||
185 | { | ||
186 | // Flag to determine if the user is logged in or not. | ||
187 | loggedIn: false, | ||
188 | |||
189 | // Username of the logged in user. | ||
190 | username: null, | ||
191 | |||
192 | // Show the login screen in a special case card. | ||
193 | showLoginScreen: function() | ||
194 | { | ||
195 | var t = new Card( | ||
196 | { | ||
197 | color: SME.colors.grey, | ||
198 | title: Strings.pleaseLogin, | ||
199 | addExtraButtons: false, | ||
200 | |||
201 | onClose: function() | ||
202 | { | ||
203 | Sketchbook.checkLogin(); | ||
204 | } | ||
205 | }); | ||
206 | |||
207 | t.setLayout(Card.Layout.Special, { url: SME.url.loginScreen }); | ||
208 | t.show(); | ||
209 | }, | ||
210 | |||
211 | /* | ||
212 | * Once the user has successfully logged in do some actions to setup | ||
213 | * the user interface. | ||
214 | */ | ||
215 | doLoggedIn: function() | ||
216 | { | ||
217 | // Assume that if your calling this function the login succeeded | ||
218 | Sketchbook.loggedIn = true; | ||
219 | |||
220 | $$('div.history')[0].innerHTML = 'Hello ' + Sketchbook.username + '! | ' + | ||
221 | '<a href="http://santoprene.com/cgi-bin/protected/register/logout_designer.pl">' + Strings.logout + '</a> | ' + | ||
222 | '<a href="#" class="manage">' + Strings.manageAccount + '</a> | ' + | ||
223 | '<a href="#" class="history">' + Strings.myHistory + '</a>'; | ||
224 | |||
225 | // On mouseover of the history link show the dropdown. | ||
226 | // We need to re-attach it here because we're changing the | ||
227 | // links (above) and that causes the DOM to lose the original | ||
228 | // event. | ||
229 | $$('div.history a.history')[0].observe('mouseover', function() | ||
230 | { | ||
231 | SME.history.getDropDown(); | ||
232 | }); | ||
233 | |||
234 | $$('a.manage')[0].observe('click', function(event) | ||
235 | { | ||
236 | var t = new Card( | ||
237 | { | ||
238 | color: SME.colors.grey, | ||
239 | addExtraButtons: false, | ||
240 | title: Strings.manageAccount | ||
241 | }); | ||
242 | |||
243 | t.setLayout(Card.Layout.Special, { url: SME.url.manageAccount }); | ||
244 | t.show(); | ||
245 | |||
246 | Event.stop(event); | ||
247 | }); | ||
248 | }, | ||
249 | |||
250 | /* | ||
251 | * Check that the user actually logged in. | ||
252 | */ | ||
253 | checkLogin: function() | ||
254 | { | ||
255 | // If the site cookie doesn't exist no point even going on | ||
256 | if (!$C('Site')) | ||
257 | { | ||
258 | return; | ||
259 | } | ||
260 | |||
261 | /* | ||
262 | * Site cookie is in a weird format, basically sub-cookies are | ||
263 | * separated by & signs. The username and password of the | ||
264 | * currently logged in user is store as username:password in | ||
265 | * the sub-cookie called cookie. Oh yeah and the | ||
266 | * username/password pair is base64 encoded. | ||
267 | */ | ||
268 | var username = decodeBase64($C('Site').split('Cookie&')[1]).split(':')[0]; | ||
269 | Sketchbook.username = username; | ||
270 | |||
271 | /* | ||
272 | * Per Andy: the username and password won't be in the cookie | ||
273 | * (even though the cookie itself is set) if the login failed. | ||
274 | * So lets assume that if we find a username in the appropriate | ||
275 | * place in the cookie we can go ahead and log the user in. | ||
276 | */ | ||
277 | if (username) | ||
278 | { | ||
279 | Sketchbook.loggedIn = true; | ||
280 | Sketchbook.doLoggedIn(); | ||
281 | return true; | ||
282 | } | ||
283 | else | ||
284 | { | ||
285 | return false; | ||
286 | } | ||
287 | } | ||
288 | }); \ No newline at end of file | ||