-
Notifications
You must be signed in to change notification settings - Fork 13
/
DropZone.php
123 lines (102 loc) · 3.61 KB
/
DropZone.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
<?php
namespace devgroup\dropzone;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
class DropZone extends Widget
{
public $model;
public $attribute;
public $htmlOptions = [];
public $name;
public $options = [];
public $eventHandlers = [];
public $url;
public $storedFiles = [];
public $sortable = false;
public $sortableOptions = [];
public $message;
public $messageOptions = [];
protected $dropzoneName = 'dropzone';
public function init()
{
parent::init();
Html::addCssClass($this->htmlOptions, 'dropzone');
Html::addCssClass($this->messageOptions, 'dz-message');
$this->dropzoneName = 'dropzone_' . $this->id;
}
private function registerAssets()
{
DropZoneAsset::register($this->getView());
$this->getView()->registerJs('Dropzone.autoDiscover = false;');
}
protected function addFiles($files = [])
{
if (empty($files) === false) {
$this->view->registerJs('var files = ' . Json::encode($files));
$this->view->registerJs('for (var i=0; i<files.length; i++) {
' . $this->dropzoneName . '.emit("addedfile", files[i]);
' . $this->dropzoneName . '.emit("thumbnail", files[i], files[i]["thumbnail"]);
' . $this->dropzoneName . '.emit("complete", files[i]);
}');
}
}
protected function decrementMaxFiles($num)
{
if ($num > 0) {
$this->getView()->registerJs(
'if (' . $this->dropzoneName . '.options.maxFiles > 0) { '
. $this->dropzoneName . '.options.maxFiles = '
. $this->dropzoneName . '.options.maxFiles - ' . $num . ';'
. ' }'
);
}
}
protected function createDropzone()
{
$options = Json::encode($this->options);
$this->getView()->registerJs($this->dropzoneName . ' = new Dropzone("#' . $this->id . '", ' . $options . ');');
}
public function run()
{
if (empty($this->name) && (!empty($this->model) && !empty($this->attribute))) {
$this->name = Html::getInputName($this->model, $this->attribute);
}
if (empty($this->url)) {
$this->url = Url::toRoute(['site/upload']);
}
$options = [
'url' => $this->url,
'paramName' => $this->name,
'params' => [],
];
if (Yii::$app->request->enableCsrfValidation) {
$options['params'][Yii::$app->request->csrfParam] = Yii::$app->request->getCsrfToken();
}
if (!empty($this->message)) {
$message = Html::tag('div', $this->message, $this->messageOptions);
} else {
$message = '';
}
$this->htmlOptions['id'] = $this->id;
$this->options = ArrayHelper::merge($this->options, $options);
echo Html::tag('div', $message, $this->htmlOptions);
$this->registerAssets();
$this->createDropzone();
foreach ($this->eventHandlers as $event => $handler) {
$handler = new \yii\web\JsExpression($handler);
$this->getView()->registerJs(
$this->dropzoneName . ".on('{$event}', {$handler})"
);
}
$this->addFiles($this->storedFiles);
$this->decrementMaxFiles(count($this->storedFiles));
if ($this->sortable) {
$options = Json::encode($this->sortableOptions);
$this->getView()->registerJs("jQuery('#{$this->id}').sortable(" . $options . ");");
}
}
}