Browse Source

JS: Mise en place du début du nouveau javascript pour la gestion du tag prompt.

Sevajol Bastien 11 years ago
parent
commit
1aee28d882

+ 8 - 1
src/Muzich/CoreBundle/Resources/views/Tag/tagsPrompt.html.twig View File

@@ -11,8 +11,14 @@
11 11
     {% endif %}
12 12
   {% endif %}
13 13
   
14
+  <input
15
+    class="tag_prompt"
16
+    value=""
17
+    placeholder="{{ 'tags.inputtext.help'|trans({}, 'userui') }}"
18
+  />
19
+   
14 20
    <input class="tags_prompt_list" name="tags" value="" /> 
15
-   <input id="tags_selected_tag_{{ form_name }}" type="hidden" value="" />
21
+   <input class="tag_prompt_selected_ids" id="tags_selected_tag_{{ form_name }}" type="hidden" value="" />
16 22
    
17 23
    <div id="search_tag_{{ form_name }}" class="search_tag_list" style="display: none;">
18 24
      <span class="info"></span>
@@ -52,4 +58,5 @@
52 58
   // On détruit la variable taginit
53 59
   delete taginit;
54 60
     
61
+    
55 62
 </script>

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

@@ -22,6 +22,7 @@
22 22
   {% endif %}
23 23
   <script src="{{ asset('js/jquery-ui-1.8.7.min.js') }}" type="text/javascript"></script>
24 24
   <script src="{{ asset('bundles/muzichcore/js/muzich.js') }}" type="text/javascript"></script>
25
+  <script src="{{ asset('js/lib/TagPrompt.js') }}" type="text/javascript"></script>
25 26
 	
26 27
   <script type="text/javascript" src="{{ asset('js/swfobject.js') }}"></script> 
27 28
   {# TODO: On peux piquer les js ?  #}

+ 20 - 1
web/bundles/muzichcore/js/muzich.js View File

@@ -235,7 +235,26 @@ function remove_tags(form_name)
235 235
   
236 236
 }
237 237
 
238
-$(document).ready(function(){
238
+function JQueryJson(url, data, callback_success)
239
+{
240
+  $.ajax({
241
+    type: 'POST',
242
+    url: url,
243
+    dataType: 'json',
244
+    data: data,
245
+    success: function(response)
246
+    {
247
+      if (response.status == 'mustbeconnected')
248
+      {
249
+        $(location).attr('href', url_index);
250
+      }
251
+      
252
+      callback_success(response);
253
+    }
254
+  });
255
+}
256
+
257
+$(document).ready(function(){  
239 258
     
240 259
   // Controle du focus sur la page
241 260
   function onBlur() {

+ 157 - 0
web/js/lib/TagPrompt.js View File

@@ -0,0 +1,157 @@
1
+function TagPrompt()
2
+{
3
+  
4
+  /* @var tags_selected array of Tag */
5
+  var tags_selected = new Array();
6
+  /* @var tags_proposed array of Tag */
7
+  var tags_proposed = new Array();
8
+  
9
+  this.getProposedTagsForString = function(search_string, callback_success, callback_error)
10
+  {
11
+    JQueryJson(url_search_tag, {'string_search': search_string}, function(response){
12
+      if (response.status == 'error')
13
+      {
14
+        callback_error(response.error);
15
+      }
16
+      else if (response.status == 'success')
17
+      {
18
+        for (i in response.data)
19
+        {
20
+          var tag = new Tag(
21
+            response.data[i].id,
22
+            response.data[i].name,
23
+            true
24
+          );
25
+          tags_proposed.push(tag);
26
+        }
27
+        callback_success(tags_proposed, search_string);
28
+      }
29
+    });
30
+  }
31
+  
32
+  this.selectProposedTag = function (tag_id)
33
+  {
34
+    // Selection d'un tag dans la liste de propositions
35
+  }
36
+  
37
+  this.openTagSubmission = function (tag_string)
38
+  {
39
+    // Ouverture du dialogue pour proposer un nouveau tag
40
+  }
41
+  
42
+  this.submitTag = function (tag_string, tag_arguments)
43
+  {
44
+    // Propotion d'un tag
45
+  }
46
+}
47
+
48
+function TagPromptConnector(input, output, proposition_list)
49
+{
50
+  var _input = input;
51
+  var _output = output;
52
+  var _tag_prompt = new TagPrompt();
53
+  var _tag_proposition_list = new TagPromptPropositionList(proposition_list);
54
+  
55
+  var launchSearchTagsIdLastKeystroke = function(search_string)
56
+  {
57
+    if (search_string == _input.val())
58
+    {
59
+      displayTagsProposedSearchTags();
60
+    }
61
+  }
62
+  
63
+  var displayTagsProposedSearchTags = function()
64
+  {
65
+    var string_search = _input.val();
66
+    tags = _tag_prompt.getProposedTagsForString(
67
+      string_search,
68
+      _tag_proposition_list.displayTagsPropositions,
69
+      _tag_proposition_list.displayError
70
+    );
71
+  }
72
+  
73
+  $(_input).bind('keyup', function() {
74
+    setTimeout(launchSearchTagsIdLastKeystroke, 1000, [_input.val()]);
75
+  });
76
+}
77
+
78
+function TagPromptPropositionList(proposition_list, string_search)
79
+{
80
+  var _proposition_list = proposition_list;
81
+  var _list;
82
+  
83
+  this.displayError = function(error_string)
84
+  {
85
+    initializeList();
86
+    var span_info = _proposition_list.find('span.info');
87
+    span_info.text(error_string);
88
+  }
89
+  
90
+  this.displayTagsPropositions = function(tags, string_search)
91
+  {
92
+    initializeList();
93
+    displayMessage(str_replace('%string_search%', string_search, string_search_tag_title));
94
+    for (i in tags)
95
+    {
96
+      addTagToList(tags[i]);
97
+      // Bouton afficher tout les tags, tag hiddent quoi
98
+    }
99
+  }
100
+  
101
+  var initializeList = function()
102
+  {
103
+    $(_proposition_list).show();
104
+    _list = _proposition_list.find('ul.search_tag_list');
105
+    _list.find('li').remove();
106
+    _proposition_list.find('a.more').hide();
107
+  }
108
+  
109
+  var displayMessage = function(message)
110
+  {
111
+    var span_info = _proposition_list.find('span.info');
112
+    span_info.text(message);
113
+  }
114
+  
115
+  var addTagToList = function(tag)
116
+  {
117
+    _list.append(getListLine(tag));
118
+  }
119
+  
120
+  var getListLine = function(tag)
121
+  {
122
+    return '<li>'+tag.name+'</li>';
123
+  }
124
+  
125
+}
126
+
127
+function Tag(id, name, knew)
128
+{
129
+  /* @var _id int */
130
+  this.id = id;
131
+  /* @var _name string */
132
+  this.name = name;
133
+  /* @var _knew boolean */
134
+  this.knew = knew;
135
+}
136
+
137
+Tag.prototype =
138
+{
139
+  isKnew: function()
140
+  {
141
+    if (this.knew)
142
+    {
143
+      return true;
144
+    }
145
+    return false;
146
+  }
147
+}
148
+
149
+$(document).ready(function(){
150
+  $('input.tag_prompt').each(function(){
151
+    new TagPromptConnector(
152
+      $(this),
153
+      $(this).parents('form').find('input.tag_prompt_selected_ids'),
154
+      $(this).parents('form').find('div.search_tag_list')
155
+   );
156
+  });
157
+});