|
4 | 4 |
|
5 | 5 | class Window |
6 | 6 | { |
| 7 | + private $trigger; |
7 | 8 | private $content; |
| 9 | + private $options = ['width' => '0.8', 'height' => '0.8']; |
8 | 10 |
|
9 | | - public function content() |
| 11 | + public function __construct(array $xhr = ['url' => '', 'method' => 'POST']) |
10 | 12 | { |
| 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 | + } |
11 | 32 |
|
| 33 | + /** |
| 34 | + * @param array $options |
| 35 | + */ |
| 36 | + public function options(array $options) |
| 37 | + { |
| 38 | + $this->options = array_merge($this->options, $options); |
12 | 39 | } |
13 | 40 |
|
14 | | - public function compile() |
| 41 | + /** |
| 42 | + * @param string|array $data |
| 43 | + */ |
| 44 | + public function content($data) |
15 | 45 | { |
| 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 | + } |
16 | 62 |
|
| 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); |
17 | 71 | } |
18 | 72 | } |
0 commit comments