Skip to content

Commit

Permalink
Merge pull request #14 from igorakaamigo/doing-with-a-modal
Browse files Browse the repository at this point in the history
A polite jQuery.fn.modal() call added
  • Loading branch information
igorakaamigo authored Jan 25, 2018
2 parents 77723d2 + c307506 commit 71dbb4b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- A development chapter improved at README.md

### Fixed
- A polite jQuery.fn.modal() call added
- A yes button click did not work correctly
- A close button click did nothing
- A repo link fixed at README.md
Expand Down
6 changes: 6 additions & 0 deletions lib/maac.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Rails.delegate document, 'a[data-confirm]', 'confirm', (event) ->
showAModal(this, 'Yes', 'No', 'Close', 'Confirm')
false

safeBsModal = (arg) ->
if @jQuery and @jQuery.fn.modal
@jQuery('.modal').modal(arg)

showAModal = (link, defaultYes, defaultNo, defaultClose, defaultTitle) ->
modal = document.createElement('div')
modal.className = 'modal'
Expand Down Expand Up @@ -46,6 +50,7 @@ showAModal = (link, defaultYes, defaultNo, defaultClose, defaultTitle) ->

close = ->
Rails.stopEverything(event)
safeBsModal('hide')
modal.remove()

Rails.delegate modal, '.close,.btn-primary', 'click', close
Expand All @@ -56,3 +61,4 @@ showAModal = (link, defaultYes, defaultNo, defaultClose, defaultTitle) ->
Rails.fire link, 'click'

document.body.appendChild(modal)
safeBsModal()
10 changes: 10 additions & 0 deletions spec/features/maac_features_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
expect(page).to have_css("#{footer_selector} > button.btn.btn-primary[type=button]", count: 1, text: text_for_no)
end
end

it 'should call a $(".modal").modal()'
end

context 'clicking on a link with a data-confirm attribute and customizations', js: true do
Expand Down Expand Up @@ -139,6 +141,8 @@
expect(page).to have_css("#{footer_selector} > button.btn.btn-primary[type=button]", count: 1, text: text_for_no)
end
end

it 'should call a $(".modal").modal()'
end

context 'while modal is shown', js: true do
Expand All @@ -154,6 +158,8 @@
it 'should not have a target page text' do
expect(page).not_to have_text(target_page_text)
end

it 'should call a $(".modal").modal("hide")'
end

context 'after pressing a No button' do
Expand All @@ -166,6 +172,8 @@
it 'should not have a target page text' do
expect(page).not_to have_text(target_page_text)
end

it 'should call a $(".modal").modal("hide")'
end

context 'after pressing a Yes button' do
Expand All @@ -174,6 +182,8 @@
it 'should have a target page text' do
expect(page).to have_text(target_page_text)
end

it 'should call a $(".modal").modal("hide")'
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions vendor/assets/javascripts/maac.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@

/*!
Modal As A Confirm (MAAC) v0.1.0-alpha (2018-01-26T01:07:26+03:00)
Modal As A Confirm (MAAC) v0.1.0-alpha (2018-01-26T02:02:08+03:00)
https://github.com/igorakaamigo/maac
Released under the MIT license:
https://github.com/igorakaamigo/maac/blob/master/MIT-LICENSE
*/

(function() {
var showAModal;
var safeBsModal, showAModal;

Rails.delegate(document, 'a[data-confirm]', 'confirm', function(event) {
showAModal(this, 'Yes', 'No', 'Close', 'Confirm');
return false;
});

safeBsModal = function(arg) {
if (this.jQuery && this.jQuery.fn.modal) {
return this.jQuery('.modal').modal(arg);
}
};

showAModal = function(link, defaultYes, defaultNo, defaultClose, defaultTitle) {
var close, dialogClose, dialogNo, dialogTitle, dialogYes, modal;
modal = document.createElement('div');
Expand All @@ -32,6 +38,7 @@ https://github.com/igorakaamigo/maac/blob/master/MIT-LICENSE
modal.querySelector('.modal-header > h5').innerText = dialogTitle;
close = function() {
Rails.stopEverything(event);
safeBsModal('hide');
return modal.remove();
};
Rails.delegate(modal, '.close,.btn-primary', 'click', close);
Expand All @@ -40,7 +47,8 @@ https://github.com/igorakaamigo/maac/blob/master/MIT-LICENSE
link.removeAttribute('data-confirm');
return Rails.fire(link, 'click');
});
return document.body.appendChild(modal);
document.body.appendChild(modal);
return safeBsModal();
};

}).call(this);

0 comments on commit 71dbb4b

Please sign in to comment.