YiiWheels
  • Package
  • Class
  • Tree

Packages

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