forked from NativePHP/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialog.php
149 lines (111 loc) · 3.05 KB
/
Dialog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Native\Laravel;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Native\Laravel\Client\Client;
use Native\Laravel\Facades\Window;
class Dialog
{
use Conditionable;
use Macroable;
protected $title;
protected $defaultPath;
protected $buttonLabel = 'Select';
protected $properties = [
'openFile',
];
protected $filters = [];
protected $windowReference;
public function __construct(protected Client $client) {}
public static function new()
{
return new static(new Client);
}
public function title(string $title): self
{
$this->title = $title;
return $this;
}
public function defaultPath(string $defaultPath): self
{
$this->defaultPath = $defaultPath;
return $this;
}
public function button(string $buttonLabel): self
{
$this->buttonLabel = $buttonLabel;
return $this;
}
public function multiple()
{
$this->properties[] = 'multiSelections';
return $this;
}
public function withHiddenFiles()
{
$this->properties[] = 'showHiddenFiles';
return $this;
}
public function files()
{
$this->properties = array_diff($this->properties, ['openDirectory']);
$this->properties = ['openFile'];
return $this;
}
public function folders()
{
$this->properties = array_diff($this->properties, ['openFile']);
$this->properties[] = 'openDirectory';
return $this;
}
public function dontResolveSymlinks(): self
{
$this->properties[] = 'noResolveAliases';
return $this;
}
public function filter(string $name, array $extensions): self
{
$this->filters[] = [
'name' => $name,
'extensions' => $extensions,
];
return $this;
}
public function properties(array $properties): self
{
$this->properties = $properties;
return $this;
}
public function asSheet(?string $windowId = null): self
{
if (is_null($windowId)) {
$this->windowReference = Window::current()->id;
} else {
$this->windowReference = $windowId;
}
return $this;
}
public function open()
{
$result = $this->client->post('dialog/open', $this->dialogData())->json('result');
if (! in_array('multiSelections', $this->properties)) {
return $result[0] ?? null;
}
return $result;
}
public function save()
{
return $this->client->post('dialog/save', $this->dialogData())->json('result');
}
public function dialogData(): array
{
return [
'title' => $this->title,
'windowReference' => $this->windowReference,
'defaultPath' => $this->defaultPath,
'filters' => $this->filters,
'buttonLabel' => $this->buttonLabel,
'properties' => array_unique($this->properties),
];
}
}