Skip to content

Commit 48717dc

Browse files
author
hikki
committed
8
1 parent dbe2668 commit 48717dc

File tree

5 files changed

+146
-17
lines changed

5 files changed

+146
-17
lines changed

resources/assets/component.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@
323323
}
324324

325325
.dlp-dot-switch{display: flex}
326-
.dlp-dot-switch>button{border-radius: unset}
327-
.dlp-dot-switch>button:first-child{border-radius: 5px 0 0 5px}
328-
.dlp-dot-switch>button:last-child{border-radius: 0 5px 5px 0}
329-
.dlp-dot-switch>button:disabled{cursor: pointer}
326+
.dlp-dot-switch>div{border-radius: unset}
327+
.dlp-dot-switch>div:first-child{border-radius: 5px 0 0 5px}
328+
.dlp-dot-switch>div:last-child{border-radius: 0 5px 5px 0}
329+
.dlp-dot-switch>div:disabled{cursor: pointer}
330330

331331
.dlp-cascadeLine{
332332
position: relative!important;

resources/assets/component.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ window.ComponentDot = class {
533533
if (!select.hasOwnProperty(id)) continue;
534534
this.id_line_hash[id] = line;
535535
line++;
536-
let option = document.createElement('button');
536+
let option = document.createElement('div');
537537
option.setAttribute('data-id', id);
538538
option.className = `dlp-button${this._modSettings.color}`;
539539
option.textContent = select[id];
@@ -594,9 +594,8 @@ window.ComponentDot = class {
594594
return this;
595595
}
596596

597-
switchMod(settings = {cols:0,color:''}){
597+
switchMod(settings = {color:''}){
598598
this._modSettings = Object.assign({
599-
cols: 0,
600599
color:'',
601600
}, settings);
602601
this._modSettings.mode = 'switch';

src/Assembly/Unit/Checkbox.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
4+
namespace DLP\Assembly\Unit;
5+
6+
use DLP\Assembly\Abs\Widget;
7+
use DLP\Assembly\Layout\Swing;
8+
use DLP\Assembly\Wing;
9+
10+
/**
11+
* Class Checkbox
12+
* @package DLP\Assembly\Unit
13+
*/
14+
class Checkbox extends Widget
15+
{
16+
private $select;
17+
private $selected = '[]';
18+
private $limit = 1;
19+
private $useHiddenInput = true;
20+
private $color = '';
21+
private $swing = [];
22+
23+
public function __construct(string $column, array $select)
24+
{
25+
parent::__construct($column);
26+
$this->select = json_encode($select, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
27+
}
28+
29+
/**
30+
* @param array $data
31+
* @return $this
32+
*/
33+
public function selected(array $data)
34+
{
35+
$this->selected = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
36+
return $this;
37+
}
38+
39+
/**
40+
* @param int $num
41+
* @return $this
42+
*/
43+
public function limit(int $num)
44+
{
45+
$this->limit = $num;
46+
return $this;
47+
}
48+
49+
public function color($color)
50+
{
51+
switch ($color) {
52+
case "red":
53+
case "blue":
54+
case "green":
55+
case "yellow":
56+
$this->color = '-'.$color;
57+
break;
58+
}
59+
return $this;
60+
}
61+
62+
/**
63+
* @return $this
64+
*/
65+
public function withoutHiddenInput()
66+
{
67+
$this->useHiddenInput = false;
68+
return $this;
69+
}
70+
71+
/**
72+
* @param $condition integer | array
73+
* @param \Closure $closure
74+
* @return $this
75+
*/
76+
public function when($condition,\Closure $closure)
77+
{
78+
if(!is_array($condition)){
79+
$condition = [$condition];
80+
}
81+
$condition = join('.',$condition);
82+
$swing = new Swing($this->column);
83+
$this->wing->swing($swing,$closure);
84+
$this->swing[$condition] = (string)$swing;
85+
return $this;
86+
}
87+
88+
public function __toString()
89+
{
90+
$this->annotate();
91+
$execute = ".switchMod({color:'{$this->color}'})";
92+
if($this->useHiddenInput){
93+
$execute .= ".useHiddenInput('{$this->column}')";
94+
}
95+
if(!empty($this->swing)){
96+
$swing = json_encode($this->swing);
97+
$execute .= <<<EOF
98+
.trigger(function (select) {
99+
let swing = {$swing};
100+
if(document.querySelector('#dlp-swing-{$this->column}'))document.querySelector('#dlp-swing-{$this->column}').remove();
101+
if(swing[select.join('.')] !== undefined){
102+
let aim = document.querySelector('#{$this->column}');
103+
if(aim.parentNode.classList.contains("dlp-form-row")) aim = aim.parentNode;
104+
let fragment = document.createRange().createContextualFragment(swing[select.join('.')]);
105+
aim.parentNode.insertBefore(fragment, aim.nextSibling);
106+
}
107+
})
108+
EOF;
109+
}
110+
$execute .= '.make()';
111+
$content = <<<EOF
112+
<div id="{$this->column}" {$this->annotation}></div>
113+
<script>new ComponentDot("#{$this->column}",{$this->select}).selected({$this->selected}).limitNum({$this->limit}){$execute};</script>
114+
EOF;
115+
if(!$this->label) return $content;
116+
117+
return <<<EOF
118+
<div class="dlp dlp-form-row">
119+
<label class="dlp-text" for="{$this->column}">{$this->label}</label>
120+
{$content}
121+
</div>
122+
EOF;
123+
}
124+
}

src/Assembly/Unit/Select.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Select extends Widget
2121
private $direction = 'down';
2222
private $useSearch = false;
2323
private $useHiddenInput = true;
24-
private Wing $wing;
2524
private $swing = [];
2625

2726
public function __construct(string $column, array $select)
@@ -98,14 +97,6 @@ public function withoutHiddenInput()
9897
return $this;
9998
}
10099

101-
/**
102-
* @param Wing $wing
103-
*/
104-
public function setWing(Wing $wing)
105-
{
106-
$this->wing = $wing;
107-
}
108-
109100
/**
110101
* @param $condition integer | array
111102
* @param \Closure $closure
@@ -126,7 +117,7 @@ public function when($condition,\Closure $closure)
126117
public function __toString()
127118
{
128119
$this->annotate();
129-
$execute = ".mod({mode:true,placeholder: '{$this->placeholder}',height:'{$this->menuHeight}',direction:'{$this->direction}'})";
120+
$execute = ".menuMod({placeholder: '{$this->placeholder}',height:'{$this->menuHeight}',direction:'{$this->direction}'})";
130121
if($this->useSearch){
131122
$execute .= '.useSearch()';
132123
}

src/Assembly/Wing.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use DLP\Assembly\Unit\Button;
1111
use DLP\Assembly\Unit\CascadeDot;
1212
use DLP\Assembly\Unit\CascadeLine;
13+
use DLP\Assembly\Unit\Checkbox;
1314
use DLP\Assembly\Unit\Datetime;
1415
use DLP\Assembly\Unit\FileInput;
1516
use DLP\Assembly\Unit\Hidden;
@@ -97,6 +98,20 @@ public function select(string $column, array $select)
9798
return $doc;
9899
}
99100

101+
/**
102+
* @param string $column
103+
* @param array $select
104+
* @return Select
105+
*/
106+
public function checkbox(string $column, array $select)
107+
{
108+
$doc = new Checkbox($column, $select);
109+
$doc->setWing($this);
110+
$doc->setStyle(['width' => '240px']);
111+
$this->node->append($doc);
112+
return $doc;
113+
}
114+
100115
/**
101116
* @param string $column
102117
* @return Datetime

0 commit comments

Comments
 (0)