1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
|
8
|
|
|
class Button extends Fluent implements Arrayable |
9
|
|
|
{ |
10
|
|
|
use HasAuthorizations; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Make a new button instance. |
14
|
|
|
* |
15
|
|
|
* @param string|array $options |
16
|
|
|
* @return static |
17
|
|
|
*/ |
18
|
|
|
public static function make($options = []) |
19
|
|
|
{ |
20
|
|
|
if (is_string($options)) { |
21
|
|
|
return new static(['extend' => $options]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return new static($options); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Make a raw button that does not extend anything. |
29
|
|
|
* |
30
|
|
|
* @param array $options |
31
|
|
|
* @return static |
32
|
|
|
*/ |
33
|
|
|
public static function raw($options = []) |
34
|
|
|
{ |
35
|
|
|
if (is_string($options)) { |
36
|
|
|
return new static(['text' => $options]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return new static($options); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set attr option value. |
44
|
|
|
* |
45
|
|
|
* @param array $value |
46
|
|
|
* @return $this |
47
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.attr |
48
|
|
|
*/ |
49
|
|
|
public function attr(array $value) |
50
|
|
|
{ |
51
|
|
|
$this->attributes['attr'] = $value; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set available option value. |
58
|
|
|
* |
59
|
|
|
* @param string $value |
60
|
|
|
* @return $this |
61
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.available |
62
|
|
|
*/ |
63
|
|
|
public function available($value) |
64
|
|
|
{ |
65
|
|
|
if ($this->isFunction($value)) { |
66
|
|
|
$this->attributes['available'] = $value; |
67
|
|
|
} else { |
68
|
|
|
$this->attributes['available'] = "function(dt, config) { $value }"; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if a given value is a function. |
76
|
|
|
* |
77
|
|
|
* @param string $value |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
protected function isFunction($value) |
81
|
|
|
{ |
82
|
|
|
return substr($value, 0, 8) == 'function'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set enabled option value. |
87
|
|
|
* |
88
|
|
|
* @param bool $value |
89
|
|
|
* @return $this |
90
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.enabled |
91
|
|
|
*/ |
92
|
|
|
public function enabled($value = true) |
93
|
|
|
{ |
94
|
|
|
$this->attributes['enabled'] = $value; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set init option value. |
101
|
|
|
* |
102
|
|
|
* @param string $value |
103
|
|
|
* @return $this |
104
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.init |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function init($value) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if ($this->isFunction($value)) { |
109
|
|
|
$this->attributes['init'] = $value; |
110
|
|
|
} else { |
111
|
|
|
$this->attributes['init'] = "function(dt, node, config) { $value }"; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set key option value. |
119
|
|
|
* |
120
|
|
|
* @param string|array $value |
121
|
|
|
* @return $this |
122
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.key |
123
|
|
|
*/ |
124
|
|
|
public function key($value) |
125
|
|
|
{ |
126
|
|
|
$this->attributes['key'] = $value; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set extend option value. |
133
|
|
|
* |
134
|
|
|
* @param string $value |
135
|
|
|
* @return $this |
136
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.extend |
137
|
|
|
*/ |
138
|
|
|
public function extend($value) |
139
|
|
|
{ |
140
|
|
|
$this->attributes['extend'] = $value; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set editor option value. |
147
|
|
|
* |
148
|
|
|
* @param string $value |
149
|
|
|
* @return $this |
150
|
|
|
* @see https://editor.datatables.net/reference/button |
151
|
|
|
*/ |
152
|
|
|
public function editor($value) |
153
|
|
|
{ |
154
|
|
|
$this->attributes['editor'] = $value; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set buttons option value. |
161
|
|
|
* |
162
|
|
|
* @param array $buttons |
163
|
|
|
* @return $this |
164
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function buttons(array $buttons) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
foreach ($buttons as $key => $button) { |
169
|
|
|
if ($button instanceof Arrayable) { |
170
|
|
|
$buttons[$key] = $button->toArray(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->attributes['buttons'] = $buttons; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $buttons |
181
|
|
|
* @return $this |
182
|
|
|
* @see https://editor.datatables.net/examples/api/cancelButton |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function formButtons(array $buttons) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
foreach ($buttons as $key => $button) { |
187
|
|
|
if ($button instanceof Arrayable) { |
188
|
|
|
$buttons[$key] = $button->toArray(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->attributes['formButtons'] = $buttons; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param mixed $message |
199
|
|
|
* @return $this |
200
|
|
|
* @see https://editor.datatables.net/examples/api/removeMessage |
201
|
|
|
* @see https://editor.datatables.net/reference/button/create |
202
|
|
|
* @see https://editor.datatables.net/reference/button/edit |
203
|
|
|
* @see https://editor.datatables.net/reference/button/remove |
204
|
|
|
*/ |
205
|
|
|
public function formMessage($message) |
206
|
|
|
{ |
207
|
|
|
$this->attributes['formMessage'] = $message; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param mixed $title |
214
|
|
|
* @return $this |
215
|
|
|
* @see https://editor.datatables.net/reference/button/create |
216
|
|
|
* @see https://editor.datatables.net/reference/button/edit |
217
|
|
|
* @see https://editor.datatables.net/reference/button/remove |
218
|
|
|
*/ |
219
|
|
|
public function formTitle($title) |
220
|
|
|
{ |
221
|
|
|
$this->attributes['formTitle'] = $title; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Set className option value. |
228
|
|
|
* |
229
|
|
|
* @param string $value |
230
|
|
|
* @return $this |
231
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.className |
232
|
|
|
*/ |
233
|
|
|
public function className($value) |
234
|
|
|
{ |
235
|
|
|
$this->attributes['className'] = $value; |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set destroy option value. |
242
|
|
|
* |
243
|
|
|
* @param string $value |
244
|
|
|
* @return $this |
245
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.destroy |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function destroy($value) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
if ($this->isFunction($value)) { |
250
|
|
|
$this->attributes['destroy'] = $value; |
251
|
|
|
} else { |
252
|
|
|
$this->attributes['destroy'] = "function(dt, node, config) { $value }"; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set customize option value. |
260
|
|
|
* |
261
|
|
|
* @param string $value |
262
|
|
|
* @return $this |
263
|
|
|
* @see https://datatables.net/reference/button/excelHtml5 |
264
|
|
|
*/ |
265
|
|
|
public function customize($value) |
266
|
|
|
{ |
267
|
|
|
$this->attributes['customize'] = $value; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Append a class name to column. |
274
|
|
|
* |
275
|
|
|
* @param string $class |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
View Code Duplication |
public function addClass($class) |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
if (! isset($this->attributes['className'])) { |
281
|
|
|
$this->attributes['className'] = $class; |
282
|
|
|
} else { |
283
|
|
|
$this->attributes['className'] .= " $class"; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Set text option value. |
291
|
|
|
* |
292
|
|
|
* @param string $value |
293
|
|
|
* @return $this |
294
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.text |
295
|
|
|
*/ |
296
|
|
|
public function text($value) |
297
|
|
|
{ |
298
|
|
|
$this->attributes['text'] = $value; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Set titleAttr option value. |
305
|
|
|
* |
306
|
|
|
* @param string $value |
307
|
|
|
* @return $this |
308
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.titleAttr |
309
|
|
|
*/ |
310
|
|
|
public function titleAttr($value) |
311
|
|
|
{ |
312
|
|
|
$this->attributes['titleAttr'] = $value; |
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Set name option value. |
319
|
|
|
* |
320
|
|
|
* @param string $value |
321
|
|
|
* @return $this |
322
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.name |
323
|
|
|
*/ |
324
|
|
|
public function name($value) |
325
|
|
|
{ |
326
|
|
|
$this->attributes['name'] = $value; |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Set namespace option value. |
333
|
|
|
* |
334
|
|
|
* @param string $value |
335
|
|
|
* @return $this |
336
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.namespace |
337
|
|
|
*/ |
338
|
|
|
public function namespace($value) |
339
|
|
|
{ |
340
|
|
|
$this->attributes['namespace'] = $value; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Set tag option value. |
347
|
|
|
* |
348
|
|
|
* @param string $value |
349
|
|
|
* @return $this |
350
|
|
|
* @see https://datatables.net/reference/option/buttons.buttons.tag |
351
|
|
|
*/ |
352
|
|
|
public function tag($value) |
353
|
|
|
{ |
354
|
|
|
$this->attributes['tag'] = $value; |
355
|
|
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Set columns option value. |
361
|
|
|
* |
362
|
|
|
* @param mixed $value |
363
|
|
|
* @return $this |
364
|
|
|
*/ |
365
|
|
|
public function columns($value) |
366
|
|
|
{ |
367
|
|
|
$this->attributes['columns'] = $value; |
368
|
|
|
|
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Set exportOptions option value. |
374
|
|
|
* |
375
|
|
|
* @param mixed $value |
376
|
|
|
* @return $this |
377
|
|
|
*/ |
378
|
|
|
public function exportOptions($value) |
379
|
|
|
{ |
380
|
|
|
$this->attributes['exportOptions'] = $value; |
381
|
|
|
|
382
|
|
|
return $this; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Set action to submit the form. |
387
|
|
|
* |
388
|
|
|
* @return \Yajra\DataTables\Html\Button |
389
|
|
|
*/ |
390
|
|
|
public function actionSubmit() |
391
|
|
|
{ |
392
|
|
|
$this->attributes['action'] = 'function() { this.submit(); }'; |
393
|
|
|
|
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Set action option value. |
399
|
|
|
* |
400
|
|
|
* @param string $value |
401
|
|
|
* @return $this |
402
|
|
|
*/ |
403
|
|
|
public function action($value) |
404
|
|
|
{ |
405
|
|
|
if (substr($value, 0, 8) == 'function') { |
406
|
|
|
$this->attributes['action'] = $value; |
407
|
|
|
} else { |
408
|
|
|
$this->attributes['action'] = "function(e, dt, node, config) { $value }"; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
return $this; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Set editor class action handler. |
416
|
|
|
* |
417
|
|
|
* @param string $action |
418
|
|
|
* @return \Yajra\DataTables\Html\Button |
419
|
|
|
*/ |
420
|
|
|
public function actionHandler($action) |
421
|
|
|
{ |
422
|
|
|
$this->attributes['action'] = "function() { this.submit(null, null, function(data) { data.action = '{$action}'; return data; }) }"; |
423
|
|
|
|
424
|
|
|
return $this; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Set action to close the form. |
429
|
|
|
* |
430
|
|
|
* @return \Yajra\DataTables\Html\Button |
431
|
|
|
*/ |
432
|
|
|
public function actionClose() |
433
|
|
|
{ |
434
|
|
|
$this->attributes['action'] = 'function() { this.close(); }'; |
435
|
|
|
|
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Set button alignment. |
441
|
|
|
* |
442
|
|
|
* @param string $align |
443
|
|
|
* @return \Yajra\DataTables\Html\Button |
444
|
|
|
*/ |
445
|
|
|
public function align($align = 'button-left') |
446
|
|
|
{ |
447
|
|
|
$this->attributes['align'] = $align; |
448
|
|
|
|
449
|
|
|
return $this; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.