1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 class WhGallery extends CWidget
16 {
17 18 19 20
21 public $htmlOptions = array();
22
23 24 25 26
27 public $pluginOptions = array();
28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 public $items = array();
44
45 46 47
48 public $displayControls = false;
49
50 51 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 63 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 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 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 115
116 public function registerGalleryScriptFiles()
117 {
118
119 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
120 $assetsUrl = $this->getAssetsUrl($path);
121
122
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 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