smart_tag 0.3.1 → 0.4.0

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.
@@ -1,3 +1,3 @@
1
1
  module SmartTag
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,9 +1,11 @@
1
1
  (function($){
2
2
  $(document).ready(function() {
3
3
 
4
- function SmartTag(select) {
4
+ function Base(select) {
5
+ var that = {};
6
+ that.select = select;
5
7
 
6
- function inArray(array, value) {
8
+ that.inArray = function(array, value) {
7
9
  for (var i = array.length - 1; i >= 0; i--) {
8
10
  if (array[i] == value) {
9
11
  return true;
@@ -11,78 +13,104 @@
11
13
  }
12
14
 
13
15
  return false;
14
- }
16
+ };
15
17
 
16
- function clearText(text) {
18
+ that.clearText = function(text) {
17
19
  // \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
18
20
  return text.replace(/^\s+|\s+$/g, ""); // 去掉开头或结尾的任何空白字符
19
- }
21
+ };
20
22
 
21
- function getOptions(selector) {
23
+ that.getOptions = function(selector) {
22
24
  var options = [];
23
25
  for (var i = selector.length - 1; i >= 0; i--) {
24
- var text = clearText($(selector[i]).text());
26
+ var text = that.clearText($(selector[i]).text());
25
27
 
26
- if (!inArray(options, text)) {
28
+ if (!that.inArray(options, text)) {
27
29
  options.push(text);
28
30
  }
29
31
  }
30
32
 
31
33
  return options;
32
- }
34
+ };
33
35
 
34
- function createTags() {
35
- for (var i = selected_options.length - 1; i >= 0; i--) {
36
- ul.tagit("createTag", selected_options[i]);
36
+ that.createTags = function() {
37
+ for (var i = that.selected_options.length - 1; i >= 0; i--) {
38
+ that.ul.tagit("createTag", that.selected_options[i]);
37
39
  }
38
- }
40
+ };
39
41
 
40
- function changeTags() {
41
- var new_tags = getOptions(select.children('option:selected'));
42
- var tags = ul.tagit("assignedTags")
42
+ that.changeTags = function() {
43
+ var new_tags = that.getOptions(that.select.children('option:selected'));
44
+ var tags = that.ul.tagit("assignedTags")
43
45
 
44
46
  // remove
45
47
  for (var i = tags.length - 1; i >= 0; i--) {
46
- if (!inArray(new_tags, tags[i])) {
47
- ul.tagit("removeTagByLabel", tags[i]);
48
+ if (!that.inArray(new_tags, tags[i])) {
49
+ that.ul.tagit("removeTagByLabel", tags[i]);
48
50
  }
49
51
  }
50
52
 
51
53
  // create
52
54
  for (var i = new_tags.length - 1; i >= 0; i--) {
53
- if (!inArray(tags, new_tags[i])) {
54
- ul.tagit("createTag", new_tags[i]);
55
+ if (!that.inArray(tags, new_tags[i])) {
56
+ that.ul.tagit("createTag", new_tags[i]);
55
57
  }
56
58
  }
57
- }
59
+ };
58
60
 
59
- function addSelected(tags, option) {
60
- if (inArray(tags, clearText(option.text()))) {
61
+ that.addSelected = function(tags, option) {
62
+ if (that.inArray(tags, that.clearText(option.text()))) {
61
63
  option.attr("selected", true);
62
64
  }
63
- }
65
+ };
64
66
 
65
- function removeSelected(tags, option) {
66
- if (!inArray(tags, clearText(option.text()))) {
67
+ that.removeSelected = function(tags, option) {
68
+ if (!that.inArray(tags, that.clearText(option.text()))) {
67
69
  option.attr("selected", false);
68
70
  }
69
- }
71
+ };
70
72
 
71
- function changeSelected(tags, method) {
72
- var options = select.children('option');
73
+ that.changeSelected = function(tags, method) {
74
+ var options = that.select.children('option');
73
75
  for (var i = options.length - 1; i >= 0; i--) {
74
76
  method(tags, $(options[i]));
75
77
  }
76
- }
78
+ };
79
+
80
+ that.all_options = that.getOptions(select.children('option'));
81
+ that.selected_options = that.getOptions(select.children('option:selected'));
82
+ that.ul = $('<ul></ul>');
83
+
84
+ select.before(that.ul);
85
+
86
+ that.ul.css({
87
+ 'margin-left': '0px',
88
+ 'width': '488px'
89
+ });
90
+
91
+ that.select.change(function(){
92
+ that.changeTags();
93
+ });
94
+
95
+ that.start = function() {
96
+ that.createTags();
97
+ };
77
98
 
78
- function ajaxAddTag(add_tag) {
79
- var controller = select.attr('controller');
99
+ return that;
100
+ }
101
+
102
+ function SmartTag(select) {
103
+
104
+ var that = Base(select);
105
+
106
+ that.ajaxAddTag = function(add_tag) {
107
+ var controller = that.select.attr('controller');
80
108
 
81
109
  if (controller) {
82
110
 
83
- var action = select.attr('action') ? select.attr('action') : 'create';
84
- var param = select.attr('param') ? select.attr('param') : 'title';
85
- var controllers = select.attr('controllers') ? select.attr('controllers') : controller+'s';
111
+ var action = that.select.attr('action') ? that.select.attr('action') : 'create';
112
+ var param = that.select.attr('param') ? that.select.attr('param') : 'title';
113
+ var controllers = that.select.attr('controllers') ? that.select.attr('controllers') : controller+'s';
86
114
 
87
115
  var url = '/'+controllers+'/'+action;
88
116
  var data = {};
@@ -104,48 +132,83 @@
104
132
  } else {
105
133
  console.log("select's controller param is null");
106
134
  }
107
- }
135
+ };
108
136
 
109
- var all_options = getOptions(select.children('option'));
110
- var selected_options = getOptions(select.children('option:selected'));
111
- var ul = $('<ul class="' + select.attr('id') + '_smart_tag"></ul>');
112
-
113
- select.before(ul);
114
-
115
- ul.css({
116
- 'margin-left': '0px',
117
- 'width': '488px'
118
- });
119
-
120
- ul.tagit({
137
+ that.ul.tagit({
121
138
  allowSpaces: true,
122
- availableTags: all_options,
139
+ availableTags: that.all_options,
123
140
  afterTagAdded: function(event, ui) {
124
141
  var add_tag = ui.tagLabel;
125
- all_options = getOptions(select.children('option'));
142
+ all_options = that.getOptions(that.select.children('option'));
126
143
 
127
- if (inArray(all_options, add_tag)) {
128
- changeSelected(ul.tagit("assignedTags"), addSelected);
144
+ if (that.inArray(all_options, add_tag)) {
145
+ that.changeSelected(that.ul.tagit("assignedTags"), that.addSelected);
129
146
  } else {
130
- ajaxAddTag(add_tag);
147
+ that.ajaxAddTag(add_tag);
131
148
  }
132
149
  },
133
150
  afterTagRemoved: function(event, ui) {
134
- changeSelected(ul.tagit("assignedTags"), removeSelected);
151
+ that.changeSelected(that.ul.tagit("assignedTags"), that.removeSelected);
135
152
  }
136
153
  });
137
154
 
138
- createTags();
155
+ return that;
156
+ }
157
+
158
+ var smart_tags = $(".smart_tag");
159
+
160
+ for (var i = smart_tags.length - 1; i >= 0; i--) {
161
+ var item = SmartTag($(smart_tags[i]));
162
+ item.start();
163
+ }
164
+
165
+ function SingleChoice(select) {
166
+ var that = Base(select);
139
167
 
140
- select.change(function(){
141
- changeTags();
168
+ that.ul.tagit({
169
+ allowSpaces: true,
170
+ availableTags: that.all_options,
171
+ tagLimit: 1,
172
+ afterTagAdded: function(event, ui) {
173
+ var add_tag = ui.tagLabel;
174
+ all_options = that.getOptions(that.select.children('option'));
175
+
176
+ if (that.inArray(all_options, add_tag)) {
177
+ that.changeSelected(that.ul.tagit("assignedTags"), that.addSelected);
178
+ that.ul.parents(".control-group").removeClass("error");
179
+ that.ul.parents(".control-group").addClass("success");
180
+ that.ul.parents(".control-group").find('.help-inline').text("Success!");
181
+ }else {
182
+ that.ul.tagit("removeAll");
183
+ that.ul.parents(".control-group").removeClass("success");
184
+ that.ul.parents(".control-group").addClass("error");
185
+ that.ul.parents(".control-group").find('.help-inline').text("You should choice one career.");
186
+ }
187
+
188
+
189
+ if(that.ul.tagit("assignedTags").length == 1){
190
+ that.ul.find(".ui-autocomplete-input").attr('disabled', 'disabled');
191
+ that.select.focus();
192
+ }
193
+ },
194
+ afterTagRemoved: function(event, ui) {
195
+
196
+ that.changeSelected(that.ul.tagit("assignedTags"), that.removeSelected);
197
+
198
+ if (that.ul.tagit("assignedTags").length < 1) {
199
+ that.ul.find(".ui-autocomplete-input").removeAttr('disabled');
200
+ }
201
+ }
142
202
  });
203
+
204
+ return that;
143
205
  }
144
206
 
145
- var selects = $(".smart_tag");
207
+ var single_choices = $(".single_choice");
146
208
 
147
- for (var i = selects.length - 1; i >= 0; i--) {
148
- SmartTag($(selects[i]));
209
+ for (var i = single_choices.length - 1; i >= 0; i--) {
210
+ var item = SingleChoice($(single_choices[i]));
211
+ item.start();
149
212
  }
150
213
 
151
214
  });
@@ -1,4 +1,7 @@
1
- (function(g){g(document).ready(function(){function h(b){function j(d,c){for(var a=d.length-1;0<=a;a--)if(d[a]==c)return!0;return!1}function f(d){return d.replace(/^\s+|\s+$/g,"")}function m(d){for(var c=[],a=d.length-1;0<=a;a--){var b=f(g(d[a]).text());j(c,b)||c.push(b)}return c}function n(b,c){j(b,f(c.text()))&&c.attr("selected",!0)}function h(b,c){j(b,f(c.text()))||c.attr("selected",!1)}function q(d,c){for(var a=b.children("option"),e=a.length-1;0<=e;e--)c(d,g(a[e]))}var p=m(b.children("option")),
2
- k=m(b.children("option:selected")),e=g('<ul class="'+b.attr("id")+'_smart_tag"></ul>');b.before(e);e.css({"margin-left":"0px",width:"488px"});e.tagit({allowSpaces:!0,availableTags:p,afterTagAdded:function(d,c){var a=c.tagLabel;p=m(b.children("option"));if(j(p,a))q(e.tagit("assignedTags"),n);else{var f=b.attr("controller");if(f){var h=b.attr("action")?b.attr("action"):"create",l=b.attr("param")?b.attr("param"):"title",h="/"+(b.attr("controllers")?b.attr("controllers"):f+"s")+"/"+h,k={};k[f+"["+l+"]"]=
3
- a;g.ajax({url:h,dataType:"json",type:"POST",data:k,success:function(c){c?b.prepend('<option selected="selected" value="'+c.id+'">'+a+"</option>"):console.log("josn is null")}})}else console.log("select's controller param is null")}},afterTagRemoved:function(){q(e.tagit("assignedTags"),h)}});for(var l=k.length-1;0<=l;l--)e.tagit("createTag",k[l]);b.change(function(){for(var d=m(b.children("option:selected")),c=e.tagit("assignedTags"),a=d.length-1;0<=a;a--)j(c,d[a])||e.tagit("createTag",d[a]);for(a=
4
- c.length-1;0<=a;a--)j(d,c[a])||e.tagit("removeTagByLabel",c[a])})}for(var n=g(".smart_tag"),f=n.length-1;0<=f;f--)h(g(n[f]))})})(window.jQuery);
1
+ (function(f){f(document).ready(function(){function k(e){var a={};a.select=e;a.inArray=function(a,c){for(var b=a.length-1;0<=b;b--)if(a[b]==c)return!0;return!1};a.clearText=function(a){return a.replace(/^\s+|\s+$/g,"")};a.getOptions=function(d){for(var c=[],b=d.length-1;0<=b;b--){var e=a.clearText(f(d[b]).text());a.inArray(c,e)||c.push(e)}return c};a.createTags=function(){for(var d=a.selected_options.length-1;0<=d;d--)a.ul.tagit("createTag",a.selected_options[d])};a.changeTags=function(){for(var d=
2
+ a.getOptions(a.select.children("option:selected")),c=a.ul.tagit("assignedTags"),b=c.length-1;0<=b;b--)a.inArray(d,c[b])||a.ul.tagit("removeTagByLabel",c[b]);for(b=d.length-1;0<=b;b--)a.inArray(c,d[b])||a.ul.tagit("createTag",d[b])};a.addSelected=function(d,c){a.inArray(d,a.clearText(c.text()))&&c.attr("selected",!0)};a.removeSelected=function(d,c){a.inArray(d,a.clearText(c.text()))||c.attr("selected",!1)};a.changeSelected=function(d,c){for(var b=a.select.children("option"),e=b.length-1;0<=e;e--)c(d,
3
+ f(b[e]))};a.all_options=a.getOptions(e.children("option"));a.selected_options=a.getOptions(e.children("option:selected"));a.ul=f("<ul></ul>");e.before(a.ul);a.ul.css({"margin-left":"0px",width:"488px"});a.select.change(function(){a.changeTags()});a.start=function(){a.createTags()};return a}function l(e){var a=k(e);a.ajaxAddTag=function(d){var c=a.select.attr("controller");if(c){var b=a.select.attr("action")?a.select.attr("action"):"create",g=a.select.attr("param")?a.select.attr("param"):"title",b=
4
+ "/"+(a.select.attr("controllers")?a.select.attr("controllers"):c+"s")+"/"+b,h={};h[c+"["+g+"]"]=d;f.ajax({url:b,dataType:"json",type:"POST",data:h,success:function(a){a?e.prepend('<option selected="selected" value="'+a.id+'">'+d+"</option>"):console.log("josn is null")}})}else console.log("select's controller param is null")};a.ul.tagit({allowSpaces:!0,availableTags:a.all_options,afterTagAdded:function(d,c){var b=c.tagLabel;all_options=a.getOptions(a.select.children("option"));a.inArray(all_options,
5
+ b)?a.changeSelected(a.ul.tagit("assignedTags"),a.addSelected):a.ajaxAddTag(b)},afterTagRemoved:function(){a.changeSelected(a.ul.tagit("assignedTags"),a.removeSelected)}});return a}function m(e){var a=k(e);a.ul.tagit({allowSpaces:!0,availableTags:a.all_options,tagLimit:1,afterTagAdded:function(d,c){var b=c.tagLabel;all_options=a.getOptions(a.select.children("option"));a.inArray(all_options,b)?(a.changeSelected(a.ul.tagit("assignedTags"),a.addSelected),a.ul.parents(".control-group").removeClass("error"),
6
+ a.ul.parents(".control-group").addClass("success"),a.ul.parents(".control-group").find(".help-inline").text("Success!")):(a.ul.tagit("removeAll"),a.ul.parents(".control-group").removeClass("success"),a.ul.parents(".control-group").addClass("error"),a.ul.parents(".control-group").find(".help-inline").text("You should choice one career."));1==a.ul.tagit("assignedTags").length&&(a.ul.find(".ui-autocomplete-input").attr("disabled","disabled"),a.select.focus())},afterTagRemoved:function(){a.changeSelected(a.ul.tagit("assignedTags"),
7
+ a.removeSelected);1>a.ul.tagit("assignedTags").length&&a.ul.find(".ui-autocomplete-input").removeAttr("disabled")}});return a}for(var h=f(".smart_tag"),g=h.length-1;0<=g;g--){var j=l(f(h[g]));j.start()}h=f(".single_choice");for(g=h.length-1;0<=g;g--)j=m(f(h[g])),j.start()})})(window.jQuery);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,8 +44,6 @@ files:
44
44
  - lib/smart_tag/helper.rb
45
45
  - lib/smart_tag/version.rb
46
46
  - smart_tag.gemspec
47
- - vendor/assets/javascripts/smart_tag/single_choice.js
48
- - vendor/assets/javascripts/smart_tag/single_choice.min.js
49
47
  - vendor/assets/javascripts/smart_tag/smart_tag.js
50
48
  - vendor/assets/javascripts/smart_tag/smart_tag.min.js
51
49
  - vendor/assets/javascripts/smart_tag/tag-it.js
@@ -1,139 +0,0 @@
1
- (function($){
2
- $(document).ready(function() {
3
-
4
- function SingleChoice(select) {
5
-
6
- function inArray(array, value) {
7
- for (var i = array.length - 1; i >= 0; i--) {
8
- if (array[i] == value) {
9
- return true;
10
- }
11
- }
12
-
13
- return false;
14
- }
15
-
16
- function clearText(text) {
17
- // \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
18
- return text.replace(/^\s+|\s+$/g, ""); // 去掉开头或结尾的任何空白字符
19
- }
20
-
21
- function getOptions(selector) {
22
- var options = [];
23
- for (var i = selector.length - 1; i >= 0; i--) {
24
- var text = clearText($(selector[i]).text());
25
-
26
- if (!inArray(options, text)) {
27
- options.push(text);
28
- }
29
- }
30
-
31
- return options;
32
- }
33
-
34
- function createTags() {
35
- for (var i = selected_options.length - 1; i >= 0; i--) {
36
- ul.tagit("createTag", selected_options[i]);
37
- }
38
- }
39
-
40
- function changeTags() {
41
- var new_tags = getOptions(select.children('option:selected'));
42
- var tags = ul.tagit("assignedTags")
43
-
44
- // remove
45
- for (var i = tags.length - 1; i >= 0; i--) {
46
- if (!inArray(new_tags, tags[i])) {
47
- ul.tagit("removeTagByLabel", tags[i]);
48
- }
49
- }
50
-
51
- // create
52
- for (var i = new_tags.length - 1; i >= 0; i--) {
53
- if (!inArray(tags, new_tags[i])) {
54
- ul.tagit("createTag", new_tags[i]);
55
- }
56
- }
57
- }
58
-
59
- function addSelected(tags, option) {
60
- if (inArray(tags, clearText(option.text()))) {
61
- option.attr("selected", true);
62
- }
63
- }
64
-
65
- function removeSelected(tags, option) {
66
- if (!inArray(tags, clearText(option.text()))) {
67
- option.attr("selected", false);
68
- }
69
- }
70
-
71
- function changeSelected(tags, method) {
72
- var options = select.children('option');
73
- for (var i = options.length - 1; i >= 0; i--) {
74
- method(tags, $(options[i]));
75
- }
76
- }
77
-
78
- var all_options = getOptions(select.children('option'));
79
- var selected_options = getOptions(select.children('option:selected'));
80
- var ul = $('<ul class="' + select.attr('id') + '_single_choice"></ul>');
81
-
82
- select.before(ul);
83
-
84
- ul.css({
85
- 'margin-left': '0px',
86
- 'width': '488px'
87
- });
88
-
89
- ul.tagit({
90
- allowSpaces: true,
91
- availableTags: all_options,
92
- tagLimit: 1,
93
- afterTagAdded: function(event, ui) {
94
- var add_tag = ui.tagLabel;
95
- all_options = getOptions(select.children('option'));
96
-
97
- if (inArray(all_options, add_tag)) {
98
- changeSelected(ul.tagit("assignedTags"), addSelected);
99
- ul.parents(".control-group").removeClass("error");
100
- ul.parents(".control-group").addClass("success");
101
- ul.parents(".control-group").find('.help-inline').text("Success!");
102
- }else {
103
- ul.tagit("removeAll");
104
- ul.parents(".control-group").removeClass("success");
105
- ul.parents(".control-group").addClass("error");
106
- ul.parents(".control-group").find('.help-inline').text("You should choice one career.");
107
- }
108
-
109
-
110
- if(ul.tagit("assignedTags").length == 1){
111
- ul.find(".ui-autocomplete-input").attr('disabled', 'disabled');
112
- select.focus();
113
- }
114
- },
115
- afterTagRemoved: function(event, ui) {
116
-
117
- changeSelected(ul.tagit("assignedTags"), removeSelected);
118
-
119
- if (ul.tagit("assignedTags").length < 1) {
120
- ul.find(".ui-autocomplete-input").removeAttr('disabled');
121
- }
122
- }
123
- });
124
-
125
- createTags();
126
-
127
- select.change(function(){
128
- changeTags();
129
- });
130
- }
131
-
132
- var selects = $(".single_choice");
133
-
134
- for (var i = selects.length - 1; i >= 0; i--) {
135
- SingleChoice($(selects[i]));
136
- }
137
-
138
- });
139
- }(window.jQuery));
@@ -1,4 +0,0 @@
1
- (function(f){f(document).ready(function(){function k(e){function g(a,c){for(var b=a.length-1;0<=b;b--)if(a[b]==c)return!0;return!1}function d(a){return a.replace(/^\s+|\s+$/g,"")}function h(a){for(var c=[],b=a.length-1;0<=b;b--){var e=d(f(a[b]).text());g(c,e)||c.push(e)}return c}function j(a,c){g(a,d(c.text()))&&c.attr("selected",!0)}function k(a,c){g(a,d(c.text()))||c.attr("selected",!1)}function n(a,c){for(var b=e.children("option"),d=b.length-1;0<=d;d--)c(a,f(b[d]))}var l=h(e.children("option")),
2
- p=h(e.children("option:selected")),a=f('<ul class="'+e.attr("id")+'_single_choice"></ul>');e.before(a);a.css({"margin-left":"0px",width:"488px"});a.tagit({allowSpaces:!0,availableTags:l,tagLimit:1,afterTagAdded:function(d,c){var b=c.tagLabel;l=h(e.children("option"));g(l,b)?(n(a.tagit("assignedTags"),j),a.parents(".control-group").removeClass("error"),a.parents(".control-group").addClass("success"),a.parents(".control-group").find(".help-inline").text("Success!")):(a.tagit("removeAll"),a.parents(".control-group").removeClass("success"),
3
- a.parents(".control-group").addClass("error"),a.parents(".control-group").find(".help-inline").text("You should choice one career."));1==a.tagit("assignedTags").length&&(a.find(".ui-autocomplete-input").attr("disabled","disabled"),e.focus())},afterTagRemoved:function(){n(a.tagit("assignedTags"),k);1>a.tagit("assignedTags").length&&a.find(".ui-autocomplete-input").removeAttr("disabled")}});for(var m=p.length-1;0<=m;m--)a.tagit("createTag",p[m]);e.change(function(){for(var d=h(e.children("option:selected")),
4
- c=a.tagit("assignedTags"),b=c.length-1;0<=b;b--)g(d,c[b])||a.tagit("removeTagByLabel",c[b]);for(b=d.length-1;0<=b;b--)g(c,d[b])||a.tagit("createTag",d[b])})}for(var j=f(".single_choice"),d=j.length-1;0<=d;d--)k(f(j[d]))})})(window.jQuery);