Completed
Pull Request — master (#1993)
by Basil
02:42
created

TextToSpeechWidget::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\texttospeech;
4
5
use luya\base\Widget;
6
use luya\helpers\Json;
7
8
/**
9
 * layButtonSelector: '#play-button',
10
                    pauseButtonSelector: '#pause-button',
11
                    stopBauttonSelector: '#stop-button',
12
                    targetSelector: '.main-content',
13
                    text: '
14
 */
15
class TextToSpeechWidget extends Widget
16
{
17
    public $playButtonSelector = '#play-button';
18
19
    public $pauseButtonSelector = '#pause-button';
20
21
    public $stopButtonSelector = '#stop-button';
22
23
    public $targetSelector = '#main-container';
24
25
    public $containerClass = 'text-to-speech-container';
26
27
    public $buttonClass = 'text-to-speech-button';
28
    
29
    /**
30
     * @var string If enabled, the {{$targetSelector}} attribute has no effect and only the text from the property will be read - on start.
31
     */
32
    public $text;
33
34
    public $buttons;
35
36
    public $playSVG = <<<'PLAYSVG'
37
<?xml version="1.0" encoding="iso-8859-1"?>
38
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
39
<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"
40
	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve">
41
<g>
42
	<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
43
		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
44
		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/>
45
	<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
46
		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/>
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
</g>
78
</svg>
79
PLAYSVG;
80
81
    public $stopSVG = <<<'STOPSVG'
82
<?xml version="1.0" encoding="iso-8859-1"?>
83
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
84
<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"
85
	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve">
86
<g>
87
	<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
88
		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
89
		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/>
90
	<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
91
		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/>
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
</g>
121
<g>
122
</g>
123
</svg>
124
STOPSVG;
125
126
    public $pauseSVG = <<<'PAUSESVG'
127
<?xml version="1.0" encoding="iso-8859-1"?>
128
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
129
<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"
130
	 viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve">
131
<g>
132
	<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
133
		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
134
		C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/>
135
	<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
136
		S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/>
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
</g>
164
<g>
165
</g>
166
<g>
167
</g>
168
</svg>
169
PAUSESVG;
170
    
171
    public function init()
172
    {
173
        if ($this->buttons === null) {
174
            $this->buttons[] = $this->createButton('Play', 'play-button', $this->playSVG);
175
            $this->buttons[] = $this->createButton('Pause', 'pause-button', $this->pauseSVG);
176
            $this->buttons[] = $this->createButton('Stop', 'stop-button', $this->stopSVG);
177
        }
178
    }
179
180
    public function run()
181
    {
182
        TextToSpeechAsset::register($this->view);
183
184
        $config = Json::htmlEncode([
185
            'playButtonSelector' => $this->playButtonSelector,
186
            'pauseButtonSelector' => $this->pauseButtonSelector,
187
            'stopButtonSelector' => $this->stopButtonSelector,
188
            'targetSelect' => $this->targetSelector,
189
            'text' => $this->text ? $this->text : '', // must be an empty string as it will checked with .length
190
        ]);
191
192
        $this->view->registerJs("$.textToSpeech({$config});");
193
194
        return $this->render('texttospeech', [
195
            'buttons' => $this->buttons,
196
            'id' => $this->getId(),
197
            'containerClass' => $this->containerClass,
198
            'buttonClass' => $this->buttonClass,
199
        ]);
200
    }
201
202
    public function createButton($label, $id, $svg)
203
    {
204
        return [
205
            'label' => $label,
206
            'id' => $id,
207
            'svg' => $svg,
208
        ];
209
    }
210
}