daisho
Version:
Modular dashboard framework
118 lines (95 loc) • 2.86 kB
text/coffeescript
import El from 'el.js'
import html from '../templates/filter-facets'
import Events from '../events'
import utils from '../utils'
moment = utils.date.moment
yyyymmdd = utils.date.yyyymmdd
class FilterFacets extends El.Form
tag: 'daisho-filter-facets'
html: html
# Supply an object with the mapping of facet fields to display names
#
# facetNames: {
# facet: 'name'
# }
#
# facetCurrency: {
# facet: true
# }
# Facets received from server should be under .results,
# Facet configuration should be under .options
# Filter query string should be set to .query
#
# data: {
# results:
# string: ...
# range: ...
# options: ...
# query: 'string query'
# }
init: ->
super
if !
= {}
'update', =>
ranges = .get 'options.range'
if ranges?
for k, range of ranges
if range.to < range.from
z = range.from
.set 'options.range.' + k + '.from', range.to
.set 'options.range.' + k + '.to', z
.set 'filter', [moment('2015-01-01').format(yyyymmdd), moment().format(yyyymmdd)]
loading: false
# check if we have a facet result with the name
hasResult: (name)->
for facet in .get 'results'
if facet[0] && facet[0].Name == name
if facet[0].Count? && facet[0].Count == 1 && typeof facet[0].Value != 'string'
return false
return true
return false
# Rewrite name?
createName: (name)->
name = name.split(/(?=[A-Z])/).join(' ')
return name.charAt(0).toUpperCase() + name.slice(1)
# Get the number of records
count: ()->
for facet in .get 'facets'
if facet[0] && facet[0].Name == 'Kind'
return facet[0].Count
return 0
# Is the facet a string checkbox?
isStringFacet: (facet)->
return facet && facet[0] && typeof facet[0].Value == 'string' && facet[0].Name != 'Kind'
# Is the facet a range selector?
isRangeFacet: (facet)->
return ! && facet[0].Name != 'Kind' && facet[0].Count > 1
reset: (e)->
# investigate why Events.Change must be called manually
if ?
if != false
.trigger Events.Change
.set 'options', undefined
.set 'query', undefined
else
.trigger Events.Change
.set 'options', undefined
.set 'query', undefined
search: (e)->
p = if ?
if p? && p.then?
= true
p.then =>
= false
if p.catch?
p.catch =>
= false
return p
export default FilterFacets