Skip to content

Commit

Permalink
Adding event to object that gets passed to event handlers for modal b…
Browse files Browse the repository at this point in the history
…uttons.
  • Loading branch information
kylepaulsen committed Jan 16, 2015
1 parent 15cf3d8 commit ed4d566
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var modalObj = nanoModal("sup", {buttons: [ /* button definition objects here */

When defining custom button objects, here are the properties you can set and what they do:
- **text**: The string of text that goes inside the button.
- **handler**: This can be the string "hide" *OR* a function. If it is the string "hide", the button will simply close the modal when it is clicked. Otherwise the passed function will be called when the button is clicked. The function will also be passed the modal API.
- **handler**: This can be the string "hide" *OR* a function. If it is the string "hide", the button will simply close the modal when it is clicked. Otherwise the passed function will be called when the button is clicked. The function will also be passed the modal API with the click event added on under the key 'event'.
- **primary**: A boolean (true or false). If this is true, it just adds the "nanoModalBtnPrimary" class to the button (which makes it look blue by default).
- **classes**: A string of space separated classes you want on the button: "myClassA myClassB"

Expand Down
16 changes: 14 additions & 2 deletions nanomodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ function Modal(content, options, overlay, customShow, customHide) {
}
};

var quickClone = function(obj) {
var newObj = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
};

pub = {
modal: modal,
overlay: overlay,
Expand Down Expand Up @@ -234,8 +244,10 @@ function Modal(content, options, overlay, customShow, customHide) {
var btnEl;
var classes;
var giveButtonCustomClickListener = function(btnEl, btnObj) {
btnEl.addClickListener(function() {
btnObj.handler(pub);
var pubCopy = quickClone(pub);
btnEl.addClickListener(function(e) {
pubCopy.event = e || window.event;
btnObj.handler(pubCopy);
});
};

Expand Down
2 changes: 1 addition & 1 deletion nanomodal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nanomodal",
"version": "5.0.3",
"version": "5.1.0",
"description": "A small, self-contained JavaScript modal library with some extra features",
"homepage": "https://github.com/kylepaulsen/NanoModal",
"author": {
Expand Down
16 changes: 14 additions & 2 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ function Modal(content, options, overlay, customShow, customHide) {
}
};

var quickClone = function(obj) {
var newObj = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
};

pub = {
modal: modal,
overlay: overlay,
Expand Down Expand Up @@ -113,8 +123,10 @@ function Modal(content, options, overlay, customShow, customHide) {
var btnEl;
var classes;
var giveButtonCustomClickListener = function(btnEl, btnObj) {
btnEl.addClickListener(function() {
btnObj.handler(pub);
var pubCopy = quickClone(pub);
btnEl.addClickListener(function(e) {
pubCopy.event = e || window.event;
btnObj.handler(pubCopy);
});
};

Expand Down
5 changes: 3 additions & 2 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@
touchClick(nanoModalButtons[0]);

assert.ok(wasHandlerCalled, "After clicking the second button, the handler should be called.");
assert.strictEqual(handlerArg, modal,
"After clicking the second button, the handler should be passed the modal as an argument.");
assert.deepEqual(Object.keys(handlerArg), ["modal", "overlay", "show", "hide", "onShow",
"onHide", "remove", "setButtons", "setContent", "getContent", "event"],
"After clicking the second button, the handler should be passed an object with these keys.");

modal.remove();
});
Expand Down

0 comments on commit ed4d566

Please sign in to comment.