muzich.js 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Scripts de Muzi.ch
  3. * Rédigé et propriété de Sevajol Bastien (http://www.bux.fr) sauf si mention
  4. * contraire sur la fonction.
  5. *
  6. */
  7. // Messages flashs
  8. var myMessages = ['info','warning','error','success']; // define the messages types
  9. function hideAllMessages()
  10. {
  11. var messagesHeights = new Array(); // this array will store height for each
  12. for (i=0; i<myMessages.length; i++)
  13. {
  14. messagesHeights[i] = $('.' + myMessages[i]).outerHeight();
  15. $('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport
  16. }
  17. }
  18. $(document).ready(function(){
  19. // Initially, hide them all
  20. hideAllMessages();
  21. $('.message').animate({top:"0"}, 500);
  22. // When message is clicked, hide it
  23. $('.message a.message-close').click(function(){
  24. $(this).parent('.message').animate({top: -$(this).outerHeight()-50}, 700);
  25. return false;
  26. });
  27. });
  28. function findKeyWithValue(arrayt, value)
  29. {
  30. for(i in arrayt)
  31. {
  32. if (arrayt[i] == value)
  33. {
  34. return i;
  35. }
  36. }
  37. return "";
  38. }
  39. function json_to_array(json_string)
  40. {
  41. if (json_string.length)
  42. {
  43. return eval("(" + json_string + ")");
  44. }
  45. return new Array();
  46. }
  47. function strpos (haystack, needle, offset) {
  48. // Finds position of first occurrence of a string within another
  49. //
  50. // version: 1109.2015
  51. // discuss at: http://phpjs.org/functions/strpos // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  52. // + improved by: Onno Marsman
  53. // + bugfixed by: Daniel Esteban
  54. // + improved by: Brett Zamir (http://brett-zamir.me)
  55. // * example 1: strpos('Kevin van Zonneveld', 'e', 5); // * returns 1: 14
  56. var i = (haystack + '').indexOf(needle, (offset || 0));
  57. return i === -1 ? false : i;
  58. }
  59. /**
  60. * Converts the given data structure to a JSON string.
  61. * Argument: arr - The data structure that must be converted to JSON
  62. * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
  63. * var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
  64. * http://www.openjs.com/scripts/data/json_encode.php
  65. */
  66. function array2json(arr) {
  67. var parts = [];
  68. var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
  69. for(var key in arr) {
  70. var value = arr[key];
  71. if(typeof value == "object") { //Custom handling for arrays
  72. if(is_list) parts.push(array2json(value)); /* :RECURSION: */
  73. else parts[key] = array2json(value); /* :RECURSION: */
  74. } else {
  75. var str = "";
  76. if(!is_list) str = '"' + key + '":';
  77. //Custom handling for multiple data types
  78. if(typeof value == "number") str += value; //Numbers
  79. else if(value === false) str += 'false'; //The booleans
  80. else if(value === true) str += 'true';
  81. else str += '"' + value + '"'; //All other things
  82. // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
  83. parts.push(str);
  84. }
  85. }
  86. var json = parts.join(",");
  87. if(is_list) return '[' + json + ']';//Return numerical JSON
  88. return '{' + json + '}';//Return associative JSON
  89. }
  90. function isInteger(s) {
  91. return (s.toString().search(/^-?[0-9]+$/) == 0);
  92. }
  93. function inArray(array, p_val) {
  94. var l = array.length;
  95. for(var i = 0; i < l; i++) {
  96. if(array[i] == p_val) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. if(typeof(String.prototype.trim) === "undefined")
  103. {
  104. String.prototype.trim = function()
  105. {
  106. return String(this).replace(/^\s+|\s+$/g, '');
  107. };
  108. }
  109. function str_replace (search, replace, subject, count) {
  110. // Replaces all occurrences of search in haystack with replace
  111. //
  112. // version: 1109.2015
  113. // discuss at: http://phpjs.org/functions/str_replace // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  114. // + improved by: Gabriel Paderni
  115. // + improved by: Philip Peterson
  116. // + improved by: Simon Willison (http://simonwillison.net)
  117. // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfixed by: Anton Ongson
  118. // + input by: Onno Marsman
  119. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  120. // + tweaked by: Onno Marsman
  121. // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  122. // + input by: Oleg Eremeev
  123. // + improved by: Brett Zamir (http://brett-zamir.me)
  124. // + bugfixed by: Oleg Eremeev
  125. // % note 1: The count parameter must be passed as a string in order // % note 1: to find a global variable in which the result will be given
  126. // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
  127. // * returns 1: 'Kevin.van.Zonneveld'
  128. // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
  129. // * returns 2: 'hemmo, mars' var i = 0,
  130. j = 0,
  131. temp = '',
  132. repl = '',
  133. sl = 0, fl = 0,
  134. f = [].concat(search),
  135. r = [].concat(replace),
  136. s = subject,
  137. ra = Object.prototype.toString.call(r) === '[object Array]', sa = Object.prototype.toString.call(s) === '[object Array]';
  138. s = [].concat(s);
  139. if (count) {
  140. this.window[count] = 0;
  141. }
  142. for (i = 0, sl = s.length; i < sl; i++) {
  143. if (s[i] === '') {
  144. continue;
  145. }for (j = 0, fl = f.length; j < fl; j++) {
  146. temp = s[i] + '';
  147. repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
  148. s[i] = (temp).split(f[j]).join(repl);
  149. if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
  150. }
  151. }
  152. }
  153. return sa ? s : s[0];
  154. }
  155. function explode (delimiter, string, limit) {
  156. // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.
  157. //
  158. // version: 1109.2015
  159. // discuss at: http://phpjs.org/functions/explode // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  160. // + improved by: kenneth
  161. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  162. // + improved by: d3x
  163. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: explode(' ', 'Kevin van Zonneveld');
  164. // * returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
  165. // * example 2: explode('=', 'a=bc=d', 2);
  166. // * returns 2: ['a', 'bc=d']
  167. var emptyArray = {0: ''
  168. };
  169. // third argument is not required
  170. if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {return null;
  171. }
  172. if (delimiter === '' || delimiter === false || delimiter === null) {
  173. return false;}
  174. if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
  175. return emptyArray;
  176. }
  177. if (delimiter === true) {
  178. delimiter = '1';
  179. }
  180. if (!limit) {
  181. return string.toString().split(delimiter.toString());
  182. }
  183. // support for limit argument
  184. var splitted = string.toString().split(delimiter.toString());var partA = splitted.splice(0, limit - 1);
  185. var partB = splitted.join(delimiter.toString());
  186. partA.push(partB);
  187. return partA;
  188. }
  189. // fonction de nettoyage des tags
  190. function remove_tags(form_name)
  191. {
  192. tagsAddeds[form_name] = new Array();
  193. $('form[name="'+form_name+'"] ul.tagbox li.tag').remove();
  194. $('form[name="'+form_name+'"] input.tagBox_tags_ids').val('');
  195. $('div#tags_prompt_'+form_name+' ul.tagbox li.input input[type="text"]')
  196. .val(string_tag_prompt_input_help)
  197. ;
  198. }
  199. $(document).ready(function(){
  200. // Controle du focus sur la page
  201. function onBlur() {
  202. document.body.className = 'blurred';
  203. }
  204. function onFocus(){
  205. document.body.className = 'focused';
  206. }
  207. if (/*@cc_on!@*/false) { // check for Internet Explorer
  208. document.onfocusin = onFocus;
  209. document.onfocusout = onBlur;
  210. } else {
  211. window.onfocus = onFocus;
  212. window.onblur = onBlur;
  213. }
  214. // Bouton de personalisation du filtre
  215. // Aucun tags
  216. $('.tags_prompt input.clear, a.filter_clear_url').live("click", function(){
  217. $('img.elements_more_loader').show();
  218. $('ul.elements').html('');
  219. form = $(this).parent('div').parent('form');
  220. remove_tags(form.attr('name'));
  221. form.submit();
  222. });
  223. // tags préférés
  224. $('.tags_prompt input.mytags').live("click", function(){
  225. $('img.elements_more_loader').show();
  226. $('ul.elements').html('');
  227. form = $(this).parent('div').parent('form');
  228. $.getJSON(url_get_favorites_tags, function(response) {
  229. if (response.status == 'mustbeconnected')
  230. {
  231. $(location).attr('href', url_index);
  232. }
  233. remove_tags(form.attr('name'));
  234. // if (tags.length)
  235. // {
  236. inputTag = $("div#tags_prompt_"+form.attr('name')+" input.form-default-value-processed");
  237. for (i in response.tags)
  238. {
  239. $('input#tags_selected_tag_'+form.attr('name')).val(i);
  240. inputTag.val(response.tags[i]);
  241. // Et on execute l'évènement selectTag de l'input
  242. inputTag.trigger("selectTag");
  243. }
  244. form.submit();
  245. //}
  246. });
  247. });
  248. // Tag cliqué dans la liste d'éléments
  249. $('ul.element_tags li a.element_tag').live('click', function(){
  250. // Si il y a une liste de tags (comme sur la page favoris, profil)
  251. if ($('ul#favorite_tags').length)
  252. {
  253. id = str_replace('#', '', $(this).attr('href'));
  254. link = $('ul#favorite_tags li a[href="#'+id+'"]');
  255. list_tag_clicked(link, true);
  256. }
  257. if ($('form[name="search"]').length)
  258. {
  259. $('img.elements_more_loader').show();
  260. $('ul.elements').html('');
  261. form = $('form[name="search"]');
  262. id = str_replace('#', '', $(this).attr('href'));
  263. remove_tags('search');
  264. inputTag = $("div#tags_prompt_search input.form-default-value-processed");
  265. $('input#tags_selected_tag_search').val(id);
  266. inputTag.val($(this).html());
  267. inputTag.trigger("selectTag");
  268. form.submit();
  269. }
  270. });
  271. // Affichage un/des embed
  272. // 1328283150_media-playback-start.png
  273. // 1328283201_emblem-symbolic-link.png
  274. $('a.element_embed_open_link').live("click", function(){
  275. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  276. li.find('a.element_embed_close_link').show();
  277. li.find('a.element_embed_open_link_text').hide();
  278. li.find('div.element_embed').show();
  279. return false;
  280. });
  281. $('a.element_name_embed_open_link').live("click", function(){
  282. li = $(this).parent('span').parent('td').parent('tr').parent().parent().parent('li.element');
  283. li.find('a.element_embed_close_link').show();
  284. li.find('a.element_embed_open_link_text').hide();
  285. li.find('div.element_embed').show();
  286. return false;
  287. });
  288. // Fermeture du embed si demandé
  289. $('a.element_embed_close_link').live("click", function(){
  290. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  291. li.find('div.element_embed').hide();
  292. li.find('a.element_embed_open_link_text').show();
  293. $(this).hide();
  294. return false;
  295. });
  296. // Affichage du "play" ou du "open" (image png)
  297. $('li.element a.a_thumbnail, li.element img.open, li.element img.play').live({
  298. mouseenter:
  299. function()
  300. {
  301. td = $(this).parent('td');
  302. a = td.find('a.a_thumbnail');
  303. if (a.hasClass('embed'))
  304. {
  305. td.find('img.play').show();
  306. }
  307. else
  308. {
  309. td.find('img.open').show();
  310. }
  311. },
  312. mouseleave:
  313. function()
  314. {
  315. td = $(this).parent('td');
  316. a = td.find('a.a_thumbnail');
  317. if (a.hasClass('embed'))
  318. {
  319. td.find('img.play').hide();
  320. }
  321. else
  322. {
  323. td.find('img.open').hide();
  324. }
  325. }
  326. }
  327. );
  328. // Mise en favoris
  329. $('a.favorite_link').live("click", function(){
  330. link = $(this);
  331. // Pour ne pas attendre la fin du chargement ajax:
  332. img = link.find('img');
  333. if (img.attr('src') == '/bundles/muzichcore/img/favorite_bw.png')
  334. {
  335. img.attr('src', '/bundles/muzichcore/img/favorite.png');
  336. }
  337. else
  338. {
  339. img.attr('src', '/bundles/muzichcore/img/favorite_bw.png');
  340. }
  341. $.getJSON($(this).attr('href'), function(response) {
  342. if (response.status == 'mustbeconnected')
  343. {
  344. $(location).attr('href', url_index);
  345. }
  346. img = link.find('img');
  347. link.attr('href', response.link_new_url);
  348. img.attr('src', response.img_new_src);
  349. img.attr('title', response.img_new_title);
  350. });
  351. return false;
  352. });
  353. // Affichage du bouton Modifier et Supprimer
  354. $('ul.elements li.element').live({
  355. mouseenter:
  356. function()
  357. {
  358. $(this).find('a.element_edit_link').show();
  359. $(this).find('a.element_remove_link').show();
  360. },
  361. mouseleave:
  362. function()
  363. {
  364. if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
  365. {
  366. $(this).find('a.element_edit_link').hide();
  367. }
  368. if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
  369. {
  370. $(this).find('a.element_remove_link').hide();
  371. }
  372. }
  373. }
  374. );
  375. // Plus d'éléments
  376. last_id = null;
  377. $('a.elements_more').click(function(){
  378. link = $(this);
  379. last_element = $('ul.elements li.element:last-child');
  380. id_last = str_replace('element_', '', last_element.attr('id'));
  381. invertcolor = 0;
  382. if (last_element.hasClass('even'))
  383. {
  384. invertcolor = 1;
  385. }
  386. $('img.elements_more_loader').show();
  387. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  388. if (response.status == 'mustbeconnected')
  389. {
  390. $(location).attr('href', url_index);
  391. }
  392. if (response.count)
  393. {
  394. $('ul.elements').append(response.html);
  395. $('img.elements_more_loader').hide();
  396. }
  397. if (response.end || response.count < 1)
  398. {
  399. $('img.elements_more_loader').hide();
  400. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  401. response.message+'</p></div>');
  402. link.hide();
  403. }
  404. });
  405. return false;
  406. });
  407. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  408. // Filtre et affichage éléments ajax
  409. $('form[name="search"] input[type="submit"]').click(function(){
  410. $('ul.elements').html('');
  411. $('div.no_elements').hide();
  412. $('img.elements_more_loader').show();
  413. });
  414. $('form[name="search"]').ajaxForm(function(response) {
  415. if (response.status == 'mustbeconnected')
  416. {
  417. $(location).attr('href', url_index);
  418. }
  419. $('ul.elements').html(response.html);
  420. if (response.count)
  421. {
  422. $('img.elements_more_loader').hide();
  423. $('span.elements_more').show();
  424. $('a.elements_more').show();
  425. }
  426. if (response.count < 1)
  427. {
  428. $('img.elements_more_loader').hide();
  429. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  430. response.message+'</p></div>');
  431. $('a.elements_more').hide()
  432. ;
  433. }
  434. $('ul.tagbox input[type="text"]').val($('ul.tagbox input[type="text"]').val());
  435. });
  436. // Suppression d'un element
  437. $('a.element_remove_link').jConfirmAction({
  438. question : "Vraiment supprimer ?",
  439. yesAnswer : "Oui",
  440. cancelAnswer : "Non",
  441. onYes: function(link){
  442. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  443. li.find('img.element_loader').show();
  444. $.getJSON(link.attr('href'), function(response){
  445. if (response.status == 'mustbeconnected')
  446. {
  447. $(location).attr('href', url_index);
  448. }
  449. if (response.status == 'success')
  450. {
  451. li.remove();
  452. }
  453. else
  454. {
  455. li.find('img.element_loader').hide();
  456. }
  457. });
  458. return false;
  459. },
  460. onOpen: function(link){
  461. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  462. li.find('a.element_edit_link').addClass('mustBeDisplayed');
  463. li.find('a.element_remove_link').addClass('mustBeDisplayed');
  464. },
  465. onClose: function(link){
  466. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  467. li.find('a.element_edit_link').removeClass('mustBeDisplayed');
  468. li.find('a.element_remove_link').removeClass('mustBeDisplayed');
  469. li.find('a.element_edit_link').hide();
  470. li.find('a.element_remove_link').hide();
  471. }
  472. });
  473. elements_edited = new Array();
  474. // Ouverture du formulaire de modification
  475. $('a.element_edit_link').live('click', function(){
  476. link = $(this);
  477. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  478. // On garde en mémoire l'élément édité en cas d'annulation
  479. elements_edited[li.attr('id')] = li.html();
  480. div_loader = li.find('div.loader');
  481. li.html(div_loader);
  482. li.find('img.element_loader').show();
  483. $.getJSON($(this).attr('href'), function(response) {
  484. if (response.status == 'mustbeconnected')
  485. {
  486. $(location).attr('href', url_index);
  487. }
  488. // On prépare le tagBox
  489. li.html(response.html);
  490. var options = new Array();
  491. options.form_name = response.form_name;
  492. options.tag_init = response.tags;
  493. ajax_query_timestamp = null;
  494. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  495. // On rend ce formulaire ajaxFormable
  496. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  497. li.prepend(div_loader);
  498. li.find('img.element_loader').show();
  499. });
  500. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  501. if (response.status == 'mustbeconnected')
  502. {
  503. $(location).attr('href', url_index);
  504. }
  505. if (response.status == 'success')
  506. {
  507. li.html(response.html);
  508. delete(elements_edited[li.attr('id')]);
  509. }
  510. else if (response.status == 'error')
  511. {
  512. li.find('img.element_loader').hide();
  513. li.find('ul.error_list').remove();
  514. ul_errors = $('<ul>').addClass('error_list');
  515. for (i in response.errors)
  516. {
  517. ul_errors.append($('<li>').append(response.errors[i]));
  518. }
  519. li.prepend(ul_errors);
  520. }
  521. });
  522. });
  523. return false;
  524. });
  525. // Annulation d'un formulaire de modification d'élément
  526. $('form.edit_element input.cancel_edit').live('click', function(){
  527. var li = $(this).parent('form').parent('li');
  528. li.html(elements_edited[li.attr('id')]);
  529. delete(elements_edited[li.attr('id')]);
  530. });
  531. ////////////////// TAG PROMPT ///////////////
  532. ajax_query_timestamp = null;
  533. tag_text_help = $('input.tag_text_help').val();
  534. // Les deux clicks ci-dessous permettent de faire disparaitre
  535. // la div de tags lorsque l'on clique ailleurs
  536. $('html').click(function() {
  537. if ($("div.search_tag_list").is(':visible'))
  538. {
  539. $("div.search_tag_list").hide();
  540. }
  541. });
  542. $("div.search_tag_list").live('click', function(event){
  543. event.stopPropagation();
  544. });
  545. function autocomplete_tag(input, form_name)
  546. {
  547. // Il doit y avoir au moin un caractère
  548. if (input.val().length > 0)
  549. {
  550. // on met en variable l'input
  551. inputTag = input;
  552. // On récupére la div de tags
  553. divtags = $("#search_tag_"+form_name);
  554. // Si la fenêtre de tags est caché
  555. if (!divtags.is(':visible'))
  556. {
  557. // On la replace
  558. position = input.position();
  559. divtags.css('left', Math.round(position.left) + 5);
  560. divtags.css('top', Math.round(position.top) + 28);
  561. // Et on l'affiche
  562. divtags.show();
  563. }
  564. // On affiche le loader
  565. $('#tag_loader_'+form_name).show();
  566. // On cache la liste de tags
  567. search_tag_list = divtags.find('ul.search_tag_list');
  568. // On supprime les anciens li
  569. search_tag_list.find('li').remove();
  570. search_tag_list.hide();
  571. // Et on affiche une info
  572. span_info = divtags.find('span.info');
  573. span_info.show();
  574. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  575. // C'est en fonction du nb de resultats qu'il sera affiché
  576. divtags.find('a.more').hide();
  577. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  578. ajax_query_timestamp = new Date().getTime();
  579. // Récupération des tags correspondants
  580. $.getJSON(url_search_tag+'/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  581. if (data.status == 'mustbeconnected')
  582. {
  583. $(location).attr('href', url_index);
  584. }
  585. // Ce contrôle permet de ne pas continuer si une requete
  586. // ajax a été faite depuis.
  587. if (data.timestamp == ajax_query_timestamp)
  588. {
  589. status = data.status;
  590. tags = data.data;
  591. // Si on spécifie une erreur
  592. if (status == 'error')
  593. {
  594. // On l'affiche a l'utilisateur
  595. span_info.text(data.error);
  596. }
  597. // Si c'est un succés
  598. else if (status == 'success')
  599. {
  600. if (tags.length > 0)
  601. {
  602. more = false;
  603. // Pour chaque tags retournés
  604. for (i in tags)
  605. {
  606. var tag_name = tags[i]['name'];
  607. var tag_id = tags[i]['id'];
  608. var t_string = tag_name
  609. // On construit un li
  610. string_exploded = explode(' ', $.trim(input.val()));
  611. for (n in string_exploded)
  612. {
  613. r_string = string_exploded[n];
  614. var re = new RegExp(r_string, "i");
  615. t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
  616. }
  617. li_tag =
  618. $('<li>').append(
  619. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  620. // qui réagit quand on clique dessus
  621. .click(function(e){
  622. // On récupère le nom du tag
  623. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  624. name = name.substr(strpos(name, '#')+1, name.length);
  625. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  626. id = str_replace(name, '', id);
  627. id = str_replace('#', '', id);
  628. $('input#tags_selected_tag_'+form_name).val(id);
  629. inputTag.val(name);
  630. // Et on execute l'évènement selectTag de l'input
  631. inputTag.trigger("selectTag");
  632. // On cache la liste puisque le choix vient d'être fait
  633. divtags.hide();
  634. inputTag.val(tag_text_help);
  635. return false;
  636. })
  637. .append(t_string)
  638. );
  639. // Si on depasse les 30 tags
  640. if (i > 30)
  641. {
  642. more = true;
  643. // On le cache
  644. li_tag.hide();
  645. }
  646. // On ajout ce li a la liste
  647. search_tag_list.append(li_tag);
  648. }
  649. if (more)
  650. {
  651. divtags.find('a.more').show();
  652. }
  653. // On cache l'info
  654. span_info.hide();
  655. // Et on affiche la liste
  656. search_tag_list.show();
  657. }
  658. else
  659. {
  660. span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  661. }
  662. }
  663. // On cache le loader
  664. $('#tag_loader_'+form_name).hide();
  665. }
  666. });
  667. }
  668. }
  669. last_keypress = 0;
  670. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  671. {
  672. if (!timed)
  673. {
  674. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  675. // et on met a jour l'id de la dernière pressé
  676. last_keypress = new Date().getTime();
  677. var this_time_id = last_keypress;
  678. }
  679. else
  680. {
  681. // Si elle a été redirigé, on met son id dans cette variable
  682. var this_time_id = time_id;
  683. }
  684. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  685. if (time_id != last_keypress && timed)
  686. {
  687. // elle disparait
  688. }
  689. else
  690. {
  691. //
  692. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  693. {
  694. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  695. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  696. // elle doit attendre au cas ou soit pressé.
  697. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  698. }
  699. else
  700. {
  701. // il n'y a plus a attendre, on envoie la demande de tag.
  702. autocomplete_tag(input, form_name);
  703. }
  704. }
  705. }
  706. // Autocompletion de tags
  707. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  708. var form_name = $(this).parent('li').parent('ul.tagbox')
  709. .parent('div.tags_prompt').parent('form').attr('name')
  710. ;
  711. var code = (e.keyCode ? e.keyCode : e.which);
  712. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  713. {
  714. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  715. }
  716. });
  717. // Un click sur ce lien affiche tout les tags cachés de la liste
  718. $('div.search_tag_list a.more').live('click', function(){
  719. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  720. $(this).show();
  721. });
  722. return false;
  723. });
  724. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  725. $('ul.tagbox li.input input[type="text"]').formDefaults();
  726. ////////////////// FIN TAG PROMPT ///////////////
  727. // Suppression d'un element
  728. $('a.group_remove_link').jConfirmAction({
  729. question : "Supprimer ce groupe ?",
  730. yesAnswer : "Oui",
  731. cancelAnswer : "Non",
  732. onYes: function(link){
  733. window.location = link.attr('href');
  734. return false;
  735. },
  736. onOpen: function(){},
  737. onClose: function(){}
  738. });
  739. // Selection Réseau global / Mon réseau
  740. $('div.select_network a').live('click', function(){
  741. divSelect = $(this).parent('div');
  742. if ($(this).hasClass('all_network'))
  743. {
  744. divSelect.find('a.all_network').addClass('active');
  745. divSelect.find('a.my_network').removeClass('active');
  746. divSelect.find('select').val('network_public');
  747. }
  748. else
  749. {
  750. divSelect.find('a.my_network').addClass('active');
  751. divSelect.find('a.all_network').removeClass('active');
  752. divSelect.find('select').val('network_personal');
  753. }
  754. });
  755. // Ajout d'un element
  756. $('form[name="add"] input[type="submit"]').live('click', function(){
  757. $('form[name="add"]').find('img.tag_loader').show();
  758. });
  759. $('form[name="add"]').ajaxForm(function(response) {
  760. if (response.status == 'mustbeconnected')
  761. {
  762. $(location).attr('href', url_index);
  763. }
  764. $('form[name="add"] img.tag_loader').hide();
  765. if (response.status == 'success')
  766. {
  767. $('form[name="add"]').find('ul.error_list').remove();
  768. $('ul.elements').prepend(response.html);
  769. $('form[name="add"] input[type="text"]').val('');
  770. $('div#element_add_box').slideUp();
  771. $('a#element_add_link').show();
  772. if ($('form[name="search"]').length)
  773. {
  774. $('form[name="search"]').slideDown();
  775. }
  776. remove_tags('add');
  777. }
  778. else if (response.status == 'error')
  779. {
  780. $('form[name="add"]').find('ul.error_list').remove();
  781. ul_errors = $('<ul>').addClass('error_list');
  782. for (i in response.errors)
  783. {
  784. ul_errors.append($('<li>').append(response.errors[i]));
  785. }
  786. $('form[name="add"]').prepend(ul_errors);
  787. }
  788. return false;
  789. });
  790. // Check périodique
  791. // TODO.
  792. /////////////////////
  793. // Filtre par tags (show, favorite)
  794. function refresh_elements_with_tags_selected(link)
  795. {
  796. // Puis on fait notre rekékéte ajax.
  797. $('ul.elements').html('');
  798. $('div.no_elements').hide();
  799. $('img.elements_more_loader').show();
  800. $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
  801. if (response.status == 'mustbeconnected')
  802. {
  803. $(location).attr('href', url_index);
  804. }
  805. $('ul.elements').html(response.html);
  806. if (response.count)
  807. {
  808. $('img.elements_more_loader').hide();
  809. $('span.elements_more').show();
  810. $('a.elements_more').show();
  811. }
  812. });
  813. return false;
  814. }
  815. function list_tag_clicked(link, erease)
  816. {
  817. if (erease)
  818. {
  819. $('ul#favorite_tags a.tag').removeClass('active');
  820. }
  821. // Ensuite on l'active ou le désactive
  822. if (link.hasClass('active'))
  823. {
  824. link.removeClass('active');
  825. }
  826. else
  827. {
  828. link.addClass('active');
  829. }
  830. // On construit notre liste de tags
  831. tags_ids = new Array();
  832. $('ul#favorite_tags a.tag.active').each(function(index){
  833. id = str_replace('#', '', link.attr('href'));
  834. tags_ids[id] = id;
  835. });
  836. // On adapte le lien afficher plus de résultats
  837. a_more = $('a.elements_more');
  838. a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
  839. return check_timelaps_and_find_with_tags(link, new Date().getTime(), false);
  840. }
  841. $('ul#favorite_tags a.tag').click(function(){
  842. list_tag_clicked();
  843. });
  844. last_keypress = 0;
  845. function check_timelaps_and_find_with_tags(link, time_id, timed)
  846. {
  847. if (!timed)
  848. {
  849. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  850. // et on met a jour l'id de la dernière pressé
  851. last_keypress = new Date().getTime();
  852. var this_time_id = last_keypress;
  853. }
  854. else
  855. {
  856. // Si elle a été redirigé, on met son id dans cette variable
  857. var this_time_id = time_id;
  858. }
  859. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  860. if (time_id != last_keypress && timed)
  861. {
  862. // elle disparait
  863. }
  864. else
  865. {
  866. //
  867. if ((new Date().getTime() - last_keypress) < 800 || timed == false)
  868. {
  869. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  870. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  871. // elle doit attendre au cas ou soit pressé.
  872. setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
  873. }
  874. else
  875. {
  876. // il n'y a plus a attendre, on envoie la demande de tag.
  877. return refresh_elements_with_tags_selected(link);
  878. }
  879. }
  880. return null;
  881. }
  882. });