Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Add namespace and make events past tense #1384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ define(function (require) {
});
});

$('#myPlacard3').on('accepted.fu.placard', function() {
console.log('accepted.fu.placard');
});

$('#myPlacard3').on('cancelled.fu.placard', function() {
console.log('cancelled.fu.placard');
});

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RADIO
Expand Down
20 changes: 11 additions & 9 deletions js/placard.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// -- BEGIN MODULE CODE HERE --

var old = $.fn.placard;
var EVENT_CALLBACK_MAP = { 'accepted': 'onAccept', 'cancelled': 'onCancel' };

// PLACARD CONSTRUCTOR AND PROTOTYPE

Expand All @@ -51,9 +52,9 @@

this.$field.on('focus.fu.placard', $.proxy(this.show, this));
this.$field.on('keydown.fu.placard', $.proxy(this.keyComplete, this));
this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accept'));
this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accepted'));
this.$cancel.on('click.fu.placard', function (e) {
e.preventDefault(); self.complete('cancel');
e.preventDefault(); self.complete('cancelled');
});

this.ellipsis();
Expand All @@ -63,30 +64,31 @@
constructor: Placard,

complete: function (action) {
var func = this.options['on' + action[0].toUpperCase() + action.substring(1)];
var func = this.options[ EVENT_CALLBACK_MAP[action] ];

var obj = {
previousValue: this.previousValue,
value: this.$field.val()
};
if (func) {
func(obj);
this.$element.trigger(action, obj);
this.$element.trigger(action + '.fu.placard', obj);
} else {
if (action === 'cancel' && this.options.revertOnCancel) {
if (action === 'cancelled' && this.options.revertOnCancel) {
this.$field.val(this.previousValue);
}

this.$element.trigger(action, obj);
this.$element.trigger(action + '.fu.placard', obj);
this.hide();
}
},

keyComplete: function (e) {
if (this.isInput && e.keyCode === 13) {
this.complete('accept');
this.complete('accepted');
this.$field.blur();
} else if (e.keyCode === 27) {
this.complete('cancel');
this.complete('cancelled');
this.$field.blur();
}
},
Expand Down Expand Up @@ -261,7 +263,7 @@
$.fn.placard.defaults = {
onAccept: undefined,
onCancel: undefined,
externalClickAction: 'cancel',
externalClickAction: 'cancelled',
externalClickExceptions: [],
explicit: false,
revertOnCancel: -1//negative 1 will check for an '.placard-accept' button. Also can be set to true or false
Expand Down
10 changes: 5 additions & 5 deletions test/placard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ define(function(require){

$('body').append($placard);
$placard.placard();
$placard.on('cancel', function(e, helpers){
$placard.on('cancelled.fu.placard', function(e, helpers){
ok(1===1, 'default action event (cancel) triggered upon external click');
});

Expand All @@ -43,7 +43,7 @@ define(function(require){
var $placard = $(html).find('#placard2');

$placard.placard();
$placard.on('cancel', function(e, helpers){
$placard.on('cancelled.fu.placard', function(e, helpers){
ok(1===1, 'default action event (cancel) triggered upon external click');
});
$('body').append($placard);
Expand Down Expand Up @@ -71,13 +71,13 @@ define(function(require){
var $placard = $(html).find('#placard1');

$placard.placard();
$placard.on('accept', function(e, helpers){
$placard.on('accepted.fu.placard', function(e, helpers){
ok(1===1, 'accept event triggers on accept');
equal(typeof e, 'object', 'event object passed in accept event');
equal(typeof helpers, 'object', 'helpers object passed in accept event');
equal((helpers.previousValue!==undefined && helpers.value!==undefined), true, 'helpers object contains correct attributes');
});
$placard.on('cancel', function(e, helpers){
$placard.on('cancelled.fu.placard', function(e, helpers){
ok(1===1, 'cancel event triggers on cancel');
equal(typeof e, 'object', 'event object passed in cancel event');
equal(typeof helpers, 'object', 'helpers object passed in cancel event');
Expand Down Expand Up @@ -146,7 +146,7 @@ define(function(require){
var $placard = $(html).find('#placard1');

$placard.placard({
externalClickAction: 'accept'
externalClickAction: 'accepted'
});

$placard.find('input').focus().focus().val('test');
Expand Down