| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace luya\texttospeech; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use luya\base\Widget; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use luya\helpers\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\base\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * layButtonSelector: '#play-button', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |                     pauseButtonSelector: '#pause-button', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |                     stopBauttonSelector: '#stop-button', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |                     targetSelector: '.main-content', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |                     text: ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class TextToSpeechWidget extends Widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $playButtonSelector = '#play-button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $pauseButtonSelector = '#pause-button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public $stopButtonSelector = '#stop-button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $targetSelector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public $containerClass = 'text-to-speech-container'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public $buttonClass = 'text-to-speech-button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string If enabled, the {{$targetSelector}} attribute has no effect and only the text from the property will be read - on start. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public $buttons; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public $playSVG = <<<'PLAYSVG' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	<path d="M45.563,29.174l-22-15c-0.307-0.208-0.703-0.231-1.031-0.058C22.205,14.289,22,14.629,22,15v30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		c0,0.371,0.205,0.711,0.533,0.884C22.679,45.962,22.84,46,23,46c0.197,0,0.394-0.059,0.563-0.174l22-15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	<path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M30,58C14.561,58,2,45.439,2,30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | </svg> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | PLAYSVG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public $stopSVG = <<<'STOPSVG' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	<path d="M45.563,29.174l-22-15c-0.307-0.208-0.703-0.231-1.031-0.058C22.205,14.289,22,14.629,22,15v30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		c0,0.371,0.205,0.711,0.533,0.884C22.679,45.962,22.84,46,23,46c0.197,0,0.394-0.059,0.563-0.174l22-15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	<path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M30,58C14.561,58,2,45.439,2,30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | </svg> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | STOPSVG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public $pauseSVG = <<<'PAUSESVG' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	<path d="M45.563,29.174l-22-15c-0.307-0.208-0.703-0.231-1.031-0.058C22.205,14.289,22,14.629,22,15v30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		c0,0.371,0.205,0.711,0.533,0.884C22.679,45.962,22.84,46,23,46c0.197,0,0.394-0.059,0.563-0.174l22-15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	<path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M30,58C14.561,58,2,45.439,2,30 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | <g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | </g> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | </svg> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | PAUSESVG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         if (!$this->text && !$this->targetSelector) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             throw new InvalidConfigException("Either text or targetSelector property must be configured."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         if ($this->buttons === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $this->buttons[] = $this->createButton('Play', 'play-button', $this->playSVG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $this->buttons[] = $this->createButton('Pause', 'pause-button', $this->pauseSVG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $this->buttons[] = $this->createButton('Stop', 'stop-button', $this->stopSVG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         TextToSpeechAsset::register($this->view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $config = Json::htmlEncode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             'playButtonSelector' => $this->playButtonSelector, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             'pauseButtonSelector' => $this->pauseButtonSelector, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             'stopButtonSelector' => $this->stopButtonSelector, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             'targetSelector' => $this->targetSelector, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             'text' => $this->text ? $this->text : '', // must be an empty string as it will checked with .length | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $this->view->registerJs("$.textToSpeech({$config});"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         return $this->render('texttospeech', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             'buttons' => $this->buttons, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             'id' => $this->getId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             'containerClass' => $this->containerClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             'buttonClass' => $this->buttonClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |     public function createButton($label, $id, $svg) | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |         return [ | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |             'label' => $label, | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |             'id' => $id, | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |             'svg' => $svg, | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  | } |