Skip to content

Commit

Permalink
add more options to confirmation popovers... (#291)
Browse files Browse the repository at this point in the history
...rework the API a little bit, too.

cf #289

I didn't add the advanced positioning, since that would best be
addressed upstream in Bootstrap.
  • Loading branch information
ajb authored Oct 26, 2016
1 parent d498ffc commit bdbf9ed
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion spec/dummy/app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@import 'dvl/components/pagination';
@import 'dvl/components/pagination_compact';
@import 'dvl/components/popovers';
@import 'dvl/components/delete_popover';
@import 'dvl/components/confirmation_popover';
@import 'dvl/components/progress';
@import 'dvl/components/sidebar_data';
@import 'dvl/components/sidebar_nav';
Expand Down
30 changes: 27 additions & 3 deletions spec/dummy/app/views/home/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,39 @@ def main
ul(class: 'delete_list') {
li(class: 'js_delete_1') {
text 'Project 1'
a(icon('minus-circle', 'aria-label' => 'Delete'), class: 'icon_secondary', 'data-confirm' => true, 'data-confirm-with' => 'popover', href: '/delete', 'data-method' => 'delete', 'data-remote' => true)
a(
icon('minus-circle', 'aria-label' => 'Delete'),
class: 'icon_secondary',
'data-confirm' => true,
'data-confirm-with' => 'popover',
href: '/delete',
'data-method' => 'delete',
'data-remote' => true
)
}
li(class: 'js_delete_2') {
text 'Project 2'
a(icon('minus-circle', 'aria-label' => 'Delete'), class: 'icon_secondary', 'data-confirm' => 'This is an important record. <strong>It will be destroyed forever.</strong>', 'data-confirm-with' => 'popover', href: '/delete', 'data-method' => 'delete', 'data-remote' => true)
a(
icon('minus-circle', 'aria-label' => 'Delete'),
class: 'icon_secondary',
'data-confirm' => 'This is an important record. <strong>It will be destroyed forever.</strong>',
'data-confirm-with' => 'popover',
href: '/delete',
'data-method' => 'delete',
'data-remote' => true
)
}
li(class: 'js_delete_3') {
text 'Project 3'
a(icon('minus-circle', 'aria-label' => 'Delete'), class: 'icon_secondary', 'data-confirm' => true, 'data-confirm-with' => 'popover', 'data-confirmation-options' => { 't_delete' => 'Archive' }.to_json, href: '/delete', 'data-method' => 'delete', 'data-remote' => true)
a(
icon('minus-circle', 'aria-label' => 'Delete'),
class: 'icon_secondary',
'data-confirm' => true,
'data-confirm-with' => 'popover',
'data-confirm-text' => 'Archive',
href: '/delete',
'data-method' => 'delete',
'data-remote' => true)
}
}
}, sub: true, hint: 'Popovers can contain headers and alternate button text.'
Expand Down
52 changes: 30 additions & 22 deletions vendor/assets/javascripts/dvl/components/confirmations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ Dvl.Confirmations = {}

class Dvl.Confirmations.Popover
defaults:
t_cancel: 'Cancel'
t_delete: 'Delete'
popoverOpts: {}
'cancel-text': 'Cancel'
'cancel-class': ''
'confirm-class': 'error'
'confirm-text': 'Delete'
'popover-opts': {}
# cancelCb:

constructor: ($el, message, cb, opts = {}) ->
Expand All @@ -15,11 +17,14 @@ class Dvl.Confirmations.Popover

@$el.data('popover-confirmation', @)

@options = $.extend {}, @defaults, @$el.data('confirmation-options'), opts
@options = $.extend {}, @defaults, opts

for i of @defaults
@options[i] = @$el.data(i) if @$el.data(i)?

if message
wrappedMessage = """
<div class='popover_delete_confirmation_message'>
<div class='popover_confirmation_message'>
#{message}
</div>
"""
Expand All @@ -28,27 +33,27 @@ class Dvl.Confirmations.Popover
$.extend({
html: true
content: """
<div class='popover_delete_confirmation'>
<div class='popover_confirmation'>
#{wrappedMessage || ''}
<a class='button error js-confirm-delete' href='#'>#{@options.t_delete}</a>
<a class='button js-cancel' href='#'>#{@options.t_cancel}</a>
<a class='button #{@options['confirm-class']} js-popover-confirm' href='#'>#{@options['confirm-text']}</a>
<a class='button #{@options['cancel-class']} js-popover-cancel' href='#'>#{@options['cancel-text']}</a>
</div>
"""
trigger: 'manual'
placement: 'bottom'
}, @options.popoverOpts)
}, @options['popover-opts'])
)

@$el.popover('show')

$tip = @$el.data('bs.popover').$tip

$tip.on 'click', '.button', (e) =>
if $(e.target).hasClass('js-confirm-delete')
if $(e.target).hasClass('js-popover-confirm')
@$el.trigger('confirm.dvl')
cb()

if $(e.target).hasClass('js-cancel')
if $(e.target).hasClass('js-popover-cancel')
@$el.trigger('cancel.dvl')
@options.cancelCb?()

Expand All @@ -66,19 +71,22 @@ class Dvl.Confirmations.Popover

class Dvl.Confirmations.Modal
defaults:
t_generic: 'This is a destructive action. Please confirm.'
t_title: 'Are you sure?'
t_cancel: 'Cancel'
t_confirm: 'Confirm'
'generic-message': 'This is a destructive action. Please confirm.'
'title': 'Are you sure?'
'cancel-text': 'Cancel'
'confirm-text': 'Confirm'
# cancelCb:

constructor: ($el, message, cb, opts = {}) ->
@options = $.extend {}, @defaults, $el.data('confirmation-options'), opts
@options = $.extend {}, @defaults, opts

for i of @defaults
@options[i] = $el.data(i) if $el.data(i)?

message ||= @options.t_generic
message ||= @options['generic-message']

$modal = Dvl.Modal.init(
Dvl.Modal.getHTML(title: @options.t_title),
Dvl.Modal.getHTML(title: @options['title']),
removeOnClose: true
)

Expand All @@ -90,9 +98,9 @@ class Dvl.Confirmations.Modal
<div class='modal_footer'>
<div class='modal_footer_primary'>
<a class='button white' data-dismiss='modal'>
#{@options.t_cancel}
#{@options['cancel-text']}
</a>
<button class='button error js-confirm-delete'>#{@options.t_confirm}</button>
<button class='button error js-popover-confirm'>#{@options['confirm-text']}</button>
</div>
</div>
"""
Expand All @@ -101,12 +109,12 @@ class Dvl.Confirmations.Modal
$el.trigger('cancel.dvl')
@options.cancelCb?()

$modal.one 'click', '.js-confirm-delete', ->
$modal.one 'click', '.js-popover-confirm', ->
$el.trigger('confirm.dvl')
cb()

$modal.
modal('hide').
remove()

$modal.find('.js-confirm-delete').focus()
$modal.find('.js-popover-confirm').focus()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.popover_delete_confirmation {
.popover_confirmation {
.button {
display: block;
padding-left: 1.5rem;
Expand All @@ -10,7 +10,7 @@
}
}

.popover_delete_confirmation_message {
.popover_confirmation_message {
font-size: $fontSmall;
text-align: center;
margin-bottom: $rhythm;
Expand Down

0 comments on commit bdbf9ed

Please sign in to comment.