Browse Source

Evolution #41: Sélection des tags dynamique (ajax): Seuls les tags inconus peuvent être utilisés.

bastien 13 years ago
parent
commit
e572d8d9c9

+ 5 - 3
src/Muzich/CoreBundle/Resources/views/Tag/tagsPrompt.html.twig View File

@@ -30,8 +30,9 @@
30 30
   // I've hacked the jQuery UI autocomplete render function
31 31
   // to highlight part of the matched string
32 32
   $.ui.autocomplete.prototype._renderItem = function( ul, item) {
33
-      var re = new RegExp("^" + this.term, "i") ;
34
-      var t = item.label.replace(re,"<strong>" + this.term + "</strong>");
33
+      var sstr = $.trim(this.term);
34
+      var re = new RegExp("^" + sstr, "i") ;
35
+      var t = item.label.replace(re,"<strong>" + sstr + "</strong>");
35 36
       return $( "<li></li>" )
36 37
           .data( "item.autocomplete", item )
37 38
           .append( "<a>" + t + "</a>" )
@@ -41,7 +42,8 @@
41 42
   // The autocomplete for the last tagbox
42 43
   $("#tags_prompt_{{ form_name }} ul.tagbox li.input input").autocomplete({
43 44
       source: function(req, responseFn) {
44
-          var re = $.ui.autocomplete.escapeRegex(req.term);
45
+          var sstr = $.trim(req.term);
46
+          var re = $.ui.autocomplete.escapeRegex(sstr);
45 47
           var matcher = new RegExp( "^" + re, "i" );
46 48
           var a = $.grep( taglist, function(item,index){
47 49
               return matcher.test(item);

+ 1 - 1
src/Muzich/CoreBundle/Resources/views/layout.html.twig View File

@@ -17,9 +17,9 @@
17 17
   {% block css %}{% endblock %}
18 18
 	<script src="{{ asset('js/jquery-1.6.4.min.js') }}" type="text/javascript"></script>
19 19
   <script src="{{ asset('js/jquery-ui-1.8.7.min.js') }}" type="text/javascript"></script>
20
+	<script src="{{ asset('bundles/muzichcore/js/muzich.js') }}" type="text/javascript"></script>
20 21
   <script src="{{ asset('js/tags/jquery.autoGrowInput.js') }}" type="text/javascript"></script>
21 22
   <script src="{{ asset('js/tags/jquery.tagBox.js') }}" type="text/javascript"></script>
22
-	<script src="{{ asset('bundles/muzichcore/js/muzich.js') }}" type="text/javascript"></script>
23 23
   {% block js %}{% endblock %}
24 24
   
25 25
 </head>

+ 8 - 0
web/bundles/muzichcore/js/muzich.js View File

@@ -46,6 +46,14 @@ function findKeyWithValue(arrayt, value)
46 46
   return "";
47 47
 }
48 48
 
49
+if(typeof(String.prototype.trim) === "undefined")
50
+{
51
+    String.prototype.trim = function() 
52
+    {
53
+        return String(this).replace(/^\s+|\s+$/g, '');
54
+    };
55
+}
56
+
49 57
 
50 58
 // Tags
51 59
 

+ 43 - 25
web/js/tags/jquery.tagBox.js View File

@@ -10,12 +10,23 @@
10 10
 *  
11 11
 */
12 12
 
13
+
14
+
13 15
 (function($) {
14 16
     
15 17
     function TagBox(input, options) {
16 18
 
17 19
         var self = this;
18 20
         
21
+        // Ce tableau contiendra les tags déjà ajoutés
22
+        if(typeof(tagsAddeds) === "undefined")
23
+        {
24
+          tagsAddeds = new Array();
25
+        }
26
+        // On permet de faire un tableau de tags ajoutés par formulaires
27
+        // TODO: Par 'champ' ce serais mieux.
28
+        tagsAddeds[options.form_name] = new Array();
29
+        
19 30
         self.options = options
20 31
         self.delimit_key = 188
21 32
         self.delimit_expr = /\s*,\s*/
@@ -80,39 +91,46 @@
80 91
               self.addTag(values[tagid], tagid, options.form_name);
81 92
           }
82 93
         }
94
+        
83 95
     }
84 96
     
85 97
     TagBox.prototype = {
86 98
         
87 99
         addTag : function(label, id, form_name) {
88 100
             
89
-            var self = this;
90
-            var tag = $('<li class="tag">' + $('<div>').text(label).remove().html() + '</li>');
91
-            
92
-            this.tags.push(label);
101
+            // Nos conditions pour ajouter le tag:
102
+            // * N'a pas déjà été ajouté a ce champ
103
+            // * Est dans la liste des tags existants
104
+            if (id && !findKeyWithValue(tagsAddeds[form_name], id))
105
+            {
106
+              var self = this;
107
+              var tag = $('<li class="tag">' + $('<div>').text(label).remove().html() + '</li>');
93 108
 
94
-            tag.append($('<a>', {
95
-                "href" : "#",
96
-                "class": "close",
97
-                "text": "close",
98
-                click: function(e) {
99
-                    e.preventDefault();
100
-                    var index = self.tagbox.find("li").index($(this).parent());
101
-                    self.removeTag(index);
102
-                }
103
-            })).append($('<input>', {
104
-              'type'   : 'checkbox',
105
-              'style'  : 'display: none;',
106
-              'value'  : id,
107
-              'name'   : form_name+'[tags]['+id+']',
108
-              'id'     : form_name+'_tags_'+id,
109
-              'checked': 'checked'
110
-            }));
111
-            
112
-            // <input type="checkbox" value="6373" name="group[tags][6373]" id="group_tags_6373">
109
+              this.tags.push(label);
110
+
111
+              tag.append($('<a>', {
112
+                  "href" : "#",
113
+                  "class": "close",
114
+                  "text": "close",
115
+                  click: function(e) {
116
+                      e.preventDefault();
117
+                      var index = self.tagbox.find("li").index($(this).parent());
118
+                      self.removeTag(index);
119
+                  }
120
+              })).append($('<input>', {
121
+                'type'   : 'checkbox',
122
+                'style'  : 'display: none;',
123
+                'value'  : id,
124
+                'name'   : form_name+'[tags]['+id+']',
125
+                'id'     : form_name+'_tags_'+id,
126
+                'checked': 'checked'
127
+              }));
128
+              
129
+              tagsAddeds[form_name][id] = id;
130
+              self.inputHolder.before(tag);
131
+              self.updateInput();
132
+            }
113 133
             
114
-            self.inputHolder.before(tag);
115
-            self.updateInput();
116 134
         },
117 135
         removeTag : function(index) {
118 136