Skip to content

Commit e98a9bc

Browse files
author
hikki
committed
7
1 parent d3caeca commit e98a9bc

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

src/Assembly/Abs/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function range(int $min,int $max)
133133
return $this;
134134
}
135135

136-
public function annotate()
136+
protected function annotate()
137137
{
138138
$style = Assistant::arrayKv2String($this->style);
139139
$attribute = Assistant::arrayKv2String($this->attribute,'=',' ');

src/Assembly/Unit/Button.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,88 @@
33

44
namespace DLP\Assembly\Unit;
55

6-
use DLP\Assembly\Abs\Component;
6+
use DLP\Assembly\Abs\Input;
7+
use DLP\Tool\Assistant;
78

89
/**
910
* Class Button
1011
* @package DLP\Assembly\Unit
1112
*/
12-
class Button implements Component
13+
class Button extends Input
1314
{
14-
private $column;
15-
private $label;
16-
private $content;
15+
private $domId;
16+
private $title;
17+
private $trigger;
1718

18-
public function __construct(string $column,string $content)
19+
public function __construct(string $title)
1920
{
20-
$this->column = $column;
21-
$this->content = $content;
21+
$this->title = $title;
22+
$this->domId = "button_".substr(md5($this->title.microtime().mt_rand(0,10000)),16);
2223
}
2324

2425
/**
25-
* @param $title
26-
* @return $this
26+
* @param array $xhr
27+
* @param string $formSelector
2728
*/
28-
public function label($title)
29+
public function bindRequest($xhr = ['url'=>'','method'=>'','data'=>[],'callback'=>'null'],$formSelector = '')
2930
{
30-
$this->label = $title;
31+
$xhr = array_merge(['url'=>'','method'=>'','data'=>[],'callback'=>'null'],$xhr);
32+
$data = json_encode($xhr['data']);
33+
$form = '';
34+
if($formSelector !== ''){
35+
$form = <<<EOF
36+
let form = document.querySelector('{$formSelector}');
37+
let formdata = new FormData(form);
38+
let flag = false;
39+
for (let pair of formdata.entries()) {
40+
let key = pair[0];
41+
let val = pair[1];
42+
let input;
43+
try {
44+
input = form.querySelector(`[name="`+key+`"]`);
45+
} catch (e) {
46+
continue;
47+
}
48+
if (input.hasAttribute('required') && input.value === '') {
49+
flag = true;
50+
input.focus();
51+
}
52+
if (/\[.*\]/.test(key) && /^\[.*\]$/.test(val) && (typeof val === 'string')) {
53+
val = JSON.parse(val);
54+
if (Array.isArray(val) && val.length > 0) {
55+
val.forEach((v) => {
56+
formdata.append(key+`[]`, v);
57+
});
58+
} else {
59+
formdata.append(key, '');
60+
}
61+
}
62+
}
63+
if (flag) return;
64+
if(typeof xhr.data === 'object' && Object.keys(xhr.data).length !== 0){
65+
for (let k in xhr.data){
66+
if(xhr.data.hasOwnProperty(k)) formdata.append(k,xhr.data[k]);
67+
}
68+
}
69+
EOF;
70+
}
71+
$this->trigger = <<<EOF
72+
document.querySelector('#{$this->domId}').addEventListener('click', function (e) {
73+
let xhr = {url:'{$xhr['url']}',method:'{$xhr['method']}',data:{$data},callback:{$xhr['callback']}};
74+
{$form}
75+
e.target.setAttribute('disabled','disabled');
76+
_component.request(xhr);
77+
});
78+
EOF;
3179
return $this;
3280
}
3381

3482
public function __toString()
3583
{
36-
if(!$this->label) return $this->content;
37-
38-
return <<<EOF
39-
<div class="dlp dlp-form-row">
40-
<label class="dlp-text" for="{$this->column}">{$this->label}</label>{$this->content}
41-
</div>
84+
$content = <<<EOF
85+
<button type="{$this->type}" id="{$this->domId}" class="dlp dlp-button" {$this->annotation}>{$this->title}</button>
86+
<script>{$this->trigger}</script>
4287
EOF;
88+
return $content;
4389
}
4490
}

src/Assembly/Wing.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DLP\Assembly\Abs\Layout;
88
use DLP\Assembly\Layout\Section;
99
use DLP\Assembly\Layout\Swing;
10+
use DLP\Assembly\Unit\Button;
1011
use DLP\Assembly\Unit\CascadeDot;
1112
use DLP\Assembly\Unit\CascadeLine;
1213
use DLP\Assembly\Unit\Datetime;
@@ -183,6 +184,18 @@ public function linear(string $column, array $columnSetting)
183184
return $doc;
184185
}
185186

187+
/**
188+
* @param $title
189+
* @return Button
190+
*/
191+
public function button($title)
192+
{
193+
$doc = new Button($title);
194+
$doc->setType('button');
195+
$this->node->append($doc);
196+
return $doc;
197+
}
198+
186199
/**
187200
* @param array $attributes
188201
* @return $this

src/Layer/Dialog.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@ public function __construct(array $xhr = ['url' => '', 'method' => 'POST'])
1515
$this->xhr = json_encode(array_merge($this->xhr, $xhr));
1616
}
1717

18-
/**
19-
* @param $selector
20-
* @param string $event
21-
* @return $this
22-
*/
23-
public function trigger($selector, $event = 'click')
24-
{
25-
$this->trigger = <<<EOF
26-
if(document.querySelector('$selector')){
27-
document.querySelector('$selector').addEventListener('$event', function () {
28-
%s
29-
});
30-
}
31-
EOF;
32-
return $this;
33-
}
34-
3518
/**
3619
* @param array $options
3720
*/

0 commit comments

Comments
 (0)