open-web-challenges

view open-web-challenges.js @ 28:923fcc0ad519

Changed boxed content of #2 to 'hai2u2'
author Atul Varma <varmaa@toolness.com>
date Tue Mar 31 10:51:03 2009 -0700 (3 years ago)
parents f44ab359f4af
children
line source
1 var OWC = {
2 _makeHintButton: function makeHintButton(hints) {
3 var hintButtonContainer = $('<div class="hint-button"></div>');
4 var hintButton = $('<span></span>');
5 hintButton.text($('#msg-show-first-hint').text());
6 hintButton.click(
7 function() {
8 hints.find('p:hidden:first').fadeIn();
9 if (hints.find('p:hidden').length)
10 $(this).text($('#msg-show-another-hint').text());
11 else
12 $(this).text($('#msg-no-more-hints').text());
13 });
14 hintButtonContainer.append(hintButton);
15 return hintButtonContainer;
16 },
18 load: function load(id) {
19 var challenge = $('#challenge-' + id);
20 if (!challenge.length)
21 challenge = $('#challenge-does-not-exist');
22 var content = challenge.find('.content').clone();
23 var current = $('#current-challenge');
24 current.empty();
26 var headerText = $('#msg-header').text().replace('{id}', id);
27 current.append($('<h1></h1>').text(headerText));
28 document.title = headerText;
30 current.append(content);
32 var hints = $('<div class="hints"></div>');
33 hints.append(challenge.find('.hints').clone());
34 current.append(this._makeHintButton(hints));
35 current.append(hints);
37 var validatorCode = challenge.find('.validator').text();
39 var validate = eval('(' + validatorCode + ')');
41 function maybeDisplaySuccess() {
42 if (validate(current)) {
43 var success = $('<div id="success"></div>');
44 success.text($('#msg-success').text());
45 success.wrapInner('<h1></h1>');
46 success.hide();
47 current.children().addClass('completed');
48 current.append(success);
49 var next = $('<a target="_self"></a>');
50 next.attr("href", "?" + (id+1));
51 next.text($('#msg-next-challenge').text());
52 success.append(next);
53 success.fadeIn('slow');
54 window.clearInterval(intervalId);
55 }
56 }
57 var intervalId = window.setInterval(maybeDisplaySuccess, 100);
59 $(".challenge").remove();
61 current.fadeIn();
62 }
63 };
65 $(window).ready(
66 function() {
67 var queryIndex = window.location.href.indexOf('?');
68 var challengeId;
69 if (queryIndex == -1)
70 challengeId = 1;
71 else
72 challengeId = Number(window.location.href.slice(queryIndex+1));
74 function showChallenge() {
75 OWC.load(challengeId);
76 }
78 showChallenge();
79 });