Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. (function (jQuery) {
  2. "use strict";
  3.  
  4. function FillBlanksGame(instance, options) {
  5. var defaultOptions = {
  6. sentenceHolder: null,
  7. wordsHolder: null,
  8. submitBtn: null,
  9. blank: "###",
  10. sentences: [],
  11. showResult: false,
  12. isBack: false,
  13. response: []
  14. };
  15. this.instance = instance;
  16. this.options = jQuery.extend(true, {}, defaultOptions, options);
  17. this.$el = jQuery(this.instance);
  18. this.data = [];
  19. this.currentData = {};
  20. this.response = [];
  21. this.index = 0;
  22. this.halt = false;
  23. this.init();
  24. }
  25. FillBlanksGame.prototype = {
  26. init: function () {
  27. var self = this;
  28. //self.options.submitBtn.prop("disabled", true);
  29. self.setupData();
  30. self.setupEventListeners();
  31. self.showChallenge();
  32. if (self.options.isBack && self.options.response.length > 0) {
  33. self.showResponse();
  34. }
  35. },
  36. showResponse: function () {
  37. let self = this;
  38. let res = self.options.response[0].response;
  39. for (let i = 0, j = res.length; i < j; i++) {
  40. if (res[i] !== null) {
  41. let $w = self.options.wordsHolder.find('.word[data-index="' + res[i].index + '"]');
  42. let $b = self.options.sentenceHolder.find('.blank[data-index="' + i + '"]');
  43. $b.empty().append($w);
  44. $b.find(".word")
  45. .removeClass("in-options game-word-mar")
  46. .addClass("in-sentence");
  47. $b.removeClass("empty");
  48. }
  49. }
  50. },
  51. setupEventListeners: function () {
  52. var self = this;
  53. jQuery(document).on("click", ".in-sentence", function () {
  54. if (self.halt) {
  55. return;
  56. }
  57. self.halt = true;
  58. self.$el.trigger("itemSelected");
  59. //self.options.submitBtn.prop("disabled", true);
  60. var $this = jQuery(this);
  61. var _this = $this[0];
  62. var rect = _this.getBoundingClientRect();
  63. var $target = self.options.wordsHolder;
  64. var $holder = $this.parent();
  65. $holder.append("<span class='word'>&nbsp;</span>");
  66. $target.append($this);
  67. TweenMax.set(_this, { x: 0, y: 0 });
  68. var newRect = _this.getBoundingClientRect();
  69.  
  70. TweenMax.from(_this, 1, {
  71. x: rect.left - newRect.left,
  72. y: rect.top - newRect.top,
  73. ease: Power3.easeOut,
  74. onStart: function () {
  75. $this.addClass("game-word-mar");
  76. },
  77. onComplete: function () {
  78. self.halt = false;
  79. $this.removeClass("in-sentence").addClass("in-options");
  80. $holder.addClass("empty");
  81. self.toggleSubmit();
  82. }
  83. });
  84. });
  85. jQuery(document).on("click", ".in-options", function () {
  86. if (self.halt) {
  87. return;
  88. }
  89. self.halt = true;
  90. self.$el.trigger("itemSelected");
  91. //self.options.submitBtn.prop("disabled", true);
  92. var $this = jQuery(this);
  93. var _this = $this[0];
  94. var rect = _this.getBoundingClientRect();
  95. var $target = self.options.sentenceHolder.find(".empty").first();
  96. $this.removeClass("game-word-mar");
  97. $target.empty().append($this);
  98.  
  99. TweenMax.set(_this, { x: 0, y: 0 });
  100.  
  101. var newRect = _this.getBoundingClientRect();
  102.  
  103. TweenMax.from(_this, 1, {
  104. x: rect.left - newRect.left,
  105. y: rect.top - newRect.top,
  106. ease: Power3.easeOut,
  107. onStart: function () {
  108. //
  109. },
  110. onComplete: function () {
  111. $this.removeClass("in-options").addClass("in-sentence");
  112. $target.removeClass("empty");
  113. self.halt = false;
  114. self.toggleSubmit();
  115. }
  116. });
  117. });
  118. self.options.submitBtn.on("click", function () {
  119. if (self.halt) return;
  120.  
  121. //var words = self.options.sentenceHolder.find(".in-sentence");
  122. var holders = self.options.sentenceHolder.find(".blank");
  123. var w = [];
  124. jQuery.each(holders, function (x, y) {
  125. var j = jQuery(y).find(".in-sentence");
  126. if (j.length > 0) {
  127. w.push({
  128. index: jQuery(j).attr("data-index"),
  129. word: jQuery(j).attr("data-word")
  130. });
  131. } else {
  132. w.push(null);
  133. }
  134. });
  135. self.response = [];
  136. self.response.push({
  137. correct_answer: self.data[self.index].options,
  138. response: w
  139. });
  140. if (self.index < self.data.length - 1) {
  141. self.index++;
  142. self.showChallenge();
  143. } else {
  144. self.onCompletion();
  145. }
  146. });
  147. },
  148. onCompletion: function () {
  149. var self = this;
  150.  
  151. if (self.options.showResult) {
  152. // jQuery(document).off("click", ".in-sentence");
  153. // jQuery(document).off("click", ".in-options");
  154. self.highlightResults();
  155. }
  156. let res = self.response[0];
  157. let skipped = res.response.length != res.correct_answer.length;
  158. let r = { skipped: skipped, correct: !skipped };
  159. for (let i = 0, j = res.response.length; i < j; i++) {
  160. if (res.response[i] === null) {
  161. r.skipped = true;
  162. r.correct = false;
  163. //break;
  164. } else if (res.response[i].index != res.correct_answer[i].index) {
  165. r.correct = false;
  166. //break;
  167. }
  168. }
  169. self.$el.trigger("gameComplete", [self.response, self.data, r]);
  170. },
  171. highlightResults: function () {
  172. let self = this;
  173. let result = self.response;
  174.  
  175. for (let i = 0; i < result.length; i++) {
  176. let wordHolder = self.$el.find(`.blank[question-index='${i}']`);
  177. for (let j = 0; j < result[i].response.length; j++) {
  178. let curHolder = Array.from(wordHolder).filter(h => h.getAttribute("data-index") == j)[0];
  179. if (curHolder) {
  180. if (result[i] && result[i].response[j] && result[i].response[j].index == result[i].correct_answer[j].index) {
  181. curHolder.classList.add("correct");
  182. } else {
  183. curHolder.classList.add("incorrect");
  184. }
  185. }
  186. }
  187. }
  188. },
  189. toggleSubmit: function () {
  190. //var self = this;
  191. //if (self.options.wordsHolder.find(".game-word-btn").length > 0) {
  192. // self.options.submitBtn.prop("disabled", true);
  193. //} else {
  194. // self.options.submitBtn.prop("disabled", false);
  195. //}
  196. },
  197. showChallenge: function () {
  198. var self = this;
  199. //self.options.submitBtn.prop("disabled", true);
  200. self.currentData = self.data[self.index];
  201. self.options.sentenceHolder.off().empty();
  202. self.options.wordsHolder.off().empty();
  203. let words = [];
  204. let blank_index = 0;
  205. jQuery.each(self.currentData.sentence_words, function (i, w) {
  206. if (w.blank) {
  207. words.push('<span id="blank-' + blank_index + `"question-index = ${self.index} data-index=${blank_index} class="blank blank-border empty"><span class="word">&nbsp;</span></span>`);
  208. blank_index++;
  209. } else {
  210. words.push('<span class="not-blank">' + w.word + "</span>");
  211. }
  212. });
  213. let buttons = [];
  214. jQuery.each(self.currentData.shuffled, function (i, d) {
  215. buttons.push('<span class="word in-options game-word-btn game-word-pad game-word-mar" data-word="' + d.word + '" data-index="' + d.index + '">' + d.word + "</span>");
  216. });
  217. self.options.wordsHolder.html(buttons.join(""));
  218. self.options.sentenceHolder.html(words.join(""));
  219. },
  220. setupData: function () {
  221. var self = this;
  222. var sentences = [];
  223. var i, j, k, l, tmp, sentence;
  224. for (i = 0, j = self.options.sentences.length; i < j; i++) {
  225. sentence = [];
  226. tmp = self.options.sentences[i].sentence.split(self.options.blank);
  227. for (k = 0, l = tmp.length; k < l; k++) {
  228. sentence.push({
  229. word: tmp[k],
  230. blank: false
  231. });
  232. if (k + 1 < l) {
  233. sentence.push({
  234. word: self.options.sentences[i].words[k],
  235. blank: true
  236. });
  237. }
  238. }
  239. sentences.push(sentence);
  240. }
  241. for (i = 0, j = sentences.length; i < j; i++) {
  242. var sentence_words = sentences[i];
  243. var options = [];
  244. var blank_index = 0;
  245. jQuery.each(sentence_words, function (i, w) {
  246. if (w.blank) {
  247. options.push({ index: blank_index, word: w.word });
  248. blank_index++;
  249. }
  250. });
  251.  
  252. tmp = {
  253. sentence_words: sentences[i].slice(),
  254. options: options,
  255. shuffled: options.slice()
  256. };
  257. self.shuffle(tmp.shuffled);
  258. self.data.push(tmp);
  259. }
  260. self.shuffle(self.data);
  261. },
  262. shuffle: function (array) {
  263. var currentIndex = array.length;
  264. var temporaryValue, randomIndex;
  265. while (0 !== currentIndex) {
  266. randomIndex = Math.floor(Math.random() * currentIndex);
  267. currentIndex -= 1;
  268. temporaryValue = array[currentIndex];
  269. array[currentIndex] = array[randomIndex];
  270. array[randomIndex] = temporaryValue;
  271. }
  272. return array;
  273. }
  274. };
  275.  
  276. jQuery.fn.FillBlanksGame = function (options) {
  277. var args = Array.prototype.slice.call(arguments, 1);
  278. var plgName = "FillBlanksGame";
  279. return this.each(function () {
  280. var inst = jQuery.data(this, plgName);
  281. if (typeof inst === "undefined") {
  282. if (typeof options === "undefined" || typeof options == "string" || options instanceof String) {
  283. throw "invalid options passed while creating new instance.";
  284. }
  285. if (typeof options.sentences === "undefined" || !jQuery.isArray(options.sentences)) {
  286. throw "invalid options";
  287. }
  288. var p = new FillBlanksGame(this, options);
  289. jQuery.data(this, plgName, p);
  290. } else if (typeof options !== "undefined") {
  291. if (typeof inst[options] === "function") {
  292. inst[options].apply(inst, args);
  293. }
  294. }
  295. });
  296. };
  297. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement