kala-rails 0.1
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.
- data/.gitignore +1 -0
- data/README.md +22 -0
- data/Rakefile +4 -0
- data/kala-rails.gemspec +22 -0
- data/lib/kala-rails.rb +3 -0
- data/lib/kala/rails.rb +6 -0
- data/lib/kala/rails/engine.rb +6 -0
- data/lib/kala/rails/version.rb +6 -0
- data/vendor/assets/javascripts/jquery.kala.js +191 -0
- metadata +77 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# kala-rails
|
2
|
+
|
3
|
+
jquery.kala for Rails 3.1+ asset pipeline
|
4
|
+
|
5
|
+
### Installation
|
6
|
+
|
7
|
+
In your Gemfile, add this line:
|
8
|
+
|
9
|
+
gem "kala-rails"
|
10
|
+
|
11
|
+
Then, run `bundle install`.
|
12
|
+
|
13
|
+
In your app/assets/javascripts/application.js, add this line:
|
14
|
+
|
15
|
+
//= require jquery.kala
|
16
|
+
|
17
|
+
You're done!
|
18
|
+
|
19
|
+
|
20
|
+
### Use
|
21
|
+
|
22
|
+
See http://github.com/archit/kala for usage info.
|
data/Rakefile
ADDED
data/kala-rails.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/kala/rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "kala-rails"
|
6
|
+
s.version = Kala::Rails::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Archit Baweja"]
|
9
|
+
s.email = ["architbaweja@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/archit/kala-rails"
|
11
|
+
s.summary = "jquery.kala for Rails 3.1+ Asset pipeline"
|
12
|
+
s.description = "Provides easy installation and usage of jQuery.kala javascript library for your Rails 3.1+ application."
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
|
16
|
+
s.add_dependency "jquery-rails"
|
17
|
+
s.add_development_dependency "rails", "~> 3.1"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
|
21
|
+
s.require_path = 'lib'
|
22
|
+
end
|
data/lib/kala-rails.rb
ADDED
data/lib/kala/rails.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2012 Archit Baweja
|
3
|
+
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
|
4
|
+
*/
|
5
|
+
(function ($) {
|
6
|
+
/*
|
7
|
+
* Allows only time characters to be input
|
8
|
+
* TODO:
|
9
|
+
* Support other separators besides ':'
|
10
|
+
* Support Jack Bauer in fight against the terrorists.
|
11
|
+
*
|
12
|
+
* @name kala
|
13
|
+
* @param config { jackBauer: true }
|
14
|
+
* @param callback A function that runs if the number is not valid (fires onblur)
|
15
|
+
* @author Archit Baweja architbaweja@gmail.com
|
16
|
+
* @example $(".kala").kala();
|
17
|
+
*/
|
18
|
+
$.fn.kala = function(config, callback)
|
19
|
+
{
|
20
|
+
// shorthand for 24 hour format
|
21
|
+
if (typeof config === 'boolean') {
|
22
|
+
config = { 'jackBauer' : true };
|
23
|
+
}
|
24
|
+
config = config || {};
|
25
|
+
// callback function
|
26
|
+
var callback = typeof callback == "function" ? callback : function(){};
|
27
|
+
// set data and methods
|
28
|
+
return this.data("kala.jackBauer", false).data("kala.callback", callback).keypress($.fn.kala.keypress).keyup($.fn.kala.keyup).blur($.fn.kala.blur);
|
29
|
+
}
|
30
|
+
|
31
|
+
$.fn.kala.keypress = function(e)
|
32
|
+
{
|
33
|
+
// get if allow 24 hour format
|
34
|
+
var jackBauer = $.data(this, "kala.jackBauer");
|
35
|
+
// get the key that was pressed
|
36
|
+
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
|
37
|
+
|
38
|
+
// Allow spaces no matter what
|
39
|
+
if (key === 32) return true
|
40
|
+
|
41
|
+
var value = $(this).val();
|
42
|
+
// remove spaces so the math can be simpler
|
43
|
+
value = value.replace(/\s*/g, '')
|
44
|
+
// If we've reached the 7 limit (xx:xxpm), just return
|
45
|
+
if (value.length > 7) return false
|
46
|
+
|
47
|
+
// allow enter/return key (only when in an input box)
|
48
|
+
if (key == 13 && this.nodeName.toLowerCase() == "input") {
|
49
|
+
return true;
|
50
|
+
} else if (key == 13) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
var allow = false;
|
54
|
+
// allow Ctrl+A
|
55
|
+
if ((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
|
56
|
+
// allow Ctrl+X (cut)
|
57
|
+
if ((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
|
58
|
+
// allow Ctrl+C (copy)
|
59
|
+
if ((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
|
60
|
+
// allow Ctrl+Z (undo)
|
61
|
+
if ((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
|
62
|
+
// allow or deny Ctrl+V (paste), Shift+Ins
|
63
|
+
if ((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
|
64
|
+
|| (e.shiftKey && key == 45)) return true;
|
65
|
+
|
66
|
+
// if a number was not pressed
|
67
|
+
if (key < 48 || key > 57) {
|
68
|
+
// Allow : only after a number is entered
|
69
|
+
if (value.length > 0 && value.indexOf(":") == -1 && key == 58 && (value.length < 3 || ($.fn.getSelectionStart(this)) < 3)) return true;
|
70
|
+
|
71
|
+
// Allow am/pm at the end
|
72
|
+
if (value.length > 0 && value.length < 7 && !value.match(/(a|p|am|pm)/i)) return (key == 97 || key == 65 || key == 112 || key == 80);
|
73
|
+
if (value.length > 0 && value.length < 7 && !value.match(/m/i)) return (key == 109 || key == 77);
|
74
|
+
|
75
|
+
// check for other keys that have special purposes
|
76
|
+
// 8 backspace
|
77
|
+
// 9 tab
|
78
|
+
// 13 enter
|
79
|
+
// 35 end
|
80
|
+
// 36 home
|
81
|
+
// 37 left
|
82
|
+
// 39 right
|
83
|
+
// 46 del
|
84
|
+
if ($.inArray([8, 9, 13, 35, 36, 37, 39, 46], key) > 0) {
|
85
|
+
// for detecting special keys (listed above)
|
86
|
+
// IE does not support 'charCode' and ignores them in keypress anyway
|
87
|
+
if (typeof e.charCode != "undefined") {
|
88
|
+
// special keys have 'keyCode' and 'which' the same (e.g. backspace)
|
89
|
+
if (e.keyCode == e.which && e.which != 0) {
|
90
|
+
return true;
|
91
|
+
// . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
|
92
|
+
if (e.which == 46) return false;
|
93
|
+
} else if (e.keyCode != 0 && e.charCode == 0 && e.which == 0) {
|
94
|
+
// or keyCode != 0 and 'charCode'/'which' = 0
|
95
|
+
return true;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
} else {
|
99
|
+
return false
|
100
|
+
}
|
101
|
+
} else {
|
102
|
+
return true;
|
103
|
+
}
|
104
|
+
return false;
|
105
|
+
}
|
106
|
+
|
107
|
+
$.fn.kala.keyup = function(e)
|
108
|
+
{
|
109
|
+
var val = $(this).value;
|
110
|
+
if (val && val.length > 0) {
|
111
|
+
// get carat (cursor) position
|
112
|
+
var carat = $.fn.getSelectionStart(this);
|
113
|
+
var jackBauer = $.data(this, "kala.jackBauer");
|
114
|
+
|
115
|
+
var validChars = [0,1,2,3,4,5,6,7,8,9,':'];
|
116
|
+
// get length of the value (to loop through)
|
117
|
+
var length = val.length;
|
118
|
+
// loop backwards (to prevent going out of bounds)
|
119
|
+
for (var i = length - 1; i >= 0; i--) {
|
120
|
+
var ch = val.charAt(i);
|
121
|
+
var validChar = false;
|
122
|
+
// loop through validChars
|
123
|
+
for(var j = 0; j < validChars.length; j++) {
|
124
|
+
// if it is valid, break out the loop
|
125
|
+
if (ch == validChars[j]) {
|
126
|
+
validChar = true;
|
127
|
+
break;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
// if not a valid character, or a space, remove
|
131
|
+
if (!validChar || ch == " ") {
|
132
|
+
val = val.substring(0, i) + val.substring(i + 1);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
// set the value and prevent the cursor moving to the end
|
136
|
+
this.value = val;
|
137
|
+
$.fn.setSelection(this, carat);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
$.fn.kala.blur = function()
|
142
|
+
{
|
143
|
+
var jackBauer = $.data(this, "kala.jackBauer");
|
144
|
+
var callback = $.data(this, "kala.callback");
|
145
|
+
var val = this.value.replace(/\s*g/, '');
|
146
|
+
if (val != "") {
|
147
|
+
var re = new RegExp("^(\\d{1,2}(:?\\d{2})?)(a|p|am|pm)?$", "i");
|
148
|
+
if (!val.match(re)) {
|
149
|
+
callback.apply(this);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
$.fn.removeKala = function()
|
155
|
+
{
|
156
|
+
return this.data("kala.jackBauer", null).data("kala.callback", null).unbind("keypress", $.fn.kala.keypress).unbind("blur", $.fn.kala.blur);
|
157
|
+
}
|
158
|
+
|
159
|
+
// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <dperini@nwbox.com>)
|
160
|
+
$.fn.getSelectionStart = function(o)
|
161
|
+
{
|
162
|
+
if (o.createTextRange) {
|
163
|
+
var r = document.selection.createRange().duplicate();
|
164
|
+
r.moveEnd('character', o.value.length);
|
165
|
+
if (r.text == '') return o.value.length;
|
166
|
+
return o.value.lastIndexOf(r.text);
|
167
|
+
} else {
|
168
|
+
return o.selectionStart;
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
// set the selection, o is the object (input), p is the position ([start, end] or just start)
|
173
|
+
$.fn.setSelection = function(o, p) {
|
174
|
+
// if p is number, start and end are the same
|
175
|
+
if (typeof p == "number") p = [p, p];
|
176
|
+
// only set if p is an array of length 2
|
177
|
+
if (p && p.constructor == Array && p.length == 2) {
|
178
|
+
if (o.createTextRange) {
|
179
|
+
var r = o.createTextRange();
|
180
|
+
r.collapse(true);
|
181
|
+
r.moveStart('character', p[0]);
|
182
|
+
r.moveEnd('character', p[1]);
|
183
|
+
r.select();
|
184
|
+
} else if (o.setSelectionRange) {
|
185
|
+
o.focus();
|
186
|
+
o.setSelectionRange(p[0], p[1]);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
})(jQuery);
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kala-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Archit Baweja
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jquery-rails
|
16
|
+
requirement: &70220178719200 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70220178719200
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rails
|
27
|
+
requirement: &70220178718520 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.1'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70220178718520
|
36
|
+
description: Provides easy installation and usage of jQuery.kala javascript library
|
37
|
+
for your Rails 3.1+ application.
|
38
|
+
email:
|
39
|
+
- architbaweja@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- kala-rails.gemspec
|
48
|
+
- lib/kala-rails.rb
|
49
|
+
- lib/kala/rails.rb
|
50
|
+
- lib/kala/rails/engine.rb
|
51
|
+
- lib/kala/rails/version.rb
|
52
|
+
- vendor/assets/javascripts/jquery.kala.js
|
53
|
+
homepage: http://github.com/archit/kala-rails
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.3.6
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.17
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: jquery.kala for Rails 3.1+ Asset pipeline
|
77
|
+
test_files: []
|