00001 <?php
00002
00003 namespace Habari;
00004
00008 class FormControlDropbutton extends FormContainer
00009 {
00010 static $outpre = false;
00011
00012 public function _extend() {
00013 $this->properties['type'] = 'hidden';
00014 $this->add_template_class('div', 'dropbutton dropbutton_control');
00015 }
00016
00021 public function pre_out()
00022 {
00023 $out = '';
00024 if ( !self::$outpre ) {
00025 self::$outpre = true;
00026 $out = <<< CUSTOM_DROPBUTTON_JS
00027 <script type="text/javascript">
00028 controls.init(function(){
00029 $('body').on('click', function(e){
00030 $('.dropbutton').removeClass('dropped');
00031 });
00032 $('.dropbutton_control').each(function(){
00033 var self = $(this);
00034 var needWidth = self.find('.primary').outerWidth()+self.find('.dropdown').outerWidth();
00035 var menu = self.find('.dropdown-menu');
00036 menu.css('margin-left', -200)
00037 toWidth = Math.max(needWidth, menu.width());
00038 marginleft = Math.min(0, needWidth - menu.width());
00039 if(marginleft < -2) {
00040 $('li:first-child input', menu).css('border-top-left-radius', '3px');
00041 }
00042 menu.width(toWidth).css('margin-left', marginleft);
00043 self.find('.dropdown').on('click', function(event){
00044 $('.dropbutton_control').not(self).removeClass('dropped');
00045 self.toggleClass('dropped');
00046 event.preventDefault();
00047 return false;
00048 });
00049 });
00050 });
00051 </script>
00052 CUSTOM_DROPBUTTON_JS;
00053 }
00054 return $this->controls_js($out);
00055 }
00056
00057
00062 public function do_success($form)
00063 {
00064 $actions = $this->get_setting('actions', array());
00065 if(isset($actions[$this->value])) {
00066 if(isset($actions[$this->value]['fn']) && is_callable($actions[$this->value]['fn'])) {
00067 $fn = $actions[$this->value]['fn'];
00068 call_user_func($fn, $form);
00069 }
00070 elseif(isset($actions[$this->value]['href']) && is_string(isset($actions[$this->value]['href']))) {
00071 Utils::redirect($actions[$this->value]['href'], true);
00072 }
00073 }
00074 return parent::do_success($form);
00075 }
00076
00077 public function get(Theme $theme)
00078 {
00079 $primary = true;
00080 $controls = array();
00082 foreach ( $this->controls as $index => $control ) {
00083 if($control->is_enabled()) {
00084 $control->add_class('dropbutton_action');
00085 $control->add_class(Utils::slugify($control->input_name()));
00086 if($primary) {
00087 $control->add_class('primary');
00088 $primary = false;
00089 }
00090 $controls[$index] = $control;
00091 }
00092 }
00093 if(count($controls) == 0) {
00094 return '';
00095 }
00096 $this->vars['first'] = array_shift($controls);
00097 $this->vars['actions'] = $controls;
00098 $this->set_template_properties('div', array('id' => $this->get_visualizer()));
00099 $this->add_template_class('ul', 'dropdown-menu');
00100 if(count($controls) > 0) {
00101 $this->add_template_class('div', 'has-drop');
00102 }
00103 else {
00104 $this->add_template_class('div', 'no-drop');
00105 }
00106 return parent::get($theme);
00107 }
00108
00112 public function get_visualizer()
00113 {
00114 return $this->get_id() . '_visualizer';
00115 }
00116
00117
00118 }
00119
00120
00121 ?>