Skip to content

Commit

Permalink
optional arg on inialization {dontFocus: true} kenkeiter#115
Browse files Browse the repository at this point in the history
* defaults to false
* when this is true, skeuocard won't focus when itself on initialization
  • Loading branch information
dylanjha committed Jan 3, 2014
1 parent 2d3067e commit 2f72307
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
21 changes: 12 additions & 9 deletions javascripts/skeuocard.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
this.visibleFace = 'front';
optDefaults = {
debug: false,
dontFocus: false,
acceptedCardProducts: null,
cardNumberPlaceholderChar: 'X',
genericPlaceholder: "XXXX XXXX XXXX XXXX",
Expand Down Expand Up @@ -391,7 +392,7 @@
*/


Skeuocard.prototype._renderProduct = function(product) {
Skeuocard.prototype._renderProduct = function(product, dontFocus) {
var destFace, fieldName, focused, view, viewEl, _ref, _ref1,
_this = this;
this._log("[_renderProduct]", "Rendering product:", product);
Expand Down Expand Up @@ -460,7 +461,7 @@
};

Skeuocard.prototype.render = function() {
this._renderProduct(this.product);
this._renderProduct(this.product, this.options.dontFocus);
return this._renderValidation();
};

Expand Down Expand Up @@ -856,15 +857,15 @@
}
};

SegmentedCardNumberInputView.prototype._focusFieldForValue = function(place) {
SegmentedCardNumberInputView.prototype._focusFieldForValue = function(place, dontFocus) {
var field, fieldOffset, fieldPosition, groupIndex, groupLength, value, _i, _lastStartPos, _len, _ref;
value = this.getValue();
if (place === 'start') {
field = this.el.find('input').first();
this._focusField(field, place);
this._focusField(field, place, dontFocus);
} else if (place === 'end') {
field = this.el.find('input').last();
this._focusField(field, place);
this._focusField(field, place, dontFocus);
} else {
field = null;
fieldOffset = null;
Expand All @@ -879,18 +880,20 @@
_lastStartPos += groupLength;
}
if ((field != null) && (fieldPosition != null)) {
this._focusField(field, [fieldPosition, fieldPosition]);
this._focusField(field, [fieldPosition, fieldPosition], dontFocus);
} else {
this._focusField(this.el.find('input'), 'end');
this._focusField(this.el.find('input'), 'end', dontFocus);
}
}
return field;
};

SegmentedCardNumberInputView.prototype._focusField = function(field, place) {
SegmentedCardNumberInputView.prototype._focusField = function(field, place, dontFocus) {
var fieldLen;
if (field.length !== 0) {
field[0].focus();
if (!dontFocus) {
field[0].focus();
}
if ($(field[0]).is(':visible') && field[0] === document.activeElement) {
if (place === 'start') {
return field[0].setSelectionRange(0, 0);
Expand Down
4 changes: 2 additions & 2 deletions javascripts/skeuocard.min.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions javascripts/src/SegmentedCardNumberInputView.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ class Skeuocard::SegmentedCardNumberInputView
if _currentField? and _currentField[0].selectionEnd is _currentField[0].maxLength
@_focusField(_currentField.next(), 'start')

_focusFieldForValue: (place)->
_focusFieldForValue: (place, dontFocus)->
value = @getValue()
if place is 'start'
field = @el.find('input').first()
@_focusField(field, place)
@_focusField(field, place, dontFocus)
else if place is 'end'
field = @el.find('input').last()
@_focusField(field, place)
@_focusField(field, place, dontFocus)
else
field = null
fieldOffset = null
Expand All @@ -232,14 +232,15 @@ class Skeuocard::SegmentedCardNumberInputView
fieldPosition = place[1] - _lastStartPos
_lastStartPos += groupLength
if field? and fieldPosition?
@_focusField(field, [fieldPosition, fieldPosition])
@_focusField(field, [fieldPosition, fieldPosition], dontFocus)
else
@_focusField(@el.find('input'), 'end')
@_focusField(@el.find('input'), 'end', dontFocus)
return field

_focusField: (field, place)->
_focusField: (field, place, dontFocus)->
if field.length isnt 0
field[0].focus()
unless dontFocus
field[0].focus()
if $(field[0]).is(':visible') and field[0] is document.activeElement
if place is 'start'
field[0].setSelectionRange(0, 0)
Expand Down
5 changes: 3 additions & 2 deletions javascripts/src/Skeuocard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Skeuocard
# configure default opts
optDefaults =
debug: false
dontFocus: false
acceptedCardProducts: null
cardNumberPlaceholderChar: 'X'
genericPlaceholder: "XXXX XXXX XXXX XXXX"
Expand Down Expand Up @@ -328,7 +329,7 @@ class Skeuocard
Assert rendering changes necessary for the current product. Passing a null
value instead of a product will revert the card to a generic state.
###
_renderProduct: (product)->
_renderProduct: (product, dontFocus)->
@_log("[_renderProduct]", "Rendering product:", product)

# remove existing product and issuer classes (destyling product)
Expand Down Expand Up @@ -382,7 +383,7 @@ class Skeuocard

# Update the card's visual representation to reflect internal state.
render: ->
@_renderProduct(@product)
@_renderProduct(@product, @options.dontFocus)
@_renderValidation()
# @_flipToInvalidSide()

Expand Down

0 comments on commit 2f72307

Please sign in to comment.