cocoon_limiter 0.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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/cocoon_limiter.gemspec +22 -0
- data/lib/cocoon_limiter/engine.rb +5 -0
- data/lib/cocoon_limiter/version.rb +3 -0
- data/lib/cocoon_limiter.rb +5 -0
- data/vendor/assets/javascripts/cocoon_limiter.js +116 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kirillov Alexander
|
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,67 @@
|
|
1
|
+
# CocoonLimiter
|
2
|
+
|
3
|
+
Makes it possible to limit the amount of added by cocoon records.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cocoon_limiter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cocoon_limiter
|
18
|
+
|
19
|
+
In application.js or other file add:
|
20
|
+
|
21
|
+
//= require cocoon
|
22
|
+
//= require cocoon_limiter
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Add 'cocoon-limit' data attribute to insert link:
|
27
|
+
|
28
|
+
``` haml
|
29
|
+
# ...
|
30
|
+
.choose_file
|
31
|
+
= f.simple_fields_for :screenshots do |screenshot_field|
|
32
|
+
= render "screenshot_fields", :f => screenshot_field
|
33
|
+
# ...
|
34
|
+
%p
|
35
|
+
You can add
|
36
|
+
<b data-counter></b>
|
37
|
+
items
|
38
|
+
= link_to_add_association t(".add_trailer"), f, :trailers, :data => {:'cocoon-limit' => 5 }
|
39
|
+
```
|
40
|
+
|
41
|
+
And from application.js (or any other) call:
|
42
|
+
|
43
|
+
``` javascript
|
44
|
+
$('.choose_file').cocoon_limiter();
|
45
|
+
```
|
46
|
+
|
47
|
+
Html element with 'data-counter' attribute is a counter widget (updated
|
48
|
+
after insert or remove items).
|
49
|
+
|
50
|
+
You can decide what the tag will be updated when you change the number
|
51
|
+
of items in the list.
|
52
|
+
|
53
|
+
Just bind 'cocoon:changed' event.
|
54
|
+
|
55
|
+
``` javascript
|
56
|
+
$('.choose_file').bind('cocoon:changed', function(e, left_count){
|
57
|
+
$('#any_other_widget').text(left_count);
|
58
|
+
})
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cocoon_limiter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "cocoon_limiter"
|
8
|
+
gem.version = CocoonLimiter::VERSION
|
9
|
+
gem.authors = ["Kirillov Alexander"]
|
10
|
+
gem.email = ["saratovsource@gmail.com"]
|
11
|
+
gem.description = %q{Extends the cocoon, adding the possibility of limiting the number of records added.}
|
12
|
+
gem.summary = %q{Extends the cocoon, adding the possibility of limiting the number of records added.}
|
13
|
+
#gem.homepage = ""
|
14
|
+
gem.rubyforge_project = "cocoon_limiter"
|
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.0", "< 5.0"
|
22
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
;(function ( $, window, undefined ) {
|
2
|
+
|
3
|
+
// Create the defaults once
|
4
|
+
var pluginName = 'cocoon_limiter',
|
5
|
+
document = window.document,
|
6
|
+
defaults = {
|
7
|
+
count: 5,
|
8
|
+
cocoon_counter_name: "cocoon_counter",
|
9
|
+
counter: "[data-counter]",
|
10
|
+
increment_link: ".add_fields"
|
11
|
+
};
|
12
|
+
|
13
|
+
// The actual plugin constructor
|
14
|
+
function Plugin( element, options ) {
|
15
|
+
this.element = element;
|
16
|
+
// reference
|
17
|
+
|
18
|
+
this.element.reference = this;
|
19
|
+
this.items = new Array();
|
20
|
+
|
21
|
+
// jQuery has an extend method which merges the contents of two or
|
22
|
+
// more objects, storing the result in the first object. The first object
|
23
|
+
// is generally empty as we don't want to alter the default options for
|
24
|
+
// future instances of the plugin
|
25
|
+
this.options = $.extend( {}, defaults, options) ;
|
26
|
+
|
27
|
+
this._defaults = defaults;
|
28
|
+
this._name = pluginName;
|
29
|
+
|
30
|
+
this.assign_all_elements = function(){
|
31
|
+
this.counter_widget = $(this.element).find(this.options.counter);
|
32
|
+
this.increment_link = $(this.element).find(this.options.increment_link);
|
33
|
+
};
|
34
|
+
|
35
|
+
this.parse_data_options = function(){
|
36
|
+
this.options.count = $(this.increment_link).data('cocoon-limit') || this.options.count;
|
37
|
+
};
|
38
|
+
|
39
|
+
// If items count > limit this hide.
|
40
|
+
// Otherwise it mush be shown
|
41
|
+
this.set_visibility = function(){
|
42
|
+
if (this.items.length < this.options.count){
|
43
|
+
// Show it
|
44
|
+
this.increment_link.show();
|
45
|
+
} else {
|
46
|
+
// Hide it
|
47
|
+
this.increment_link.hide();
|
48
|
+
}
|
49
|
+
};
|
50
|
+
|
51
|
+
// Define events for increment and decrement items in array
|
52
|
+
this.cocoon_events = {
|
53
|
+
increment_event: function(e, task){
|
54
|
+
var parent = this.reference;
|
55
|
+
parent.items.push(task);
|
56
|
+
parent.set_visibility();
|
57
|
+
$( this ).trigger('cocoon:changed', [parent.items.length]);
|
58
|
+
},
|
59
|
+
decrement_event: function(e, task){
|
60
|
+
var parent = this.reference;
|
61
|
+
parent.items.splice($.inArray(task, parent.items), 1);
|
62
|
+
$( this ).trigger('cocoon:changed', [parent.items.length]);
|
63
|
+
parent.set_visibility();
|
64
|
+
}
|
65
|
+
};
|
66
|
+
|
67
|
+
this.update_counter = function(e, count){
|
68
|
+
var left_count = (this.reference.options.count - count)
|
69
|
+
$( this.reference.counter_widget ).text(left_count);
|
70
|
+
};
|
71
|
+
|
72
|
+
this.bind_events = function(){
|
73
|
+
$( this.element ).bind('cocoon:after-insert', this.cocoon_events.increment_event)
|
74
|
+
$( this.element ).bind('cocoon:after-remove', this.cocoon_events.decrement_event)
|
75
|
+
$( this.element ).bind('cocoon:changed', this.update_counter)
|
76
|
+
}
|
77
|
+
|
78
|
+
this.calc_existing_elements = function(){
|
79
|
+
var plugin = this;
|
80
|
+
$( this.element ).find('.remove_fields').each(function(){
|
81
|
+
$(plugin.element).trigger('cocoon:after-insert');
|
82
|
+
});
|
83
|
+
}
|
84
|
+
|
85
|
+
this.init();
|
86
|
+
}
|
87
|
+
|
88
|
+
Plugin.prototype.init = function () {
|
89
|
+
|
90
|
+
// Initial counter value
|
91
|
+
this.items_count = 0;
|
92
|
+
|
93
|
+
// Find counter widget
|
94
|
+
this.assign_all_elements();
|
95
|
+
|
96
|
+
this.parse_data_options();
|
97
|
+
|
98
|
+
this.bind_events();
|
99
|
+
|
100
|
+
this.calc_existing_elements();
|
101
|
+
|
102
|
+
// First
|
103
|
+
$( this.element ).trigger('cocoon:changed', [this.items.length]);
|
104
|
+
};
|
105
|
+
|
106
|
+
// A really lightweight plugin wrapper around the constructor,
|
107
|
+
// preventing against multiple instantiations
|
108
|
+
$.fn[pluginName] = function ( options ) {
|
109
|
+
return this.each(function () {
|
110
|
+
if (!$.data(this, 'plugin_' + pluginName)) {
|
111
|
+
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
|
112
|
+
}
|
113
|
+
});
|
114
|
+
}
|
115
|
+
|
116
|
+
}(jQuery, window));
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cocoon_limiter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kirillov Alexander
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-03 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.0'
|
22
|
+
- - <
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '5.0'
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
- - <
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '5.0'
|
36
|
+
description: Extends the cocoon, adding the possibility of limiting the number of
|
37
|
+
records added.
|
38
|
+
email:
|
39
|
+
- saratovsource@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE.txt
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- cocoon_limiter.gemspec
|
50
|
+
- lib/cocoon_limiter.rb
|
51
|
+
- lib/cocoon_limiter/engine.rb
|
52
|
+
- lib/cocoon_limiter/version.rb
|
53
|
+
- vendor/assets/javascripts/cocoon_limiter.js
|
54
|
+
homepage:
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
hash: 546965903
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: 546965903
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: cocoon_limiter
|
80
|
+
rubygems_version: 1.8.24
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Extends the cocoon, adding the possibility of limiting the number of records
|
84
|
+
added.
|
85
|
+
test_files: []
|