forked from MartinF/jQuery.Autosize.Input
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.autosize.input.ts
140 lines (112 loc) · 4.29 KB
/
jquery.autosize.input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/// <reference path="jquery.d.ts" />
interface JQuery {
autosizeInput(): JQuery;
}
module Plugins {
export interface IAutosizeInput {
update(): void;
}
export interface IAutosizeInputOptions {
space: number;
isTinyMCE: boolean;
returnInstance: boolean
}
export class AutosizeInputOptions implements IAutosizeInputOptions {
constructor(public space: number = 30, public isTinyMCE: boolean = false, public returnInstance: boolean = false ) { }
}
export class AutosizeInput implements IAutosizeInput {
private _input: JQuery;
private _mirror: JQuery;
private _options: IAutosizeInputOptions;
private static _defaultOptions: IAutosizeInputOptions = new AutosizeInputOptions();
constructor(input: HTMLElement, options?: IAutosizeInputOptions) {
this._input = $(input);
this._options = $.extend({}, AutosizeInput.getDefaultOptions(), options);
// Init mirror
this._mirror = $('<span style="position:absolute; top:-999px; left:0; white-space:pre;"/>');
// Copy to mirror
$.each(['fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'letterSpacing', 'textTransform', 'wordSpacing', 'textIndent'], (i, val) => {
this._mirror[0].style[val] = this._input.css(val);
});
$("body").append(this._mirror);
// Bind events - change update paste click mousedown mouseup focus blur
// IE 9 need keydown to keep updating while deleting (keeping backspace in - else it will first update when backspace is released)
// IE 9 need keyup incase text is selected and backspace/deleted is hit - keydown is to early
// 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", (e) => { this.update(); });
// Update
(() => { this.update(); })();
}
public getOptions(): IAutosizeInputOptions {
return this._options;
}
public update() {
var value = this._input.val() || "";
if (value === this._mirror.text()) {
// Nothing have changed - skip
return;
}
// Update mirror
this._mirror.text(value);
// Calculate the width
var newWidth = this._mirror.width() + this._options.space;
// Update the width
this._input.width(newWidth);
if(this._options.isTinyMCE){
this._input.attr('data-mce-style', 'width:'+newWidth+'px');
}
}
public static getDefaultOptions(): IAutosizeInputOptions {
return this._defaultOptions;
}
public static getInstanceKey(): string {
// Use camelcase because .data()['autosize-input-instance'] will not work
return "autosizeInputInstance";
}
}
// jQuery Plugin
(function ($) {
var pluginDataAttributeName = "autosize-input";
var validTypes = ["text", "password", "search", "url", "tel", "email", "number"];
// jQuery Plugin
$.fn.autosizeInput = function (options?: IAutosizeInputOptions) {
var instance = null;
var collection = 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?
if (!(this.tagName == "INPUT" && $.inArray(this.type, validTypes) > -1)) {
// Skip - if not input and of valid type
return;
}
var $this = $(this);
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);
}
// Create and attach instance
instance = new Plugins.AutosizeInput(this, options);
$this.data(Plugins.AutosizeInput.getInstanceKey(), instance);
}
});
if(options !== undefined && options.returnInstance === true){
return instance;
}else{
return collection;
}
};
// On Document Ready
$(function () {
// Instantiate for all with data-provide=autosize-input attribute
$("input[data-" + pluginDataAttributeName + "]").autosizeInput();
});
// Alternative to use On Document Ready and creating the instance immediately
//$(document).on('focus.autosize-input', 'input[data-autosize-input]', function (e)
//{
// $(this).autosizeInput();
//});
})(jQuery);
}