nooku is a Public Portfolio from Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

Create your own Portfolio or check out other free and subscription products from Assembla that will help accelerate your development projects.

nooku-framework

Commit 4474

User picture
  • Author: johanjanssens
  • 2012-02-10 02:54 (about 2 years ago)

re #213 : Removed KTemplateHelperAutocomplete. Moved functionality into KTemplateHelperListbox. The settings of the autocomplete helper have been cleaned up to match those of the listbox. You can tell any listbox to render as an autocomplete by adding 'autocomplete' => true to it's config parameters.

Files Affected

 
44734474
269
    public function autocomplete($config = array())
269
    public function autocomplete($config = array())
270
    {
270
    {
271
        $config = new KConfig($config);
271
        $config = new KConfig($config);
272
        
 
 
273
        $config->append(array(
272
        $config->append(array(
274
            'identifier'    => null,
273
            'identifier'    => null,
275
            'element'       => null,
274
            'element'       => null,
276
            'filter_column' => 'name'
275
            'path'          => 'name',
277
        ));
276
            'filter'        => array(),
278
 
277
            'validate'        => true,
279
        if(!is_a($config->identifier, 'KServiceIdentifier')) {
278
            'selected'        => null    
280
            $config->identifier = $this->getIdentifier($config->identifier);
 
 
281
        }
 
 
282
        
 
 
283
        $config->append(array(
 
 
284
            'url'     => JRoute::_('&option=com_'.$config->identifier->package.'&view='.$config->identifier->name.'&format=json', false)
 
 
285
        ))->append(array(
279
        ))->append(array(
286
            'options' => array(
280
            'value_element' => $config->element.'-value',
287
                'filter'     => array(
281
            'attribs' => array(
288
                    'type' => 'contains',
282
                'id'    => $config->element,
289
                    'path' => $config->filter_column                    
283
                'type'  => 'text',
290
                ),
284
                'class' => 'inputbox value',
291
                'urlOptions' => array(
285
                'size'    => 60
292
                    'queryVarName' => 'search'
286
            ),
293
                ),
287
        ))->append(array(
294
                'requestOptions' => array(
288
            'options' => array( 
295
                    'method' => 'get'
289
                'valueField'     => $config->value_element,
 
 
290
                'filter'         => array('path' => $config->path),
 
 
291
                'requestOptions' => array('method' => 'get'),
 
 
292
                'urlOptions'     => array(
 
 
293
                    'queryVarName' => 'search',
 
 
294
                    'extraParams'  => KConfig::unbox($config->filter)
296
                )
295
                )
297
            )
296
            )
298
        ));
297
        ));
299
 
298
 
 
 
299
        if($config->validate) 
 
 
300
        {
 
 
301
            $config->attribs['data-value']  = $config->element.'-value';
 
 
302
            $config->attribs['data-value'] .= ' ma-required';
 
 
303
        }
 
 
304
 
 
 
305
        if(!isset($config->url)) 
 
 
306
        {
 
 
307
            $identifier = $this->getIdentifier($config->identifier);
 
 
308
            $config->url =
JRoute::_('index.php?option=com_'.$identifier->package.'&view='.$identifier->name.'&format=json', false);
 
 
309
        }
 
 
310
 
300
        $html = '';
311
        $html = '';
301
 
312
 
302
        // Load the necessary files if they haven't yet been loaded
313
        // Load the necessary files if they haven't yet been loaded
...
 
...
 
310
        $html .= "
321
        $html .= "
311
        <script>
322
        <script>
312
            window.addEvent('domready', function(){                
323
            window.addEvent('domready', function(){                
313
                new Koowa.Autocomplete($('".$config->element."'), ".json_encode($config->url).", ".$config->options.");
324
                new Koowa.Autocomplete($('".$config->element."'), ".json_encode($config->url).", ".json_encode(KConfig::unbox($config->options)).");
314
            });
325
            });
315
        </script>";
326
        </script>";
 
 
327
 
 
 
328
        $html .= '<input '.KHelperArray::toString($config->attribs).' />';
 
 
329
        $html .= '<input '.KHelperArray::toString(array(
 
 
330
            'type'  => 'hidden',
 
 
331
            'name'  => $config->value,
 
 
332
            'id'    => $config->element.'-value',
 
 
333
            'value' => $config->selected
 
 
334
           )).' />';
316
 
335
 
317
        return $html;
336
        return $html;
318
    }
337
    }
44734474
35
     * @return    string    Html
35
     * @return    string    Html
36
     * @see __call()
36
     * @see __call()
37
     */
37
     */
 
 
38
    protected function _render($config = array())
 
 
39
     {
 
 
40
         $config = new KConfig($config);
 
 
41
         $config->append(array(
 
 
42
             'autocomplete' => false
 
 
43
         ));
 
 
44
 
 
 
45
         if($config->autocomplete) {
 
 
46
             $result = $this->_autocomplete($config);
 
 
47
         } else {
 
 
48
             $result = $this->_listbox($config);
 
 
49
         }
 
 
50
 
 
 
51
         return $result;
 
 
52
     }
 
 
53
 
 
 
54
    /**
 
 
55
     * Generates an HTML optionlist based on the distinct data from a model column.
 
 
56
     * 
 
 
57
     * The column used will be defined by the name -> value => column options in
 
 
58
     * cascading order. 
 
 
59
     * 
 
 
60
     * If no 'model' name is specified the model identifier will be created using 
 
 
61
     * the helper identifier. The model name will be the pluralised package name. 
 
 
62
     * 
 
 
63
     * If no 'value' option is specified the 'name' option will be used instead. 
 
 
64
     * If no 'text'  option is specified the 'value' option will be used instead.
 
 
65
     * 
 
 
66
     * @param     array     An optional array with configuration options
 
 
67
     * @return    string    Html
 
 
68
     * @see __call()
 
 
69
     */
38
    protected function _listbox($config = array())
70
    protected function _listbox($config = array())
39
     {
71
     {
40
        $config = new KConfig($config);
72
        $config = new KConfig($config);
...
 
...
 
42
            'name'          => '',
74
            'name'          => '',
43
            'attribs'      => array(),
75
            'attribs'      => array(),
44
            'model'          => KInflector::pluralize($this->getIdentifier()->package),
76
            'model'          => KInflector::pluralize($this->getIdentifier()->package),
 
 
77
            'deselect'    => true,
45
            'prompt'      => '- Select -', 
78
            'prompt'      => '- Select -', 
46
            'unique'      => true
79
            'unique'      => true
47
        ))->append(array(
80
        ))->append(array(
...
 
...
 
50
            'identifier' =>
'com://'.$this->getIdentifier()->application.'/'.$this->getIdentifier()->package.'.model.'.KInflector::pluralize($conf
ig->model)
83
            'identifier' =>
'com://'.$this->getIdentifier()->application.'/'.$this->getIdentifier()->package.'.model.'.KInflector::pluralize($conf
ig->model)
51
        ))->append(array(
84
        ))->append(array(
52
            'text'        => $config->value,
85
            'text'        => $config->value,
53
            'column'    => $config->value,
 
 
54
            'deselect'  => true,
 
 
55
        ))->append(array(
86
        ))->append(array(
56
            'filter'     => array('sort' => $config->text),
87
            'filter'     => array('sort' => $config->text),
57
        ));
88
        ));
...
 
...
 
83
     }
114
     }
84
 
115
 
85
    /**
116
    /**
 
 
117
     * Renders a listbox with autocomplete behavior
 
 
118
     *
 
 
119
     * @see    KTemplateHelperBehavior::autocomplete
 
 
120
     * @return string    The html output
 
 
121
     */
 
 
122
    protected function _autocomplete($config = array())
 
 
123
     {        
 
 
124
        $config = new KConfig($config);
 
 
125
        $config->append(array(
 
 
126
            'name'         => '',
 
 
127
            'attribs'     => array(),
 
 
128
            'model'         => KInflector::pluralize($this->getIdentifier()->package),
 
 
129
            'validate'   => true,
 
 
130
        ))->append(array(
 
 
131
            'value'         => $config->name,
 
 
132
            'selected'   => $config->{$config->name},
 
 
133
            'identifier' =>
'com://'.$this->getIdentifier()->application.'/'.$this->getIdentifier()->package.'.model.'.KInflector::pluralize($conf
ig->model)
 
 
134
        ))->append(array(
 
 
135
            'text'        => $config->value,
 
 
136
        ))->append(array(
 
 
137
            'filter'     => array(),
 
 
138
        ));
 
 
139
 
 
 
140
        //For the autocomplete behavior
 
 
141
        $config->element = $config->value;
 
 
142
        $config->path    = $config->text;
 
 
143
 
 
 
144
        $html = $this->getTemplate()->getHelper('behavior')->autocomplete($config);
 
 
145
 
 
 
146
        return $html;
 
 
147
     }
 
 
148
 
 
 
149
    /**
86
     * Search the mixin method map and call the method or trigger an error
150
     * Search the mixin method map and call the method or trigger an error
87
     * 
151
     * 
88
     * This function check to see if the method exists in the mixing map if not
152
     * This function check to see if the method exists in the mixing map if not
...
 
...
 
104
            $config = $arguments[0];
168
            $config = $arguments[0];
105
            $config['name']  = KInflector::singularize(strtolower($method));
169
            $config['name']  = KInflector::singularize(strtolower($method));
106
 
170
 
107
            return $this->_listbox($config);
171
            return $this->_render($config);
108
        }
172
        }
109
 
173
 
110
        return parent::__call($method, $arguments);
174
        return parent::__call($method, $arguments);