From 21195ee705f2f3d49618e8851910203d465dbead Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 3 Dec 2018 12:36:15 -0300 Subject: [PATCH] Personal user filters component progress --- addon/components/user-filters-manager.coffee | 2 +- .../user-filters-manager/modal.coffee | 54 +++++++++++++++++-- .../user-filters-manager/-modal.emblem | 14 ++--- tests/dummy/app/i18n/translations.coffee | 8 +++ .../table-filters/user-filters-manager.coffee | 1 + 5 files changed, 67 insertions(+), 12 deletions(-) diff --git a/addon/components/user-filters-manager.coffee b/addon/components/user-filters-manager.coffee index e13f640..fce1662 100644 --- a/addon/components/user-filters-manager.coffee +++ b/addon/components/user-filters-manager.coffee @@ -6,4 +6,4 @@ export default Ember.Component.extend class: 'tiny black' actions: open_modal: -> - @modal 'user-filters-manager/modal', scope_key: @get('scope_key'), content: @get('content') + @modal 'user-filters-manager/modal', scope_key: @get('scope_key'), content: @get('content'), title_path: 'user_filters.modal_title' diff --git a/app/components/user-filters-manager/modal.coffee b/app/components/user-filters-manager/modal.coffee index e97f93f..116c6e4 100644 --- a/app/components/user-filters-manager/modal.coffee +++ b/app/components/user-filters-manager/modal.coffee @@ -1,6 +1,7 @@ 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' @@ -11,7 +12,12 @@ export default ModalBase.extend 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') @@ -22,16 +28,54 @@ export default ModalBase.extend 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') - set content.filters, key, value + 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', 'Current given content filters do not respond to toProperties method') unless filters = @get('content.filters').toProperties() + 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: filters - new_filter.save() + filters: filter_storage + new_filter.save().then => + @store.query('dunlop/user-filter', q: {scope_key_eq: @get('scope_key')}) + @get('content').reload() @send 'close' diff --git a/app/templates/components/user-filters-manager/-modal.emblem b/app/templates/components/user-filters-manager/-modal.emblem index b33e73d..7df6aaa 100644 --- a/app/templates/components/user-filters-manager/-modal.emblem +++ b/app/templates/components/user-filters-manager/-modal.emblem @@ -11,9 +11,11 @@ if error .header= user_filter.name else = user_filter.name - -.ui.horizontal.divider Create new from current selection -.ui.action.input - = input value=new_filter_name - button.ui.primary.button disabled=new_filter_name_blank click='create_new_filter' - span CREATE +.ui.horizontal.divider= t 'user_filters.create_new_filter_title' +if cannot_create_filter_message + .ui.visible.warning.message= cannot_create_filter_message +else + .ui.action.input + = input value=new_filter_name + button.ui.primary.button disabled=new_filter_name_blank click='create_new_filter' + span= t 'user_filter.create_new_filter_button' diff --git a/tests/dummy/app/i18n/translations.coffee b/tests/dummy/app/i18n/translations.coffee index 3289681..f0ea22b 100644 --- a/tests/dummy/app/i18n/translations.coffee +++ b/tests/dummy/app/i18n/translations.coffee @@ -18,6 +18,8 @@ window.$translations = clear: 'Clear' apply: 'Apply' and: 'and' + new: 'New' + create: 'Create' confirm: cancel: 'Cancel' confirm: 'Yes' @@ -68,3 +70,9 @@ window.$translations = between: 'Between' inclusive: 'inclusive' lower_larger_than_upper_error: 'lower limit (%{lower}) cannot be greater than the upper limit (%{upper}).' + user_filters: + modal_title: 'Manage personal filters' + create_new_filter_title: 'Create new filter from current selection' + create_new_filter_button: '${general.create}' + cannot_create_on_monday: 'Creating a filter on Monday is ambiguous since the smart relative time feature cannot distinguish today from the start of the week' + cannot_create_on_first_day_of_month: 'Creating a filter on the first day of the month is ambiguous since the smart relative time feature cannot distinguish today from the start of the month' diff --git a/tests/dummy/app/routes/table-filters/user-filters-manager.coffee b/tests/dummy/app/routes/table-filters/user-filters-manager.coffee index 96fb3de..ea423a6 100644 --- a/tests/dummy/app/routes/table-filters/user-filters-manager.coffee +++ b/tests/dummy/app/routes/table-filters/user-filters-manager.coffee @@ -11,6 +11,7 @@ export default Ember.Route.extend filters = Ember.Object.create key1_eq: 45 key2_matches: 'blah%' + key3_gteq: moment() result = PagedArray.create modelName: 'dunlop/user-filter' store: @store