-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.html
484 lines (449 loc) · 21.1 KB
/
new.html
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quiz Generation Form</title>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
.col-md-8 .input-group {margin-bottom:10px;}
.question {border:1px solid #ddd; padding:10px; margin-top:10px;}
.more {padding:10px; text-align:center;}
.content {position:relative;}
.output-wrapper {padding:10px;}
.thumbnail img {margin-bottom:10px;}
.caption textarea {margin:10px 0;}
#test {width:100%; height:600px; border:0;}
</style>
<script>var pageId = "builder";</script>
</head>
<body>
<div class="container">
<div class="page-header">
<!-- <h1></h1> -->
<p class="lead">
Quiz builder
<button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target=".import-modal">Import</button></p>
</div>
<div id="questions"></div>
<div class="row">
<div class="cold-md-12 more">
<button type="button" class="btn btn-success addQuestion">Add another question</button>
<button type="button" class="btn btn-primary pull-right generateQuiz">Generate quiz</button>
</div>
</div>
<div class="row">
<div class="cold-md-12 msgs">
</div>
</div>
<div class="row">
<div class="cold-md-12 output-wrapper">
<div class="panel panel-default">
<div class="panel-heading" style="overflow-y:auto;">
Copy and paste the output below into the "Content with JS" field of a JSO General Page or script module.
<button type="button" class="btn btn-primary pull-right btn-xs" data-toggle="modal" data-target=".preview-modal" onclick="preview();">Preview</button>
</div>
<div class="panel-body">
<pre id="output">Click "Generate quiz" to create embed code.</pre>
</div>
</div>
</div>
</div>
<div class="modal fade preview-modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="padding:20px;">
<iframe id="test"></iframe>
</div>
</div>
</div>
<div class="modal fade import-modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="padding:20px;">
<p class="lead" style="border-bottom: 1px solid #eee;">Quiz Importer</p>
<textarea class="form-control" rows="5" id="pasted-code" placeholder="Paste existing embed code here..." ></textarea>
<button type="button" class="btn btn-success" onclick="parseEmbedCode('');" style="margin-top:10px;">Import</button>
<p class="lead" style="border-bottom: 1px solid #eee; margin-top:10px;">Autosaves<button class="btn pull-right btn-xs btn-warning" onclick="clearSaves();">Clear Autosaves</button></p>
</div>
</div>
</div>
<!--
TEMPLATE: new question
-->
<script id="question-template" type="text/template">
<div class="row question question<%= number %>">
<div class="col-md-8 col-sm-8">
<form>
<div class="form-group">
<label for="question<%= number %>">Question <%= number %></label>
<input type="text" class="form-control" id="question<%= number %>" placeholder="Question" data-item="question">
</div>
<p><strong>Answers</strong></p>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer1check">
</span>
<input type="text" class="form-control" placeholder="Required" data-item="answer1">
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer2check">
</span>
<input type="text" class="form-control" placeholder="Required" data-item="answer2">
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer3check">
</span>
<input type="text" class="form-control" placeholder="Optional" data-item="answer3">
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer4check">
</span>
<input type="text" class="form-control" placeholder="Optional" data-item="answer4">
</div>
</form>
</div>
<div class="col-md-4 col-sm-4 content">
<div class="form-group content-input">
<label for="contentId<%= number %>">Content Id</label>
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" id="contentId<%= number %>" placeholder="Clickability ID" data-item="contentId">
</div>
</div>
<div class="btn btn-primary" id="contentId<%= number %>prefill">Prefill</div>
</form>
<div class="checkbox">
<label>
<input type="checkbox" data-item="togglePhoto"> Hide photo?
</label>
</div>
</div>
<div style="position:absolute; top:0; right:0;"><a href="" class="text-danger" onclick='$(".question<%= number %>").remove();return false;'>Delete Question</a></div>
<label>Related Content</label>
<div class="thumbnail">
<img src="" alt="No Image" data-item="imageUrl">
<div class="caption">
<input type="text" class="form-control" placeholder="Related image source" data-item="imageUrl">
<textarea class="form-control" rows="8" data-item="answerDetails" placeholder="Enter a Clickability ID above to prefill or manually enter related text."></textarea>
<input type="text" class="form-control" placeholder="Related link url" data-item="articleUrl">
</div>
</div>
</div>
</div>
</script>
<!--
TEMPLATE: filled question
-->
<script id="filled-question-template" type="text/template">
<div class="row question question<%= number %>">
<div class="col-md-8 col-sm-8">
<form>
<div class="form-group">
<label for="question<%= number %>">Question <%= number %></label>
<input type="text" class="form-control" id="question<%= number %>" placeholder="Question" data-item="question" value='<%= question.question %>'>
</div>
<p><strong>Answers</strong></p>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer1check" <%= question.answer === 0 ? "checked=checked" : "" %>>
</span>
<input type="text" class="form-control" placeholder="Required" data-item="answer1" value='<%= question.options[0] %>'>
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer2check" <%= question.answer === 1 ? "checked=checked" : "" %>>
</span>
<input type="text" class="form-control" placeholder="Required" data-item="answer2" value='<%= question.options[1] %>'>
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer3check" <%= question.answer === 2 ? "checked=checked" : "" %>>
</span>
<input type="text" class="form-control" placeholder="Optional" data-item="answer3" value='<%= question.options[2] %>'>
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="question<%= number %>" data-item="answer4check" <%= question.answer === 3 ? "checked=checked" : "" %>>
</span>
<input type="text" class="form-control" placeholder="Optional" data-item="answer4" value='<%= question.options[3] %>'>
</div>
</form>
</div>
<div class="col-md-4 col-sm-4 content">
<div class="form-group content-input">
<label for="contentId<%= number %>">Content Id</label>
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" id="contentId<%= number %>" placeholder="Clickability ID" data-item="contentId" value='<%= question.cid %>'>
</div>
</div>
<div class="btn btn-primary" id="contentId<%= number %>prefill" onclick="getContent($('#contentId<%= number %>').val(),<%= number %>)">Prefill</div>
</form>
<div class="checkbox">
<label>
<input type="checkbox" data-item="togglePhoto" <%= question.hidePhoto ? "checked=checked" : "" %>> Hide photo?
</label>
</div>
</div>
<div style="position:absolute; top:0; right:0;"><a href="" class="text-danger" onclick='$(".question<%= number %>").remove();return false;'>Delete Question</a></div>
<div class="thumbnail">
<img src="<%= question.imgsrc %>" alt="No Image" >
<div class="caption">
<input type="text" class="form-control" placeholder="Related image source" data-item="imageUrl" value='<%= question.imgsrc %>'>
<textarea class="form-control" rows="8" data-item="answerDetails" placeholder="No content found. Double-check Clickability ID or manually enter related text."><%= question.answerDetails %></textarea>
<% if (question.articleUrl) { %>
<a href="<%= question.articleUrl %>" target="_blank">Link</a>
<% } %>
<input type="text" class="form-control" placeholder="Related link url" data-item="articleUrl" value='<%= question.articleUrl %>'>
</div>
</div>
</div>
</div>
</script>
<script src="http://graphics.jsonline.com/jsi_news/javascripts/underscore/v1.7.0/underscore.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script>
var questionNumber = 1;
var questionTemplate = _.template(document.getElementById('question-template').innerHTML);
var filledQuestionTemplate = _.template(document.getElementById('filled-question-template').innerHTML);
var contentVarName;
function createQuestion(number, data) {
$('#questions').append(questionTemplate({number: number}));
$('#contentId' + number + 'prefill').on('click', function (){
var cid = $('#contentId' + number).val();
getContent(cid,number);
});
};
$(document).ready(function() {
loadSaves();
$(document).on('focusout','input, textarea',function(){
saveQuizThrottle();
});
});
//create first question
createQuestion(questionNumber);
$('.addQuestion').on('click', function() {
saveQuiz();
questionNumber++;
createQuestion(questionNumber);
if($('#output').text().length > 50) {
addMsg("A new question has been added, please re-generate the quiz.",1);
}
});
$('.generateQuiz').on('click', function() {
$('#output').text(compileEmbedCode());
saveQuiz();
selectText('output');
removeMsg(1);
});
var saveQuiz = function() {
var output = generateQuizObject(true);
var now = new Date();
var ts = now.getMonth() + "/" + now.getDate() + "/" + now.getFullYear() + " version " + now.getHours();
localStorage.setItem('Quiz Autosave: ' + ts,JSON.stringify(output));
}
var saveQuizThrottle = _.debounce(saveQuiz, 1000);
function loadSaves(){
var html = '';
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
var key = localStorage.key(i);
html += '<li><a onclick="parseEmbedCode(localStorage.getItem(\'' + key + '\'))">Load ' + key + '</a></li>';
}
$(".import-modal .modal-content").append('<ul class="autosaves">' + html + '</ul>');
}
function clearSaves(){
localStorage.clear();
$('.import-modal').modal('hide');
$('.autosaves').remove();
}
function compileEmbedCode() {
var output = generateQuizObject(false);
var outputStr = JSON.stringify(output);
var varName = "questionData";
var scriptUrl = "http://graphics.jsonline.com/jsi_news/javascripts/quiz/jso-quiz-1.1.0.js";
var div = "<div id=\"quiz\"></div>";
var scriptInclude = "<scr" + "ipt src=\"" + scriptUrl + "\"></scr" + "ipt>";
var variableWrapper = "<script>var " + varName + " = " + outputStr + ";</scr" + "ipt>";
var quizFcn = "<scr" + "ipt>createQuiz(" + varName + ");</scr" + "ipt>";
var embedCode = div + variableWrapper + scriptInclude + quizFcn;
return embedCode;
}
function getContent(id,qnum) {
var url = 'http://m.jsonline.com/api/v1/?id=' + id + '&bootstrap=question' + qnum;
contentVarName = "question" + qnum;
bootstrapContent(url, parseContent);
}
function bootstrapContent(url, callback) {
var tag = document.createElement('script');
tag.type = 'text/javascript';
tag.src = url;
tag.onreadystatechange = callback;
tag.onload = callback;
document.getElementsByTagName('head')[0].appendChild(tag);
}
function parseContent(){
var data = window[contentVarName].collection;
data = data[0];
var tempData = {}
//resize image url - messy
var t = data.thumbnailUrl;
var dims = t.match(/\/\d+\*\d+\//g);
if (dims) {
var dim = dims[0];
var trimmed = dim.replace(/\//g,'');
var parts = trimmed.split('*');
var x = parseInt(parts[0],10);
var y = parseInt(parts[1],10);
var factor = 3;
var final = '/' + (x*factor) + '*' + (y*factor) + '/';
tempData.thumbnailUrl = t.replace(/\/\d+\*\d+\//g, final) || "";
} else {
tempData.thumbnailUrl = "";
}
//end messy
tempData.shortBody = data.shortBody.replace(/<\/?[^>]+(>|$)/g, " ").replace('"','"').replace('&','&') || "";
tempData.url = data.url || "";
$('.' + contentVarName + ' .content .thumbnail img').attr("src",tempData.thumbnailUrl);
$('.' + contentVarName + ' .content .thumbnail input[data-item=imageUrl]').val(tempData.thumbnailUrl);
$('.' + contentVarName + ' .content .thumbnail textarea[data-item=answerDetails]').val(tempData.shortBody);
$('.' + contentVarName + ' .content .thumbnail input[data-item=articleUrl').val(tempData.url);
}
function generateQuizObject(noWarnings) {
if (!noWarnings) {
removeMsg("all");
}
try {
var questions = document.getElementsByClassName("question");
var questionsArray = [];
_.each(questions, function(q, index){
var qNum = index + 1;
var tempObj = {};
var questionText = $(q).find("input[data-item=question]").val() || "";
tempObj["question"] = questionText;
if (!tempObj["question"] || tempObj["question"] === "") {
if (!noWarnings) {
addMsg("Question " + qNum + ": Question field cannot be blank.");
}
}
var options = $(q).find(".col-md-8 form .input-group");
if (!options || options.length < 2) {
if (!noWarnings) {
addMsg("Question " + qNum + ": Minimum of 2 answers required.");
}
}
var tempOptionsObj = {};
var answerNumber = 4;
_.each(options, function(option, index){
var answerText = $(option).find("input[type=text]").val();
if ((!answerText || answerText === "") && index < 2) {
if (!noWarnings) {
addMsg("Question " + qNum + ": Answer " + (index+1) + " cannot be blank.");
}
}
if (index > 1) {
if (answerText || answerText !== "") {
var correctToggle = $(option).find("input[type=radio]").is(":checked");
var questionNumber = index;
tempOptionsObj[questionNumber] = answerText;
if (correctToggle) {
answerNumber = questionNumber;
}
}
} else {
var correctToggle = $(option).find("input[type=radio]").is(":checked");
var questionNumber = index;
tempOptionsObj[questionNumber] = answerText;
if (correctToggle) {
answerNumber = questionNumber;
}
}
});
tempObj["options"] = tempOptionsObj;
tempObj["answer"] = answerNumber;
if (answerNumber < 0 || answerNumber > 3){
if (!noWarnings) {
addMsg("Question " + qNum + ": Please check a valid answer.");
}
}
var cid = $(q).find("input[data-item=contentId]").val() || "";
tempObj["cid"] = cid;
var answerDetails = $(q).find(".caption textarea").val() || "";
tempObj["answerDetails"] = answerDetails;
var imgsrc = $(q).find("input[data-item=imageUrl]").val() || "";
tempObj["imgsrc"] = imgsrc;
var hidePhoto = $(q).find("input[data-item=togglePhoto]").is(':checked') || false;
tempObj["hidePhoto"] = hidePhoto;
var articleUrl = $(q).find("input[data-item=articleUrl]").val() || "";
tempObj.articleUrl = articleUrl;
questionsArray.push(tempObj);
});
return questionsArray;
} catch (err) {
addMsg("Data generation Error. Please check each field for unusual characters. Error Message: " + err);
}
}
function addMsg(msg, msgId){
var alert = '<div class="alert alert-danger message' + msgId + '" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>' + msg + '</div>'
$('.msgs').append(alert);
}
function removeMsg(msgId) {
if(msgId === "all") {
$('.msgs').html('');
} else {
$('.message' + msgId).remove();
}
}
function preview(){
$('#test').empty();
var ifrm = document.getElementById('test');
ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
ifrm.document.open();
ifrm.document.write('<scr'+'ipt>var pageId = "builder"; localStorage.removeItem(pageId + "-quiz");</scr'+'ipt> ' + compileEmbedCode());
ifrm.document.close();
}
function parseEmbedCode(input) {
var code = "";
if(input === ""){
code = $("#pasted-code").val();
} else {
code = input;
}
var qArray = code.match(/\[(.*?)\]/g);
var obj = JSON.parse(qArray[0]);
questionNumber = 0;
$('#questions').empty();
_.each(obj, function(q, index) {
questionNumber++;
console.log(q.question);
console.log(q.options[0]);
$('#questions').append(filledQuestionTemplate({number: questionNumber, question: q}));
});
$('.import-modal').modal('hide');
}
function selectText(element) {
var text = document.getElementById(element);
if (document.body.createTextRange) { // ms
var range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) { // moz, opera, webkit
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
</script>
</body>
</html>