Completed
Pull Request — master (#124)
by De Cramer
02:35
created

uiInputMasked::setWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
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 uiInputMasked 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, 5);
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 - 5, $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(2);
76
77
        $button = new uiButton("", uiButton::TYPE_DECORATED);
78
        $button->setSize(5, 5)
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
     */
101
    public function setName($name)
102
    {
103
        $this->name = $name;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getDefault()
112
    {
113
        return $this->default;
114
    }
115
116
    /**
117
     * @param string $default
118
     */
119
    public function setDefault($default)
120
    {
121
        $this->default = $default;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return float
128
     */
129
    public function getWidth()
130
    {
131
        return $this->width;
132
    }
133
134
    /**
135
     * @param float $width
136
     */
137
    public function setWidth($width)
138
    {
139
        $this->width = $width;
140
141
        return $this;
142
    }
143
144
    public function getHeight()
145
    {
146
        return $this->height + 2;
147
    }
148
149
    /**
150
     * Prepare the given Script for rendering by adding the needed Labels, etc.
151
     *
152
     * @param Script $script Script to prepare
153
     */
154
    public function prepare(Script $script)
155
    {
156
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
157
    }
158
159
    /**
160
     * Get the Script Features
161
     *
162
     * @return ScriptFeature[]
163
     */
164
    public function getScriptFeatures()
165
    {
166
        return ScriptFeature::collect($this);
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getTextFormat()
173
    {
174
        return $this->textFormat;
175
    }
176
177
    /**
178
     * @param string $textFormat
179
     * @return $this
180
     */
181
    public function setTextFormat($textFormat)
182
    {
183
        $this->textFormat = $textFormat;
184
185
        return $this;
186
    }
187
188
    protected function getScriptMouseClick()
189
    {
190
        return <<<EOL
191
             if (Event.Control.HasClass("uiInputMasked") ) {              
192
                 declare CMlFrame frame <=> Event.Control.Parent;
193
                 (frame.Controls[2] as CMlEntry).StartEdition();               
194
            }	
195
             if (Event.Control.HasClass("uiMaskedToggle") ) {              
196
                 declare CMlFrame frame <=> Event.Control.Parent.Parent;
197
                 declare CMlEntry input <=> (frame.Controls[2] as CMlEntry);
198
                 if (input.DataAttributeGet("type") == "Basic") {
199
                   input.DataAttributeSet("type", "Password");
200
                   input.TextFormat = CMlEntry::ETextFormat::Password;
201
                 } else {
202
                   input.DataAttributeSet("type", "Basic");
203
                   input.TextFormat = CMlEntry::ETextFormat::Basic;
204
                 }               
205
            }	
206
            				
207
EOL;
208
209
    }
210
}
211