Skip to content
Open
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
34 changes: 29 additions & 5 deletions jquery.autosize.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var Plugins;
var _this = this;
this._input = $(input);
this._options = $.extend({}, AutosizeInput.getDefaultOptions(), options);
this._updateHandler = function(e){
_this.update()
};

// Init mirror
this._mirror = $('<span style="position:absolute; top:-999px; left:0; white-space:pre;"/>');
Expand All @@ -30,9 +33,7 @@ var Plugins;
// How to fix problem with hitting the delete "X" in the box - but not updating!? mouseup is apparently to early
// Could bind separatly and set timer
// Add so it automatically updates if value of input is changed http://stackoverflow.com/a/1848414/58524
this._input.on("keydown keyup input propertychange change", function (e) {
_this.update();
});
this._input.on("keydown keyup input propertychange change", this._updateHandler);

// Update
(function () {
Expand All @@ -43,6 +44,11 @@ var Plugins;
return this._options;
};

AutosizeInput.prototype.destroy = function() {
this._mirror.remove();
this._input.off("keydown keyup input propertychange change", null, this._updateHandler);
};

AutosizeInput.prototype.update = function () {
var value = this._input.val() || "";

Expand Down Expand Up @@ -78,9 +84,15 @@ var Plugins;
(function ($) {
var pluginDataAttributeName = "autosize-input";
var validTypes = ["text", "password", "search", "url", "tel", "email", "number"];
var methods = {
destroy : function() {
var $this = $(this);
$this.data(Plugins.AutosizeInput.getInstanceKey()).destroy()
}
};

// jQuery Plugin
$.fn.autosizeInput = function (options) {
$.fn.autosizeInput = function (optionsAndMethods) {
return this.each(function () {
// Make sure it is only applied to input elements of valid type
// Or let it be the responsibility of the programmer to only select and apply to valid elements?
Expand All @@ -91,16 +103,28 @@ var Plugins;

var $this = $(this);

if ( options == undefined ) {
if (methods[optionsAndMethods]){
var method = optionsAndMethods;
}else{
var options = optionsAndMethods;
}
}

if (!$this.data(Plugins.AutosizeInput.getInstanceKey())) {
// If instance not already created and attached
if (options == undefined) {
// Try get options from attribute
options = $this.data(pluginDataAttributeName);
var options = $this.data(pluginDataAttributeName);
}

// Create and attach instance
$this.data(Plugins.AutosizeInput.getInstanceKey(), new Plugins.AutosizeInput(this, options));
}

if ( method != undefined) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
});
};

Expand Down