1 <?php
2 3 4 5 6 7
8
9 namespace foundation\helpers;
10
11 12 13 14 15 16
17 class Foundation
18 {
19 20 21
22 protected static $assetsUrl;
23
24 25 26
27 public static function register($useZepto = false, $ie8Support = false)
28 {
29 static::registerCoreCss();
30 static::registerCoreScripts($useZepto);
31
32 if ($ie8Support)
33 static::registerBlockGridIeSupport();
34 }
35
36 37 38 39 40 41
42 public static function registerCoreScripts(
43 $useZepto = false,
44 $minimized = true,
45 $position = \CClientScript::POS_END
46 ) {
47
48 $cs = \Yii::app()->getClientScript();
49
50 if ($useZepto) {
51 $cs->registerScriptFile(static::getAssetsUrl() . '/js/vendor/zepto.js');
52 } else {
53 $cs->registerCoreScript('jquery');
54 }
55
56 $cs->registerScriptFile(static::getAssetsUrl() . '/js/vendor/custom.modernizr.js', \CClientScript::POS_HEAD);
57 $script = $minimized
58 ? '/js/foundation.min.js'
59 : '/js/foundation/foundation.js';
60 $cs->registerScriptFile(static::getAssetsUrl() . $script, $position);
61 $cs->registerScript(__CLASS__, '$(document).foundation();');
62 }
63
64 65 66 67 68 69
70 public static function isCoreRegistered($minimized = true, $position = \CClientScript::POS_END)
71 {
72 $script = $minimized
73 ? '/js/foundation.min.js'
74 : '/js/foundation/foundation.js';
75
76 return \Yii::app()->getClientScript()
77 ->isScriptFileRegistered(static::getAssetsUrl() . $script, $position);
78 }
79
80 81 82 83
84 public static function registerFonts($fonts = array())
85 {
86 if (empty($fonts))
87 return;
88
89 foreach ($fonts as $font) {
90 Icon::registerIconFontSet($font);
91 }
92 }
93
94 95 96
97 public static function registerCoreCss()
98 {
99 $fileName = \YII_DEBUG ? 'foundation.css' : 'foundation.min.css';
100
101 \Yii::app()->clientScript->registerCssFile(static::getAssetsUrl() . '/css/normalize.css');
102 \Yii::app()->clientScript->registerCssFile(static::getAssetsUrl() . '/css/' . $fileName);
103 }
104
105 106 107 108 109 110
111 public static function getAssetsUrl($forceCopyAssets = false, $cdn = false)
112 {
113 if (!isset(static::$assetsUrl)) {
114 if ($cdn) {
115 static::$assetsUrl = '//cdn.jsdelivr.net/foundation/4.3.1/';
116 } else {
117 $assetsPath = static::getAssetsPath();
118 static::$assetsUrl = \Yii::app()->assetManager->publish($assetsPath, true, -1, $forceCopyAssets);
119 }
120 }
121 return static::$assetsUrl;
122 }
123
124 125 126 127
128 public static function getAssetsPath()
129 {
130 return \Yii::getPathOfAlias('foundation.assets');
131 }
132
133 134 135 136 137 138 139
140 public static function registerPlugin($name, $selector, $options = array(), $position = \CClientScript::POS_READY)
141 {
142 $options = !empty($options) ? \CJavaScript::encode($options) : '';
143 $script = ";jQuery('{$selector}').{$name}({$options});";
144 \Yii::app()->clientScript->registerScript(static::getUniqueScriptId(), $script, $position);
145 }
146
147 148 149 150 151
152 public static function registerScriptFile($asset, $position = \CClientScript::POS_END)
153 {
154 \Yii::app()->clientScript->registerScriptFile(static::getAssetsUrl() . "/{$asset}", $position);
155 }
156
157 158 159 160 161 162
163 public static function registerEvents($selector, $events, $position = \CClientScript::POS_READY)
164 {
165 if (empty($events)) {
166 return;
167 }
168
169 $script = '';
170 foreach ($events as $name => $handler) {
171 $handler = ($handler instanceof \CJavaScriptExpression)
172 ? $handler
173 : new \CJavaScriptExpression($handler);
174
175 $script .= ";jQuery('{$selector}').on('{$name}', {$handler});";
176 }
177 \Yii::app()->clientScript->registerScript(static::getUniqueScriptId(), $script, $position);
178 }
179
180 181 182 183
184 public static function getUniqueScriptId()
185 {
186 return uniqid(__CLASS__ . '#', true);
187 }
188
189 190 191 192
193 public static function registerBlockGridIeSupport($position = \CClientScript::POS_END)
194 {
195 \Yii::app()->getClientScript()
196 ->registerScript(
197 __CLASS__,
198 "(function($){ $(function(){
199 $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
200 $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
201 $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
202 $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});
203 });})(jQuery);
204 ",
205 $position
206 );
207 }
208 }