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

TextToSpeechWidget::createButton()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 3
1
<?php
2
3
namespace luya\texttospeech;
4
5
use luya\base\Widget;
6
use luya\helpers\Json;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * layButtonSelector: '#play-button',
12
                    pauseButtonSelector: '#pause-button',
13
                    stopBauttonSelector: '#stop-button',
14
                    targetSelector: '.main-content',
15
                    text: '
16
 */
17
class TextToSpeechWidget extends Widget
18
{
19
    /**
20
     * @var string The jQuery selector which should be used to read the content from, examples:
21
     * - `.container`: All elements which class `.container` will be spoken.
22
     * - `#content`: The element with id `id="content"` only will be spoken.
23
     * 
24
     * The input data will be wrapped with `$()`.
25
     */
26
    public $targetSelector;
27
28
    /**
29
     * @var string If enabled, the {{$targetSelector}} attribute has no effect and only the text from the property will be read - on start.
30
     */
31
    public $text;
32
33
    public $playButtonSelector = '#play-button';
34
35
    public $pauseButtonSelector = '#pause-button';
36
37
    public $stopButtonSelector = '#stop-button';
38
39
    public $containerClass = 'text-to-speech-container';
40
41
    public $buttonClass = 'text-to-speech-button';
42
43
    /**
44
     * @var array|boolean You can either disable the default buttons by setting buttons to false, or you can provide an array with button configurations, each element requires the following keys:
45
     * - label:
46
     * - id:
47
     * - content:
48
     */
49
    public $buttons;
50
51
    public $playSVG = '<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" viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"><g><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,15v30c0,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-15C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/><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,30S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>';
52
53
    public $stopSVG = '<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" viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"><path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M44,44H16V16h28V44z"/><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>';
54
55
    public $pauseSVG = '<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" viewBox="0 0 202.205 202.205" style="enable-background:new 0 0 202.205 202.205;" xml:space="preserve"><g> <g> <path style="fill:#010002;" d="M23.483,202.205H85.83V0H23.483V202.205z M31.417,7.934h46.479v186.336H31.417V7.934z"/> <path style="fill:#010002;" d="M116.372,0v202.205h62.351V0H116.372z M170.788,194.271h-46.486V7.934h46.482v186.336H170.788z"/> </g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>';
56
    
57
    public function init()
58
    {
59
        if (!$this->text && !$this->targetSelector) {
60
            throw new InvalidConfigException("Either text or targetSelector property must be configured.");
61
        }
62
        if ($this->buttons === null) {
63
            $this->buttons[] = $this->createButton('Play', 'play-button', $this->playSVG);
64
            $this->buttons[] = $this->createButton('Pause', 'pause-button', $this->pauseSVG);
65
            $this->buttons[] = $this->createButton('Stop', 'stop-button', $this->stopSVG);
66
        }
67
    }
68
69
    public function run()
70
    {
71
        TextToSpeechAsset::register($this->view);
72
73
        $config = Json::htmlEncode([
74
            'playButtonSelector' => $this->playButtonSelector,
75
            'pauseButtonSelector' => $this->pauseButtonSelector,
76
            'stopButtonSelector' => $this->stopButtonSelector,
77
            'targetSelector' => $this->targetSelector,
78
            'text' => $this->text ? $this->text : '', // must be an empty string as it will checked with .length
79
            'language' => Yii::$app->composition->langShortCode,
80
        ]);
81
82
        $this->view->registerJs("$.textToSpeech({$config});");
83
84
        return $this->render('texttospeech', [
85
            'buttons' => $this->buttons,
86
            'id' => $this->getId(),
87
            'containerClass' => $this->containerClass,
88
            'buttonClass' => $this->buttonClass,
89
        ]);
90
    }
91
92
    public function createButton($label, $id, $svg)
93
    {
94
        return [
95
            'label' => $label,
96
            'id' => $id,
97
            'content' => $svg,
98
        ];
99
    }
100
}