Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 138 lines (112 sloc) 2.5 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
root = window
$ = root.jQuery

if typeof exports != 'undefined'
  ThingyPicker = exports
else
  ThingyPicker = root.ThingyPicker = {}

###*
A Item is a single, selectable item in a ThingyPicker

@class Item
@constructor
@param {Object} data
@param {Object} options
###
class Item
  ###*
@static
@method itemToHtml
@param {Object} data
@return {String} html string
###
  @itemToHtml: (contact) ->
    "<div class='item' id='#{contact.id}'><img src='#{contact.picture}'/><div class='item-name'>#{contact.name}</div></div>"


  @EVENTS:
    ###*
@event selection-changed
###
    SELECTION_CHANGED: 'selection-changed'

  @SELECTED_CLASS = 'selected'

  constructor: (@data, options) ->
    $.extend(@, options) if options

    @debug ||= false
    @$el = $(@itemToHtml(@data))
    @$el.data("tp-item", this)

    # handle when a item is clicked for selection
    @$el.click (event) =>
      @toggle()

  itemToHtml: (data) =>
    Item.itemToHtml(data)

  ###*
this.data

@method toJSON
@return {Object}
###
  toJSON: =>
    @data

  ###*
true if not overwritten

@method canBeSelected
@return {Boolean}
###
  canBeSelected: =>
    true

  ###*
@method on
@param {jQuery.Event} event
@param {Function} handler
###
  on: (event, handler) =>
    @$el.on(event, handler)

  ###*
selects the item if it is unselected, unselects if it is selected

@method toggle
@return {Item} this
###
  toggle: =>
    if @isSelected() then @deselect() else @select()

  ###*
@method isVisible
@return {Boolean}
###
  isVisible: =>
    @$el.css('display') != "none"


  ###*
delegates to $el.show and removes .filtered from $el
@method show
###
  show: =>
    @$el.show()
    @$el.removeClass 'filtered'

  ###*
delegates to $el.hide and adds the class 'filtered' to $el
@method hide
###
  hide: =>
    @$el.hide()
    @$el.addClass 'filtered'

  ###*
@method deselect
###
  deselect: =>
    if @debug
      console.log("deselect called")
    if @isSelected()
      @$el.removeClass(Item.SELECTED_CLASS)
      @$el.trigger(Item.EVENTS.SELECTION_CHANGED)

  ###*
Marks this item as selected

@method select
###
  select: =>
    if @canBeSelected() and not @isSelected()
      @$el.addClass(Item.SELECTED_CLASS)
      @$el.trigger(Item.EVENTS.SELECTION_CHANGED)

  ###*
@method isSelected
@return {Boolean}
###
  isSelected: =>
    if @debug
      console.log "isSelected() in", $el[0]
    @$el.hasClass("selected")

ThingyPicker.Item = Item
Something went wrong with that request. Please try again.