Skip to content

Commit 0fcdceb

Browse files
committed
Init
0 parents  commit 0fcdceb

File tree

7 files changed

+323
-0
lines changed

7 files changed

+323
-0
lines changed

Asset.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace rkit\fileapi;
4+
5+
use yii\web\AssetBundle;
6+
7+
class Asset extends AssetBundle
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
public $sourcePath = '@vendor/rubaxa/fileapi';
13+
/**
14+
* @inheritdoc
15+
*/
16+
public $js = [
17+
'FileAPI/FileAPI.min.js',
18+
'FileAPI/FileAPI.exif.js',
19+
'jquery.fileapi.min.js'
20+
];
21+
/**
22+
* @inheritdoc
23+
*/
24+
public $depends = [
25+
'yii\web\JqueryAsset',
26+
];
27+
}

CropAsset.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace rkit\fileapi;
4+
5+
use yii\web\AssetBundle;
6+
7+
class CropAsset extends AssetBundle
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
public $sourcePath = '@vendor/rubaxa/fileapi';
13+
/**
14+
* @inheritdoc
15+
*/
16+
public $css = [
17+
'jcrop/jquery.Jcrop.min.css'
18+
];
19+
/**
20+
* @inheritdoc
21+
*/
22+
public $js = [
23+
'jcrop/jquery.Jcrop.min.js'
24+
];
25+
/**
26+
* @inheritdoc
27+
*/
28+
public $depends = [
29+
'rkit\fileapi\Asset',
30+
'yii\bootstrap\BootstrapAsset',
31+
'yii\bootstrap\BootstrapPluginAsset',
32+
];
33+
}

LICENSE.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2015, Igor Romanov
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of filemanager-yii2 nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# FileApi widget for Yii2
2+
3+
## Installation
4+
5+
```
6+
composer require rkit/fileapi-widget-yii2
7+
```
8+
9+
## Usage
10+
11+

Widget.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
/**
4+
* @link https://github.com/rkit/fileapi-yii2
5+
* @copyright Copyright (c) 2015 Igor Romanov
6+
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
7+
*/
8+
9+
namespace rkit\fileapi;
10+
11+
use yii\helpers\Html;
12+
use yii\helpers\Json;
13+
use yii\helpers\ArrayHelper;
14+
use yii\web\JsExpression;
15+
use yii\widgets\InputWidget;
16+
use Yii;
17+
18+
/**
19+
* FileApi
20+
* Widget for https://github.com/RubaXa/jquery.fileapi/
21+
*/
22+
class Widget extends InputWidget
23+
{
24+
/**
25+
* @var string FileAPI selector
26+
*/
27+
public $selector;
28+
/**
29+
* @var string
30+
*/
31+
public $inputName = 'file';
32+
/**
33+
* @var array {@link https://github.com/RubaXa/jquery.fileapi}
34+
*/
35+
public $settings = [];
36+
/**
37+
* @var string Path to template
38+
*/
39+
public $template;
40+
/**
41+
* @var array {@link https://github.com/RubaXa/jquery.fileapi/#events}
42+
*/
43+
public $callbacks = [];
44+
/**
45+
* @var boolean
46+
*/
47+
public $preview = true;
48+
/**
49+
* @var boolean
50+
*/
51+
public $crop = false;
52+
/**
53+
* @var array {@link https://github.com/RubaXa/jquery.fileapi/#elementsobject}
54+
*/
55+
private $defaultSettings = [
56+
'autoUpload' => true,
57+
'elements' => [
58+
'progress' => '[data-fileapi="progress"]',
59+
'active' => [
60+
'show' => '[data-fileapi="active.show"]',
61+
'hide' => '[data-fileapi="active.hide"]'
62+
],
63+
'name' => '[data-fileapi="name"]',
64+
'preview' => [
65+
'el' => '[data-fileapi="preview"]',
66+
'width' => 200,
67+
'height' => 200,
68+
'keepAspectRatio' => true
69+
],
70+
'dnd' => [
71+
'el' => '.fileapi-dnd',
72+
'hover' => '.fileapi-dnd-active'
73+
]
74+
]
75+
];
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function init()
81+
{
82+
parent::init();
83+
84+
$request = Yii::$app->getRequest();
85+
86+
if ($request->enableCsrfValidation === true) {
87+
$this->settings['data'][$request->csrfParam] = $request->getCsrfToken();
88+
}
89+
90+
if (!isset($this->settings['url'])) {
91+
$this->settings['url'] = $request->getUrl();
92+
}
93+
94+
if ($this->crop === true) {
95+
$this->settings['autoUpload'] = false;
96+
}
97+
98+
$this->settings = ArrayHelper::merge($this->defaultSettings, $this->settings);
99+
}
100+
101+
/**
102+
* @inheritdoc
103+
*/
104+
public function run()
105+
{
106+
$this->registerScripts();
107+
108+
if ($this->hasModel()) {
109+
$input = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
110+
} else {
111+
$input = Html::hiddenInput($this->name, $this->value, $this->options);
112+
}
113+
114+
return $this->render(
115+
$this->template,
116+
[
117+
'selector' => $this->getSelector(),
118+
'input' => $input,
119+
'inputName' => $this->inputName,
120+
'value' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
121+
'preview' => $this->preview,
122+
'crop' => $this->crop,
123+
'model' => $this->model,
124+
'attribute' => $this->attribute
125+
]
126+
);
127+
}
128+
129+
/**
130+
* @return string
131+
*/
132+
public function getSelector()
133+
{
134+
return $this->selector !== null ? $this->selector : 'fileapi-' . $this->options['id'];
135+
}
136+
137+
/**
138+
* Register scripts
139+
*/
140+
public function registerScripts()
141+
{
142+
$view = $this->getView();
143+
144+
Asset::register($view);
145+
if ($this->crop === true) {
146+
CropAsset::register($view);
147+
}
148+
149+
$view->registerJs(
150+
'jQuery(\'#' . $this->getSelector() . '\').fileapi(' . Json::encode($this->settings) .');'
151+
);
152+
153+
$this->registerCallbacks();
154+
}
155+
156+
/**
157+
* Register widget callbacks
158+
*/
159+
protected function registerCallbacks()
160+
{
161+
if (!empty($this->callbacks)) {
162+
$selector = $this->getSelector();
163+
$view = $this->getView();
164+
foreach ($this->callbacks as $event => $callback) {
165+
if (is_array($callback)) {
166+
foreach ($callback as $function) {
167+
if (!$function instanceof JsExpression) {
168+
$function = new JsExpression($function);
169+
}
170+
$view->registerJs("jQuery('#$selector').on('$event', $function);");
171+
}
172+
} else {
173+
if (!$callback instanceof JsExpression) {
174+
$callback = new JsExpression($callback);
175+
}
176+
$view->registerJs("jQuery('#$selector').on('$event', $callback);");
177+
}
178+
}
179+
}
180+
}
181+
}

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "rkit/fileapi-widget-yii2",
3+
"description": "FileApi widget for Yii2",
4+
"keywords": ["yii2", "extension", "fileapi", "widget", "upload", "files"],
5+
"homepage": "https://github.com/rkit/fileapi-widget-yii2",
6+
"type": "yii2-extension",
7+
"license": "BSD-3-Clause",
8+
"authors": [
9+
{
10+
"name": "Igor Romanov",
11+
"email": "rkit.ru@gmail.com"
12+
}
13+
],
14+
"support": {
15+
"issues": "https://github.com/rkit/fileapi-widget-yii2/issues?state=open",
16+
"source": "https://github.com/rkit/fileapi-widget-yii2"
17+
},
18+
"require": {
19+
"yiisoft/yii2": "^2.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"rkit\\fileapi\\": ""
24+
}
25+
}
26+
}

phpcs.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PSR-MOD">
3+
<description>Based on PSR-2</description>
4+
5+
<rule ref="PSR2">
6+
<exclude name="PSR1.Classes.ClassDeclaration"/>
7+
<exclude name="PSR1.Files.SideEffects"/>
8+
<exclude name="Squiz.WhiteSpace.SuperfluousWhitespace"/>
9+
</rule>
10+
11+
<rule ref="Generic.Files.LineLength">
12+
<properties>
13+
<property name="lineLimit" value="120"/>
14+
<property name="absoluteLineLimit" value="120"/>
15+
</properties>
16+
</rule>
17+
18+
</ruleset>

0 commit comments

Comments
 (0)