]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #2102: Replaced input/autocomplete with jQuery UI autocomplete (untested)
authorewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>
Mon, 14 Feb 2011 23:41:14 +0000 (23:41 +0000)
committerewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>
Mon, 14 Feb 2011 23:41:14 +0000 (23:41 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@8240 36083f99-b078-4883-b0ff-0f9b5a30f544

js/lib/autocomplete.js [new file with mode: 0644]
views/default/input/autocomplete.php

diff --git a/js/lib/autocomplete.js b/js/lib/autocomplete.js
new file mode 100644 (file)
index 0000000..eb59f51
--- /dev/null
@@ -0,0 +1,39 @@
+/**
+ * 
+ */
+elgg.provide('elgg.autocomplete');
+
+elgg.autocomplete.init = function() {
+       $('.elgg-input-autocomplete').autocomplete({
+               source: elgg.autocomplete.url, //gets set by input/autocomplete
+               minLength: 1,
+               select: function(event, ui) {
+                       var item = ui.item;
+                       $(this).val(item.name);
+       
+                       var hidden = $(this).next();
+                       hidden.val(item.guid);
+               }
+       })
+       
+       //@todo This seems convoluted
+       .data("autocomplete")._renderItem = function(ul, item) {
+               switch (item.type) {
+                       case 'user':
+                       case 'group':
+                               r = item.icon + item.name + ' - ' + item.desc;
+                               break;
+
+                       default:
+                               r = item.name + ' - ' + item.desc;
+                               break;
+               }
+               
+               return $("<li/>")
+                       .data("item.autocomplete", item)
+                       .append(r)
+                       .appendTo(ul);
+       };
+};
+
+elgg.register_event_handler('init', 'system', elgg.autocomplete.init);
\ No newline at end of file
index 1b2652c166182cf6535ee51d81324b7614dae7ae..8fe5ab664e0fbb42098ca6d030048815f82e10c3 100644 (file)
@@ -7,95 +7,31 @@
  *
  * @todo This currently only works for ONE AUTOCOMPLETE TEXT FIELD on a page.
  *
- * @uses $vars['value'] The current value, if any
- * @uses $vars['js'] Any Javascript to enter into the input tag
- * @uses $vars['internalname'] The name of the input field
  * @uses $vars['match_on'] Array | str What to match on. all|array(groups|users|friends|subtype)
  * @uses $vars['match_owner'] Bool.  Match only entities that are owned by logged in user.
  *
  */
 
-global $autocomplete_js_loaded;
+$defaults = array(
+       'class' => '',
+       'value' => '',
+);
 
-$internalname = $vars['internalname'];
-$value = $vars['value'];
+$vars = array_merge($defaults, $vars);
 
-if(!$value) {
-       $value= '';
-}
-
-if($vars['internal_id']) {
-       $id_autocomplete = $vars['internal_id'];
-}
+$vars['class'] = trim("elgg-input-autocomplete {$vars['class']}");
 
 $ac_url_params = http_build_query(array(
        'match_on' => $vars['match_on'],
        'match_owner' => $vars['match_owner'],
 ));
-$ac_url = elgg_get_site_url() . 'pg/livesearch?' . $ac_url_params;
 
-if (!isset($autocomplete_js_loaded)) {
-       $autocomplete_js_loaded = false;
-}
+elgg_register_js('js/lib/autocomplete.js', 'autocomplete', 'footer');
 
 ?>
 
-<!-- show the input -->
-<input type="text" class='autocomplete' name ='<?php echo $internalname; ?>_autocomplete' value='<?php echo $value?>' />
-<input type="hidden" name="<?php echo $internalname; ?>" value='<?php echo $value; ?>' />
-
-<?php
-if (!$autocomplete_js_loaded) {
-       ?>
-<?php //@todo JS 1.8: no ?>
-       <!-- include autocomplete -->
-       <script type="text/javascript" src="<?php echo elgg_get_site_url(); ?>vendors/jquery/jquery.autocomplete.min.js"></script>
-       <script type="text/javascript">
-       function bindAutocomplete() {
-       $('input[type=text].autocomplete').autocomplete("<?php echo $ac_url; ?>", {
-               minChars: 1,
-               matchContains: true,
-               autoFill: false,
-               formatItem: function(row, i, max, term) {
-                       eval("var info = " + row + ";");
-                       var r = '';
-
-                       switch (info.type) {
-                               case 'user':
-                               case 'group':
-                                       r = info.icon + info.name + ' - ' + info.desc;
-                                       break;
-
-                               default:
-                                       r = info.name + ' - ' + info.desc;
-                                       break;
-                       }
-                       return r.replace(new RegExp("(" + term + ")", "gi"), "<b>$1</b>");
-               }
-       });
-
-       $('input[type=text].autocomplete').result(function(event, data, formatted) {
-               eval("var info = " + data + ";");
-               $(this).val(info.name);
-
-               var hidden = $(this).next();
-               hidden.val(info.guid);
-       });
-       }
-
-       $(document).ready(function() {
-       bindAutocomplete();
-       });
-
-       </script>
-
-       <?php
+<script type="text/javascript">
+elgg.autocomplete.url = "<?php elgg_get_site_url() . 'pg/livesearch?' . $ac_url_params; ?>";
+</script> 
+<input type="text" <?php echo elgg_format_attributes($vars); ?> />
 
-       $autocomplete_js_loaded = true;
-} else {
-       ?>
-       <!-- rebind autocomplete -->
-       <?php //@todo JS 1.8: no ?>
-       <script type="text/javascript">bindAutocomplete();</script>
-       <?php
-}
\ No newline at end of file