type_number_imitate 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -3
- data/lib/type_number_imitate/version.rb +1 -1
- data/sources/type_number_imitate.coffee +7 -4
- data/vendor/assets/javascripts/type_number_imitate.js +171 -162
- metadata +1 -3
- data/demo/demo.html +0 -17
- data/demo/demo.slim +0 -15
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: efe98c7bf15185ec6f410ebf10ae4c8c6a29c0e9
|
|
4
|
+
data.tar.gz: 577ec8f60e4d382db16609b4faa1d11ca3c6f8a2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ce32c0a5c896dcbc51d129924f1d1b128ad85ceb491e1311fba27c9ca6747097de5f224b5771bfce1b7fc4bf6cacf7254a7058e6f8d158504559f7fd87fd3066
|
|
7
|
+
data.tar.gz: db2818a2c9d087a270da0371f695319b701d7991cd7771d7807e9db6dafdacf61d34b4f01b443c8a2b682c9dfebaea4244e705ecc11c2d4af7f08ff5bb786cb5
|
data/README.md
CHANGED
|
@@ -29,7 +29,7 @@ Or install it yourself as:
|
|
|
29
29
|
## Usage
|
|
30
30
|
|
|
31
31
|
```html
|
|
32
|
-
<input class="first" float="" min="1"
|
|
32
|
+
<input class="first" float="" min="1" type="text">
|
|
33
33
|
```
|
|
34
34
|
```javascript
|
|
35
35
|
new TypeNumberImitate('input.first');
|
|
@@ -48,12 +48,18 @@ Result:
|
|
|
48
48
|
```javascript
|
|
49
49
|
new TypeNumberImitate(inputSelector, options);
|
|
50
50
|
```
|
|
51
|
-
* *wrapperClasses* - additional
|
|
52
|
-
* *plusClasses*, *minusClasses* - additional
|
|
51
|
+
* *wrapperClasses* - additional classes for wrapper div.type-number-imitate
|
|
52
|
+
* *plusClasses*, *minusClasses* - additional classes for plus and minus spans
|
|
53
|
+
* *skipPlaceholder* - Use this attribute to clear placeholder. By default placeholder is set from placeholder-attribute, if it's absent placeholder is set as min-attribute.
|
|
53
54
|
```javascript
|
|
54
55
|
new TypeNumberImitate('input.second', {wrapperClasses: 'green-border large'});
|
|
55
56
|
```
|
|
56
57
|
|
|
58
|
+
All options are available from *data-tni-ATTR* i.e:
|
|
59
|
+
```html
|
|
60
|
+
<input class="first" float="" min="1" type="text" data-tni-wrapper-classes='green-border large'>
|
|
61
|
+
```
|
|
62
|
+
|
|
57
63
|
[Live demo](http://codepen.io/byzg/pen/VaNPMO)
|
|
58
64
|
|
|
59
65
|
## Development
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# created by byzg
|
|
2
2
|
# https://github.com/byzg/type_number_imitate
|
|
3
|
-
# created by byzg
|
|
4
|
-
# https://github.com/byzg/type_number_imitate
|
|
5
3
|
window.TypeNumberImitate = class TypeNumberImitate
|
|
6
4
|
constructor: ($input, opts = {}) ->
|
|
7
5
|
@$input = jQuery($input)
|
|
@@ -37,7 +35,7 @@ window.TypeNumberImitate = class TypeNumberImitate
|
|
|
37
35
|
|
|
38
36
|
stepFn: (val)->
|
|
39
37
|
oldVal = @$input.val()
|
|
40
|
-
oldVal =
|
|
38
|
+
oldVal = @min - 1 if oldVal == ''
|
|
41
39
|
oldVal = @parseFn(oldVal)
|
|
42
40
|
newVal = oldVal + @parseFn(val)
|
|
43
41
|
if @isValid(newVal)
|
|
@@ -64,10 +62,15 @@ window.TypeNumberImitate = class TypeNumberImitate
|
|
|
64
62
|
@$input.data('type-number-imitated', true)
|
|
65
63
|
|
|
66
64
|
initAttrs: ->
|
|
65
|
+
_.each @$input.data(), (v, k)=>
|
|
66
|
+
@opts[_.camelCase(k[3..-1])] = v if k[0..2] == 'tni'
|
|
67
67
|
@float = @$input.is('[float]')
|
|
68
68
|
@parseFn = if @float then parseFloat else parseInt
|
|
69
69
|
@min = @parseFn(min) if min = @$input.attr('min')
|
|
70
70
|
@max = @parseFn(max) if max = @$input.attr('max')
|
|
71
|
+
unless typeof(@opts.skipPlaceholder) == 'string'
|
|
72
|
+
@placeholer = @$input.attr('placeholder') || @min
|
|
73
|
+
@$input.attr('placeholder', @placeholer)
|
|
71
74
|
@step = @parseFn(@$input.attr('step') || 1)
|
|
72
75
|
regexStr = '^[0-9]+'
|
|
73
76
|
regexStr += '\.?([0-9]+)?' if @float
|
|
@@ -100,4 +103,4 @@ window.TypeNumberImitate = class TypeNumberImitate
|
|
|
100
103
|
@$plus = jQuery("<span class='plus #{@opts.plusClasses}'>+</span>")
|
|
101
104
|
@$minus = jQuery("<span class='minus #{@opts.minusClasses}'>–</span>")
|
|
102
105
|
@$container.append(@$plus)
|
|
103
|
-
@$container.append(@$minus)
|
|
106
|
+
@$container.append(@$minus)
|
|
@@ -1,181 +1,190 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
(function () {
|
|
4
|
-
var TypeNumberImitate;
|
|
1
|
+
//created by byzg
|
|
2
|
+
//https://github.com/byzg/type_number_imitate
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
this.$input = jQuery($input);
|
|
13
|
-
this.opts = opts;
|
|
14
|
-
(base = this.opts).wrapperClasses || (base.wrapperClasses = '');
|
|
15
|
-
(base1 = this.opts).plusClasses || (base1.plusClasses = '');
|
|
16
|
-
(base2 = this.opts).minusClasses || (base2.minusClasses = '');
|
|
17
|
-
if (this.$input.length && !this.alreadyImitated()) {
|
|
18
|
-
this.init();
|
|
19
|
-
this.toggleDisabled();
|
|
20
|
-
}
|
|
4
|
+
var TypeNumberImitate;
|
|
5
|
+
window.TypeNumberImitate = TypeNumberImitate = (function() {
|
|
6
|
+
function TypeNumberImitate($input, opts) {
|
|
7
|
+
var base, base1, base2;
|
|
8
|
+
if (opts == null) {
|
|
9
|
+
opts = {};
|
|
21
10
|
}
|
|
11
|
+
this.$input = jQuery($input);
|
|
12
|
+
this.opts = opts;
|
|
13
|
+
(base = this.opts).wrapperClasses || (base.wrapperClasses = '');
|
|
14
|
+
(base1 = this.opts).plusClasses || (base1.plusClasses = '');
|
|
15
|
+
(base2 = this.opts).minusClasses || (base2.minusClasses = '');
|
|
16
|
+
if (this.$input.length && !this.alreadyImitated()) {
|
|
17
|
+
this.init();
|
|
18
|
+
this.toggleDisabled();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
TypeNumberImitate.prototype.isValid = function(val) {
|
|
23
|
+
var parsed, result;
|
|
24
|
+
if (val === '' || !((this.min != null) || (this.max != null))) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
parsed = this.parseFn(val);
|
|
28
|
+
result = !isNaN(parsed) && !!('' + val).match(this.regex);
|
|
29
|
+
if (this.min) {
|
|
30
|
+
result &= parsed >= this.min;
|
|
31
|
+
}
|
|
32
|
+
if (this.max) {
|
|
33
|
+
result &= parsed <= this.max;
|
|
34
|
+
}
|
|
35
|
+
return !!result;
|
|
36
|
+
};
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
38
|
+
TypeNumberImitate.prototype.valAfterType = function(typedStr, keyDownCode) {
|
|
39
|
+
var currentVal, deltaEnd, deltaStart, selectionEnd, selectionStart;
|
|
40
|
+
if (keyDownCode == null) {
|
|
41
|
+
keyDownCode = null;
|
|
42
|
+
}
|
|
43
|
+
currentVal = this.$input.val();
|
|
44
|
+
selectionStart = this.$input.get(0).selectionStart;
|
|
45
|
+
selectionEnd = this.$input.get(0).selectionEnd;
|
|
46
|
+
deltaStart = deltaEnd = 0;
|
|
47
|
+
if (keyDownCode === 8) {
|
|
48
|
+
deltaStart = -1;
|
|
49
|
+
}
|
|
50
|
+
if (keyDownCode === 46) {
|
|
51
|
+
deltaEnd = 1;
|
|
52
|
+
}
|
|
53
|
+
return currentVal.substr(0, selectionStart + deltaStart) + typedStr + currentVal.substr(selectionEnd + deltaEnd);
|
|
54
|
+
};
|
|
56
55
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
TypeNumberImitate.prototype.toggleDisabled = function() {
|
|
57
|
+
var val;
|
|
58
|
+
val = this.$input.val();
|
|
59
|
+
this.$plus.toggleClass('disabled', val === this.max);
|
|
60
|
+
return this.$minus.toggleClass('disabled', val === this.min);
|
|
61
|
+
};
|
|
63
62
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
63
|
+
TypeNumberImitate.prototype.stepFn = function(val) {
|
|
64
|
+
var newVal, oldVal;
|
|
65
|
+
oldVal = this.$input.val();
|
|
66
|
+
if (oldVal === '') {
|
|
67
|
+
oldVal = this.min - 1;
|
|
68
|
+
}
|
|
69
|
+
oldVal = this.parseFn(oldVal);
|
|
70
|
+
newVal = oldVal + this.parseFn(val);
|
|
71
|
+
if (this.isValid(newVal)) {
|
|
72
|
+
this.$input.val(newVal);
|
|
73
|
+
this.toggleDisabled();
|
|
74
|
+
this.$input.trigger('change');
|
|
75
|
+
return newVal;
|
|
76
|
+
} else {
|
|
77
|
+
return oldVal;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
81
80
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
TypeNumberImitate.prototype.alreadyImitated = function() {
|
|
82
|
+
return this.$input.data().typeNumberImitated;
|
|
83
|
+
};
|
|
85
84
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
TypeNumberImitate.prototype.increment = function() {
|
|
86
|
+
return this.stepFn(this.step);
|
|
87
|
+
};
|
|
89
88
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
TypeNumberImitate.prototype.decrement = function() {
|
|
90
|
+
return this.stepFn(-this.step);
|
|
91
|
+
};
|
|
93
92
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
TypeNumberImitate.prototype.init = function() {
|
|
94
|
+
this.initAttrs();
|
|
95
|
+
this.initWrap();
|
|
96
|
+
this.initHandlers();
|
|
97
|
+
return this.$input.data('type-number-imitated', true);
|
|
98
|
+
};
|
|
100
99
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
100
|
+
TypeNumberImitate.prototype.initAttrs = function() {
|
|
101
|
+
var max, min, regexStr;
|
|
102
|
+
_.each(this.$input.data(), (function(_this) {
|
|
103
|
+
return function(v, k) {
|
|
104
|
+
if (k.slice(0, 3) === 'tni') {
|
|
105
|
+
return _this.opts[_.camelCase(k.slice(3))] = v;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
})(this));
|
|
109
|
+
this.float = this.$input.is('[float]');
|
|
110
|
+
this.parseFn = this.float ? parseFloat : parseInt;
|
|
111
|
+
if (min = this.$input.attr('min')) {
|
|
112
|
+
this.min = this.parseFn(min);
|
|
113
|
+
}
|
|
114
|
+
if (max = this.$input.attr('max')) {
|
|
115
|
+
this.max = this.parseFn(max);
|
|
116
|
+
}
|
|
117
|
+
if (typeof this.opts.skipPlaceholder !== 'string') {
|
|
118
|
+
this.placeholer = this.$input.attr('placeholder') || this.min;
|
|
119
|
+
this.$input.attr('placeholder', this.placeholer);
|
|
120
|
+
}
|
|
121
|
+
this.step = this.parseFn(this.$input.attr('step') || 1);
|
|
122
|
+
regexStr = '^[0-9]+';
|
|
123
|
+
if (this.float) {
|
|
124
|
+
regexStr += '\.?([0-9]+)?';
|
|
125
|
+
}
|
|
126
|
+
regexStr += '$';
|
|
127
|
+
regexStr = regexStr.replace(/[.]/g, '\\$&');
|
|
128
|
+
return this.regex = new RegExp(regexStr, 'g');
|
|
129
|
+
};
|
|
120
130
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
if (e.keyCode === 40) {
|
|
133
|
-
_this.decrement();
|
|
134
|
-
}
|
|
135
|
-
return e.preventDefault();
|
|
136
|
-
} else if (_.includes([8, 46], e.keyCode)) {
|
|
137
|
-
if (!_this.isValid(_this.valAfterType('', e.keyCode))) {
|
|
138
|
-
return e.preventDefault();
|
|
139
|
-
}
|
|
131
|
+
TypeNumberImitate.prototype.initHandlers = function() {
|
|
132
|
+
this.$input.on('keypress', (function(_this) {
|
|
133
|
+
return function(e) {
|
|
134
|
+
return _this.isValid(_this.valAfterType(String.fromCharCode(e.charCode)));
|
|
135
|
+
};
|
|
136
|
+
})(this)).on('keydown', (function(_this) {
|
|
137
|
+
return function(e) {
|
|
138
|
+
if (_.includes([38, 40], e.keyCode)) {
|
|
139
|
+
if (e.keyCode === 38) {
|
|
140
|
+
_this.increment();
|
|
140
141
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return function(e) {
|
|
144
|
-
var pasted;
|
|
145
|
-
pasted = e.originalEvent.clipboardData.getData('text');
|
|
146
|
-
if (!_this.isValid(_this.valAfterType(pasted))) {
|
|
147
|
-
return e.preventDefault();
|
|
142
|
+
if (e.keyCode === 40) {
|
|
143
|
+
_this.decrement();
|
|
148
144
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (!_this.isValid(_this.valAfterType(''))) {
|
|
145
|
+
return e.preventDefault();
|
|
146
|
+
} else if (_.includes([8, 46], e.keyCode)) {
|
|
147
|
+
if (!_this.isValid(_this.valAfterType('', e.keyCode))) {
|
|
153
148
|
return e.preventDefault();
|
|
154
149
|
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
})(this)).on('paste', (function(_this) {
|
|
153
|
+
return function(e) {
|
|
154
|
+
var pasted;
|
|
155
|
+
pasted = e.originalEvent.clipboardData.getData('text');
|
|
156
|
+
if (!_this.isValid(_this.valAfterType(pasted))) {
|
|
157
|
+
return e.preventDefault();
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
})(this)).on('cut', (function(_this) {
|
|
161
|
+
return function(e) {
|
|
162
|
+
if (!_this.isValid(_this.valAfterType(''))) {
|
|
163
|
+
return e.preventDefault();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
})(this));
|
|
167
|
+
this.$plus.click((function(_this) {
|
|
168
|
+
return function() {
|
|
169
|
+
return _this.increment();
|
|
170
|
+
};
|
|
171
|
+
})(this));
|
|
172
|
+
return this.$minus.click((function(_this) {
|
|
173
|
+
return function() {
|
|
174
|
+
return _this.decrement();
|
|
175
|
+
};
|
|
176
|
+
})(this));
|
|
177
|
+
};
|
|
168
178
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
179
|
+
TypeNumberImitate.prototype.initWrap = function() {
|
|
180
|
+
this.$input.wrap("<div class='type-number-imitate " + this.opts.wrapperClasses + "'></div>");
|
|
181
|
+
this.$container = this.$input.parent();
|
|
182
|
+
this.$plus = jQuery("<span class='plus " + this.opts.plusClasses + "'>+</span>");
|
|
183
|
+
this.$minus = jQuery("<span class='minus " + this.opts.minusClasses + "'>–</span>");
|
|
184
|
+
this.$container.append(this.$plus);
|
|
185
|
+
return this.$container.append(this.$minus);
|
|
186
|
+
};
|
|
177
187
|
|
|
178
|
-
|
|
188
|
+
return TypeNumberImitate;
|
|
179
189
|
|
|
180
|
-
|
|
181
|
-
}.call(this));
|
|
190
|
+
})();
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: type_number_imitate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- byzg
|
|
@@ -52,8 +52,6 @@ files:
|
|
|
52
52
|
- Rakefile
|
|
53
53
|
- bin/console
|
|
54
54
|
- bin/setup
|
|
55
|
-
- demo/demo.html
|
|
56
|
-
- demo/demo.slim
|
|
57
55
|
- lib/type_number_imitate.rb
|
|
58
56
|
- lib/type_number_imitate/version.rb
|
|
59
57
|
- sources/type_number_imitate.coffee
|
data/demo/demo.html
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<style>
|
|
2
|
-
.green-border { border: solid 3px green; }
|
|
3
|
-
.large { zoom: 1.2; }
|
|
4
|
-
</style>
|
|
5
|
-
|
|
6
|
-
Default input:
|
|
7
|
-
<input class="first" float="" min="1" placeholder="1" type="text">
|
|
8
|
-
<br>
|
|
9
|
-
Input with classes of wrapper:
|
|
10
|
-
<input class="second" float="" min="1" placeholder="1" type="text">
|
|
11
|
-
|
|
12
|
-
<script>
|
|
13
|
-
jQuery(function() {
|
|
14
|
-
new TypeNumberImitate('input.first');
|
|
15
|
-
new TypeNumberImitate('input.second', {wrapperClasses: 'green-border large'});
|
|
16
|
-
});
|
|
17
|
-
</script>
|
data/demo/demo.slim
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
style
|
|
2
|
-
| .green-border { border: solid 3px green; }
|
|
3
|
-
| .large { zoom: 1.2; }
|
|
4
|
-
|
|
5
|
-
| Default input:
|
|
6
|
-
input.first type='text' min='1' placeholder='1' float=true
|
|
7
|
-
br
|
|
8
|
-
| Input with classes of wrapper:
|
|
9
|
-
input.second type='text' min='1' placeholder='1' float=true
|
|
10
|
-
|
|
11
|
-
script
|
|
12
|
-
| jQuery(function() {
|
|
13
|
-
| new TypeNumberImitate('input.first');
|
|
14
|
-
| new TypeNumberImitate('input.second', {wrapperClasses: 'green-border large'});
|
|
15
|
-
| });
|