• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/controls/formcontrolautocomplete.php

00001 <?php
00002 
00003 namespace Habari;
00004 
00008 class FormControlAutocomplete extends FormControl
00009 {
00010   static $outpre;
00011 
00015   public function _extend()
00016   {
00017     $this->properties['type'] = 'text';
00018     $this->properties['class'] = 'autocomplete_control';
00019 
00020 
00021     $config = new \stdClass();
00022     $config->minimumInputLength = 2;
00023     $config->tags = array();
00024     $this->properties['data-autocomplete-config'] = $config;
00025   }
00026 
00031   public function pre_out()
00032   {
00033     $out = '';
00034     if ( !self::$outpre ) {
00035       self::$outpre = true;
00036       $out = <<<  CUSTOM_AUTOCOMPLETE_JS
00037         <script type="text/javascript">
00038 controls.init(function(){
00039   $('.autocomplete_control').each(function(){
00040     var self = $(this);
00041     var autocomplete_config = self.data('autocomplete-config');
00042     if(autocomplete_config.ajax_url) {
00043       $.extend(autocomplete_config, {
00044         ajax: {
00045           url: autocomplete_config.ajax_url,
00046           dataType: 'json',
00047           data: function (term, page) { return {q: term} },
00048           results: function (data, page) { return data.data; }
00049         }
00050       });
00051     }
00052     if(autocomplete_config.ajax_ishtml) {
00053       $.extend(autocomplete_config, {
00054         escapeMarkup: function(m) { return m; }
00055       });
00056     }
00057     if(autocomplete_config.allow_new) {
00058       $.extend(autocomplete_config, {
00059         createSearchChoice: function(term) {
00060           return {id:term, text:term};
00061         }
00062       });
00063     }
00064     if(autocomplete_config.init_selection) {
00065       $.extend(autocomplete_config, {
00066         initSelection: function(element, callback) {
00067           var data = [];
00068           $(element.val().split(',')).each(function () {
00069             data.push({id: this, text: this});
00070           });
00071           callback(data);
00072         }
00073       });
00074     }
00075     $.extend(autocomplete_config, {
00076       width: 'resolve'
00077     });
00078     console.log(autocomplete_config);
00079     self.select2(autocomplete_config);
00080   });
00081 
00082   $('.autocomplete_control').closest('form').submit(function(){
00083   });
00084 });
00085         </script>
00086 CUSTOM_AUTOCOMPLETE_JS;
00087     }
00088     return $this->controls_js($out);
00089   }
00090 
00098   public function set_ajax($url, $ishtml = false) {
00099     $this->set_settings(
00100       array(
00101         'ajax_url' => $url,
00102         'ajax_ishtml' => $ishtml,
00103       ),
00104       false
00105     );
00106     return $this;
00107   }
00108 
00109 
00110   public function get(Theme $theme)
00111   {
00112     if(isset($this->settings['ajax_url'])) {
00113       $this->properties['data-autocomplete-config']->ajax_url = $this->settings['ajax_url'];
00114     }
00115     if(isset($this->settings['ajax_ishtml'])) {
00116       $this->properties['data-autocomplete-config']->ajax_ishtml = $this->settings['ajax_ishtml'];
00117     }
00118     if(isset($this->settings['allow_new'])) {
00119       $this->properties['data-autocomplete-config']->tokenSeparators = array(',');
00120       $this->properties['data-autocomplete-config']->allow_new = true;
00121     }
00122     if(isset($this->settings['init_selection'])) {
00123       $this->properties['data-autocomplete-config']->init_selection = true;
00124     }
00125     $this->properties['data-autocomplete-config'] = json_encode($this->properties['data-autocomplete-config'] );
00126 /*
00127     Stack::add('template_header_javascript', 'select2' );
00128     Stack::add('template_stylesheet', 'select2-css');
00129 */
00130     return parent::get($theme);
00131   }
00132 
00133 
00134 }
00135 
00136 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1