CJuiButton.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * CJuiButton class file.
  4. *
  5. * @author Sebastian Thierer <sebas@artfos.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.jui.CJuiInputWidget');
  11. /**
  12. * CJuiButton displays a button widget.
  13. *
  14. * CJuiButton encapsulates the {@link http://jqueryui.com/button/ JUI Button}
  15. * plugin.
  16. *
  17. * To use this widget as a submit button, you may insert the following code in a view:
  18. * <pre>
  19. * $this->widget('zii.widgets.jui.CJuiButton',array(
  20. * 'buttonType'=>'submit',
  21. * 'name'=>'btnSubmit',
  22. * 'value'=>'1',
  23. * 'caption'=>'Submit form',
  24. * 'htmlOptions'=>array('class'=>'ui-button-primary')
  25. * ),
  26. * ));
  27. * </pre>
  28. *
  29. * To use this widget as a button, you may insert the following code in a view:
  30. * <pre>
  31. * $this->widget('zii.widgets.jui.CJuiButton',array(
  32. * 'buttonType'=>'button',
  33. * 'name'=>'btnSave',
  34. * 'caption'=>'Save',
  35. * 'onclick'=>new CJavaScriptExpression('function(){alert("Save button clicked"); this.blur(); return false;}'),
  36. * ));
  37. * </pre>
  38. *
  39. * By configuring the {@link options} property, you may specify the options
  40. * that need to be passed to the JUI button plugin. Please refer to
  41. * the {@link http://api.jqueryui.com/button/ JUI Button API} documentation
  42. * for possible options (name-value pairs) and
  43. * {@link http://jqueryui.com/button/ JUI Button page} for general description
  44. * and demo.
  45. *
  46. * @author Sebastian Thierer <sebathi@gmail.com>
  47. * @package zii.widgets.jui
  48. * @since 1.1.3
  49. */
  50. class CJuiButton extends CJuiInputWidget
  51. {
  52. /**
  53. * @var string The button type (possible types: submit, button, link, radio, checkbox, buttonset).
  54. * "submit" is used as default.
  55. */
  56. public $buttonType='submit';
  57. /**
  58. * @var string The default html tag for the buttonset
  59. */
  60. public $htmlTag='div';
  61. /**
  62. * @var mixed a URL or an action route that can be used to create a URL. Used when a buttonType "link" is selected.
  63. * See {@link normalizeUrl} for more details about how to specify this parameter.
  64. */
  65. public $url=null;
  66. /**
  67. * @var mixed The value of the current item. Used only for "radio" and "checkbox"
  68. */
  69. public $value;
  70. /**
  71. * @var string The button text
  72. */
  73. public $caption="";
  74. /**
  75. * @var string The javascript function to be raised when this item is clicked (client event).
  76. */
  77. public $onclick;
  78. /**
  79. * (non-PHPdoc)
  80. * @see framework/zii/widgets/jui/CJuiWidget::init()
  81. */
  82. public function init()
  83. {
  84. parent::init();
  85. if($this->buttonType=='buttonset')
  86. {
  87. if(!isset($this->htmlOptions['id']))
  88. $this->htmlOptions['id']=$this->getId();
  89. echo CHtml::openTag($this->htmlTag,$this->htmlOptions);
  90. }
  91. }
  92. /**
  93. * (non-PHPdoc)
  94. * @see framework/CWidget::run()
  95. */
  96. public function run()
  97. {
  98. $cs=Yii::app()->getClientScript();
  99. list($name,$id)=$this->resolveNameID();
  100. if(isset($this->htmlOptions['id']))
  101. $id=$this->htmlOptions['id'];
  102. else
  103. $this->htmlOptions['id']=$id;
  104. if(isset($this->htmlOptions['name']))
  105. $name=$this->htmlOptions['name'];
  106. else
  107. $this->htmlOptions['name']=$name;
  108. if($this->buttonType=='buttonset')
  109. {
  110. echo CHtml::closeTag($this->htmlTag);
  111. $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').buttonset();");
  112. }
  113. else
  114. {
  115. switch($this->buttonType)
  116. {
  117. case 'submit':
  118. echo CHtml::submitButton($this->caption,$this->htmlOptions)."\n";
  119. break;
  120. case 'button':
  121. echo CHtml::htmlButton($this->caption,$this->htmlOptions)."\n";
  122. break;
  123. case 'link':
  124. echo CHtml::link($this->caption,$this->url,$this->htmlOptions)."\n";
  125. break;
  126. case 'radio':
  127. if($this->hasModel())
  128. {
  129. echo CHtml::activeRadioButton($this->model,$this->attribute,$this->htmlOptions);
  130. echo CHtml::label($this->caption,CHtml::activeId($this->model,$this->attribute))."\n";
  131. }
  132. else
  133. {
  134. echo CHtml::radioButton($name,$this->value,$this->htmlOptions);
  135. echo CHtml::label($this->caption,$id)."\n";
  136. }
  137. break;
  138. case 'checkbox':
  139. if($this->hasModel())
  140. {
  141. echo CHtml::activeCheckbox($this->model,$this->attribute,$this->htmlOptions);
  142. echo CHtml::label($this->caption,CHtml::activeId($this->model,$this->attribute))."\n";
  143. }
  144. else
  145. {
  146. echo CHtml::checkbox($name,$this->value,$this->htmlOptions);
  147. echo CHtml::label($this->caption,$id)."\n";
  148. }
  149. break;
  150. default:
  151. throw new CException(Yii::t('zii','The button type "{type}" is not supported.',array('{type}'=>$this->buttonType)));
  152. }
  153. $options=CJavaScript::encode($this->options);
  154. if($this->onclick!==null)
  155. {
  156. if(!($this->onclick instanceof CJavaScriptExpression))
  157. $this->onclick=new CJavaScriptExpression($this->onclick);
  158. $click=CJavaScript::encode($this->onclick);
  159. $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').button($options).click($click);");
  160. }
  161. else
  162. $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').button($options);");
  163. }
  164. }
  165. }