1 <?php
2 /**
3 * EChosen class file.
4 *
5 * @author Andrius Marcinkevicius <andrew.web@ifdattic.com>
6 * @copyright Copyright © 2011 Andrius Marcinkevicius
7 * @license Licensed under MIT license. http://ifdattic.com/MIT-license.txt
8 * @version 1.5.1
9 */
10
11 /**
12 * EChosen makes select boxes much more user-friendly.
13 *
14 * @author Andrius Marcinkevicius <andrew.web@ifdattic.com>
15 */
16 class EChosen extends CWidget
17 {
18 /**
19 * @var string apply chosen plugin to these elements.
20 */
21 public $target = '.chzn-select';
22
23 /**
24 * @var boolean use jQuery plugin, otherwise use Prototype plugin.
25 */
26 public $useJQuery = true;
27
28 /**
29 * @var boolean include un-minified plugin then debuging.
30 */
31 public $debug = false;
32
33 /**
34 * @var array native Chosen plugin options.
35 */
36 public $options = array();
37
38 /**
39 * @var int script registration position.
40 */
41 public $scriptPosition = CClientScript::POS_END;
42
43 /**
44 * Apply Chosen plugin to select boxes.
45 */
46 public function run()
47 {
48 // Publish extension assets
49 $assets = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias(
50 'ext.EChosen' ) . '/assets' );
51
52 // Register extension assets
53 $cs = Yii::app()->getClientScript();
54 $cs->registerCssFile( $assets . '/chosen.css' );
55
56 // Get extension for JavaScript file
57 $ext = '.min.js';
58 if( $this->debug )
59 $ext = '.js';
60
61 // Use jQuery plugin version
62 if( $this->useJQuery )
63 {
64 // Register jQuery scripts
65 $options = CJavaScript::encode( $this->options );
66 $cs->registerScriptFile( $assets . '/chosen.jquery' . $ext,
67 $this->scriptPosition );
68 $cs->registerScript( 'chosen',
69 "$( '{$this->target}' ).chosen({$options});", CClientScript::POS_READY );
70 }
71 // Use Prototype plugin version
72 else
73 {
74 // Register Prototype scripts
75 $cs->registerScriptFile( $assets . '/chosen.proto' . $ext,
76 $this->scriptPosition );
77 }
78 }
79 }
80 ?>