-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise.js
193 lines (168 loc) · 5.02 KB
/
exercise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var level;
var currentLevel = parseInt(window.localStorage.getItem('currentLevel'), 10) || 0;
$(document).ready(function () {
(function () {
var tour = new Tour();
tour.addSteps([
{
element: "h1",
placement: "bottom",
backdrop: true,
title: "Welcome to the `locator game`!",
content: "Find here the assignments you have to complete."
},
{
element: ".html",
placement: "bottom",
backdrop: true,
title: "HTML",
content: "This is how the HTML looks like."
},
{
element: "#render",
placement: "top",
backdrop: true,
title: "Rendered",
content: "This is how the HTML will look like once rendered."
},
{
element: "#type",
placement: "top",
backdrop: true,
title: "Select type",
content: "Select how you want to locate the element. CSS or XPath."
},
{
element: "#expression",
placement: "top",
backdrop: true,
title: "Locate",
content: "Type the locator expression."
},
{
element: "#toggleHelp",
placement: "bottom",
backdrop: true,
title: "Help",
content: "Click here if you need help."
}
]);
// Initialize the tour
tour.init();
// Start the tour
tour.start();
}());
level = levels[currentLevel];
loadLevel();
setProgress();
$('pre code').each(function (i, block) {
hljs.highlightBlock(block);
});
$('#toggleHelp').click(function (event) {
$('#help').toggle();
});
$('#try').click(function (event) {
event.preventDefault();
var userExpr;
if ($("#type").val() === 'XPath') {
userExpr = $('#render').find(_x($("[name='expression']").val()));
} else {
userExpr = $('#render').find($("[name='expression']").val())
}
userExpr.addClass('highlight');
setTimeout(function () {
userExpr.removeClass('highlight');
}, 500);
});
$('form').submit(function (event) {
event.preventDefault();
var win = false;
var userExpr;
var ruleExpr;
if (level.selector.css === undefined) {
ruleExpr = $('#render').find(_x(level.selector.xpath));
} else {
ruleExpr = $("#render").find(level.selector.css);
}
if ($("#type").val() === 'XPath') {
userExpr = $('#render').find(_x($("[name='expression']").val()));
} else {
userExpr = $('#render').find($("[name='expression']").val())
}
userExpr.addClass('highlight');
win = (userExpr.sequenceEqual(ruleExpr));
if (win) {
currentLevel++;
$.toaster({message: 'Well done!', title: '', priority: 'info'});
// $('#console').text("Well done!");
setProgress();
if (currentLevel >= levels.length) {
$.toaster({message: 'Completed everything!', title: '', priority: 'success'});
// $('#console').text("Completed everything");
window.localStorage.setItem("currentLevel", 0);
$("#type").prop("disabled", true);
$('input[name="expression"]').prop("disabled", true);
$('button').prop("disabled", true);
$('#share').modal('toggle');
} else {
setTimeout(function () {
loadLevel();
}, 500);
}
} else {
setTimeout(function () {
userExpr.removeClass('highlight');
$('input[name="expression"]').val('');
// $('#console').text("Please, try agian!");
$.toaster({message: 'Please, try again...', title: '', priority: 'warning'});
}, 500);
}
});
});
$.fn.sequenceEqual = function (compareTo) {
if (!compareTo || !compareTo.length || this.length !== compareTo.length) {
return false;
}
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] !== compareTo[i]) {
return false;
}
}
return true;
}
$.fn.contentsEqual = function (compareTo) {
return compareTo && this.length === compareTo.length && this.length === this.filter(compareTo).length;
}
function loadLevel() {
// clean form
$('#help').hide();
$('#type').val('CSS');
$('input[name="expression"]').val('');
$('#console').text('');
$('#helpTitle').text('');
$('#helpText').html('');
$('#examples').html('');
window.localStorage.setItem("currentLevel", currentLevel);
level = levels[currentLevel];
$('h1').text(level.instruction);
$('#render').html(level.html);
$('pre code').text(level.html);
$('#helpTitle').text(level.helpTitle);
$('#helpText').html(level.helpText);
level.examples.forEach(function (item) {
$('#examples').append('<div>' + item + '</div>');
});
}
function setProgress() {
var progress = 100 / (levels.length / currentLevel);
$('.progress-bar').css('width', progress + '%').attr('aria-valuenow', progress).text("Level " + (currentLevel) + " of " + levels.length);
}
function _x(STR_XPATH) {
var xresult = document.evaluate(STR_XPATH, document, null, XPathResult.ANY_TYPE, null);
var xnodes = [];
var xres;
while (xres = xresult.iterateNext()) {
xnodes.push(xres);
}
return xnodes;
}