Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsSelectToInput
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 9
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSourceId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTargetId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTargetId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlAndPrepareJS
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getButton
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 injectJs
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types=1 );
3
4namespace MediaWiki\Extension\Translate\Utilities;
5
6use RequestContext;
7use RuntimeException;
8use Xml;
9use XmlSelect;
10
11/**
12 * Code for JavaScript enhanced \<option> selectors.
13 * @author Niklas Laxström
14 * @license GPL-2.0-or-later
15 */
16class JsSelectToInput {
17    /// Id of the text field where stuff is appended
18    protected $targetId;
19    /// Id of the \<option> field
20    protected $sourceId;
21    /** @var XmlSelect */
22    protected $select;
23    /// Id on the button
24    protected $buttonId;
25    /** @var string Text for the append button */
26    protected $msg = 'translate-jssti-add';
27
28    public function __construct( XmlSelect $select ) {
29        $this->select = $select;
30    }
31
32    public function getSourceId(): string {
33        return $this->sourceId;
34    }
35
36    public function setTargetId( string $id ) {
37        $this->targetId = $id;
38    }
39
40    public function getTargetId(): string {
41        return $this->targetId;
42    }
43
44    /** Set the message key. */
45    public function setMessage( string $message ): void {
46        $this->msg = $message;
47    }
48
49    /** @return string a message key. */
50    public function getMessage(): string {
51        return $this->msg;
52    }
53
54    /**
55     * Returns the whole input element and injects needed JavaScript
56     * @return string Html code.
57     */
58    public function getHtmlAndPrepareJS(): string {
59        $this->sourceId = $this->select->getAttribute( 'id' );
60
61        if ( !is_string( $this->sourceId ) ) {
62            throw new RuntimeException( 'ID needs to be specified for the selector' );
63        }
64
65        self::injectJs();
66        $html = $this->select->getHTML();
67        $html .= $this->getButton( $this->msg, $this->sourceId, $this->targetId );
68
69        return $html;
70    }
71
72    /**
73     * Constructs the append button.
74     * @param string $msg Message key.
75     * @param string $source Html id.
76     * @param string $target Html id.
77     * @return string
78     */
79    protected function getButton( string $msg, string $source, string $target ): string {
80        $html = Xml::element( 'input', [
81            'type' => 'button',
82            'value' => wfMessage( $msg )->text(),
83            'class' => 'mw-translate-jssti',
84            'data-translate-jssti-sourceid' => $source,
85            'data-translate-jssti-targetid' => $target
86        ] );
87
88        return $html;
89    }
90
91    /** Inject needed JavaScript in the page. */
92    public static function injectJs(): void {
93        static $done = false;
94        if ( $done ) {
95            return;
96        }
97
98        RequestContext::getMain()->getOutput()->addModules( 'ext.translate.selecttoinput' );
99    }
100}