close

  
<div><p> 
/**
 * Enumeration control to show a autocomplete/combobox of all instances of a specific type
 * For example, to show an enumeration of all US Presidents:
 *
 * $("input").suggesttype({type:"/government/us_president"});
 */
;(function($) {
    if (!$.suggest) {
      alert("$.suggest required");
    }

    var base = {
      _init: $.suggest.suggest.prototype._init,
      response: $.suggest.suggest.prototype.response
    };

    $.suggest("suggesttype",
              $.extend(true, {}, $.suggest.suggest.prototype, {
                         _init: function() {
                           var self = this;
                           // call super._init()
                           base._init.call(self);

                           this.result = null;

                           // add drop down button
                           var $dropdown = this.$input.next(".fbs-list-dropdown");
                           if ($dropdown.length) {
                             this.$dropdown = $dropdown;
                             this.$dropdown.unbind(".suggest");
                           }
                           else {
                             this.$dropdown = $('');
                             this.$input.after(this.$dropdown);
                           }
                           this.$dropdown.bind("click.suggest", function() {
                                                 self.request();
                                                 self.$input.focus();
                                               });
                         },

                         // jquery.suggest clears list on focus if input is empty
                         // override not to do this
                         focus: function(e) {
                           this.focus_hook();
                         },

                         textchange: function() {
                           window.clearTimeout(this.textchange.timeout);
                           var self = this;
                           function timeout() {
                             self.textchange_delay();
                           }
                           this.textchange.timeout = window.setTimeout(timeout, 200);
                         },

                         textchange_delay: function() {
                           this.$input.removeData("data.suggest");
                           var val = $.trim(this.$input.val());
                           this.request(val);
                         },

                         request: function(val) {
                           if (this.result) {
                             this.response(val);
                             return;
                           }

                           if (this.ac_xhr) {
                             this.ac_xhr.abort();
                           }

                           var mql = '{"query":[{"id":null,"name":null,"optional":true,"/common/topic/alias":[{"optional":true,"value":null}],\"sort":"name","type":"'+this.options.type+'","limit":'+this.options.limit+'}],"lang":"'+this.options.lang+'"}';
                           var data = {
                             query: mql
                           };
                           var self = this;
                           $.ajax({
                                    "type": "GET",
                                    "url": this.options.service_url + this.options.service_path,
                                    "data": data,
                                    "beforeSend": function(xhr) {
                                      self.ac_xhr = xhr;
                                    },
                                    "success": function(data, status) {
                                      if (data.code == "/api/status/ok") {
                                        self.result = data.result;
                                        self.response(val, true);
                                      }
                                    },
                                    "dataType": this.jsonp ? "jsonp" : "json"
                                  });

                         },

                         build: function() {
                           var self = this;
                           this.$list.empty();
                           $.each(this.result, function() {
                                    var data = this;
                                    if ('class' in self.options) {
                                      data['class'] = self.options['class'];
                                    }
                                    if (!data.alias &amp;&amp; "/common/topic/alias" in data) {
                                      // map data["/common/topic/alias"] to alias
                                      data.alias = $.map(data["/common/topic/alias"], function(n,i) {
                                                           return n.value;
                                                         });
                                    }

                                    var $li = self.create_item(data)
                                      .bind("mouseover.suggest", function(e) {
                                              self.mouseover_item(e);
                                            })
                                      .data("data.suggest", data);
                                      self.$list.append($li);
                                    });
                         },

                         response: function(val, build) {
                           var self = this;
                           // check if list has been built?
                           if (build) {
                             this.build();
                           }

                           var $items = $("&gt;li", this.$list);
                           if (val == null || val == "") {
                             // show all items
                             $items.show();
                           }
                           else {
                             var prefix = new RegExp("\\b" + val, "i");
                             $.each($items, function() {
                                      var $this = $(this);
                                      var data = $(this).data("data.suggest");
                                      if (prefix.test(data.name)) {
                                        $this.show();
                                      }
                                      else {
                                        var alias = false;
                                        $.each(data.alias, function(i,n) {
                                                 if (prefix.test(n)) {
                                                   alias = true;
                                                   return false;
                                                 }
                                               });
                                        if (alias) {
                                          $this.show();
                                        }
                                        else {
                                          $this.hide();
                                        }
                                      }
                                    });
                             if (!$("&gt;li:visible", this.$list).length) {
                               $items.show();
                             }
                           }

                           this.$input.data("original.suggest", this.$input.val());
                           this.$pane.data("suggest", this);
                           this.$list.data("suggest", this);

                           this.position();
                           this.$pane.show();
                         },

                         check_response: function(response_data) {
                           return true;
                         },

                         create_item: function(data) {
                           var opts = this.options,
                           $li = $('</p><li class="' + opts.css.item + '">');
                           $li.append('<div class="' + opts.css.item_name + '">' + data.name + '</div>');
                           return $li;
                         },

                         _pane_selector: function(options) {
                           return "#suggesttype" + options.type.replace(/\//g, "_");
                         }

                       }));



     $.extend($.suggest.suggesttype, {
                defaults:  $.extend(true, {}, $.suggest.suggest.defaults, {
                                      type: "/location/country",
                                      limit: 500,
                                      service_url: "http://www.freebase.com",
                                      service_path: "/api/service/mqlread",
                                      flyout_service_url: "http://flyout.freebaseapps.com",
                                      lang: "/lang/en",
                                      "class": "topic"  // required by flyoutservice
                                    })
             });

})(jQuery);

 </div>

Comments

Hide