| 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 | 1× 1× 1× 1× 4× 4× 4× 4× 3× 3× 1× 1× 2× 1× 1× 1× 1× 1× 1× 1× 1× 3× 2× 3× 1× 1× 1× 1× | /* Copyright 2013-2015 OCAD University Licensed under the Educational Community License (ECL), Version 2.0 or the New BSD license. You may not use this file except in compliance with one these Licenses. You may obtain a copy of the ECL 2.0 License and BSD License at https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt */ var fluid_3_0_0 = fluid_3_0_0 || {}; (function ($, fluid) { "use strict"; /******************************************************************************* * speak * * An enactor that is capable of speaking text. * Typically this will be used as a base grade to an enactor that supplies * the text to be spoken. *******************************************************************************/ fluid.defaults("fluid.prefs.enactor.speak", { gradeNames: "fluid.prefs.enactor", preferenceMap: { "fluid.prefs.speak": { "model.enabled": "default" } }, components: { tts: { type: "fluid.textToSpeech", options: { model: "{speak}.model", invokers: { queueSpeech: { funcName: "fluid.prefs.enactor.speak.queueSpeech", args: ["{that}", "fluid.textToSpeech.queueSpeech", "{arguments}.0", "{arguments}.1", "{arguments}.2"] } } } } } }); // Accepts a speechFn (either a function or function name), which will be used to perform the // underlying queuing of the speech. This allows the SpeechSynthesis to be replaced (e.g. for testing) fluid.prefs.enactor.speak.queueSpeech = function (that, speechFn, text, interrupt, options) { // force a string value var str = text.toString(); // remove extra whitespace str = str.trim(); str.replace(/\s{2,}/gi, " "); if (that.model.enabled && str) { Iif (typeof(speechFn) === "string") { fluid.invokeGlobalFunction(speechFn, [that, str, interrupt, options]); } else { speechFn(that, str, interrupt, options); } } }; /******************************************************************************* * selfVoicing * * The enactor that enables self voicing of an entire page *******************************************************************************/ fluid.defaults("fluid.prefs.enactor.selfVoicing", { gradeNames: ["fluid.prefs.enactor.speak", "fluid.viewComponent"], modelListeners: { "enabled": { listener: "{that}.handleSelfVoicing", args: ["{change}.value"] } }, invokers: { handleSelfVoicing: { funcName: "fluid.prefs.enactor.selfVoicing.handleSelfVoicing", // Pass in invokers to force them to be resolved args: ["{that}.options.strings.welcomeMsg", "{tts}.queueSpeech", "{that}.readFromDOM", "{tts}.cancel", "{arguments}.0"] }, readFromDOM: { funcName: "fluid.prefs.enactor.selfVoicing.readFromDOM", args: ["{that}", "{that}.container"] } }, strings: { welcomeMsg: "text to speech enabled" } }); fluid.prefs.enactor.selfVoicing.handleSelfVoicing = function (welcomeMsg, queueSpeech, readFromDOM, cancel, enabled) { if (enabled) { queueSpeech(welcomeMsg, true); readFromDOM(); } else { cancel(); } }; // Constants representing DOM node types. fluid.prefs.enactor.selfVoicing.nodeType = { ELEMENT_NODE: 1, TEXT_NODE: 3 }; // TODO: Currently only reads text nodes and alt text. // This should be expanded to read other text descriptors as well. fluid.prefs.enactor.selfVoicing.readFromDOM = function (that, elm) { elm = $(elm); var nodes = elm.contents(); fluid.each(nodes, function (node) { if (node.nodeType === fluid.prefs.enactor.selfVoicing.nodeType.TEXT_NODE && node.nodeValue) { that.tts.queueSpeech(node.nodeValue); } if (node.nodeType === fluid.prefs.enactor.selfVoicing.nodeType.ELEMENT_NODE && window.getComputedStyle(node).display !== "none") { Eif (node.nodeName === "IMG") { var altText = node.getAttribute("alt"); Eif (altText) { that.tts.queueSpeech(altText); } } else { fluid.prefs.enactor.selfVoicing.readFromDOM(that, node); } } }); }; })(jQuery, fluid_3_0_0); |