Skip to content

Commit b1e0da8

Browse files
author
hikki
committed
7
1 parent db7d68a commit b1e0da8

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

src/Layer/Window.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,69 @@
44

55
class Window
66
{
7+
private $trigger;
78
private $content;
9+
private $options = ['width' => '0.8', 'height' => '0.8'];
810

9-
public function content()
11+
public function __construct(array $xhr = ['url' => '', 'method' => 'POST'])
1012
{
13+
$this->xhr = json_encode(array_merge($this->xhr, $xhr));
14+
}
15+
16+
/**
17+
* @param $selector
18+
* @param string $event
19+
* @return $this
20+
*/
21+
public function trigger($selector, $event = 'click')
22+
{
23+
$this->trigger = <<<EOF
24+
if(document.querySelector('$selector')){
25+
document.querySelector('$selector').addEventListener('$event', function () {
26+
%s
27+
});
28+
}
29+
EOF;
30+
return $this;
31+
}
1132

33+
/**
34+
* @param array $options
35+
*/
36+
public function options(array $options)
37+
{
38+
$this->options = array_merge($this->options, $options);
1239
}
1340

14-
public function compile()
41+
/**
42+
* @param string|array $data
43+
*/
44+
public function content($data)
1545
{
46+
$this->content = $data;
47+
if(is_string($data)){
48+
$this->content = <<<EOF
49+
function(){
50+
let window = document.createElement('div');
51+
let fragment = document.createRange().createContextualFragment(response);
52+
window.appendChild(fragment);
53+
return window;
54+
}()
55+
EOF;
56+
return;
57+
}
58+
$xhr = array_merge(['url'=>'','method'=>'','data'=>[],'callback'=>'null'],$data);
59+
$data = json_encode($xhr['data']);
60+
$this->content = "{url:'{$xhr['url']}',method:'{$xhr['method']}',data:{$data},callback:{$xhr['callback']}}";
61+
}
1662

63+
public function __toString()
64+
{
65+
$this->options = json_encode($this->options);
66+
$content = <<<EOF
67+
new ComponentPlane({$this->content},$this->options).make();
68+
EOF;
69+
if (!$this->trigger) return $content;
70+
return sprintf($this->trigger, $content);
1771
}
1872
}

src/Widget/PlaneAction/HeadPosAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct($title, $url, $xhr, $options)
1919
unset($xhr['callback']);
2020
$xhr = json_encode($xhr);
2121
$options = json_encode($options);
22-
$this->document_id = substr(md5($title . $url), 16);
22+
$this->document_id = "button_".substr(md5($this->title.microtime().mt_rand(0,10000)),16);
2323

2424
$this->binding = ".bindRequest('button[type=\"submit\"]','click',XHR)";
2525
$this->url = $url;
@@ -30,7 +30,7 @@ public function __construct($title, $url, $xhr, $options)
3030

3131
public function bindEvent($bind)
3232
{
33-
$bind = array_merge(['selector'=>'','event'=>'click','params'=>'{}'],$bind);
33+
$bind = array_merge(['selector'=>'','event'=>'click','params'=>[]],$bind);
3434
$params = json_encode($bind['params']);
3535
$this->binding = ".bindEvent('{$bind['selector']}','click',{$bind['event']}, {$params})";
3636
return $this;

src/Widget/PlaneAction/RowPosAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct($title, $url, $xhr, $options)
1919
unset($xhr['callback']);
2020
$xhr = json_encode($xhr);
2121
$options = json_encode($options);
22-
$this->document_class = substr(md5($title . $url), 16);
22+
$this->document_class = "button_".substr(md5($this->title.microtime().mt_rand(0,10000)),16);
2323

2424
$this->binding = ".bindRequest('button[type=\"submit\"]','click',XHR)";
2525
$this->url = $url;
@@ -30,7 +30,7 @@ public function __construct($title, $url, $xhr, $options)
3030

3131
public function bindEvent($bind)
3232
{
33-
$bind = array_merge(['selector'=>'','event'=>'click','params'=>'{}'],$bind);
33+
$bind = array_merge(['selector'=>'','event'=>'click','params'=>[]],$bind);
3434
$params = json_encode($bind['params']);
3535
$this->binding = ".bindEvent('{$bind['selector']}','click',{$bind['event']}, {$params})";
3636
return $this;

0 commit comments

Comments
 (0)