From a2c153620ca57a5c7461fd98feb35eae40995330 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 4 Apr 2014 13:19:16 +0200 Subject: [PATCH] Minor cleanup making Blanks easier to work with. Some optimization and minor bug fixes. --- .gitignore | 1 + js/blanks.js | 309 +++++++++++++++++++++++++++++---------------------- 2 files changed, 178 insertions(+), 132 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/js/blanks.js b/js/blanks.js index d8267b5..2967224 100644 --- a/js/blanks.js +++ b/js/blanks.js @@ -6,7 +6,10 @@ var H5P = H5P || {}; * @param {jQuery} $ */ H5P.Blanks = (function ($) { - + var STATE_ONGOING = 'ongoing'; + var STATE_CHECKING = 'checking'; + var STATE_SHOWING_SOLUTION = 'showing-solution'; + /** * Initialize module. * @@ -35,13 +38,8 @@ H5P.Blanks = (function ($) { postUserStatistics: (H5P.postUserStatistics === true) }, params); - this.answers = []; - this.$inputs = []; - } - - var STATE_ONGOING = 'ongoing'; - var STATE_CHECKING = 'checking'; - var STATE_SHOWING_SOLUTION = 'showing-solution'; + this.clozes = []; + }; /** * Append field to wrapper. @@ -55,6 +53,48 @@ H5P.Blanks = (function ($) { // Add "show solutions" button and evaluation area this.addFooter(); }; + + /** + * Append questitons to the given container. + * + * @param {jQuery} $container + */ + C.prototype.appendQuestionsTo = function ($container) { + var self = this; + + for (var i = 0; i < self.params.questions.length; i++) { + var question = self.params.questions[i]; + + // Go through the text and replace all the asterisks with input fields + var clozeEnd, clozeStart = question.indexOf('*'); + while (clozeStart !== -1 && clozeEnd !== -1) { + clozeStart++; + clozeEnd = question.indexOf('*', clozeStart); + if (clozeEnd === -1) { + continue; // No end + } + + // Create new cloze + var cloze = new Cloze(question.substring(clozeStart, clozeEnd), self.params.caseSensitive); + clozeEnd++; + question = question.slice(0, clozeStart - 1) + cloze + question.slice(clozeEnd); + self.clozes.push(cloze); + + // Find the next cloze + clozeStart = question.indexOf('*', clozeEnd); + } + + $container[0].innerHTML += '
' + question + '
'; + } + + $container.find('input').each(function (i) { + self.clozes[i].setInput($(this)); + }).keydown(function (event) { + if (event.keyCode === 13) { + return false; // Prevent form submission on enter key + } + }); + }; /** * Append footer to Blanks block. @@ -74,8 +114,6 @@ H5P.Blanks = (function ($) { return; } - - var $buttonBar = $('
', {'class': 'h5p-button-bar'}); // Check answer button @@ -87,9 +125,9 @@ H5P.Blanks = (function ($) { that.showEvaluation(); } ); - + // Display solution button - this._$solutionButton = $('