forked from devleaks/yii2-metafizzy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraggabilly.php
85 lines (77 loc) · 2.27 KB
/
Draggabilly.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
<?php
namespace devleaks\metafizzy;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\base\Widget;
/**
* Chosen Select Widget based on Chosen jQuery plugin {@link http://masonry.desandro.com)
* @package devleaks\metafizzy
*
* @author Pierre M <[email protected]>
*/
class Draggabilly extends Widget {
/**
* @var array the HTML attributes for the div tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [
'itemSelector' => '.item'
];
/**
* @var array Plugin options
*/
public $pluginOptions = [
];
/**
* Initializes the object.
* This method is invoked at the end of the constructor after the object is initialized with the
* given configuration.
*/
public function init()
{
//checks for the element id
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
parent::init();
}
/**
* Render chosen select
* @return string|void
*/
public function run()
{
$this->registerAssets();
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
DraggabillyAsset::register($view);
// $js = '$("' . $this->options['itemSelector'] .'", "#' . $this->options['id'] . '").draggabilly(' . $this->getPluginOptions() . ');';
$js = <<<JAVASCRIPT
var contId = "{$this->options['id']}";
var container = $("#"+contId);
// var selector = "{$this->options['itemSelector']}";
var selector = container
container.find(selector).each( function( i, itemElem ) {
// make element draggable with Draggabilly
var draggie = new Draggabilly( itemElem );
// bind Draggabilly events to Packery
container.packery( 'bindDraggabillyEvents', draggie );
});
JAVASCRIPT;
$view->registerJs($js, $view::POS_END);
}
/**
* Return plugin options in json format
* @return string
*/
public function getPluginOptions()
{
return Json::encode($this->pluginOptions);
}
}