Guest User

Untitled

a guest
Feb 7th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This software is licensed under a BSD license; see the LICENSE file for details. */
  2.  
  3. function boolToInt(x) { if (x) return 1; else return 0; }
  4.  
  5. define_ibex_controller({
  6. name: "DashedSentence",
  7.  
  8. jqueryWidget: {
  9.     _init: function() {
  10.         this.cssPrefix = this.options._cssPrefix;
  11.         this.utils = this.options._utils;
  12.         this.finishedCallback = this.options._finishedCallback;
  13.  
  14.         if (typeof(this.options.s) == "string") {
  15.             // replace all linebreaks (and surrounding space) with 'space-return-space'
  16.             var inputString = this.options.s.replace(/\s*[\r\n]\s*/g, " \r ");
  17.             this.words = inputString.split(/[ \t]+/);
  18.         } else {
  19.             assert_is_arraylike(this.options.s, "Bad value for 's' option of DashedSentence.");
  20.             this.words = this.options.s;
  21.         }
  22.         this.mode = dget(this.options, "mode", "self-paced reading");
  23.         assert(this.mode == "self-paced reading" || this.mode == "speeded acceptability",
  24.                "Value of 'mode' option for DashedSentence controller must be either " +
  25.                "'self-paced reading' or 'speeded acceptability'.");
  26.         this.display = dget(this.options, "display", "dashed");
  27.         this.blankText = dget(this.options, "blankText", "\u2014\u2014"/*mdash*/);
  28.         this.wordTime = dget(this.options, "wordTime", this.display == "in place" ? 400 : 300); // Only for speeded accpetability.
  29.         this.wordPauseTime = dget(this.options, "wordPauseTime", this.display == "in place" ? 0 : 100); // Ditto.
  30.         this.showAhead = dget(this.options, "showAhead", true);
  31.         this.showBehind = dget(this.options, "showBehind", true);
  32.         assert(this.display == "dashed" || this.display == "in place",
  33.                "Value of 'display' option for DashedSentence controller must be either " +
  34.                "'dashed' (default) or 'in place'.");
  35.  
  36.         this.currentWord = 0;
  37.  
  38.         // Is there a "stopping point" specified?
  39.         this.stoppingPoint = this.words.length;
  40.         for (var i = 0; i < this.words.length; ++i) {
  41.             if (stringStartsWith("@", this.words[i])) {
  42.                 this.words[i] = this.words[i].substring(1);
  43.                 this.stoppingPoint = i + 1;
  44.                 break;
  45.             }
  46.         }
  47.  
  48.         this.hideUnderscores = dget(this.options, "hideUnderscores", true);
  49.         if (this.hideUnderscores) {
  50.             this.words = $.map(this.words, function(word) { return word.replace(/_/g, ' ') });
  51.         }
  52.  
  53.         this.mainDiv = $("<div>");
  54.         this.element.append(this.mainDiv);
  55.  
  56.         this.background = this.element.css('background-color') || "white";
  57.         this.isIE7;
  58.         /*@cc_on this.isIE = true; @*/
  59.         if (this.isIE)
  60.             this.background = "white";
  61.  
  62.         // Defaults.
  63.         this.unshownBorderColor = dget(this.options, "unshownBorderColor", "#9ea4b1");
  64.         this.shownBorderColor = dget(this.options, "shownBorderColor", "black");
  65.         this.unshownWordColor = dget(this.options, "unshownWordColor", this.background);
  66.         this.shownWordColor = dget(this.options, "shownWordColor", "black");
  67.  
  68.         // Precalculate MD5 of sentence.
  69.         this.sentenceDescType = dget(this.options, "sentenceDescType", "literal");
  70.         assert(this.sentenceDescType == "md5" || this.sentenceDescType == "literal", "Bad value for 'sentenceDescType' option of DashedSentence.");
  71.         if (this.sentenceDescType == "md5") {
  72.             var canonicalSentence = this.words.join(' ');
  73.             this.sentenceDesc = hex_md5(canonicalSentence);
  74.         }
  75.         else {
  76.         if (typeof(this.options.s) == "string")
  77.         this.sentenceDesc = csv_url_encode(this.options.s);
  78.         else
  79.         this.sentenceDesc = csv_url_encode(this.options.s.join(' '));
  80.         }
  81.  
  82.         this.mainDiv.addClass(this.cssPrefix + "sentence");
  83.  
  84.         this.resultsLines = [];
  85.         if (this.mode == "self-paced reading") {
  86.             // Don't want to be allocating arrays in time-critical code.
  87.             this.sprResults = [];
  88.             for (var i = 0; i < this.words.length; ++i)
  89.                 this.sprResults[i] = new Array(2);
  90.         }
  91.         this.previousTime = null;
  92.  
  93.         if (this.display == "in place") {
  94.             this.wordSpan = $(document.createElement("span")).text(this.blankText);
  95.             if (conf_centerItems) {
  96.                 this.mainDiv.css('text-align', 'center');
  97.                 this.wordSpan.css('text-align', 'center');
  98.             }
  99.             this.mainDiv.append(this.wordSpan);
  100.  
  101.             this.blankWord = this.blankWord_inplace;
  102.             this.showWord = this.showWord_inplace;
  103.         }
  104.         else { // dashed
  105.             this.blankWord = this.blankWord_dashed;
  106.             this.showWord = this.showWord_dashed;
  107.  
  108.             this.wordISpans = []; // Inner spans.
  109.             this.wordOSpans = []; // Outer spans.
  110.             this.owsnjq = []; // 'outer word spans no jQuery'.
  111.             this.iwsnjq = []; // 'inner word spans no jQuery'.
  112.             for (var j = 0; j < this.words.length; ++j) {
  113.                 if ( this.words[j] == "\r" ) {
  114.                     this.mainDiv.append('<br/>');
  115.  
  116.                     if (j <= this.stoppingPoint)
  117.                         this.stoppingPoint--;
  118.                    
  119.                     continue;
  120.                 }
  121.  
  122.                 var ispan;
  123.                 var ospan = $(document.createElement("span"))
  124.                             .addClass(this.cssPrefix + 'ospan')
  125.                             .append(ispan = $(document.createElement("span"))
  126.                                             .addClass(this.cssPrefix + 'ispan')
  127.                                             .text(this.words[j]));
  128.                 if (! this.showAhead)
  129.                     ospan.css('border-color', this.background);
  130.                 this.mainDiv.append(ospan);
  131.                 if (j + 1 < this.words.length)
  132.                     this.mainDiv.append("&nbsp; ");
  133.                 this.wordISpans.push(ispan);
  134.                 this.wordOSpans.push(ospan);
  135.                 this.iwsnjq.push(ispan[0]);
  136.                 this.owsnjq.push(ospan[0]);
  137.             }
  138.         }
  139.  
  140.         if (this.mode == "speeded acceptability") {
  141.             this.showWord(0);
  142.             var t = this;
  143.             function wordTimeout() {
  144.                 t.blankWord(t.currentWord);
  145.                 ++(t.currentWord);
  146.                 if (t.currentWord >= t.stoppingPoint)
  147.                     t.finishedCallback([[["Sentence (or sentence MD5)", t.sentenceDesc]]]);
  148.                 else
  149.                     t.utils.setTimeout(wordPauseTimeout, t.wordPauseTime);
  150.             }
  151.             function wordPauseTimeout() {
  152.                 t.showWord(t.currentWord);
  153.                 t.utils.clearTimeout(wordPauseTimeout);
  154.                 t.utils.setTimeout(wordTimeout, t.wordTime);
  155.             }
  156.             this.utils.setTimeout(wordTimeout, this.wordTime);
  157.         }
  158.         else if (this.mode == "self-paced reading") {
  159.             var t = this;
  160.             // Inlining this to minimize function calls in code for updating screen after space is pressed.
  161. /*            function goToNext(time) {
  162.                 t.recordSprResult(time, t.currentWord);
  163.  
  164.                 if (t.currentWord - 1 >= 0)
  165.                     t.blankWord(t.currentWord - 1);
  166.                 if (t.currentWord < t.stoppingPoint)
  167.                     t.showWord(t.currentWord);
  168.                 ++(t.currentWord);
  169.                 if (t.currentWord > t.stoppingPoint) {
  170.                     t.processSprResults();
  171.                     t.finishedCallback(t.resultsLines);
  172.                 }
  173.  
  174.                 return false;
  175.             }*/
  176.  
  177.             this.safeBind($(document), 'keydown', function(e) {
  178.                 var time = new Date().getTime();
  179.                 var code = e.keyCode;
  180.  
  181.                 if (code == 32) {
  182.                     // *** goToNext() ***
  183. //                    t.recordSprResult(time, t.currentWord);
  184.                     var word = t.currentWord;
  185.                     if (word > 0 && word <= t.stoppingPoint) {
  186.                         var rs = t.sprResults[word-1];
  187.                         rs[0] = time;
  188.                         rs[1] = t.previousTime;
  189.                     }
  190.                     t.previousTime = time;
  191.  
  192.                     if (t.currentWord - 1 >= 0)
  193.                         t.blankWord(t.currentWord - 1);
  194.                     if (t.currentWord < t.stoppingPoint)
  195.                         t.showWord(t.currentWord);
  196.                     ++(t.currentWord);
  197.                     if (t.currentWord > t.stoppingPoint) {
  198.                         t.processSprResults();
  199.                         t.finishedCallback(t.resultsLines);
  200.                     }
  201.                     return false;
  202.                     // ***
  203.                 }
  204.                 else {
  205.                     return true;
  206.                 }
  207.             });
  208.  
  209.             // For iPhone/iPod touch -- add button for going to next word.
  210.             if (true) {
  211.                 var btext = dget(this.options, "iPhoneNextButtonText", "next");
  212.                 var next = $("<div>")
  213.                            .addClass(this.cssPrefix + "iphone-next")
  214.                            .text(btext);
  215.                 this.element.append(next);
  216.                 next.click(function () {
  217.                     var time = new Date().getTime();
  218.  
  219.                     // *** goToNext() ***
  220.                     //t.recordSprResult(time, t.currentWord);
  221.                     var word = t.currentWord;
  222.                     if (word > 0 && word <= t.stoppingPoint) {
  223.                         var rs = t.sprResults[word-1];
  224.                         rs[0] = time;
  225.                         rs[1] = t.previousTime;
  226.                     }
  227.                     t.previousTime = time;
  228.  
  229.                     if (t.currentWord - 1 >= 0)
  230.                         t.blankWord(t.currentWord - 1);
  231.                     if (t.currentWord < t.stoppingPoint)
  232.                         t.showWord(t.currentWord);
  233.                     ++(t.currentWord);
  234.                     if (t.currentWord > t.stoppingPoint) {
  235.                         t.processSprResults();
  236.                         t.finishedCallback(t.resultsLines);
  237.                     }
  238.  
  239.                     return false;
  240.                     // ***
  241.                 });
  242.             }
  243.         }
  244.     },
  245.  
  246.     // Not using JQuery in these two methods just in case it slows things down too much.
  247.     // NOTE: [0] subscript gets DOM object from JQuery selector.
  248.     blankWord_dashed: function(w) {
  249.         if (this.currentWord <= this.stoppingPoint) {
  250.             this.owsnjq[w].style.borderColor = this.unshownBorderColor;
  251.             this.iwsnjq[w].style.visibility = "hidden";
  252.             if (! this.showBehind)
  253.                 this.owsnjq[w].style.borderColor = this.background;
  254.         }
  255.     },
  256.     showWord_dashed: function(w) {
  257.         if (this.currentWord < this.stoppingPoint) {
  258.             if (this.showAhead || this.showBehind)
  259.                 this.owsnjq[w].style.borderColor = this.shownBorderColor;
  260.             this.iwsnjq[w].style.visibility = "visible";
  261.         }
  262.     },
  263.  
  264.     blankWord_inplace: function (w) {
  265.         if (this.wordPauseTime > 0 && this.currentWord <= this.stoppingPoint) {
  266.             this.wordSpan.empty();
  267.         }
  268.     },
  269.     showWord_inplace: function (w) {
  270.         if (this.currentWord < this.stoppingPoint) {
  271.             this.wordSpan.text(this.words[this.currentWord]);
  272.         }
  273.     },
  274.  
  275.     // Inlining this now.
  276.     /*recordSprResult: function(time, word) {
  277.         if (word > 0 && word < this.stoppingPoint) {
  278.             var rs = this.sprResults[word-1];
  279.             rs[0] = time;
  280.             rs[1] = this.previousTime;
  281.         }
  282.         this.previousTime = time;
  283.     },*/
  284.  
  285.     processSprResults: function () {
  286.         var nonSpaceWords = [];
  287.         for (var i = 0; i < this.words.length; ++i) {
  288.             if ( this.words[i] != "\r" )
  289.                 nonSpaceWords.push(this.words[i]);
  290.         }
  291.  
  292.         for (var i = 0; i < nonSpaceWords.length; ++i) {
  293.             this.resultsLines.push([
  294.                 ["Word number", i+1],
  295.                 ["Word", csv_url_encode(nonSpaceWords[i])],
  296.                 ["Reading time", this.sprResults[i][0] - this.sprResults[i][1]],
  297.                 ["Newline?", (! this.display == "in place") &&
  298.                              boolToInt(((i+1) < this.wordOSpans.length) &&
  299.                              (this.wordOSpans[i].offset().top != this.wordOSpans[i+1].offset().top))],
  300.                 ["Sentence (or sentence MD5)", this.sentenceDesc]
  301.             ]);
  302.         }
  303.     }
  304. },
  305.  
  306. properties: {
  307.     obligatory: ["s"],
  308.     htmlDescription: function (opts) {
  309.         return $(document.createElement("div")).text(opts.s);
  310.     }
  311. }
  312. });
  313.  
Advertisement
Add Comment
Please, Sign In to add comment