YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhCarousel
  • WhGallery
  • WhVideoCarousel
  1 <?php
  2 /**
  3  * WhGallery widget class
  4  *
  5  * Renders a gallery of blueimp Gallery
  6  * @see http://blueimp.github.io/Gallery/
  7  *
  8  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  9  * @copyright Copyright &copy; 2amigos.us 2013-
 10  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 11  * @package YiiWheels.widgets.gallery
 12  * @uses YiiStrap.helpers.TbArray
 13  * @uses YiiStrap.helpers.TbHtml
 14  */
 15 Yii::import('bootstrap.helpers.TbArray');
 16 
 17 class WhGallery extends CWidget
 18 {
 19     /**
 20      * @var array
 21      * box HTML additional attributes
 22      */
 23     public $htmlOptions = array();
 24 
 25     /**
 26      * @var array $options the blueimp gallery js configuration options
 27      * @see https://github.com/blueimp/Gallery/blob/master/README.md#options
 28      */
 29     public $pluginOptions = array();
 30 
 31     /**
 32      * The array of items that compound the gallery. The syntax is as follows:
 33      *
 34      * <pre>
 35      *  'items' => array(
 36      *        array(
 37      *            'url' => 'big image',
 38      *            'src' => 'source image (thumb)',
 39      *            'options' => array(...) // link options
 40      *        )
 41      * )
 42      * </pre>
 43      * @var array
 44      */
 45     public $items = array();
 46 
 47     /**
 48      * @var bool whether to display the controls on initialization
 49      */
 50     public $displayControls = false;
 51 
 52     /**
 53      * Widget's initialization
 54      */
 55     public function init()
 56     {
 57         $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
 58         $this->pluginOptions['container'] = '#' . $this->htmlOptions['id'] . '-gallery';
 59         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 60         parent::init();
 61     }
 62 
 63     /**
 64      * Renders widget
 65      * @return null|void
 66      */
 67     public function run()
 68     {
 69         if (empty($this->items)) {
 70             return null;
 71         }
 72         $this->renderLinks();
 73         $this->renderTemplate();
 74         $this->registerClientScript();
 75     }
 76 
 77     /**
 78      * Renders links
 79      */
 80     public function renderLinks()
 81     {
 82         echo CHtml::openTag('div', $this->htmlOptions);
 83         foreach ($this->items as $item) {
 84             $url = TbArray::getValue('url', $item, '#');
 85             $src = TbArray::getValue('src', $item, '#');
 86             $options = TbArray::getValue('options', $item );
 87             echo CHtml::link(CHtml::image($src), $url, $options);
 88         }
 89         echo CHtml::closeTag('div');
 90     }
 91 
 92     /**
 93      * Renders gallery template
 94      */
 95     public function renderTemplate()
 96     {
 97         $options = array(
 98             'id' => $this->htmlOptions['id'] . '-gallery',
 99             'class' => 'blueimp-gallery'
100         );
101         if($this->displayControls) {
102             TbHtml::addCssClass('blueimp-gallery-controls', $options);
103         }
104         echo CHtml::openTag('div', $options);
105         echo '<div class="slides"></div>
106         <h3 class="title"></h3>
107         <a class="prev">‹</a>
108         <a class="next">›</a>
109         <a class="close">×</a>
110         <a class="play-pause"></a>
111         <ol class="indicator"></ol>';
112         echo CHtml::closeTag('div');
113     }
114 
115     /**
116      * Registers gallery script files
117      */
118     public function registerGalleryScriptFiles()
119     {
120         /* publish assets dir */
121         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
122         $assetsUrl = $this->getAssetsUrl($path);
123 
124         /* @var $cs CClientScript */
125         $cs = Yii::app()->getClientScript();
126 
127         $cs->registerScriptFile($assetsUrl . '/js/blueimp-gallery.min.js', CClientScript::POS_END);
128         $cs->registerScriptFile($assetsUrl . '/js/blueimp-gallery-indicator.js', CClientScript::POS_END);
129         $cs->registerCssFile($assetsUrl . '/css/blueimp-gallery.min.css');
130     }
131 
132     /**
133      * Registers client script
134      */
135     public function registerClientScript()
136     {
137         $this->registerGalleryScriptFiles();
138         $selector = $this->htmlOptions['id'];
139 
140         $options = CJavaScript::encode($this->pluginOptions);
141         $js = "
142 ;var galleryLinks = [];
143 $(document).on('click', '#{$selector} a', function(e){
144     var links = $(this).parent()[0].getElementsByTagName('a');
145     var options = {$options};
146     options.index = $(this)[0];
147     blueimp.Gallery(links, options);
148     return false;
149 });
150         ";
151 
152         Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->getId(), $js);
153     }
154 
155 }
156 
YiiWheels API documentation generated by ApiGen 2.8.0