inputmask-multi-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODYxNWIwY2UxZjI5ZDdkYzMyMmE4YmUzMjhjZjNmMjIyYzFlMWM2MQ==
5
+ data.tar.gz: !binary |-
6
+ OWIxODA5OTNkNjIzMzU4ZDdlNTU4YTdjMWMwMzcyN2FjOTI5ZDAxNQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjczZmU4YTZmZDQ1YWZlMTI3Y2RhYTJkYWYzZjVhZmYxN2VmZTVhZTA5YTMz
10
+ NDg2NjE4NDNjZmJiMDJkMjRhZWRhYTM3MWI3NGZmNmU0MTNmZDVkY2YwMmQw
11
+ MjNjYTNhMWI1OWViMzZiMTU5ZDVkNWEyZjI3OTBhNGNkOGVkMDk=
12
+ data.tar.gz: !binary |-
13
+ MWY4NDk4ZjgwNzM5YTQzYjllMGQzZDM1MGFlNGJjMTgxM2QyOWJlZGYxZTM0
14
+ MTI5ZmM4N2ZlYjBkMDBiZWI3MzFiMTFlZjY5YjA1ODQ5NDJjN2M3MDdhNWFi
15
+ NjdmNzIyMmY0NmFmYzA0NDUxNmJmYmM5NGJmYzIwNjk5YmZiNWQ=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 vaski
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,39 @@
1
+ # Inputmask::Multi::Rails
2
+
3
+ Inputmask Multi for Rails. https://github.com/andr-04/inputmask-multi
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'inputmask-multi-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install inputmask-multi-rails
18
+
19
+ Add the following to your `app/assets/javascripts/application.js`:
20
+
21
+ //= require jquery.bind-first
22
+ //= require jquery.inputmask
23
+ //= require jquery.inputmask-multi
24
+
25
+ You can also get the masks files to public directory by running the install generator:
26
+
27
+ $ rails generate inputmask_multi_rails:install
28
+
29
+ ## Usage
30
+
31
+ TODO: Write usage instructions here
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inputmask-multi-rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'inputmask-multi-rails'
8
+ gem.version = InputmaskMultiRails::VERSION
9
+ gem.authors = ['vaski']
10
+ gem.email = ['vadimmasakovski@gmail.com']
11
+ gem.description = %q{Inputmask Multi for Rails. https://github.com/andr-04/inputmask-multi}
12
+ gem.summary = %q{Inputmask Multi for Rails. https://github.com/andr-04/inputmask-multi}
13
+ gem.homepage = 'https://github.com/vaski/inputmask-multi-rails'
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_dependency 'railties', '~> 3.1'
22
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails'
2
+
3
+ module InputmaskMultiRails
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ desc 'Copy Inputmask Multi masks files to public directory.'
7
+ source_root File.expand_path('../../../../vendor/assets/javascripts', __FILE__)
8
+
9
+ def copy_public_data
10
+ say_status('copying', 'phone-codes.json, phones-ru.json, phones-us.json', :green)
11
+ copy_file 'phone-codes.json', 'public/phone-codes.json'
12
+ copy_file 'phones-ru.json', 'public/phones-ru.json'
13
+ copy_file 'phones-us.json', 'public/phones-us.json'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ require 'inputmask-multi-rails/version'
2
+
3
+ module InputmaskMultiRails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module InputmaskMultiRails
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,8 @@
1
+ /*
2
+ * jQuery.bind-first library v0.2.0
3
+ * Copyright (c) 2013 Vladimir Zhuravlev
4
+ *
5
+ * Released under MIT License
6
+ *
7
+ * Date: Sun Jan 20 16:12:09 ICT 2013
8
+ **/(function(a){function e(b,c,e){var f=c.split(/\s+/);b.each(function(){for(var b=0;b<f.length;++b){var c=a.trim(f[b]).match(/[^\.]+/i)[0];d(a(this),c,e)}})}function d(a,d,e){var f=c(a),g=f[d];if(!b){var h=e?g.splice(g.delegateCount-1,1)[0]:g.pop();g.splice(e?0:g.delegateCount||0,0,h)}else e?f.live.unshift(f.live.pop()):g.unshift(g.pop())}function c(c){return b?c.data("events"):a._data(c[0]).events}var b=parseFloat(a.fn.jquery)<1.7;a.fn.bindFirst=function(){var b=a.makeArray(arguments),c=b.shift();c&&(a.fn.bind.apply(this,arguments),e(this,c));return this},a.fn.delegateFirst=function(){var b=a.makeArray(arguments),c=b[1];c&&(b.splice(0,2),a.fn.delegate.apply(this,arguments),e(this,c,!0));return this},a.fn.liveFirst=function(){var b=a.makeArray(arguments);b.unshift(this.selector),a.fn.delegateFirst.apply(a(document),b);return this}})(jQuery)
@@ -0,0 +1,382 @@
1
+ /*
2
+ * @license Multi Input Mask plugin for jquery
3
+ * https://github.com/andr-04/inputmask-multi
4
+ * Copyright (c) 2012 Andrey Egorov
5
+ * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
6
+ * Version: 1.0.1
7
+ *
8
+ * Requriements:
9
+ * https://github.com/RobinHerbots/jquery.inputmask
10
+ * https://github.com/private-face/jquery.bind-first
11
+ */
12
+ (function ($) {
13
+ $.masksLoad = function(url) {
14
+ var maskList;
15
+ $.ajax({
16
+ url: url,
17
+ async: false,
18
+ dataType: 'json',
19
+ success: function (response) {
20
+ maskList = response;
21
+ }
22
+ });
23
+ return maskList;
24
+ }
25
+
26
+ $.masksSort = function(maskList, defs, match, key) {
27
+ maskList.sort(function (a, b) {
28
+ var ia = 0, ib = 0;
29
+ for (; (ia<a[key].length && ib<b[key].length);) {
30
+ var cha = a[key].charAt(ia);
31
+ var chb = b[key].charAt(ib);
32
+ if (!match.test(cha)) {
33
+ ia++;
34
+ continue;
35
+ }
36
+ if (!match.test(chb)) {
37
+ ib++;
38
+ continue;
39
+ }
40
+ if ($.inArray(cha, defs) != -1 && $.inArray(chb, defs) == -1) {
41
+ return 1;
42
+ }
43
+ if ($.inArray(cha, defs) == -1 && $.inArray(chb, defs) != -1) {
44
+ return -1;
45
+ }
46
+ if ($.inArray(cha, defs) == -1 && $.inArray(chb, defs) == -1) {
47
+ if (cha != chb) {
48
+ return cha < chb ? -1 : 1;
49
+ }
50
+ }
51
+ ia++;
52
+ ib++;
53
+ }
54
+ for (; (ia<a[key].length || ib<b[key].length);) {
55
+ if (ia<a[key].length && !match.test(a[key].charAt(ia))) {
56
+ ia++;
57
+ continue;
58
+ }
59
+ if (ib<b[key].length && !match.test(b[key].charAt(ib))) {
60
+ ib++;
61
+ continue;
62
+ }
63
+ if (ia<a[key].length) {
64
+ return 1;
65
+ }
66
+ if (ib<b[key].length) {
67
+ return -1;
68
+ }
69
+ }
70
+ return 0;
71
+ });
72
+ return maskList;
73
+ }
74
+
75
+ $.fn.inputmasks = function(maskOpts, mode) {
76
+ //Helper Functions
77
+ var caret = function(begin, end) {
78
+ if (typeof begin == 'number') {
79
+ end = (typeof end == 'number') ? end : begin;
80
+ if (this.setSelectionRange) {
81
+ this.setSelectionRange(begin, end);
82
+ } else if (this.createTextRange) {
83
+ var range = this.createTextRange();
84
+ range.collapse(true);
85
+ range.moveEnd('character', end);
86
+ range.moveStart('character', begin);
87
+ range.select();
88
+ }
89
+ } else {
90
+ if (this.setSelectionRange) {
91
+ begin = this.selectionStart;
92
+ end = this.selectionEnd;
93
+ } else if (document.selection && document.selection.createRange) {
94
+ var range = document.selection.createRange();
95
+ begin = 0 - range.duplicate().moveStart('character', -100000);
96
+ end = begin + range.text.length;
97
+ }
98
+ return {
99
+ begin: begin,
100
+ end: end
101
+ };
102
+ }
103
+ };
104
+
105
+ var keys = Object.keys || function(obj) {
106
+ if (obj !== Object(obj)) {
107
+ throw new TypeError('Invalid object');
108
+ }
109
+ var keys = [];
110
+ for (var key in obj) {
111
+ keys[keys.length] = key;
112
+ }
113
+ return keys;
114
+ };
115
+
116
+ maskOpts = $.extend(true, {
117
+ onMaskChange: $.noop
118
+ }, maskOpts);
119
+ var defs = {};
120
+ for (var def in maskOpts.inputmask.definitions) {
121
+ var validator = maskOpts.inputmask.definitions[def].validator;
122
+ switch (typeof validator) {
123
+ case "string":
124
+ defs[def] = new RegExp(validator);
125
+ break;
126
+ case "object":
127
+ if ("test" in maskOpts.definitions[def].validator) {
128
+ defs[def] = validator;
129
+ }
130
+ break;
131
+ case "function":
132
+ defs[def] = {
133
+ test: validator
134
+ };
135
+ break;
136
+ }
137
+ }
138
+ maskOpts.inputmask.definitions[maskOpts.replace] = {
139
+ validator: maskOpts.match.source,
140
+ cardinality: 1
141
+ };
142
+ var iphone = navigator.userAgent.match(/iphone/i) != null;
143
+ var oldmatch = false;
144
+ var placeholder = $.extend(true, {}, $.inputmask.defaults, maskOpts.inputmask).placeholder;
145
+ var insertMode = $.extend(true, {}, $.inputmask.defaults, maskOpts.inputmask).insertMode;
146
+
147
+ var maskMatch = function(text) {
148
+ var mtxt = "";
149
+ for (var i=0; i<text.length; i++) {
150
+ var ch = text.charAt(i);
151
+ if (ch == placeholder) {
152
+ break;
153
+ }
154
+ if (maskOpts.match.test(ch)) {
155
+ mtxt += ch;
156
+ }
157
+ }
158
+ for (var mid in maskOpts.list) {
159
+ var mask = maskOpts.list[mid][maskOpts.listKey];
160
+ var pass = true;
161
+ for (var it=0, im=0; (it<mtxt.length && im<mask.length);) {
162
+ var chm = mask.charAt(im);
163
+ var cht = mtxt.charAt(it);
164
+ if (!maskOpts.match.test(chm) && !(chm in defs)) {
165
+ im++;
166
+ continue;
167
+ }
168
+ if (((chm in defs) && defs[chm].test(cht)) || (cht == chm)) {
169
+ it++;
170
+ im++;
171
+ } else {
172
+ pass = false;
173
+ break;
174
+ }
175
+ }
176
+ if (pass && it==mtxt.length) {
177
+ var determined = mask.substr(im).search(maskOpts.match) == -1;
178
+ mask = mask.replace(new RegExp([maskOpts.match.source].concat(keys(defs)).join('|'), 'g'), maskOpts.replace);
179
+ var completed = mask.substr(im).search(maskOpts.replace) == -1;
180
+ return {
181
+ mask: mask,
182
+ obj: maskOpts.list[mid],
183
+ determined: determined,
184
+ completed: completed
185
+ };
186
+ }
187
+ }
188
+ return false;
189
+ }
190
+
191
+ var caretApply = function(oldMask, newMask, oldPos) {
192
+ if (!oldMask) {
193
+ return 0;
194
+ }
195
+ var pos = 0, startPos = 0;
196
+ for (; pos < oldPos.begin; pos++) {
197
+ if (oldMask.charAt(pos) == maskOpts.replace) {
198
+ startPos++;
199
+ }
200
+ }
201
+ var endPos = 0;
202
+ for (; pos < oldPos.end; pos++) {
203
+ if (oldMask.charAt(pos) == maskOpts.replace) {
204
+ endPos++;
205
+ }
206
+ }
207
+ for (pos = 0; (pos < newMask.length && (startPos > 0 || newMask.charAt(pos) != maskOpts.replace)); pos++) {
208
+ if (newMask.charAt(pos) == maskOpts.replace) {
209
+ startPos--;
210
+ }
211
+ }
212
+ startPos = pos;
213
+ for (; (pos < newMask.length && endPos > 0); pos++) {
214
+ if (newMask.charAt(pos) == maskOpts.replace) {
215
+ endPos--;
216
+ }
217
+ }
218
+ endPos = pos;
219
+ return {
220
+ begin: startPos,
221
+ end: endPos
222
+ };
223
+ }
224
+
225
+ var maskUnbind = function() {
226
+ $(this)
227
+ .unbind("keypress.inputmask", masksKeyPress)
228
+ .unbind("input.inputmask", masksPaste)
229
+ .unbind("paste.inputmask", masksPaste)
230
+ .unbind("dragdrop.inputmask", masksPaste)
231
+ .unbind("drop.inputmask", masksPaste)
232
+ .unbind("keydown.inputmask", masksKeyDown)
233
+ .unbind("setvalue.inputmask", masksSetValue)
234
+ .unbind("blur.inputmask", masksChange);
235
+ }
236
+
237
+ var maskRebind = function() {
238
+ maskUnbind.call(this);
239
+ $(this)
240
+ .bindFirst("keypress.inputmask", masksKeyPress)
241
+ .bindFirst("input.inputmask", masksPaste)
242
+ .bindFirst("paste.inputmask", masksPaste)
243
+ .bindFirst("dragdrop.inputmask", masksPaste)
244
+ .bindFirst("drop.inputmask", masksPaste)
245
+ .bindFirst("keydown.inputmask", masksKeyDown)
246
+ .bindFirst("setvalue.inputmask", masksSetValue)
247
+ .bind("blur.inputmask", masksChange);
248
+ }
249
+
250
+ var maskApply = function(match, newtext) {
251
+ if (match && (newtext || match.mask != oldmatch.mask)) {
252
+ var caretPos;
253
+ if (!newtext) {
254
+ caretPos = caretApply(oldmatch.mask, match.mask, caret.call(this));
255
+ }
256
+ if (newtext) {
257
+ if (this._valueSet) {
258
+ this._valueSet(newtext);
259
+ } else {
260
+ this.value = newtext;
261
+ }
262
+ }
263
+ $(this).inputmask(match.mask, $.extend(true, maskOpts.inputmask, {
264
+ insertMode: insertMode
265
+ }));
266
+ if (!newtext) {
267
+ caret.call(this, caretPos.begin, caretPos.end);
268
+ }
269
+ }
270
+ oldmatch = match;
271
+ maskOpts.onMaskChange.call(this, match.obj, match.determined);
272
+ return true;
273
+ }
274
+
275
+ var keyboardApply = function(e, text, insert) {
276
+ var match = maskMatch(text);
277
+ if (!match || match.obj != oldmatch.obj || match.determined != oldmatch.determined) {
278
+ if (match) {
279
+ maskUnbind.call(this);
280
+ if (insert) {
281
+ maskApply.call(this, match);
282
+ $(this).trigger(e);
283
+ } else {
284
+ $(this).trigger(e);
285
+ maskApply.call(this, match);
286
+ }
287
+ maskRebind.call(this);
288
+ }
289
+ e.stopImmediatePropagation();
290
+ return false;
291
+ }
292
+ return true;
293
+ }
294
+
295
+ var masksKeyDown = function(e) {
296
+ e = e || window.event;
297
+ var k = e.which || e.charCode || e.keyCode;
298
+ if (k == 8 || k == 46 || (iphone && k == 127)) { // delete or backspace
299
+ var text = this._valueGet();
300
+ var caretPos = caret.call(this);
301
+ if (caretPos.begin == caretPos.end || (!insertMode && caretPos.begin == caretPos.end-1)) {
302
+ var pos = caretPos.begin;
303
+ do {
304
+ if (k != 46) { // backspace
305
+ pos--;
306
+ }
307
+ var chr = text.charAt(pos);
308
+ text = text.substring(0, pos) + text.substring(pos+1);
309
+ } while (pos>0 && pos<text.length && chr != placeholder && !maskOpts.match.test(chr));
310
+ } else {
311
+ var test = text.substring(0, caretPos.begin) + text.substring(caretPos.end);
312
+ if (test.search(maskOpts.match) == -1) {
313
+ text = test;
314
+ }
315
+ }
316
+ return keyboardApply.call(this, e, text, false);
317
+ }
318
+ if (k == 45) { // insert
319
+ insertMode = !insertMode;
320
+ }
321
+ return true;
322
+ }
323
+
324
+ var masksKeyPress = function(e) {
325
+ var text = this._valueGet();
326
+ e = e || window.event;
327
+ var k = e.which || e.charCode || e.keyCode, c = String.fromCharCode(k);
328
+ caretPos = caret.call(this);
329
+ text = text.substring(0, caretPos.begin) + c + text.substring(caretPos.end);
330
+ return keyboardApply.call(this, e, text, true);
331
+ }
332
+
333
+ var masksChange = function(e) {
334
+ var match = maskMatch(this._valueGet());
335
+ maskApply.call(this, match);
336
+ maskRebind.call(this);
337
+ return true;
338
+ }
339
+
340
+ var masksSetValue = function(e) {
341
+ maskInit.call(this);
342
+ e.stopImmediatePropagation();
343
+ return true;
344
+ }
345
+
346
+ var maskInit = function() {
347
+ var text;
348
+ if (this._valueGet) {
349
+ text = this._valueGet();
350
+ } else {
351
+ text = this.value;
352
+ }
353
+ var match = maskMatch(text);
354
+ while (!match && text.length>0) {
355
+ text = text.substr(0, text.length-1);
356
+ match = maskMatch(text);
357
+ }
358
+ maskApply.call(this, match, text);
359
+ maskRebind.call(this);
360
+ }
361
+
362
+ var masksPaste = function(e) {
363
+ var input = this;
364
+ setTimeout(function() {
365
+ maskInit.call(input);
366
+ }, 0);
367
+ e.stopImmediatePropagation();
368
+ return true;
369
+ }
370
+
371
+ switch (mode) {
372
+ case "isCompleted":
373
+ var res = maskMatch((this[0]._valueGet && this[0]._valueGet()) || this[0].value);
374
+ return (res && res.completed);
375
+ default:
376
+ this.each(function () {
377
+ maskInit.call(this);
378
+ });
379
+ return this;
380
+ }
381
+ }
382
+ })(jQuery);