YiiFoundation
  • Namespace
  • Class
  • Tree

Namespaces

  • foundation
    • enum
    • exception
    • helpers
    • widgets
      • base
  • PHP

Classes

  • Alert
  • ArrayHelper
  • Button
  • Foundation
  • Html
  • Icon
  • Nav
  • Panel
  • Pricing
  • Progress
  • Typo
  1 <?php
  2 /**
  3  * @copyright Copyright &copy; 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 
  9 namespace foundation\helpers;
 10 
 11 /**
 12  * Foundation helper contains functions to work with the core assets.
 13  *
 14  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 15  * @package foundation\helpers
 16  */
 17 class Foundation
 18 {
 19     /**
 20      * @var string holds the url of Foundation's assets folder
 21      */
 22     protected static $assetsUrl;
 23 
 24     /**
 25      * Registers all core css and js scripts for foundation
 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      * Registers jQuery and Foundation JavaScript.
 38      * @param bool $useZepto whether to use zepto or not
 39      * @param bool $minimized whether to register full minimized library or not
 40      * @param int $position the position to register the core script
 41      */
 42     public static function registerCoreScripts(
 43         $useZepto = false,
 44         $minimized = true,
 45         $position = \CClientScript::POS_END
 46     ) {
 47         /** @var CClientScript $cs */
 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         // normalizer goes at the head no matter what
 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      * Checks whether the core script has been registered or not
 66      * @param bool $minimized whether to check full minimized library or not
 67      * @param int $position the position to register the core script
 68      * @return bool
 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      * Registers foundation fonts
 82      * @param mixed $fonts the array or string name to register.
 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      * Registers the Foundation CSS.
 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      * Returns the url to the published assets folder.
107      * @param bool $forceCopyAssets whether to force assets registration or not
108      * @param bool $cdn whether to use CDN version or not
109      * @return string the url.
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      * Returns the full path of foundation assets
126      * @return mixed the full path
127      */
128     public static function getAssetsPath()
129     {
130         return \Yii::getPathOfAlias('foundation.assets');
131     }
132 
133     /**
134      * Registers a specific plugin using the given selector and options.
135      * @param string $name the plugin name.
136      * @param string $selector the CSS selector.
137      * @param array $options the JavaScript options for the plugin.
138      * @param int $position the position of the JavaScript code.
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      * Registers a specific js assets file
149      * @param string $asset the assets file (ie 'foundation/foundation.abide.js'
150      * @param int $position the position where the script should be registered on the page
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      * Registers events using the given selector.
159      * @param string $selector the CSS selector.
160      * @param string[] $events the JavaScript event configuration (name=>handler).
161      * @param int $position the position of the JavaScript code.
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      * Generates a "somewhat" random id string.
182      * @return string the id
183      */
184     public static function getUniqueScriptId()
185     {
186         return uniqid(__CLASS__ . '#', true);
187     }
188 
189     /**
190      * Registers block-grid support for ie8 if set by its `ie8Support` attribute.
191      * @param int $position the position to render the script
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 }
YiiFoundation API documentation generated by ApiGen 2.8.0