countable-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +33 -0
- data/LICENSE.txt +22 -0
- data/README.md +130 -0
- data/Rakefile +1 -0
- data/countable-rails.gemspec +23 -0
- data/lib/countable/rails/version.rb +6 -0
- data/lib/countable/rails.rb +8 -0
- data/vendor/assets/javascripts/countable.js +377 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 466203ca5ba87067c5ba7d0657f90f955f6a2408
|
4
|
+
data.tar.gz: 5bfa6acafd0afd2656d5723a471ef35b01b8e582
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fccc6811184777f0c22a437a62b96d0ef9f5ffef3130d51cb7581816d67950c2121be5d3c1c8ab9eb261e40f14835134e36808940dc2a46ab364f0b143324faf
|
7
|
+
data.tar.gz: a86acde04a373572d1e7830b5b5499304a4102f27a8627ef045e2512a4a830b27b9b31b68fa6d76a20b12bed53ee91cc9c394a5d23e865d389066cf44591a6d1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.8'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
|
10
|
+
|
11
|
+
# Gems used only for assets and not required
|
12
|
+
# in production environments by default.
|
13
|
+
group :assets do
|
14
|
+
gem 'sass-rails', '~> 3.2.3'
|
15
|
+
gem 'coffee-rails', '~> 3.2.1'
|
16
|
+
|
17
|
+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
18
|
+
# gem 'therubyracer', :platforms => :ruby
|
19
|
+
|
20
|
+
gem 'uglifier', '>= 1.0.3'
|
21
|
+
|
22
|
+
gem 'jquery-ui-rails'
|
23
|
+
end
|
24
|
+
|
25
|
+
gem 'jquery-rails'
|
26
|
+
|
27
|
+
# Specify your gem's dependencies in jquery-modal-rails.gemspec
|
28
|
+
gemspec
|
29
|
+
|
30
|
+
# add rspec
|
31
|
+
gem 'rspec'
|
32
|
+
gem 'rspec-rails'
|
33
|
+
gem 'shoulda', :require => false
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kyle Szives
|
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,130 @@
|
|
1
|
+
# Countable
|
2
|
+
|
3
|
+
Countable Rails is a Rails wrapper for the javascript plugin Coutnable by [Sacha Schmid](http://sachaschmid.ch) ([**@sachaschmid**](https://twitter.com/sachaschmid))
|
4
|
+
|
5
|
+
Countable is a JavaScript function to add **live paragraph-, word- and character-counting** to an HTML element. Countable is a *zero-dependency* library and comes in at **1KB** when minified and gzipped.
|
6
|
+
|
7
|
+
[**View the Demo**](http://radlikewhoa.github.io/Countable#demo)
|
8
|
+
|
9
|
+
[**View the Original**](https://github.com/RadLikeWhoa/Countable)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install 'countable-rails'
|
15
|
+
```
|
16
|
+
|
17
|
+
or
|
18
|
+
|
19
|
+
```
|
20
|
+
gem 'countable-rails'
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Countable is available as a Node / CommonJS module, an AMD module and as a global. All methods are accessed on the Countable object directly.
|
26
|
+
|
27
|
+
### Callbacks
|
28
|
+
|
29
|
+
The `live` and `once` method both accept a callback. The given callback is then called whenever needed with a single parameter that carries all the releavant data. `this` is bound to the current element. Take the following code for an example.
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
var area = document.getElementById('text')
|
33
|
+
|
34
|
+
Countable.once(area, function (counter) {
|
35
|
+
console.log(this, counter)
|
36
|
+
})
|
37
|
+
```
|
38
|
+
|
39
|
+
```
|
40
|
+
=> <textarea id="text"></textarea>, { all: 0, characters: 0, paragraphs: 0, words: 0 }
|
41
|
+
```
|
42
|
+
|
43
|
+
Property | Meaning
|
44
|
+
---------- | --------------------------------------------------------------------------------------------
|
45
|
+
paragraphs | The number of paragraphs. Paragraphs can be separated by either a soft or a hard (two line breaks) return. To use hard returns, set the corresponding option (`hardReturns`).
|
46
|
+
words | The number of words. Words are split using spaces.
|
47
|
+
characters | The number of characters (without spaces). This contains all non-whitespace characters.
|
48
|
+
all | The number of characters including whitespace. This is the total number of all characters in the element.
|
49
|
+
|
50
|
+
### Countable#live(elements, callback, options)
|
51
|
+
|
52
|
+
Bind the callback to all given elements. The callback gets called everytime the element's value or text is changed.
|
53
|
+
|
54
|
+
```javascript
|
55
|
+
Countable.live(area, function (counter) {
|
56
|
+
console.log(counter)
|
57
|
+
})
|
58
|
+
```
|
59
|
+
|
60
|
+
### Countable#die(elements)
|
61
|
+
|
62
|
+
Remove the bound callback from all given elements.
|
63
|
+
|
64
|
+
```javascript
|
65
|
+
Countable.die(area)
|
66
|
+
```
|
67
|
+
|
68
|
+
### Countable#once(elements, callback, options)
|
69
|
+
|
70
|
+
Similar to `Countable.live()`, but the callback is only executed once, there are no events bound.
|
71
|
+
|
72
|
+
```javascript
|
73
|
+
Countable.once(area, function (counter) {
|
74
|
+
console.log(counter)
|
75
|
+
})
|
76
|
+
```
|
77
|
+
|
78
|
+
### Countable#enabled(element)
|
79
|
+
|
80
|
+
Checks the live-counting functionality is bound to the given.
|
81
|
+
|
82
|
+
```javascript
|
83
|
+
Countable.enabled(area)
|
84
|
+
```
|
85
|
+
|
86
|
+
### Options
|
87
|
+
|
88
|
+
`Countable.live()` and `Countable.once()` both accept a third argument, an options object that allows you to change how Countable treats certain aspects of your element's text.
|
89
|
+
|
90
|
+
```javascript
|
91
|
+
{
|
92
|
+
hardReturns: false,
|
93
|
+
stripTags: false
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
97
|
+
By default, paragraphs are split by a single return (a soft return). By setting `hardReturns` to true, Countable splits paragraphs after two returns.
|
98
|
+
|
99
|
+
Depending on your application and audience, you might need to strip HTML tags from the text before counting it. You can do this by setting `stripTags` to true.
|
100
|
+
|
101
|
+
## Browser Support
|
102
|
+
|
103
|
+
Countable supports all modern browsers. Internet Explorer is supported down to version 7. Note that some browsers don't implement the `oninput` event consistently so there might be differences in the way Countable works in different browsers.
|
104
|
+
|
105
|
+
## Upgrading from version 1.x.x
|
106
|
+
|
107
|
+
Upgrading from version 1.x.x is easy. Most likely, you've used something like the following:
|
108
|
+
|
109
|
+
```javascript
|
110
|
+
var area = document.getElementById('area')
|
111
|
+
|
112
|
+
new Countable(area, function (counter) {
|
113
|
+
console.log(counter)
|
114
|
+
}, { stripTags: true })
|
115
|
+
```
|
116
|
+
|
117
|
+
The new syntax offers more functions as described above, but to keep the live-counting functionality, you just write this:
|
118
|
+
|
119
|
+
```javascript
|
120
|
+
var area = document.getElementById('area')
|
121
|
+
|
122
|
+
Countable.live(area, function (counter) {
|
123
|
+
console.log(counter)
|
124
|
+
}, { stripTags: true })
|
125
|
+
```
|
126
|
+
|
127
|
+
* The callback parameter is no longer optional
|
128
|
+
* `options.once` has been replaced with `Countable.once()`
|
129
|
+
* `Countable.live()` and `Countable.once()` both accept one or more elements, rather than just a single one
|
130
|
+
* Inside the callback, `this` is now bound to the current element
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'countable/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "countable-rails"
|
8
|
+
spec.version = Countable::Rails::VERSION
|
9
|
+
spec.authors = ["Kyle Szives"]
|
10
|
+
spec.email = ["kjszives@gmail.com"]
|
11
|
+
spec.summary = "Countable is a script to allow for live paragraph-, word- and character- counting on an HTML element."
|
12
|
+
spec.description = "Countable is a script to allow for live paragraph-, word- and character- counting on an HTML element based on http://radlikewhoa.github.io/Countable."
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "railties", ">= 3.1"
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,377 @@
|
|
1
|
+
/**
|
2
|
+
* Countable is a script to allow for live paragraph-, word- and character-
|
3
|
+
* counting on an HTML element.
|
4
|
+
*
|
5
|
+
* @author Sacha Schmid (<https://github.com/RadLikeWhoa>)
|
6
|
+
* @version 2.0.1
|
7
|
+
* @license MIT
|
8
|
+
* @see <http://radlikewhoa.github.io/Countable/>
|
9
|
+
*/
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Note: For the purpose of this internal documentation, arguments of the type
|
13
|
+
* {Nodes} are to be interpreted as either {NodeList} or {Element}.
|
14
|
+
*/
|
15
|
+
|
16
|
+
;(function (global) {
|
17
|
+
'use strict'
|
18
|
+
|
19
|
+
/**
|
20
|
+
* @private
|
21
|
+
*
|
22
|
+
* `_liveElements` holds all elements that have the live-counting
|
23
|
+
* functionality bound to them.
|
24
|
+
*
|
25
|
+
* `_event` holds the event to handle the live counting, based on the
|
26
|
+
* browser's capabilities.
|
27
|
+
*/
|
28
|
+
|
29
|
+
var _liveElements = [],
|
30
|
+
_event = 'oninput' in document ? 'input' : 'keyup'
|
31
|
+
|
32
|
+
/**
|
33
|
+
* `String.trim()` polyfill for non-supporting browsers. This is the
|
34
|
+
* recommended polyfill on MDN.
|
35
|
+
*
|
36
|
+
* @see <http://goo.gl/uYveB>
|
37
|
+
* @see <http://goo.gl/xjIxJ>
|
38
|
+
*
|
39
|
+
* @return {String} The original string with leading and trailing whitespace
|
40
|
+
* removed.
|
41
|
+
*/
|
42
|
+
|
43
|
+
if (!String.prototype.trim) {
|
44
|
+
String.prototype.trim = function () {
|
45
|
+
return this.replace(/^\s+|\s+$/g, '')
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
* `ucs2decode` function from the punycode.js library.
|
51
|
+
*
|
52
|
+
* Creates an array containing the decimal code points of each Unicode
|
53
|
+
* character in the string. While JavaScript uses UCS-2 internally, this
|
54
|
+
* function will convert a pair of surrogate halves (each of which UCS-2
|
55
|
+
* exposes as separate characters) into a single code point, matching
|
56
|
+
* UTF-16.
|
57
|
+
*
|
58
|
+
* @see <http://goo.gl/8M09r>
|
59
|
+
* @see <http://goo.gl/u4UUC>
|
60
|
+
*
|
61
|
+
* @param {String} string The Unicode input string (UCS-2).
|
62
|
+
*
|
63
|
+
* @return {Array} The new array of code points.
|
64
|
+
*/
|
65
|
+
|
66
|
+
function _decode (string) {
|
67
|
+
var output = [],
|
68
|
+
counter = 0,
|
69
|
+
length = string.length,
|
70
|
+
value, extra
|
71
|
+
|
72
|
+
while (counter < length) {
|
73
|
+
value = string.charCodeAt(counter++)
|
74
|
+
|
75
|
+
if ((value & 0xF800) == 0xD800 && counter < length) {
|
76
|
+
|
77
|
+
// High surrogate, and there is a next character.
|
78
|
+
|
79
|
+
extra = string.charCodeAt(counter++)
|
80
|
+
|
81
|
+
if ((extra & 0xFC00) == 0xDC00) {
|
82
|
+
|
83
|
+
// Low surrogate.
|
84
|
+
|
85
|
+
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000)
|
86
|
+
} else {
|
87
|
+
output.push(value, extra)
|
88
|
+
}
|
89
|
+
} else {
|
90
|
+
output.push(value)
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
return output
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* `_validateArguments` validates the arguments given to each function call.
|
99
|
+
* Errors are logged to the console as warnings, but Countable fails silently.
|
100
|
+
*
|
101
|
+
* @private
|
102
|
+
*
|
103
|
+
* @param {Nodes} elements The (collection of) element(s) to
|
104
|
+
* validate.
|
105
|
+
*
|
106
|
+
* @param {Function} callback The callback function to validate.
|
107
|
+
*
|
108
|
+
* @return {Boolean} Returns whether all arguments are vaild.
|
109
|
+
*/
|
110
|
+
|
111
|
+
function _validateArguments (elements, callback) {
|
112
|
+
var elementsValid = elements && ((Object.prototype.toString.call(elements) === '[object NodeList]' && elements.length) || (elements.nodeType === 1)),
|
113
|
+
callbackValid = callback && typeof callback === 'function'
|
114
|
+
|
115
|
+
if ('console' in window && 'warn' in console) {
|
116
|
+
if (!elementsValid) console.warn('Countable: No valid elements were found')
|
117
|
+
if (!callbackValid) console.warn('Countable: "' + callback + '" is not a valid callback function')
|
118
|
+
}
|
119
|
+
|
120
|
+
return elementsValid && callbackValid
|
121
|
+
}
|
122
|
+
|
123
|
+
/**
|
124
|
+
* `_extendDefaults` is a function to extend a set of default options with the
|
125
|
+
* ones given in the function call. Available options are described below.
|
126
|
+
*
|
127
|
+
* {Boolean} hardReturns Use two returns to seperate a paragraph instead of
|
128
|
+
* one.
|
129
|
+
* {Boolean} stripTags Strip HTML tags before counting the values.
|
130
|
+
*
|
131
|
+
* @private
|
132
|
+
*
|
133
|
+
* @param {Object} options Countable allows the options described above.
|
134
|
+
* They can be used in a function call to override
|
135
|
+
* the default behaviour.
|
136
|
+
*
|
137
|
+
* @return {Object} The new options object.
|
138
|
+
*/
|
139
|
+
|
140
|
+
function _extendDefaults (options) {
|
141
|
+
var defaults = { hardReturns: false, stripTags: false }
|
142
|
+
|
143
|
+
for (var prop in options) {
|
144
|
+
if (defaults.hasOwnProperty(prop)) defaults[prop] = options[prop]
|
145
|
+
}
|
146
|
+
|
147
|
+
return defaults
|
148
|
+
}
|
149
|
+
|
150
|
+
/**
|
151
|
+
* `_count` trims an element's value, optionally strips HTML tags and counts
|
152
|
+
* paragraphs, words, characters and characters plus spaces.
|
153
|
+
*
|
154
|
+
* @private
|
155
|
+
*
|
156
|
+
* @param {Element} element The element whose value is to be counted.
|
157
|
+
*
|
158
|
+
* @param {Object} options The options to use for the counting.
|
159
|
+
*
|
160
|
+
* @return {Object} The object containing the number of paragraphs,
|
161
|
+
* words, characters and characters plus spaces.
|
162
|
+
*/
|
163
|
+
|
164
|
+
function _count (element, options) {
|
165
|
+
var original = 'value' in element ? element.value : element.innerText || element.textContent,
|
166
|
+
trimmed
|
167
|
+
|
168
|
+
/**
|
169
|
+
* The initial implementation to allow for HTML tags stripping was created
|
170
|
+
* @craniumslows while the current one was created by @Rob--W.
|
171
|
+
*
|
172
|
+
* @see <http://goo.gl/Exmlr>
|
173
|
+
* @see <http://goo.gl/gFQQh>
|
174
|
+
*/
|
175
|
+
|
176
|
+
if (options.stripTags) original = original.replace(/<\/?[a-z][^>]*>/gi, '')
|
177
|
+
|
178
|
+
trimmed = original.trim()
|
179
|
+
|
180
|
+
/**
|
181
|
+
* Most of the performance improvements are based on the works of @epmatsw.
|
182
|
+
*
|
183
|
+
* @see <http://goo.gl/SWOLB>
|
184
|
+
*/
|
185
|
+
|
186
|
+
return {
|
187
|
+
paragraphs: trimmed ? (trimmed.match(options.hardReturns ? /\n{2,}/g : /\n+/g) || []).length + 1 : 0,
|
188
|
+
words: trimmed ? (trimmed.replace(/['";:,.?¿\-!¡]+/g, '').match(/\S+/g) || []).length : 0,
|
189
|
+
characters: trimmed ? _decode(trimmed.replace(/\s/g, '')).length : 0,
|
190
|
+
all: _decode(original.replace(/[\n\r]/g, '')).length
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
/**
|
195
|
+
* `_loop` is a helper function to iterate over a collection, e.g. a NodeList
|
196
|
+
* or an Array. The callback receives the current element as the single
|
197
|
+
* parameter.
|
198
|
+
*
|
199
|
+
* @private
|
200
|
+
*
|
201
|
+
* @param {Array} which The collection to iterate over.
|
202
|
+
*
|
203
|
+
* @param {Function} callback The callback function to call on each
|
204
|
+
* iteration.
|
205
|
+
*/
|
206
|
+
|
207
|
+
function _loop (which, callback) {
|
208
|
+
var len = which.length
|
209
|
+
|
210
|
+
if (typeof len !== 'undefined') {
|
211
|
+
while (len--) {
|
212
|
+
callback(which[len])
|
213
|
+
}
|
214
|
+
} else {
|
215
|
+
callback(which)
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
/**
|
220
|
+
* This is the main object that will later be exposed to other scripts. It
|
221
|
+
* holds all the public methods that can be used to enable the Countable
|
222
|
+
* functionality.
|
223
|
+
*/
|
224
|
+
|
225
|
+
var Countable = {
|
226
|
+
|
227
|
+
/**
|
228
|
+
* The `live` method binds the counting handler to all given elements. The
|
229
|
+
* event is either `oninput` or `onkeydown`, based on the capabilities of
|
230
|
+
* the browser.
|
231
|
+
*
|
232
|
+
* @param {Nodes} elements All elements that should receive the
|
233
|
+
* Countable functionality.
|
234
|
+
*
|
235
|
+
* @param {Function} callback The callback to fire whenever the
|
236
|
+
* element's value changes. The callback is
|
237
|
+
* called with the relevant element bound to
|
238
|
+
* `this` and the counted values as the
|
239
|
+
* single parameter.
|
240
|
+
*
|
241
|
+
* @param {Object} [options] An object to modify Countable's
|
242
|
+
* behaviour. Refer to `_extendDefaults` for
|
243
|
+
* a list of available options.
|
244
|
+
*
|
245
|
+
* @return {Object} Returns the Countable object to allow for chaining.
|
246
|
+
*/
|
247
|
+
|
248
|
+
live: function (elements, callback, options) {
|
249
|
+
var ops = _extendDefaults(options),
|
250
|
+
bind = function (element) {
|
251
|
+
var handler = function () {
|
252
|
+
callback.call(element, _count(element, ops))
|
253
|
+
}
|
254
|
+
|
255
|
+
_liveElements.push({ element: element, handler: handler })
|
256
|
+
|
257
|
+
handler()
|
258
|
+
|
259
|
+
if (element.addEventListener) {
|
260
|
+
element.addEventListener(_event, handler, false)
|
261
|
+
} else if (element.attachEvent) {
|
262
|
+
element.attachEvent('on' + _event, handler)
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
if (!_validateArguments(elements, callback)) return
|
267
|
+
|
268
|
+
if (elements.length) {
|
269
|
+
_loop(elements, bind)
|
270
|
+
} else {
|
271
|
+
bind(elements)
|
272
|
+
}
|
273
|
+
|
274
|
+
return this
|
275
|
+
},
|
276
|
+
|
277
|
+
/**
|
278
|
+
* The `die` method removes the Countable functionality from all given
|
279
|
+
* elements.
|
280
|
+
*
|
281
|
+
* @param {Nodes} elements All elements whose Countable functionality
|
282
|
+
* should be unbound.
|
283
|
+
*
|
284
|
+
* @return {Object} Returns the Countable object to allow for chaining.
|
285
|
+
*/
|
286
|
+
|
287
|
+
die: function (elements) {
|
288
|
+
if (!_validateArguments(elements, function () {})) return
|
289
|
+
|
290
|
+
_loop(elements, function (element) {
|
291
|
+
var liveElement
|
292
|
+
|
293
|
+
_loop(_liveElements, function (live) {
|
294
|
+
if (live.element === element) liveElement = live
|
295
|
+
})
|
296
|
+
|
297
|
+
if (!liveElement) return
|
298
|
+
|
299
|
+
if (element.removeEventListener) {
|
300
|
+
element.removeEventListener(_event, liveElement.handler, false)
|
301
|
+
} else if (element.detachEvent) {
|
302
|
+
element.detachEvent('on' + _event, liveElement.handler)
|
303
|
+
}
|
304
|
+
|
305
|
+
_liveElements.splice(_liveElements.indexOf(liveElement), 1)
|
306
|
+
})
|
307
|
+
|
308
|
+
return this
|
309
|
+
},
|
310
|
+
|
311
|
+
/**
|
312
|
+
* The `once` method works mostly like the `live` method, but no events are
|
313
|
+
* bound, the functionality is only executed once.
|
314
|
+
*
|
315
|
+
* @param {Nodes} elements All elements that should receive the
|
316
|
+
* Countable functionality.
|
317
|
+
*
|
318
|
+
* @param {Function} callback The callback to fire whenever the
|
319
|
+
* element's value changes. The callback is
|
320
|
+
* called with the relevant element bound to
|
321
|
+
* `this` and the counted values as the
|
322
|
+
* single parameter.
|
323
|
+
*
|
324
|
+
* @param {Object} [options] An object to modify Countable's
|
325
|
+
* behaviour. Refer to `_extendDefaults`
|
326
|
+
* for a list of available options.
|
327
|
+
*
|
328
|
+
* @return {Object} Returns the Countable object to allow for chaining.
|
329
|
+
*/
|
330
|
+
|
331
|
+
once: function (elements, callback, options) {
|
332
|
+
if (!_validateArguments(elements, callback)) return
|
333
|
+
|
334
|
+
_loop(elements, function (element) {
|
335
|
+
callback.call(element, _count(element, _extendDefaults(options)))
|
336
|
+
})
|
337
|
+
|
338
|
+
return this
|
339
|
+
},
|
340
|
+
|
341
|
+
/**
|
342
|
+
* The `enabled` method checks if the live-counting functionality is bound
|
343
|
+
* to an element.
|
344
|
+
*
|
345
|
+
* @param {Element} element A single Element.
|
346
|
+
*
|
347
|
+
* @return {Boolean} A boolean value representing whether Countable
|
348
|
+
* functionality is bound to the given element.
|
349
|
+
*/
|
350
|
+
|
351
|
+
enabled: function (element) {
|
352
|
+
var isEnabled = false
|
353
|
+
|
354
|
+
if (element && element.nodeType === 1) {
|
355
|
+
_loop(_liveElements, function (live) {
|
356
|
+
if (live.element === element) isEnabled = true
|
357
|
+
})
|
358
|
+
}
|
359
|
+
|
360
|
+
return isEnabled
|
361
|
+
}
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
/**
|
366
|
+
* Expose Countable depending on the module system used across the
|
367
|
+
* application. (Node / CommonJS, AMD, global)
|
368
|
+
*/
|
369
|
+
|
370
|
+
if (typeof exports === 'object') {
|
371
|
+
module.exports = Countable
|
372
|
+
} else if (typeof define === 'function' && define.amd) {
|
373
|
+
define(function () { return Countable })
|
374
|
+
} else {
|
375
|
+
global.Countable = Countable
|
376
|
+
}
|
377
|
+
}(this))
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: countable-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Szives
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Countable is a script to allow for live paragraph-, word- and character-
|
56
|
+
counting on an HTML element based on http://radlikewhoa.github.io/Countable.
|
57
|
+
email:
|
58
|
+
- kjszives@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- countable-rails.gemspec
|
69
|
+
- lib/countable/rails.rb
|
70
|
+
- lib/countable/rails/version.rb
|
71
|
+
- vendor/assets/javascripts/countable.js
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.7
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Countable is a script to allow for live paragraph-, word- and character-
|
96
|
+
counting on an HTML element.
|
97
|
+
test_files: []
|