Permalink
Browse files

Close #10 - Add resetPattern method

1 parent 7e3ff86 commit d9dbbfbf9766c759efdb3d8f00bbe4f69a633d30 @jaridmargolin jaridmargolin committed Nov 4, 2013
Showing with 161 additions and 24 deletions.
  1. +21 −1 README.md
  2. +28 −3 lib/formatter.js
  3. +1 −1 lib/formatter.min.js
  4. +53 −9 lib/jquery.formatter.js
  5. +1 −1 lib/jquery.formatter.min.js
  6. +28 −3 src/formatter.js
  7. +25 −6 src/tmpls/jquery.outro.js
  8. +4 −0 test/formatter.js
View
@@ -42,7 +42,7 @@ Usage
#### new Formatter(el, opts)
- new Formatter(document.getElementById('credit-input'), {
+ var formatted = new Formatter(document.getElementById('credit-input'), {
'pattern': '{{999}}-{{999}}-{{999}}-{{9999}}',
'persistent': true
});
@@ -62,6 +62,26 @@ Usage
+Methods
+-------
+
+#### resetPattern(pattern)
+
+Fairly self explanatory here :) reset the pattern on an existing Formatter instance.
+
+**Vanilla Javascript**
+
+(assuming you already created a new instance and saved it to the var `formatted`)
+
+ formatted.resetPattern('{{999}}.{{999}}.{{9999}}');
+
+**Jquery**
+
+(assuming you already initiated formatter on `#selector`)
+
+ $('#selector').formatter().resetPattern();
+
+
Opts
----
View
@@ -73,7 +73,7 @@ function Formatter(el, opts) {
// Persistence
if (self.opts.persistent) {
// Format on start
- self._processKey(null, true);
+ self._processKey('', false);
self.el.blur();
// Add Listeners
@@ -90,6 +90,32 @@ function Formatter(el, opts) {
}
//
+// @public
+// Handler called on all keyDown strokes. All keys trigger
+// this handler. Only process delete keys.
+//
+Formatter.prototype.resetPattern = function (str) {
+ // Get current state
+ this.sel = inptSel.get(this.el);
+ this.val = this.el.value;
+
+ // Init values
+ this.delta = 0;
+
+ // Remove all formatted chars from val
+ this._removeChars();
+
+ // Update pattern
+ var parsed = pattern.parse(str);
+ this.mLength = parsed.mLength;
+ this.chars = parsed.chars;
+ this.inpts = parsed.inpts;
+
+ // Format on start
+ this._processKey('', false);
+};
+
+//
// @private
// Handler called on all keyDown strokes. All keys trigger
// this handler. Only process delete keys.
@@ -226,8 +252,7 @@ Formatter.prototype._nextPos = function () {
//
Formatter.prototype._formatValue = function () {
// Set caret pos
- this.curPos = this.sel.end;
- this.newPos = this.curPos + this.delta;
+ this.newPos = this.sel.end + this.delta;
// Remove all formatted chars from val
this._removeChars();
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -70,7 +70,7 @@ function Formatter(el, opts) {
// Persistence
if (self.opts.persistent) {
// Format on start
- self._processKey(null, true);
+ self._processKey('', false);
self.el.blur();
// Add Listeners
@@ -87,6 +87,32 @@ function Formatter(el, opts) {
}
//
+// @public
+// Handler called on all keyDown strokes. All keys trigger
+// this handler. Only process delete keys.
+//
+Formatter.prototype.resetPattern = function (str) {
+ // Get current state
+ this.sel = inptSel.get(this.el);
+ this.val = this.el.value;
+
+ // Init values
+ this.delta = 0;
+
+ // Remove all formatted chars from val
+ this._removeChars();
+
+ // Update pattern
+ var parsed = pattern.parse(str);
+ this.mLength = parsed.mLength;
+ this.chars = parsed.chars;
+ this.inpts = parsed.inpts;
+
+ // Format on start
+ this._processKey('', false);
+};
+
+//
// @private
// Handler called on all keyDown strokes. All keys trigger
// this handler. Only process delete keys.
@@ -223,8 +249,7 @@ Formatter.prototype._nextPos = function () {
//
Formatter.prototype._formatValue = function () {
// Set caret pos
- this.curPos = this.sel.end;
- this.newPos = this.curPos + this.delta;
+ this.newPos = this.sel.end + this.delta;
// Remove all formatted chars from val
this._removeChars();
@@ -594,13 +619,32 @@ utils.isModifier = function (evt) {
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
var pluginName = 'formatter';
+
$.fn[pluginName] = function (options) {
- return this.each(function () {
- if (!$.data(this, 'plugin_' + pluginName)) {
- $.data(this, 'plugin_' + pluginName,
- new Formatter(this, options));
- }
- });
+
+ // Initiate plugin if options passed
+ if (typeof options == 'object') {
+ this.each(function () {
+ if (!$.data(this, 'plugin_' + pluginName)) {
+ $.data(this, 'plugin_' + pluginName,
+ new Formatter(this, options));
+ }
+ });
+ }
+
+ // Add resetPattern method to plugin
+ this.resetPattern = function (str) {
+ this.each(function () {
+ var formatted = $.data(this, 'plugin_' + pluginName);
+ // resetPattern for instance
+ if (formatted) { formatted.resetPattern(str); }
+ });
+ // Chainable please
+ return this
+ };
+
+ // Chainable please
+ return this;
};
})( jQuery, window, document);
Oops, something went wrong.

0 comments on commit d9dbbfb

Please sign in to comment.