|
3 | 3 |
|
4 | 4 | namespace DLP\Assembly\Unit; |
5 | 5 |
|
6 | | -use DLP\Assembly\Abs\Component; |
| 6 | +use DLP\Assembly\Abs\Input; |
| 7 | +use DLP\Tool\Assistant; |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * Class Button |
10 | 11 | * @package DLP\Assembly\Unit |
11 | 12 | */ |
12 | | -class Button implements Component |
| 13 | +class Button extends Input |
13 | 14 | { |
14 | | - private $column; |
15 | | - private $label; |
16 | | - private $content; |
| 15 | + private $domId; |
| 16 | + private $title; |
| 17 | + private $trigger; |
17 | 18 |
|
18 | | - public function __construct(string $column,string $content) |
| 19 | + public function __construct(string $title) |
19 | 20 | { |
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); |
22 | 23 | } |
23 | 24 |
|
24 | 25 | /** |
25 | | - * @param $title |
26 | | - * @return $this |
| 26 | + * @param array $xhr |
| 27 | + * @param string $formSelector |
27 | 28 | */ |
28 | | - public function label($title) |
| 29 | + public function bindRequest($xhr = ['url'=>'','method'=>'','data'=>[],'callback'=>'null'],$formSelector = '') |
29 | 30 | { |
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; |
31 | 79 | return $this; |
32 | 80 | } |
33 | 81 |
|
34 | 82 | public function __toString() |
35 | 83 | { |
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> |
42 | 87 | EOF; |
| 88 | + return $content; |
43 | 89 | } |
44 | 90 | } |
0 commit comments