Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 3.09 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  /* =============================================================
  2.   * bootstrap-typeahead.js v2.0.0
  3.   * http://twitter.github.com/bootstrap/javascript.html#typeahead
  4.   * =============================================================
  5.   * Copyright 2012 Twitter, Inc.
  6. @@ -29,6 +29,8 @@
  7.      this.highlighter = this.options.highlighter || this.highlighter
  8.      this.$menu = $(this.options.menu).appendTo('body')
  9.      this.source = this.options.source
  10. +    this.onselect = this.options.onselect
  11. +    this.strings = true
  12.      this.shown = false
  13.      this.listen()
  14.    }
  15. @@ -38,8 +40,17 @@
  16.      constructor: Typeahead
  17.  
  18.    , select: function () {
  19. -      var val = this.$menu.find('.active').attr('data-value')
  20. -      this.$element.val(val)
  21. +      var val = JSON.parse(this.$menu.find('.active').attr('data-value'))
  22. +        , text
  23. +
  24. +      if (!this.strings) text = val[this.options.property]
  25. +      else text = val
  26. +
  27. +      this.$element.val(text)
  28. +
  29. +      if (typeof this.onselect == "function")
  30. +          this.onselect(val)
  31. +
  32.        return this.hide()
  33.      }
  34.  
  35. @@ -68,6 +79,25 @@
  36.        var that = this
  37.          , items
  38.          , q
  39. +        , value
  40. +
  41. +      this.query = this.$element.val()
  42. +
  43. +      if (typeof this.source == "function")
  44. +        value = this.source(this, this.query)
  45. +        if (value)
  46. +          this.process(value)
  47. +      else
  48. +        this.process(this.source)
  49. +    }
  50. +
  51. +  , process: function (results) {
  52. +      var that = this
  53. +        , items
  54. +        , q
  55. +
  56. +      if (results.length && typeof results[0] != "string")
  57. +          this.strings = false
  58.  
  59.        this.query = this.$element.val()
  60.  
  61. @@ -75,7 +105,9 @@
  62.          return this.shown ? this.hide() : this
  63.        }
  64.  
  65. -      items = $.grep(this.source, function (item) {
  66. +      items = $.grep(results, function (item) {
  67. +        if (!that.strings)
  68. +          item = item[that.options.property]
  69.          if (that.matcher(item)) return item
  70.        })
  71.  
  72. @@ -97,10 +129,14 @@
  73.          , caseSensitive = []
  74.          , caseInsensitive = []
  75.          , item
  76. +        , sortby
  77.  
  78.        while (item = items.shift()) {
  79. -        if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
  80. -        else if (~item.indexOf(this.query)) caseSensitive.push(item)
  81. +        if (this.strings) sortby = item
  82. +        else sortby = item[this.options.property]
  83. +
  84. +        if (!sortby.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
  85. +        else if (~sortby.indexOf(this.query)) caseSensitive.push(item)
  86.          else caseInsensitive.push(item)
  87.        }
  88.  
  89. @@ -117,7 +153,9 @@
  90.        var that = this
  91.  
  92.        items = $(items).map(function (i, item) {
  93. -        i = $(that.options.item).attr('data-value', item)
  94. +        i = $(that.options.item).attr('data-value', JSON.stringify(item))
  95. +        if (!that.strings)
  96. +            item = item[that.options.property]
  97.          i.find('a').html(that.highlighter(item))
  98.          return i[0]
  99.        })
  100. @@ -251,6 +289,8 @@
  101.    , items: 8
  102.    , menu: '<ul class="typeahead dropdown-menu"></ul>'
  103.    , item: '<li><a href="#"></a></li>'
  104. +  , onselect: null
  105. +  , property: 'value'
  106.    }
  107.  
  108.    $.fn.typeahead.Constructor = Typeahead