meio_mask-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/meio_mask-rails.rb +1 -0
- data/lib/meio_mask/rails.rb +7 -0
- data/lib/meio_mask/rails/engine.rb +6 -0
- data/lib/meio_mask/rails/version.rb +10 -0
- data/meio_mask-rails.gemspec +19 -0
- data/vendor/assets/javascripts/jquery.meio.mask.js +683 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rafael Macedo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# MaskedInputRails
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'masked_input-rails'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install masked_input-rails
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "meio_mask/rails"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/meio_mask/rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Rafael Macedo"]
|
6
|
+
gem.email = ["macedo.rafaelfernandes@gmail.com"]
|
7
|
+
gem.description = "Easy way to use meio_mask jquery plugin on rails"
|
8
|
+
gem.summary = gem.description
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "meio_mask-rails"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MeioMask::Rails::Version::STRING
|
17
|
+
|
18
|
+
gem.add_dependency "railties", "~> 3.1"
|
19
|
+
end
|
@@ -0,0 +1,683 @@
|
|
1
|
+
/**
|
2
|
+
* jquery.meio.mask.js
|
3
|
+
* @author: fabiomcosta
|
4
|
+
* @version: 1.1.3
|
5
|
+
*
|
6
|
+
* Created by Fabio M. Costa on 2008-09-16. Please report any bug at http://www.meiocodigo.com
|
7
|
+
*
|
8
|
+
* Copyright (c) 2008 Fabio M. Costa http://www.meiocodigo.com
|
9
|
+
*
|
10
|
+
* The MIT License (http://www.opensource.org/licenses/mit-license.php)
|
11
|
+
*
|
12
|
+
* Permission is hereby granted, free of charge, to any person
|
13
|
+
* obtaining a copy of this software and associated documentation
|
14
|
+
* files (the "Software"), to deal in the Software without
|
15
|
+
* restriction, including without limitation the rights to use,
|
16
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
17
|
+
* copies of the Software, and to permit persons to whom the
|
18
|
+
* Software is furnished to do so, subject to the following
|
19
|
+
* conditions:
|
20
|
+
*
|
21
|
+
* The above copyright notice and this permission notice shall be
|
22
|
+
* included in all copies or substantial portions of the Software.
|
23
|
+
*
|
24
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
25
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
26
|
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
27
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
28
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
29
|
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
30
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
31
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
*/
|
33
|
+
|
34
|
+
(function($){
|
35
|
+
|
36
|
+
var isIphone = (window.orientation != undefined),
|
37
|
+
// browsers like firefox2 and before and opera doenst have the onPaste event, but the paste feature can be done with the onInput event.
|
38
|
+
pasteEvent = (($.browser.opera || ($.browser.mozilla && parseFloat($.browser.version.substr(0,3)) < 1.9 ))? 'input': 'paste');
|
39
|
+
|
40
|
+
$.event.special.paste = {
|
41
|
+
setup: function() {
|
42
|
+
if(this.addEventListener)
|
43
|
+
this.addEventListener(pasteEvent, pasteHandler, false);
|
44
|
+
else if (this.attachEvent)
|
45
|
+
this.attachEvent(pasteEvent, pasteHandler);
|
46
|
+
},
|
47
|
+
|
48
|
+
teardown: function() {
|
49
|
+
if(this.removeEventListener)
|
50
|
+
this.removeEventListener(pasteEvent, pasteHandler, false);
|
51
|
+
else if (this.detachEvent)
|
52
|
+
this.detachEvent(pasteEvent, pasteHandler);
|
53
|
+
}
|
54
|
+
};
|
55
|
+
|
56
|
+
// the timeout is set because we can't get the value from the input without it
|
57
|
+
function pasteHandler(e){
|
58
|
+
var self = this;
|
59
|
+
e = $.event.fix(e || window.e);
|
60
|
+
e.type = 'paste';
|
61
|
+
// Execute the right handlers by setting the event type to paste
|
62
|
+
setTimeout(function(){ $.event.handle.call(self, e); }, 1);
|
63
|
+
};
|
64
|
+
|
65
|
+
$.extend({
|
66
|
+
mask : {
|
67
|
+
|
68
|
+
// the mask rules. You may add yours!
|
69
|
+
// number rules will be overwritten
|
70
|
+
rules : {
|
71
|
+
'z': /[a-z]/,
|
72
|
+
'Z': /[A-Z]/,
|
73
|
+
'a': /[a-zA-Z]/,
|
74
|
+
'*': /[0-9a-zA-Z]/,
|
75
|
+
'@': /[0-9a-zA-ZçÇáàãâéèêíìóòôõúùü]/
|
76
|
+
},
|
77
|
+
|
78
|
+
// these keys will be ignored by the mask.
|
79
|
+
// all these numbers where obtained on the keydown event
|
80
|
+
keyRepresentation : {
|
81
|
+
8 : 'backspace',
|
82
|
+
9 : 'tab',
|
83
|
+
13 : 'enter',
|
84
|
+
16 : 'shift',
|
85
|
+
17 : 'control',
|
86
|
+
18 : 'alt',
|
87
|
+
27 : 'esc',
|
88
|
+
33 : 'page up',
|
89
|
+
34 : 'page down',
|
90
|
+
35 : 'end',
|
91
|
+
36 : 'home',
|
92
|
+
37 : 'left',
|
93
|
+
38 : 'up',
|
94
|
+
39 : 'right',
|
95
|
+
40 : 'down',
|
96
|
+
45 : 'insert',
|
97
|
+
46 : 'delete',
|
98
|
+
116 : 'f5',
|
99
|
+
123 : 'f12',
|
100
|
+
224 : 'command'
|
101
|
+
},
|
102
|
+
|
103
|
+
iphoneKeyRepresentation : {
|
104
|
+
10 : 'go',
|
105
|
+
127 : 'delete'
|
106
|
+
},
|
107
|
+
|
108
|
+
signals : {
|
109
|
+
'+' : '',
|
110
|
+
'-' : '-'
|
111
|
+
},
|
112
|
+
|
113
|
+
// default settings for the plugin
|
114
|
+
options : {
|
115
|
+
attr: 'alt', // an attr to look for the mask name or the mask itself
|
116
|
+
mask: null, // the mask to be used on the input
|
117
|
+
type: 'fixed', // the mask of this mask
|
118
|
+
maxLength: -1, // the maxLength of the mask
|
119
|
+
defaultValue: '', // the default value for this input
|
120
|
+
signal: false, // this should not be set, to use signal at masks put the signal you want ('-' or '+') at the default value of this mask.
|
121
|
+
// See the defined masks for a better understanding.
|
122
|
+
|
123
|
+
textAlign: true, // use false to not use text-align on any mask (at least not by the plugin, you may apply it using css)
|
124
|
+
selectCharsOnFocus: true, // select all chars from input on its focus
|
125
|
+
autoTab: true, // auto focus the next form element when you type the mask completely
|
126
|
+
setSize: false, // sets the input size based on the length of the mask (work with fixed and reverse masks only)
|
127
|
+
fixedChars : '[(),.:/ -]', // fixed chars to be used on the masks. You may change it for your needs!
|
128
|
+
|
129
|
+
onInvalid : function(){},
|
130
|
+
onValid : function(){},
|
131
|
+
onOverflow : function(){}
|
132
|
+
},
|
133
|
+
|
134
|
+
// masks. You may add yours!
|
135
|
+
// Ex: $.fn.setMask.masks.msk = {mask: '999'}
|
136
|
+
// and then if the 'attr' options value is 'alt', your input should look like:
|
137
|
+
// <input type="text" name="some_name" id="some_name" alt="msk" />
|
138
|
+
masks : {
|
139
|
+
'phone' : { mask : '(99) 9999-9999' },
|
140
|
+
'phone-us' : { mask : '(999) 999-9999' },
|
141
|
+
'cpf' : { mask : '999.999.999-99' }, // cadastro nacional de pessoa fisica
|
142
|
+
'cnpj' : { mask : '99.999.999/9999-99' },
|
143
|
+
'date' : { mask : '39/19/9999' }, //uk date
|
144
|
+
'date-us' : { mask : '19/39/9999' },
|
145
|
+
'cep' : { mask : '99999-999' },
|
146
|
+
'time' : { mask : '29:59' },
|
147
|
+
'cc' : { mask : '9999 9999 9999 9999' }, //credit card mask
|
148
|
+
'integer' : { mask : '999.999.999.999', type : 'reverse' },
|
149
|
+
'decimal' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '000' },
|
150
|
+
'decimal-us' : { mask : '99.999,999,999,999', type : 'reverse', defaultValue : '000' },
|
151
|
+
'signed-decimal' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' },
|
152
|
+
'signed-decimal-us' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' }
|
153
|
+
},
|
154
|
+
|
155
|
+
init : function(){
|
156
|
+
// if has not inited...
|
157
|
+
if( !this.hasInit ){
|
158
|
+
|
159
|
+
var self = this, i,
|
160
|
+
keyRep = (isIphone)? this.iphoneKeyRepresentation: this.keyRepresentation;
|
161
|
+
|
162
|
+
this.ignore = false;
|
163
|
+
|
164
|
+
// constructs number rules
|
165
|
+
for(i=0; i<=9; i++) this.rules[i] = new RegExp('[0-'+i+']');
|
166
|
+
|
167
|
+
this.keyRep = keyRep;
|
168
|
+
// ignore keys array creation for iphone or the normal ones
|
169
|
+
this.ignoreKeys = [];
|
170
|
+
$.each(keyRep,function(key){
|
171
|
+
self.ignoreKeys.push( parseInt(key) );
|
172
|
+
});
|
173
|
+
|
174
|
+
this.hasInit = true;
|
175
|
+
}
|
176
|
+
},
|
177
|
+
|
178
|
+
set: function(el,options){
|
179
|
+
|
180
|
+
var maskObj = this,
|
181
|
+
$el = $(el),
|
182
|
+
mlStr = 'maxLength';
|
183
|
+
|
184
|
+
options = options || {};
|
185
|
+
this.init();
|
186
|
+
|
187
|
+
return $el.each(function(){
|
188
|
+
|
189
|
+
if(options.attr) maskObj.options.attr = options.attr;
|
190
|
+
|
191
|
+
var $this = $(this),
|
192
|
+
o = $.extend({}, maskObj.options),
|
193
|
+
attrValue = $this.attr(o.attr),
|
194
|
+
tmpMask = '';
|
195
|
+
|
196
|
+
// then we look for the 'attr' option
|
197
|
+
tmpMask = (typeof options == 'string')? options: (attrValue != '')? attrValue: null;
|
198
|
+
if(tmpMask) o.mask = tmpMask;
|
199
|
+
|
200
|
+
// then we see if it's a defined mask
|
201
|
+
if(maskObj.masks[tmpMask]) o = $.extend(o, maskObj.masks[tmpMask]);
|
202
|
+
|
203
|
+
// then it looks if the options is an object, if it is we will overwrite the actual options
|
204
|
+
if(typeof options == 'object' && options.constructor != Array) o = $.extend(o, options);
|
205
|
+
|
206
|
+
//then we look for some metadata on the input
|
207
|
+
if($.metadata) o = $.extend(o, $this.metadata());
|
208
|
+
|
209
|
+
if(o.mask != null){
|
210
|
+
|
211
|
+
if($this.data('mask')) maskObj.unset($this);
|
212
|
+
|
213
|
+
var defaultValue = o.defaultValue,
|
214
|
+
reverse = (o.type=='reverse'),
|
215
|
+
fixedCharsRegG = new RegExp(o.fixedChars, 'g');
|
216
|
+
|
217
|
+
if(o.maxLength == -1) o.maxLength = $this.attr(mlStr);
|
218
|
+
|
219
|
+
o = $.extend({}, o,{
|
220
|
+
fixedCharsReg: new RegExp(o.fixedChars),
|
221
|
+
fixedCharsRegG: fixedCharsRegG,
|
222
|
+
maskArray: o.mask.split(''),
|
223
|
+
maskNonFixedCharsArray: o.mask.replace(fixedCharsRegG, '').split('')
|
224
|
+
});
|
225
|
+
|
226
|
+
//setSize option (this is not removed from the input (while removing the mask) since this would be kind of funky)
|
227
|
+
if((o.type=='fixed' || reverse) && o.setSize && !$this.attr('size')) $this.attr('size', o.mask.length);
|
228
|
+
|
229
|
+
//sets text-align right for reverse masks
|
230
|
+
if(reverse && o.textAlign) $this.css('text-align', 'right');
|
231
|
+
|
232
|
+
if(this.value!='' || defaultValue!=''){
|
233
|
+
// apply mask to the current value of the input or to the default value
|
234
|
+
var val = maskObj.string((this.value!='')? this.value: defaultValue, o);
|
235
|
+
//setting defaultValue fixes the reset button from the form
|
236
|
+
this.defaultValue = val;
|
237
|
+
$this.val(val);
|
238
|
+
}
|
239
|
+
|
240
|
+
// compatibility patch for infinite mask, that is now repeat
|
241
|
+
if(o.type=='infinite') o.type = 'repeat';
|
242
|
+
|
243
|
+
$this.data('mask', o);
|
244
|
+
|
245
|
+
// removes the maxLength attribute (it will be set again if you use the unset method)
|
246
|
+
$this.removeAttr(mlStr);
|
247
|
+
|
248
|
+
// setting the input events
|
249
|
+
$this.bind('keydown.mask', {func:maskObj._onKeyDown, thisObj:maskObj}, maskObj._onMask)
|
250
|
+
.bind('keypress.mask', {func:maskObj._onKeyPress, thisObj:maskObj}, maskObj._onMask)
|
251
|
+
.bind('keyup.mask', {func:maskObj._onKeyUp, thisObj:maskObj}, maskObj._onMask)
|
252
|
+
.bind('paste.mask', {func:maskObj._onPaste, thisObj:maskObj}, maskObj._onMask)
|
253
|
+
.bind('focus.mask', maskObj._onFocus)
|
254
|
+
.bind('blur.mask', maskObj._onBlur)
|
255
|
+
.bind('change.mask', maskObj._onChange);
|
256
|
+
}
|
257
|
+
});
|
258
|
+
},
|
259
|
+
|
260
|
+
//unsets the mask from el
|
261
|
+
unset : function(el){
|
262
|
+
var $el = $(el);
|
263
|
+
|
264
|
+
return $el.each(function(){
|
265
|
+
var $this = $(this);
|
266
|
+
if($this.data('mask')){
|
267
|
+
var maxLength = $this.data('mask').maxLength;
|
268
|
+
if(maxLength != -1) $this.attr('maxLength', maxLength);
|
269
|
+
$this.unbind('.mask')
|
270
|
+
.removeData('mask');
|
271
|
+
}
|
272
|
+
});
|
273
|
+
},
|
274
|
+
|
275
|
+
//masks a string
|
276
|
+
string : function(str, options){
|
277
|
+
this.init();
|
278
|
+
var o={};
|
279
|
+
if(typeof str != 'string') str = String(str);
|
280
|
+
switch(typeof options){
|
281
|
+
case 'string':
|
282
|
+
// then we see if it's a defined mask
|
283
|
+
if(this.masks[options]) o = $.extend(o, this.masks[options]);
|
284
|
+
else o.mask = options;
|
285
|
+
break;
|
286
|
+
case 'object':
|
287
|
+
o = options;
|
288
|
+
}
|
289
|
+
if(!o.fixedChars) o.fixedChars = this.options.fixedChars;
|
290
|
+
|
291
|
+
var fixedCharsReg = new RegExp(o.fixedChars),
|
292
|
+
fixedCharsRegG = new RegExp(o.fixedChars, 'g');
|
293
|
+
|
294
|
+
// insert signal if any
|
295
|
+
if( (o.type=='reverse') && o.defaultValue ){
|
296
|
+
if( typeof this.signals[o.defaultValue.charAt(0)] != 'undefined' ){
|
297
|
+
var maybeASignal = str.charAt(0);
|
298
|
+
o.signal = (typeof this.signals[maybeASignal] != 'undefined') ? this.signals[maybeASignal] : this.signals[o.defaultValue.charAt(0)];
|
299
|
+
o.defaultValue = o.defaultValue.substring(1);
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
return this.__maskArray(str.split(''),
|
304
|
+
o.mask.replace(fixedCharsRegG, '').split(''),
|
305
|
+
o.mask.split(''),
|
306
|
+
o.type,
|
307
|
+
o.maxLength,
|
308
|
+
o.defaultValue,
|
309
|
+
fixedCharsReg,
|
310
|
+
o.signal);
|
311
|
+
},
|
312
|
+
|
313
|
+
// all the 3 events below are here just to fix the change event on reversed masks.
|
314
|
+
// It isn't fired in cases that the keypress event returns false (needed).
|
315
|
+
_onFocus: function(e){
|
316
|
+
var $this = $(this), dataObj = $this.data('mask');
|
317
|
+
dataObj.inputFocusValue = $this.val();
|
318
|
+
dataObj.changed = false;
|
319
|
+
if(dataObj.selectCharsOnFocus) $this.select();
|
320
|
+
},
|
321
|
+
|
322
|
+
_onBlur: function(e){
|
323
|
+
var $this = $(this), dataObj = $this.data('mask');
|
324
|
+
if(dataObj.inputFocusValue != $this.val() && !dataObj.changed)
|
325
|
+
$this.trigger('change');
|
326
|
+
},
|
327
|
+
|
328
|
+
_onChange: function(e){
|
329
|
+
$(this).data('mask').changed = true;
|
330
|
+
},
|
331
|
+
|
332
|
+
_onMask : function(e){
|
333
|
+
var thisObj = e.data.thisObj,
|
334
|
+
o = {};
|
335
|
+
o._this = e.target;
|
336
|
+
o.$this = $(o._this);
|
337
|
+
// if the input is readonly it does nothing
|
338
|
+
if(o.$this.attr('readonly')) return true;
|
339
|
+
o.data = o.$this.data('mask');
|
340
|
+
o[o.data.type] = true;
|
341
|
+
o.value = o.$this.val();
|
342
|
+
o.nKey = thisObj.__getKeyNumber(e);
|
343
|
+
o.range = thisObj.__getRange(o._this);
|
344
|
+
o.valueArray = o.value.split('');
|
345
|
+
return e.data.func.call(thisObj, e, o);
|
346
|
+
},
|
347
|
+
|
348
|
+
_onKeyDown : function(e,o){
|
349
|
+
// lets say keypress at desktop == keydown at iphone (theres no keypress at iphone)
|
350
|
+
this.ignore = $.inArray(o.nKey, this.ignoreKeys) > -1 || e.ctrlKey || e.metaKey || e.altKey;
|
351
|
+
if(this.ignore){
|
352
|
+
var rep = this.keyRep[o.nKey];
|
353
|
+
o.data.onValid.call(o._this, rep? rep: '', o.nKey);
|
354
|
+
}
|
355
|
+
return isIphone ? this._keyPress(e, o) : true;
|
356
|
+
},
|
357
|
+
|
358
|
+
_onKeyUp : function(e, o){
|
359
|
+
//9=TAB_KEY 16=SHIFT_KEY
|
360
|
+
//this is a little bug, when you go to an input with tab key
|
361
|
+
//it would remove the range selected by default, and that's not a desired behavior
|
362
|
+
if(o.nKey==9 || o.nKey==16) return true;
|
363
|
+
|
364
|
+
if(o.data.type=='repeat'){
|
365
|
+
this.__autoTab(o);
|
366
|
+
return true;
|
367
|
+
}
|
368
|
+
|
369
|
+
return this._onPaste(e, o);
|
370
|
+
},
|
371
|
+
|
372
|
+
_onPaste : function(e,o){
|
373
|
+
// changes the signal at the data obj from the input
|
374
|
+
if(o.reverse) this.__changeSignal(e.type, o);
|
375
|
+
|
376
|
+
var $thisVal = this.__maskArray(
|
377
|
+
o.valueArray,
|
378
|
+
o.data.maskNonFixedCharsArray,
|
379
|
+
o.data.maskArray,
|
380
|
+
o.data.type,
|
381
|
+
o.data.maxLength,
|
382
|
+
o.data.defaultValue,
|
383
|
+
o.data.fixedCharsReg,
|
384
|
+
o.data.signal
|
385
|
+
);
|
386
|
+
|
387
|
+
o.$this.val( $thisVal );
|
388
|
+
// this makes the caret stay at first position when
|
389
|
+
// the user removes all values in an input and the plugin adds the default value to it (if it haves one).
|
390
|
+
if( !o.reverse && o.data.defaultValue.length && (o.range.start==o.range.end) )
|
391
|
+
this.__setRange(o._this, o.range.start, o.range.end);
|
392
|
+
|
393
|
+
//fix so ie's and safari's caret won't go to the end of the input value.
|
394
|
+
if( ($.browser.msie || $.browser.safari) && !o.reverse)
|
395
|
+
this.__setRange(o._this,o.range.start,o.range.end);
|
396
|
+
|
397
|
+
if(this.ignore) return true;
|
398
|
+
|
399
|
+
this.__autoTab(o);
|
400
|
+
return true;
|
401
|
+
},
|
402
|
+
|
403
|
+
_onKeyPress: function(e, o){
|
404
|
+
|
405
|
+
if(this.ignore) return true;
|
406
|
+
|
407
|
+
// changes the signal at the data obj from the input
|
408
|
+
if(o.reverse) this.__changeSignal(e.type, o);
|
409
|
+
|
410
|
+
var c = String.fromCharCode(o.nKey),
|
411
|
+
rangeStart = o.range.start,
|
412
|
+
rawValue = o.value,
|
413
|
+
maskArray = o.data.maskArray;
|
414
|
+
|
415
|
+
if(o.reverse){
|
416
|
+
// the input value from the range start to the value start
|
417
|
+
var valueStart = rawValue.substr(0, rangeStart),
|
418
|
+
// the input value from the range end to the value end
|
419
|
+
valueEnd = rawValue.substr(o.range.end, rawValue.length);
|
420
|
+
|
421
|
+
rawValue = valueStart+c+valueEnd;
|
422
|
+
//necessary, if not decremented you will be able to input just the mask.length-1 if signal!=''
|
423
|
+
//ex: mask:99,999.999.999 you will be able to input 99,999.999.99
|
424
|
+
if(o.data.signal && (rangeStart-o.data.signal.length > 0)) rangeStart-=o.data.signal.length;
|
425
|
+
}
|
426
|
+
|
427
|
+
var valueArray = rawValue.replace(o.data.fixedCharsRegG, '').split(''),
|
428
|
+
// searches for fixed chars begining from the range start position, till it finds a non fixed
|
429
|
+
extraPos = this.__extraPositionsTill(rangeStart, maskArray, o.data.fixedCharsReg);
|
430
|
+
|
431
|
+
o.rsEp = rangeStart+extraPos;
|
432
|
+
|
433
|
+
if(o.repeat) o.rsEp = 0;
|
434
|
+
|
435
|
+
// if the rule for this character doesnt exist (value.length is bigger than mask.length)
|
436
|
+
// added a verification for maxLength in the case of the repeat type mask
|
437
|
+
if( !this.rules[maskArray[o.rsEp]] || (o.data.maxLength != -1 && valueArray.length >= o.data.maxLength && o.repeat)){
|
438
|
+
// auto focus on the next input of the current form
|
439
|
+
o.data.onOverflow.call(o._this, c, o.nKey);
|
440
|
+
return false;
|
441
|
+
}
|
442
|
+
|
443
|
+
// if the new character is not obeying the law... :P
|
444
|
+
else if( !this.rules[maskArray[o.rsEp]].test( c ) ){
|
445
|
+
o.data.onInvalid.call(o._this, c, o.nKey);
|
446
|
+
return false;
|
447
|
+
}
|
448
|
+
|
449
|
+
else o.data.onValid.call(o._this, c, o.nKey);
|
450
|
+
|
451
|
+
var $thisVal = this.__maskArray(
|
452
|
+
valueArray,
|
453
|
+
o.data.maskNonFixedCharsArray,
|
454
|
+
maskArray,
|
455
|
+
o.data.type,
|
456
|
+
o.data.maxLength,
|
457
|
+
o.data.defaultValue,
|
458
|
+
o.data.fixedCharsReg,
|
459
|
+
o.data.signal,
|
460
|
+
extraPos
|
461
|
+
);
|
462
|
+
|
463
|
+
o.$this.val( $thisVal );
|
464
|
+
|
465
|
+
return (o.reverse)? this._keyPressReverse(e, o): (o.fixed)? this._keyPressFixed(e, o): true;
|
466
|
+
},
|
467
|
+
|
468
|
+
_keyPressFixed: function(e, o){
|
469
|
+
|
470
|
+
if(o.range.start==o.range.end){
|
471
|
+
// the 0 thing is cause theres a particular behavior i wasnt liking when you put a default
|
472
|
+
// value on a fixed mask and you select the value from the input the range would go to the
|
473
|
+
// end of the string when you enter a char. with this it will overwrite the first char wich is a better behavior.
|
474
|
+
// opera fix, cant have range value bigger than value length, i think it loops thought the input value...
|
475
|
+
if((o.rsEp==0 && o.value.length==0) || o.rsEp < o.value.length)
|
476
|
+
this.__setRange(o._this, o.rsEp, o.rsEp+1);
|
477
|
+
}
|
478
|
+
else
|
479
|
+
this.__setRange(o._this, o.range.start, o.range.end);
|
480
|
+
|
481
|
+
return true;
|
482
|
+
},
|
483
|
+
|
484
|
+
_keyPressReverse: function(e, o){
|
485
|
+
//fix for ie
|
486
|
+
//this bug was pointed by Pedro Martins
|
487
|
+
//it fixes a strange behavior that ie was having after a char was inputted in a text input that
|
488
|
+
//had its content selected by any range
|
489
|
+
if($.browser.msie && ((o.range.start==0 && o.range.end==0) || o.range.start != o.range.end ))
|
490
|
+
this.__setRange(o._this, o.value.length);
|
491
|
+
return false;
|
492
|
+
},
|
493
|
+
|
494
|
+
__autoTab: function(o){
|
495
|
+
if(o.data.autoTab
|
496
|
+
&& (
|
497
|
+
(
|
498
|
+
o.$this.val().length >= o.data.maskArray.length
|
499
|
+
&& !o.repeat
|
500
|
+
) || (
|
501
|
+
o.data.maxLength != -1
|
502
|
+
&& o.valueArray.length >= o.data.maxLength
|
503
|
+
&& o.repeat
|
504
|
+
)
|
505
|
+
)
|
506
|
+
){
|
507
|
+
var nextEl = this.__getNextInput(o._this, o.data.autoTab);
|
508
|
+
if(nextEl){
|
509
|
+
o.$this.trigger('blur');
|
510
|
+
nextEl.focus().select();
|
511
|
+
}
|
512
|
+
}
|
513
|
+
},
|
514
|
+
|
515
|
+
// changes the signal at the data obj from the input
|
516
|
+
__changeSignal : function(eventType,o){
|
517
|
+
if(o.data.signal!==false){
|
518
|
+
var inputChar = (eventType=='paste')? o.value.charAt(0): String.fromCharCode(o.nKey);
|
519
|
+
if( this.signals && (typeof this.signals[inputChar] != 'undefined') ){
|
520
|
+
o.data.signal = this.signals[inputChar];
|
521
|
+
}
|
522
|
+
}
|
523
|
+
},
|
524
|
+
|
525
|
+
__getKeyNumber : function(e){
|
526
|
+
return (e.charCode||e.keyCode||e.which);
|
527
|
+
},
|
528
|
+
|
529
|
+
// this function is totaly specific to be used with this plugin, youll never need it
|
530
|
+
// it gets the array representing an unmasked string and masks it depending on the type of the mask
|
531
|
+
__maskArray : function(valueArray, maskNonFixedCharsArray, maskArray, type, maxlength, defaultValue, fixedCharsReg, signal, extraPos){
|
532
|
+
if(type == 'reverse') valueArray.reverse();
|
533
|
+
valueArray = this.__removeInvalidChars(valueArray, maskNonFixedCharsArray, type=='repeat'||type=='infinite');
|
534
|
+
if(defaultValue) valueArray = this.__applyDefaultValue.call(valueArray, defaultValue);
|
535
|
+
valueArray = this.__applyMask(valueArray, maskArray, extraPos, fixedCharsReg);
|
536
|
+
switch(type){
|
537
|
+
case 'reverse':
|
538
|
+
valueArray.reverse();
|
539
|
+
return (signal || '')+valueArray.join('').substring(valueArray.length-maskArray.length);
|
540
|
+
case 'infinite': case 'repeat':
|
541
|
+
var joinedValue = valueArray.join('');
|
542
|
+
return (maxlength != -1 && valueArray.length >= maxlength)? joinedValue.substring(0, maxlength): joinedValue;
|
543
|
+
default:
|
544
|
+
return valueArray.join('').substring(0, maskArray.length);
|
545
|
+
}
|
546
|
+
return '';
|
547
|
+
},
|
548
|
+
|
549
|
+
// applyes the default value to the result string
|
550
|
+
__applyDefaultValue : function(defaultValue){
|
551
|
+
var defLen = defaultValue.length,thisLen = this.length,i;
|
552
|
+
//removes the leading chars
|
553
|
+
for(i=thisLen-1;i>=0;i--){
|
554
|
+
if(this[i]==defaultValue.charAt(0)) this.pop();
|
555
|
+
else break;
|
556
|
+
}
|
557
|
+
// apply the default value
|
558
|
+
for(i=0;i<defLen;i++) if(!this[i])
|
559
|
+
this[i] = defaultValue.charAt(i);
|
560
|
+
|
561
|
+
return this;
|
562
|
+
},
|
563
|
+
|
564
|
+
// Removes values that doesnt match the mask from the valueArray
|
565
|
+
// Returns the array without the invalid chars.
|
566
|
+
__removeInvalidChars : function(valueArray, maskNonFixedCharsArray, repeatType){
|
567
|
+
// removes invalid chars
|
568
|
+
for(var i=0, y=0; i<valueArray.length; i++ ){
|
569
|
+
if( maskNonFixedCharsArray[y] &&
|
570
|
+
this.rules[maskNonFixedCharsArray[y]] &&
|
571
|
+
!this.rules[maskNonFixedCharsArray[y]].test(valueArray[i]) ){
|
572
|
+
valueArray.splice(i,1);
|
573
|
+
if(!repeatType) y--;
|
574
|
+
i--;
|
575
|
+
}
|
576
|
+
if(!repeatType) y++;
|
577
|
+
}
|
578
|
+
return valueArray;
|
579
|
+
},
|
580
|
+
|
581
|
+
// Apply the current input mask to the valueArray and returns it.
|
582
|
+
__applyMask : function(valueArray, maskArray, plus, fixedCharsReg){
|
583
|
+
if( typeof plus == 'undefined' ) plus = 0;
|
584
|
+
// apply the current mask to the array of chars
|
585
|
+
for(var i=0; i<valueArray.length+plus; i++ ){
|
586
|
+
if( maskArray[i] && fixedCharsReg.test(maskArray[i]) )
|
587
|
+
valueArray.splice(i, 0, maskArray[i]);
|
588
|
+
}
|
589
|
+
return valueArray;
|
590
|
+
},
|
591
|
+
|
592
|
+
// searches for fixed chars begining from the range start position, till it finds a non fixed
|
593
|
+
__extraPositionsTill : function(rangeStart, maskArray, fixedCharsReg){
|
594
|
+
var extraPos = 0;
|
595
|
+
while(fixedCharsReg.test(maskArray[rangeStart++])){
|
596
|
+
extraPos++;
|
597
|
+
}
|
598
|
+
return extraPos;
|
599
|
+
},
|
600
|
+
|
601
|
+
__getNextInput: function(input, selector){
|
602
|
+
var formEls = input.form.elements,
|
603
|
+
initialInputIndex = $.inArray(input, formEls) + 1,
|
604
|
+
$input = null,
|
605
|
+
i;
|
606
|
+
// look for next input on the form of the pased input
|
607
|
+
for(i = initialInputIndex; i < formEls.length; i++){
|
608
|
+
$input = $(formEls[i]);
|
609
|
+
if(this.__isNextInput($input, selector))
|
610
|
+
return $input;
|
611
|
+
}
|
612
|
+
|
613
|
+
var forms = document.forms,
|
614
|
+
initialFormIndex = $.inArray(input.form, forms) + 1,
|
615
|
+
y, tmpFormEls = null;
|
616
|
+
// look for the next forms for the next input
|
617
|
+
for(y = initialFormIndex; y < forms.length; y++){
|
618
|
+
tmpFormEls = forms[y].elements;
|
619
|
+
for(i = 0; i < tmpFormEls.length; i++){
|
620
|
+
$input = $(tmpFormEls[i]);
|
621
|
+
if(this.__isNextInput($input, selector))
|
622
|
+
return $input;
|
623
|
+
}
|
624
|
+
}
|
625
|
+
return null;
|
626
|
+
},
|
627
|
+
|
628
|
+
__isNextInput: function($formEl, selector){
|
629
|
+
var formEl = $formEl.get(0);
|
630
|
+
return formEl
|
631
|
+
&& (formEl.offsetWidth > 0 || formEl.offsetHeight > 0)
|
632
|
+
&& formEl.nodeName != 'FIELDSET'
|
633
|
+
&& (selector === true || (typeof selector == 'string' && $formEl.is(selector)));
|
634
|
+
},
|
635
|
+
|
636
|
+
// http://www.bazon.net/mishoo/articles.epl?art_id=1292
|
637
|
+
__setRange : function(input, start, end) {
|
638
|
+
if(typeof end == 'undefined') end = start;
|
639
|
+
if (input.setSelectionRange){
|
640
|
+
input.setSelectionRange(start, end);
|
641
|
+
}
|
642
|
+
else{
|
643
|
+
// assumed IE
|
644
|
+
var range = input.createTextRange();
|
645
|
+
range.collapse();
|
646
|
+
range.moveStart('character', start);
|
647
|
+
range.moveEnd('character', end - start);
|
648
|
+
range.select();
|
649
|
+
}
|
650
|
+
},
|
651
|
+
|
652
|
+
// adaptation from http://digitarald.de/project/autocompleter/
|
653
|
+
__getRange : function(input){
|
654
|
+
if (!$.browser.msie) return {start: input.selectionStart, end: input.selectionEnd};
|
655
|
+
var pos = {start: 0, end: 0},
|
656
|
+
range = document.selection.createRange();
|
657
|
+
pos.start = 0 - range.duplicate().moveStart('character', -100000);
|
658
|
+
pos.end = pos.start + range.text.length;
|
659
|
+
return pos;
|
660
|
+
},
|
661
|
+
|
662
|
+
//deprecated
|
663
|
+
unmaskedVal : function(el){
|
664
|
+
return $(el).val().replace($.mask.fixedCharsRegG, '');
|
665
|
+
}
|
666
|
+
|
667
|
+
}
|
668
|
+
});
|
669
|
+
|
670
|
+
$.fn.extend({
|
671
|
+
setMask : function(options){
|
672
|
+
return $.mask.set(this, options);
|
673
|
+
},
|
674
|
+
unsetMask : function(){
|
675
|
+
return $.mask.unset(this);
|
676
|
+
},
|
677
|
+
//deprecated
|
678
|
+
unmaskedVal : function(){
|
679
|
+
return $.mask.unmaskedVal(this[0]);
|
680
|
+
}
|
681
|
+
});
|
682
|
+
})(jQuery);
|
683
|
+
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meio_mask-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rafael Macedo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
description: Easy way to use meio_mask jquery plugin on rails
|
31
|
+
email:
|
32
|
+
- macedo.rafaelfernandes@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/meio_mask-rails.rb
|
43
|
+
- lib/meio_mask/rails.rb
|
44
|
+
- lib/meio_mask/rails/engine.rb
|
45
|
+
- lib/meio_mask/rails/version.rb
|
46
|
+
- meio_mask-rails.gemspec
|
47
|
+
- vendor/assets/javascripts/jquery.meio.mask.js
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.21
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Easy way to use meio_mask jquery plugin on rails
|
72
|
+
test_files: []
|