|
@@ -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);
|
0 comments on commit
d9dbbfb