user-filters-manager basic working crud minus update (delete -> create for this one)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Component.extend
|
||||
apply_filter: ->
|
||||
user_filter: null
|
||||
classNames: ['item']
|
||||
classNameBindings: ['user_filter.is_default:is-default']
|
||||
being_hovered: false
|
||||
actions:
|
||||
apply_filter: -> @apply_filter(@get('user_filter'))
|
||||
delete_filter: ->
|
||||
user_filter = @get('user_filter')
|
||||
@modal 'confirm',
|
||||
title_path: 'general.destroy.are_you_sure'
|
||||
model: user_filter
|
||||
ok: ->
|
||||
model = @get("model")
|
||||
model.destroyRecord()
|
||||
@send 'closeModal'
|
||||
make_default: ->
|
||||
model = @get('user_filter')
|
||||
model.set 'is_default', true
|
||||
model.save().then =>
|
||||
@store.query('dunlop/user-filter', q: {scope_key_eq: model.get('scope_key')})
|
||||
|
||||
mouseEnter: -> @set 'being_hovered', true
|
||||
mouseLeave: -> @set 'being_hovered', false
|
||||
@@ -0,0 +1,82 @@
|
||||
import ModalBase from 'ember-cli-dunlop/mixins/modal-base'
|
||||
import Ember from 'ember'
|
||||
{set} = Ember
|
||||
relative_matcher = /(start of|end of) (day|week|month)/
|
||||
|
||||
export default ModalBase.extend
|
||||
scope_key: Ember.computed.alias 'modal_options.scope_key'
|
||||
content: Ember.computed.alias 'modal_options.content'
|
||||
#class: 'user-filters-manager'
|
||||
error: ''
|
||||
new_filter_name_blank: Ember.computed 'new_filter_name', ->
|
||||
return true unless name = @get('new_filter_name')
|
||||
return true unless typeof name is 'string'
|
||||
return true if name.match /^\s*$/
|
||||
false
|
||||
willOpenModal: ->
|
||||
now = moment()
|
||||
if now.day() is 1
|
||||
@set 'cannot_create_filter_message', t('user_filters.cannot_create_on_monday')
|
||||
else if now.format('D') is '1'
|
||||
@set 'cannot_create_filter_message', t('user_filters.cannot_create_on_first_day_of_month')
|
||||
|
||||
user_filters: Ember.computed 'scope_key', ->
|
||||
return Ember.A() unless scope_key = @get('scope_key')
|
||||
@store.peekAll('dunlop/user-filter').filterBy('scope_key', scope_key).sortBy('name')
|
||||
actions:
|
||||
apply_filter: (user_filter) ->
|
||||
return @set('error', 'no content given') unless content = @get('content')
|
||||
return @set('error', 'content has no filters object to manipulate') unless typeof content.filters is 'object'
|
||||
Object.keys(content.filters).forEach (key) -> delete content.filters[key]
|
||||
for key, value of user_filter.get('filters')
|
||||
if typeof value is 'string' && match = value.match relative_matcher
|
||||
boundary_function = if match[1] is 'end of' then 'endOf' else 'startOf'
|
||||
period_name = switch match[2]
|
||||
when 'week' then 'isoWeek'
|
||||
else match[2]
|
||||
filter_value = moment()[boundary_function](period_name)
|
||||
else
|
||||
filter_value = switch value
|
||||
when 'today'
|
||||
boundary_function = if key.match(/_lt/) then 'endOf' else 'startOf'
|
||||
moment()[boundary_function]('day')
|
||||
else value
|
||||
set content.filters, key, filter_value
|
||||
content.reload() if typeof content.reload is 'function'
|
||||
@set 'globals.flash_message', "Filter #{user_filter.get('name')}"
|
||||
@send 'close'
|
||||
create_new_filter: ->
|
||||
return @set('error', 'no content given') unless content = @get('content')
|
||||
return @set('error', 'content has no filters object to manipulate') unless typeof content.filters is 'object'
|
||||
return @set('error', 'Current given content filters do not respond to toProperties method') unless typeof content.filters.toProperties is 'function'
|
||||
filters = content.filters.toProperties()
|
||||
today_date_string = moment().format('YYYY-MM-DD')
|
||||
start_of_week_string = moment().startOf('isoWeek').format('YYYY-MM-DD')
|
||||
end_of_week_string = moment().endOf('isoWeek').format('YYYY-MM-DD')
|
||||
start_of_month_string = moment().startOf('month').format('YYYY-MM-DD')
|
||||
end_of_month_string = moment().endOf('month').format('YYYY-MM-DD')
|
||||
filter_storage = {}
|
||||
for key, value of content.filters.toProperties()
|
||||
if typeof value is 'string' && match = value.match(/^(\d\d\d\d-\d\d-\d\d)/)
|
||||
date_value = switch match[1]
|
||||
when today_date_string then 'today'
|
||||
when start_of_week_string then 'start of week'
|
||||
when end_of_week_string then 'end of week'
|
||||
when start_of_month_string then 'start of month'
|
||||
when end_of_month_string then 'end of month'
|
||||
else value
|
||||
filter_storage[key] = date_value
|
||||
|
||||
# use relative notation if can be inferred
|
||||
else
|
||||
# literal copy
|
||||
filter_storage[key] = value
|
||||
new_filter = @store.createRecord 'dunlop/user-filter',
|
||||
name: @get('new_filter_name')
|
||||
scope_key: @get('scope_key')
|
||||
filters: filter_storage
|
||||
new_filter.save().then =>
|
||||
@store.query('dunlop/user-filter', q: {scope_key_eq: @get('scope_key')})
|
||||
@get('content').reload()
|
||||
@send 'close'
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
width: auto
|
||||
height: 1em
|
||||
margin: 0em 0em 0em 0.5em
|
||||
.modal.user-filters-manager
|
||||
.is-default
|
||||
font-weight: bold
|
||||
font-size: 1.1em
|
||||
background-color: rgba(200, 200, 200, 0.3)
|
||||
|
||||
table thead tr.table-filters-row, table.ui.table thead tr.table-filters-row
|
||||
input
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
= yield
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'ember-cli-dunlop/components/user-filters-manager/filter-row';
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'ember-cli-dunlop/components/user-filters-manager/modal';
|
||||
@@ -0,0 +1,11 @@
|
||||
.right.floated.content
|
||||
button.ui.tiny.secondary.button{action 'apply_filter'}= t 'general.apply'
|
||||
.content
|
||||
= user_filter.name
|
||||
if being_hovered
|
||||
'
|
||||
button.ui.tiny.negative.icon.button{action 'delete_filter'}: i.delete.icon
|
||||
unless user_filter.is_default
|
||||
'
|
||||
button.ui.tiny.secondary.button{action 'make_default'}= t 'user_filters.make_default'
|
||||
|
||||
@@ -13,10 +13,20 @@ config = ->
|
||||
result = result.filter (model) ->
|
||||
model.scope_key.match match
|
||||
result
|
||||
|
||||
@post '/dunlop/user_filters', (schema, request) ->
|
||||
attributes = JSON.parse(request.requestBody)['dunlop/user_filter']
|
||||
schema.dunlopUserFilters.create(attributes)
|
||||
|
||||
@put '/dunlop/user_filters/:id', (schema, request) ->
|
||||
attributes = JSON.parse(request.requestBody)['dunlop/user_filter']
|
||||
if attributes.is_default
|
||||
for record in schema.dunlopUserFilters.all().filter((r) -> r.scope_key is attributes.scope_key).models
|
||||
record.update is_default: false
|
||||
model = schema.dunlopUserFilters.find(request.params.id)
|
||||
model.update(attributes)
|
||||
model
|
||||
|
||||
@delete '/dunlop/user_filters/:id', (schema, request) ->
|
||||
model = schema.dunlopUserFilters.find(request.params.id)
|
||||
null
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { test, moduleForComponent } from 'ember-qunit'
|
||||
import hbs from 'htmlbars-inline-precompile'
|
||||
|
||||
moduleForComponent 'user-filters-manager/filter-row', 'Integration | Component | user filters manager/filter row', {
|
||||
integration: true
|
||||
}
|
||||
|
||||
test 'it renders', (assert) ->
|
||||
assert.expect 2
|
||||
|
||||
# Set any properties with @set 'myProperty', 'value'
|
||||
# Handle any actions with @on 'myAction', (val) ->
|
||||
|
||||
@render hbs """{{user-filters-manager/filter-row}}"""
|
||||
|
||||
assert.equal @$().text().trim(), ''
|
||||
|
||||
# Template block usage:
|
||||
@render hbs """
|
||||
{{#user-filters-manager/filter-row}}
|
||||
template block text
|
||||
{{/user-filters-manager/filter-row}}
|
||||
"""
|
||||
|
||||
assert.equal @$().text().trim(), 'template block text'
|
||||
Reference in New Issue
Block a user