nested_form_ui 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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Tomasz Bak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # NestedFormUI
2
+
3
+ ## Getting Started
4
+
5
+ Gemfile:
6
+
7
+ gem 'formtastic-bootstrap', git: 'git://github.com/tb/nested_form_ui.git'
8
+
9
+ application.js:
10
+
11
+ //= require nested_form_ui/sortable
12
+
13
+ application.scss:
14
+
15
+ @import "nested_form_ui/sortable";
16
+
17
+ ## Usage
18
+
19
+ # Copyright
20
+
21
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
14
+
15
+ require 'rspec/core'
16
+ require 'rspec/core/rake_task'
17
+
18
+ desc "Run all specs in spec directory (excluding plugin specs)"
19
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
20
+ task :default => :spec
21
+
@@ -0,0 +1,169 @@
1
+ (function($){
2
+
3
+ var defaults = {};
4
+
5
+ var NestedFormSortableTabs = function(element, options) {
6
+ var me = this;
7
+ this.container = $(element);
8
+ this.options = $.extend({}, defaults, options);
9
+ this.association = this.container.data('association') || options.association;
10
+ this.countTabs = 0;
11
+
12
+ this.init();
13
+ };
14
+
15
+ var NestedFormSortableBase = function() {};
16
+ NestedFormSortableBase.prototype = {
17
+ renderTemplate: function(template, params) {
18
+ var param;
19
+ for(param in params) {
20
+ var value = typeof(params[param]) == 'function' ? params[param]() : params[param];
21
+ template = template.split('{{'+param+'}}').join(value)
22
+ }
23
+ return template;
24
+ }
25
+ }
26
+
27
+ NestedFormSortableTabs.prototype = $.extend({}, NestedFormSortableBase.prototype, {
28
+ init: function() {
29
+ var me = this;
30
+
31
+ // tabs
32
+ this.tabs = this.container.tabs({
33
+ select: function(event, ui) {
34
+ // prevent triggering tab select on tabs actions
35
+ return !$(ui.tab).hasClass('nested-form-action');
36
+ }
37
+ });
38
+
39
+ // nestedFormEvents.insertFields
40
+ window.nestedFormEvents.insertFields = function(content, assoc, link) {
41
+ var $newPanel = $(content);
42
+ me.container.append( $newPanel );
43
+ me.addTab( $newPanel );
44
+ return $newPanel;
45
+ }
46
+
47
+ this.renderTabs();
48
+ this.sortable();
49
+ this.selectFirst();
50
+ },
51
+ renderTabs: function() {
52
+ var me = this;
53
+
54
+ // add tabs/panel in order
55
+ me.container.find(".fields").sort(function(a,b){
56
+ var av = parseInt( $(a).find('input[name$="[position]"]').val(), '10'),
57
+ bv = parseInt( $(b).find('input[name$="[position]"]').val(), '10');
58
+
59
+ return av > bv;
60
+ }).each(function(idx,panel){
61
+ me.addTab( $(panel) );
62
+ });
63
+ },
64
+ sortable: function() {
65
+ var me = this;
66
+ this.tabs.find(".ui-tabs-nav").sortable({
67
+ items: 'li.tabs-tab',
68
+ handle: '.nested-form-drag',
69
+ axis: "x",
70
+ stop: function() {
71
+ me.tabs.tabs("refresh");
72
+ me.updateSortOrder();
73
+ }
74
+ });
75
+ },
76
+ updateSortOrder: function() {
77
+ $('.nested-form-nav > li.tabs-tab').each(function(idx,tab){
78
+ var $tab = $(tab);
79
+ $tab.data('panel').find('input[name$="[position]"]').val(idx+1);
80
+ })
81
+ },
82
+ addTab: function($panel) {
83
+ var $tabsUl = this.container.find('ul.nested-form-nav'),
84
+ tabId = this.association + '-' + this.countTabs++;
85
+ tabName = $panel.find('input:first').val(),
86
+ tabTemplateData = $.extend({}, this.options.templates, { id: tabId, text: tabName });
87
+ $tab = $(this.renderTemplate(this.options.templates.tab, tabTemplateData));
88
+
89
+ $tab.appendTo($tabsUl);
90
+ $panel.prop('id', tabId );
91
+ $tab.data('panel', $panel)
92
+ $panel.data('tab', $tab)
93
+
94
+ this.syncTabNameFrom($panel);
95
+ this.addTabRemove($tab, $panel);
96
+ this.updateSortOrder();
97
+ this.tabs.tabs("refresh");
98
+ this.selectLast();
99
+ },
100
+ syncTabNameFrom: function($panel) {
101
+ $panel.find('input:first').on("input", function(){
102
+ $panel.data('tab').find('a:first').html( $(this).val() );
103
+ });
104
+ },
105
+ addTabRemove: function ($tab, $panel) {
106
+ var me = this;
107
+ $tab.find('.ui-icon-close').on("click", function() {
108
+ $(this).closest("li.tabs-tab").remove().attr("aria-controls");
109
+ me.updateSortOrder();
110
+ me.tabs.tabs("refresh");
111
+ $panel.find('input[name$="[_destroy]"]').val('true');
112
+ });
113
+ },
114
+ selectLast: function() {
115
+ this.tabs.find('li.tabs-tab:last > a').click();
116
+ },
117
+ selectFirst: function() {
118
+ this.tabs.find('li.tabs-tab:first > a').click();
119
+ }
120
+ });
121
+
122
+ $.fn.nestedFormSortableTabs = function (options) {
123
+ return this.each(function () {
124
+ if (!$.data(this, 'nestedFormSortableTabs')) {
125
+ $.data(this, 'nestedFormSortableTabs', new NestedFormSortableTabs( this, options ));
126
+ }
127
+ });
128
+ }
129
+
130
+ var NestedFormSortable = function(element, options) {
131
+ this.container = $(element);
132
+ this.options = $.extend({}, defaults, options);
133
+ this.association = this.container.data('association') || options.association;
134
+ this.init();
135
+ };
136
+
137
+ NestedFormSortable.prototype = $.extend({}, NestedFormSortableBase.prototype, {
138
+ init: function() {
139
+ var me = this;
140
+
141
+ this.container.sortable({
142
+ connectWith: ".nested-form-sortable",
143
+ handle: '.nested-form-drag',
144
+ axis: "y",
145
+ stop: function() {
146
+ me.updateSortOrder();
147
+ }
148
+ });
149
+
150
+ window.nestedFormEvents.insertFields = function(content, assoc, link) {
151
+ return me.container.append($(content));
152
+ }
153
+ },
154
+ updateSortOrder: function() {
155
+ this.container.find('.fields').each(function(idx,fields){
156
+ $(fields).find('input[name$="[position]"]').val(idx+1);
157
+ })
158
+ }
159
+ });
160
+
161
+ $.fn.nestedFormSortable = function (options) {
162
+ return this.each(function () {
163
+ if (!$.data(this, 'nestedFormSortable')) {
164
+ $.data(this, 'nestedFormSortable', new NestedFormSortable( this, options ));
165
+ }
166
+ });
167
+ }
168
+
169
+ })(jQuery);
@@ -0,0 +1,34 @@
1
+ .nested-form-sortable {
2
+
3
+ .nested-form-drag {
4
+ cursor: move;
5
+ }
6
+
7
+ .nested-form-tabs {
8
+ .nested-form-action {
9
+ float: right;
10
+ }
11
+
12
+ li {
13
+ position: relative;
14
+
15
+ a {
16
+ min-width: 20px;
17
+ min-height: 14px;
18
+ }
19
+
20
+ .nested-form-drag {
21
+ position: absolute;
22
+ top: 5px;
23
+ left: -2px;
24
+ }
25
+
26
+ .nested-form-remove {
27
+ position: absolute;
28
+ top: -3px;
29
+ right: -2px;
30
+ cursor: pointer;
31
+ }
32
+ }
33
+ }
34
+ }
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ NestedFormUi::Engine.routes.draw do
2
+ end
@@ -0,0 +1,4 @@
1
+ require "nested_form_ui/engine"
2
+
3
+ module NestedFormUi
4
+ end
@@ -0,0 +1,12 @@
1
+ module NestedFormUi
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace NestedFormUi
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, :fixture => false
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module NestedFormUi
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested_form_ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tomasz Bak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
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'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jquery-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: nested_form
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description:
63
+ email:
64
+ - t.bak@selleo.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - app/assets/javascripts/nested_form_ui/sortable.js
70
+ - app/assets/stylesheets/nested_form_ui/sortable.scss
71
+ - config/routes.rb
72
+ - lib/nested_form_ui.rb
73
+ - lib/nested_form_ui/engine.rb
74
+ - lib/nested_form_ui/version.rb
75
+ - MIT-LICENSE
76
+ - Rakefile
77
+ - README.md
78
+ homepage: https://github.com/tb/nested_form_ui
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.25
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: nested_form UI helpers (sortable list, sortable tabs)
102
+ test_files: []