1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets;
9
10 use foundation\helpers\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\enum\Enum;
13
14 /**
15 * Orbit generates a foundation orbits.
16 *
17 * @see http://foundation.zurb.com/docs/components/orbit.html
18 *
19 * @author Antonio Ramirez <amigo.cobos@gmail.com>
20 * @package foundation\widgets
21 */
22 class Orbit extends base\Widget
23 {
24 /**
25 * @var array the items to render in the orbit. The syntax is as follows:
26 *
27 * <pre>
28 * 'items' => array(
29 * array('content', 'caption')
30 * )
31 * </pre>
32 */
33 public $items = array();
34
35 /**
36 * @var array the orbit plugin options. The options are (option: default value):
37 *
38 * - animation: 'fade'
39 * - timer_speed: 10000
40 * - pause_on_hover: true
41 * - resume_on_mouseout: false
42 * - animation_speed: 500
43 * - stack_on_small: true
44 * - navigation_arrows: true
45 * - slide_number: true
46 * - container_class: 'orbit-container'
47 * - stack_on_small_class: 'orbit-stack-on-small'
48 * - next_class: 'orbit-next'
49 * - prev_class: 'orbit-prev'
50 * - timer_container_class: 'orbit-timer'
51 * - timer_paused_class: 'paused'
52 * - timer_progress_class: 'orbit-progress'
53 * - slides_container_class: 'orbit-slides-container'
54 * - bullets_container_class: 'orbit-bullets'
55 * - bullets_active_class: 'active'
56 * - slide_number_class: 'orbit-slide-number'
57 * - caption_class: 'orbit-caption'
58 * - active_slide_class: 'active'
59 * - orbit_transition_class: 'orbit-transitioning'
60 * - bullets: true
61 * - timer: true
62 * - variable_height: false
63 * - before_slide_change: function(){}
64 * - after_slide_change: function(){}
65 *
66 */
67 public $pluginOptions = array();
68 /**
69 * @var string[] the JavaScript event configuration (name=>handler). The following list shows the available ones:
70 *
71 * - orbit:ready, param: event, Fires when the slider has loaded
72 * - orbit:before-slide-change, param: event, Fires before a slide changes
73 * - orbit:after-slide-change, param: event, Fires after a slide transition animation has finished. The orbit
74 * parameter contains slide_number and total_slides.
75 * - orbit:timer-started, param: event, Fires each time the timer is started/resumed
76 * - orbit:timer-stopped, param: event, Fires each time the timer is paused/stopped
77 */
78 public $events = array();
79 /**
80 * @var bool whether to display the preloader or not
81 */
82 public $showPreloader = true;
83
84
85 /**
86 * Initializes the widget
87 */
88 public function init()
89 {
90 $this->assets = array(
91 'js' => YII_DEBUG ? 'foundation/foundation.orbit.js' : 'foundation.min.js'
92 );
93
94 Html::addCssClass($this->htmlOptions, Enum::ORBIT_WRAPPER);
95 $this->setId(ArrayHelper::removeValue($this->htmlOptions, 'id', $this->getId()));
96 $this->registerClientScript();
97 parent::init();
98 }
99
100 /**
101 * Renders the widget
102 */
103 public function run()
104 {
105 echo $this->renderOrbit();
106 }
107
108 /**
109 * Renders the orbit plugin
110 * @return string the generated HTML string
111 */
112 public function renderOrbit()
113 {
114 $contents = $list = array();
115
116 foreach ($this->items as $item) {
117 $list[] = $this->renderItem($item);
118 }
119 if ($this->showPreloader) {
120 $contents[] = \CHtml::tag('div', array('class' => Enum::ORBIT_LOADER), ' ');
121 }
122
123 $contents[] = \CHtml::tag(
124 'ul',
125 array(
126 'id' => $this->getId(),
127 'data-orbit' => 'data-orbit'
128 ),
129 implode("\n", $list)
130 );
131 return \CHtml::tag('div', $this->htmlOptions, implode("\n", $contents));
132 }
133
134 /**
135 * Returns a generated LI tag item
136 * @param array $item the item configuration
137 * @return string the resulting li tag
138 */
139 public function renderItem($item)
140 {
141 $content = ArrayHelper::getValue($item, 'content', '');
142 $caption = ArrayHelper::getValue($item, 'caption');
143
144 if ($caption !== null) {
145 $caption = \CHtml::tag('div', array('class' => Enum::ORBIT_CAPTION), $caption);
146 }
147 return \CHtml::tag('li', array(), $content . $caption);
148 }
149
150 /**
151 * Registers the plugin script
152 */
153 public function registerClientScript()
154 {
155 if (!empty($this->pluginOptions)) {
156 $options = \CJavaScript::encode($this->pluginOptions);
157 \Yii::app()->clientScript
158 ->registerScript('Orbit#' . $this->getId(), "$(document).foundation('orbit', {$options}");
159 }
160
161 if (!empty($this->events)) {
162 $this->registerEvents("#{$this->getId()}", $this->events);
163 }
164 }
165 }