Coder Social home page Coder Social logo

guilleiguaran / data-confirm-modal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ifad/data-confirm-modal

0.0 2.0 0.0 93 KB

Makes Rails' link_to confirm: 'foo' build a Bootstrap Modal instead of calling the browser's confirm() API.

License: MIT License

Ruby 100.00%

data-confirm-modal's Introduction

Data-Confirm Modal

Gem Version

Uses Bootstrap's modals in place of the browser's builtin confirm() API for links generated through Rails' helpers with the :confirm option.

Any link with the data-confirm attribute will trigger a Bootstrap modal.

HTML in the modal supported, and also the ability to have the user input a certain value, for extra willingness confirmation (inspired by GitHub's "delete repository" function).

Installation

Add this line to your application's Gemfile:

gem 'data-confirm-modal'

if you are stuck on Bootstrap 2.3, use the bootstrap2 branch:

gem 'data-confirm-modal', github: 'ifad/data-confirm-modal', branch: 'bootstrap2'

Then execute:

$ bundle

And then require the Javascript from your application.js:

//= require data-confirm-modal

Usage

With Rails (example)

By default, the Gem's Javascript overrides Rails' [data-confirm behaviour][] for you, with no change required to your code. The modal is applicable to <a>, <button> and <input[submit]> elements by default.

<%= link_to 'Delete', data: {confirm: 'Are you sure?'} %>

The modal's title text can be customized using the data-title attribute. If data-title is not defined it falls back to the title attribute.

<%= link_to 'Delete', data: {title: 'Are You Sure?'} %>

The modal's 'confirm' button text can be customized using the data-commit attribute.

<%= link_to 'Delete', data: {confirm: 'Are you sure?', commit: 'Sure!'} %>

Add a data-verify attribute to your input if you want an extra confirmation from the user. The modal will contain an extra text input, and the user will be asked to type the verification value before being allowed to proceed.

<%= link_to 'Delete', data: {confirm: 'Are you sure?', verify: 'Foo', verify_text: 'Type "Foo" to confirm'} %>

You can set global setting using dataConfirmModal.setDefaults, for example:

dataConfirmModal.setDefaults({
  title: 'Confirm your action',
  commit: 'Continue',
  cancel: 'Cancel'
});

To restore default settings use dataConfirmModal.restoreDefaults().

Without Rails, with data attributes (example)

Given an element with data-confirm attributes in place, such as

<a id="foo" href="#" data-confirm="Really do this?" data-commit="Do it" data-cancel="Not really"/>

you can then invoke .confirmModal() on it using:

$('#foo').confirmModal();

that'll display the confirmation modal. If the user confirms, then the #foo link will receive a click event.

Without Rails, without data attributes (example)

Use dataConfirmModal.confirm() passing any of the supported options, and pass an onConfirm and onCancel callbacks that'll be invoked when the user clicks the confirm or the cancel buttons.

dataConfirmModal.confirm({
  title: 'Are you sure?',
  text: 'Really do this?',
  commit: 'Yes do it',
  cancel: 'Not really',
  zIindex: 10099,
  onConfirm: function() { alert('confirmed') },
  onCancel:  function() { alert('cancelled') }
});

Modal Options

The default bootstrap modal options can be passed either via JavaScript or through data attributes.

 $('#foo').confirmModal({backdrop: 'static', keyboard: false});

or

 <a href="#" data-confirm="Really?" data-backdrop="static" data-keyboard="false">

Authors

Background

Spinned off a corporate IFAD application in which an user did too much damage because the confirm wasn't THAT explicit ... ;-).

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

data-confirm-modal's People

Contributors

vjt avatar magicmarkker avatar nark avatar roman-wb avatar dminchev avatar aaronlerch avatar mehanoid avatar robincwillis avatar amedeo avatar etothepiipower avatar emiddleton avatar codextremist avatar jordigg avatar lucaspiller avatar parabuzzle avatar omockler avatar tommyblue avatar westonganger avatar kntmrkm avatar

Watchers

Guillermo Iguaran avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.