Completed
Pull Request — master (#270)
by
unknown
03:25
created

InputMasked::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Entry;
6
use FML\Controls\Frame;
7
use FML\Controls\Quad;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class InputMasked extends AbstractUiElement implements Renderable, ScriptFeatureable
15
{
16
17
    const TYPE_DEFAULT = "Basic";
18
    const TYPE_PASSWORD = "Password";
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
    /**
25
     * @var string
26
     */
27
    protected $default;
28
29
    protected $textFormat = "Password";
30
31 View Code Duplication
    public function __construct($name, $default = "", $width = 30, $textFormat = "Password")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $this->name = $name;
34
        $this->default = $default;
35
        $this->width = $width;
36
        $this->setSize($width, 4);
37
        $this->textFormat = $textFormat;
38
    }
39
40
    /**
41
     * Render the XML element
42
     *
43
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
44
     * @return \DOMElement
45
     */
46
    public function render(\DOMDocument $domDocument)
47
    {
48
        $frame = new Frame();
49
        $frame->setPosition($this->posX, $this->posY)
50
            ->setSize($this->width, $this->height)
51
            ->addClasses(["uiContainer", "uiInputMasked"]);
52
53
        $quad = new Quad();
54
        $quad->setSize($this->width * 2, $this->height * 2)
55
            ->setScale(0.5)
56
            ->setPosition($this->width / 2, -$this->height / 2)
57
            ->setStyles('Bgs1', 'BgColorContour')
58
            ->setAlign("center", "center")
59
            ->setBackgroundColor('FFFA')
60
            ->addClass("uiInputMasked")
61
            ->setScriptEvents(true)
62
            ->setDataAttributes($this->_dataAttributes)->addClasses($this->_classes);
63
64
        $input = new Entry();
65
        $input->setSize($this->width - 4, $this->height)
66
            ->setPosition(0, -$this->height / 2)
67
            ->setDefault($this->default)
68
            ->setSelectText(true)
69
            ->setAlign("left", "center2")
70
            ->setAreaColor("0005")
71
            ->setAreaFocusColor('000a')
72
            ->setTextFormat($this->textFormat)
73
            ->addDataAttribute('type', 'Password')
74
            ->setName($this->name)
75
            ->setTextSize(1.5);
76
77
        $button = new Button("", Button::TYPE_DECORATED);
78
        $button->setSize(4, 4)
79
            ->addClass("uiMaskedToggle")
80
            ->setPosition($input->getWidth(), 0);
81
82
        $frame->addChild($button);
83
        $frame->addChild($quad);
84
        $frame->addChild($input);
85
86
87
        return $frame->render($domDocument);
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getName()
94
    {
95
        return $this->name;
96
    }
97
98
    /**
99
     * @param string $name
100
     * @return InputMasked
101
     */
102
    public function setName($name)
103
    {
104
        $this->name = $name;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getDefault()
113
    {
114
        return $this->default;
115
    }
116
117
    /**
118
     * @param string $default
119
     */
120
    public function setDefault($default)
121
    {
122
        $this->default = $default;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return integer
129
     */
130
    public function getWidth()
131
    {
132
        return $this->width;
133
    }
134
135
    /**
136
     * @param float $width
137
     */
138
    public function setWidth($width)
139
    {
140
        $this->width = $width;
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but $width is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
141
142
        return $this;
143
    }
144
145
    public function getHeight()
146
    {
147
        return $this->height + 2;
148
    }
149
150
    /**
151
     * Prepare the given Script for rendering by adding the needed Labels, etc.
152
     *
153
     * @param Script $script Script to prepare
154
     */
155
    public function prepare(Script $script)
156
    {
157
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
158
    }
159
160
    /**
161
     * Get the Script Features
162
     *
163
     * @return ScriptFeature[]
164
     */
165
    public function getScriptFeatures()
166
    {
167
        return ScriptFeature::collect($this);
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getTextFormat()
174
    {
175
        return $this->textFormat;
176
    }
177
178
    /**
179
     * @param string $textFormat
180
     * @return $this
181
     */
182
    public function setTextFormat($textFormat)
183
    {
184
        $this->textFormat = $textFormat;
185
186
        return $this;
187
    }
188
189
    protected function getScriptMouseClick()
190
    {
191
        return <<<EOL
192
             if (Event.Control.HasClass("uiInputMasked") ) {              
193
                 declare CMlFrame frame <=> Event.Control.Parent;
194
                 (frame.Controls[2] as CMlEntry).StartEdition();               
195
            }	
196
             if (Event.Control.HasClass("uiMaskedToggle") ) {              
197
                 declare CMlFrame frame <=> Event.Control.Parent.Parent;
198
                 declare CMlEntry input <=> (frame.Controls[2] as CMlEntry);
199
                 if (input.DataAttributeGet("type") == "Basic") {
200
                   input.DataAttributeSet("type", "Password");
201
                   input.TextFormat = CMlEntry::ETextFormat::Password;
202
                 } else {
203
                   input.DataAttributeSet("type", "Basic");
204
                   input.TextFormat = CMlEntry::ETextFormat::Basic;
205
                 }               
206
            }	
207
            				
208
EOL;
209
210
    }
211
}
212