daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace x1\dragula;
  4.  
  5. use Yii;
  6. use yii\helpers\Html;
  7. use yii\helpers\Json;
  8. use yii\helpers\ArrayHelper;
  9. use yii\web\JsExpression;
  10. use yii\web\View;
  11. use yii\helpers\Url;
  12.  
  13. /**
  14.  *
  15.  *  EXAMPLE
  16.  *  =======
  17.  *
  18.  * <?= \x1\dragula\Dragula::widget([
  19.  *     'containers' => ['#files'],
  20.  *     'options'    => [
  21.  *         'revertOnSpill' => true,
  22.  *     ],
  23.  *     'drop' => new \yii\web\JsExpression('my.dropped'),
  24.  * ]); ?>
  25.  *
  26.  *
  27.  * <script>
  28.  *     var my = {};
  29.  *
  30.  *     my.dropped = function(el, container) {
  31.  *         var c      = $(container);
  32.  *         var items  = c.find('li[data-filename]');
  33.  *         var result = [];
  34.  *         $.each(c.find('li[data-filename]'), function(key, item) {
  35.  *             result.push($(item).data('filename'));
  36.  *         });
  37.  *
  38.  *         console.log('result', result);
  39.  *         // your logic here, e.g. ajax post
  40.  *     }
  41.  * </script>
  42.  *
  43.  */
  44. class Dragula extends \yii\base\Widget {
  45.  
  46.     public $containers = null;
  47.     public $options    = null;
  48.  
  49.     // events
  50.     public $drag    = null;
  51.     public $dragend = null;
  52.     public $drop    = null;
  53.     public $cancel  = null;
  54.     public $remove  = null;
  55.     public $shadow  = null;
  56.     public $over    = null;
  57.     public $out     = null;
  58.     public $cloned  = null;
  59.  
  60.     const EVENTS  = ['drag', 'dragend', 'drop', 'cancel', 'remove', 'shadow', 'over', 'out', 'cloned'];
  61.     const PARAMS2 = ['el', 'container', 'source'];
  62.     const PARAMS  = [
  63.         'drag'    => ['el', 'source'],
  64.         'dragend' => ['el'],
  65.         'drop'    => ['el', 'target', 'source', 'sibling'],
  66.         'cancel'  => self::PARAMS2,
  67.         'remove'  => self::PARAMS2,
  68.         'shadow'  => self::PARAMS2,
  69.         'over'    => self::PARAMS2,
  70.         'out'     => self::PARAMS2,
  71.         'cloned'  => ['clone', 'original', 'type'],
  72.     ];
  73.  
  74.     public function init() {
  75.         parent::init();
  76.  
  77.         if ($this->containers == null) {
  78.             throw new \yii\base\InvalidConfigException("`containers` must be set");
  79.         }
  80.  
  81.         if (!is_array($this->containers))
  82.             $this->containers = [$this->containers];
  83.  
  84.         foreach ($this->containers as $key => $container) {
  85.             if (!($container instanceof JsExpression)) {
  86.                 $this->containers[$key] = new JsExpression(sprintf('document.querySelector(%s)', json_encode($container)));
  87.             }
  88.         }
  89.     }
  90.  
  91.     public function run() {
  92.         DragulaAsset::register($this->view);
  93.  
  94.         $events = [];
  95.         foreach (self::EVENTS as $event) {
  96.             if ($this->$event !== null) {
  97.                 if ($this->$event instanceof JsExpression) {
  98.                     $events[] = new JsExpression(sprintf('.on("%s", %s)', $event, $this->$event));
  99.                 } else {
  100.                     $events[] = new JsExpression(sprintf('.on("%s", function(%s) { %s })', $event, implode(',', self::PARAMS[$event]), $this->$event));
  101.                 }
  102.             }
  103.         }
  104.  
  105.         $this->view->registerJs(sprintf('dragula(%s, %s)%s;', Json::encode($this->containers), Json::encode($this->options), implode('', $events)));
  106.         return null;
  107.     }
  108.  
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top