smart_tag 0.2.3 → 0.3.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.
data/lib/smart_tag/version.rb
CHANGED
@@ -0,0 +1,121 @@
|
|
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
|
+
ul.tagit("removeAll");
|
43
|
+
ul.tagit("createTag", new_tags[0]);
|
44
|
+
}
|
45
|
+
|
46
|
+
function addSelected(tags, option) {
|
47
|
+
if (inArray(tags, clearText(option.text()))) {
|
48
|
+
option.attr("selected", true);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
function removeSelected(tags, option) {
|
53
|
+
if (!inArray(tags, clearText(option.text()))) {
|
54
|
+
option.attr("selected", false);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
function changeSelected(tags, method) {
|
59
|
+
var options = select.children('option');
|
60
|
+
for (var i = options.length - 1; i >= 0; i--) {
|
61
|
+
method(tags, $(options[i]));
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
var all_options = getOptions(select.children('option'));
|
66
|
+
var selected_options = getOptions(select.children('option:selected'));
|
67
|
+
var ul = $('<ul class="' + select.attr('id') + '_single_choice"></ul>');
|
68
|
+
|
69
|
+
select.before(ul);
|
70
|
+
|
71
|
+
ul.css({
|
72
|
+
'margin-left': '0px',
|
73
|
+
'width': '488px'
|
74
|
+
});
|
75
|
+
|
76
|
+
ul.tagit({
|
77
|
+
allowSpaces: true,
|
78
|
+
availableTags: all_options,
|
79
|
+
tagLimit: 1,
|
80
|
+
afterTagAdded: function(event, ui) {
|
81
|
+
var add_tag = ui.tagLabel;
|
82
|
+
all_options = getOptions(select.children('option'));
|
83
|
+
|
84
|
+
if (inArray(all_options, add_tag)) {
|
85
|
+
changeSelected(ul.tagit("assignedTags"), addSelected);
|
86
|
+
ul.parents(".control-group").removeClass("error");
|
87
|
+
ul.parents(".control-group").addClass("success");
|
88
|
+
ul.parents(".control-group").find('.help-inline').text("Success!");
|
89
|
+
}else {
|
90
|
+
ul.tagit("removeAll");
|
91
|
+
ul.parents(".control-group").removeClass("success");
|
92
|
+
ul.parents(".control-group").addClass("error");
|
93
|
+
ul.parents(".control-group").find('.help-inline').text("You should choice one career.");
|
94
|
+
}
|
95
|
+
|
96
|
+
if(ul.tagit("assignedTags").length == 1){
|
97
|
+
ul.find(".ui-autocomplete-input").attr('disabled', 'disabled');
|
98
|
+
select.focus();
|
99
|
+
}
|
100
|
+
},
|
101
|
+
afterTagRemoved: function(event, ui) {
|
102
|
+
changeSelected(ul.tagit("assignedTags"), removeSelected);
|
103
|
+
ul.find(".ui-autocomplete-input").removeAttr('disabled');
|
104
|
+
}
|
105
|
+
});
|
106
|
+
|
107
|
+
createTags();
|
108
|
+
|
109
|
+
select.change(function(){
|
110
|
+
changeTags();
|
111
|
+
});
|
112
|
+
}
|
113
|
+
|
114
|
+
var selects = $(".single_choice");
|
115
|
+
|
116
|
+
for (var i = selects.length - 1; i >= 0; i--) {
|
117
|
+
SingleChoice($(selects[i]));
|
118
|
+
}
|
119
|
+
|
120
|
+
});
|
121
|
+
}(window.jQuery));
|
@@ -0,0 +1,4 @@
|
|
1
|
+
(function(f){f(document).ready(function(){function k(d){function g(a,b){for(var e=a.length-1;0<=e;e--)if(a[e]==b)return!0;return!1}function c(a){return a.replace(/^\s+|\s+$/g,"")}function h(a){for(var b=[],e=a.length-1;0<=e;e--){var d=c(f(a[e]).text());g(b,d)||b.push(d)}return b}function j(a,b){g(a,c(b.text()))&&b.attr("selected",!0)}function k(a,b){g(a,c(b.text()))||b.attr("selected",!1)}function n(a,b){for(var e=d.children("option"),c=e.length-1;0<=c;c--)b(a,f(e[c]))}var l=h(d.children("option")),
|
2
|
+
p=h(d.children("option:selected")),a=f('<ul class="'+d.attr("id")+'_single_choice"></ul>');d.before(a);a.css({"margin-left":"0px",width:"488px"});a.tagit({allowSpaces:!0,availableTags:l,tagLimit:1,afterTagAdded:function(c,b){var e=b.tagLabel;l=h(d.children("option"));g(l,e)?(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"),d.focus())},afterTagRemoved:function(){n(a.tagit("assignedTags"),k);a.find(".ui-autocomplete-input").removeAttr("disabled")}});for(var m=p.length-1;0<=m;m--)a.tagit("createTag",p[m]);d.change(function(){var c=h(d.children("option:selected"));a.tagit("removeAll");a.tagit("createTag",
|
4
|
+
c[0])})}for(var j=f(".single_choice"),c=j.length-1;0<=c;c--)k(f(j[c]))})})(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.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jquery-ui-rails
|
@@ -44,6 +44,8 @@ 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
|
47
49
|
- vendor/assets/javascripts/smart_tag/smart_tag.js
|
48
50
|
- vendor/assets/javascripts/smart_tag/smart_tag.min.js
|
49
51
|
- vendor/assets/javascripts/smart_tag/tag-it.js
|