muzich.js 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 (!link.hasClass('loading'))
  334. {
  335. if (img.attr('src') == '/bundles/muzichcore/img/favorite_bw.png')
  336. {
  337. img.attr('src', '/bundles/muzichcore/img/favorite.png');
  338. }
  339. else
  340. {
  341. img.attr('src', '/bundles/muzichcore/img/favorite_bw.png');
  342. }
  343. }
  344. link.addClass('loading');
  345. $.getJSON($(this).attr('href'), function(response) {
  346. if (response.status == 'mustbeconnected')
  347. {
  348. $(location).attr('href', url_index);
  349. }
  350. img = link.find('img');
  351. link.attr('href', response.link_new_url);
  352. img.attr('src', response.img_new_src);
  353. img.attr('title', response.img_new_title);
  354. link.removeClass('loading');
  355. });
  356. return false;
  357. });
  358. // Affichage du bouton Modifier et Supprimer
  359. $('ul.elements li.element').live({
  360. mouseenter:
  361. function()
  362. {
  363. $(this).find('a.element_edit_link').show();
  364. $(this).find('a.element_remove_link').show();
  365. },
  366. mouseleave:
  367. function()
  368. {
  369. if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
  370. {
  371. $(this).find('a.element_edit_link').hide();
  372. }
  373. if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
  374. {
  375. $(this).find('a.element_remove_link').hide();
  376. }
  377. }
  378. }
  379. );
  380. // Plus d'éléments
  381. last_id = null;
  382. $('a.elements_more').click(function(){
  383. link = $(this);
  384. last_element = $('ul.elements li.element:last-child');
  385. id_last = str_replace('element_', '', last_element.attr('id'));
  386. invertcolor = 0;
  387. if (last_element.hasClass('even'))
  388. {
  389. invertcolor = 1;
  390. }
  391. $('img.elements_more_loader').show();
  392. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  393. if (response.status == 'mustbeconnected')
  394. {
  395. $(location).attr('href', url_index);
  396. }
  397. if (response.count)
  398. {
  399. $('ul.elements').append(response.html);
  400. $('img.elements_more_loader').hide();
  401. }
  402. if (response.end || response.count < 1)
  403. {
  404. $('img.elements_more_loader').hide();
  405. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  406. response.message+'</p></div>');
  407. link.hide();
  408. }
  409. });
  410. return false;
  411. });
  412. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  413. // Filtre et affichage éléments ajax
  414. $('form[name="search"] input[type="submit"]').click(function(){
  415. $('ul.elements').html('');
  416. $('div.no_elements').hide();
  417. $('img.elements_more_loader').show();
  418. });
  419. $('form[name="search"]').ajaxForm(function(response) {
  420. if (response.status == 'mustbeconnected')
  421. {
  422. $(location).attr('href', url_index);
  423. }
  424. $('ul.elements').html(response.html);
  425. if (response.count)
  426. {
  427. $('img.elements_more_loader').hide();
  428. $('span.elements_more').show();
  429. $('a.elements_more').show();
  430. }
  431. if (response.count < 1)
  432. {
  433. $('img.elements_more_loader').hide();
  434. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  435. response.message+'</p></div>');
  436. $('a.elements_more').hide()
  437. ;
  438. }
  439. $('ul.tagbox input[type="text"]').val($('ul.tagbox input[type="text"]').val());
  440. });
  441. // Suppression d'un element
  442. $('a.element_remove_link').jConfirmAction({
  443. question : "Vraiment supprimer ?",
  444. yesAnswer : "Oui",
  445. cancelAnswer : "Non",
  446. onYes: function(link){
  447. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  448. li.find('img.element_loader').show();
  449. $.getJSON(link.attr('href'), function(response){
  450. if (response.status == 'mustbeconnected')
  451. {
  452. $(location).attr('href', url_index);
  453. }
  454. if (response.status == 'success')
  455. {
  456. li.remove();
  457. }
  458. else
  459. {
  460. li.find('img.element_loader').hide();
  461. }
  462. });
  463. return false;
  464. },
  465. onOpen: function(link){
  466. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  467. li.find('a.element_edit_link').addClass('mustBeDisplayed');
  468. li.find('a.element_remove_link').addClass('mustBeDisplayed');
  469. },
  470. onClose: function(link){
  471. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  472. li.find('a.element_edit_link').removeClass('mustBeDisplayed');
  473. li.find('a.element_remove_link').removeClass('mustBeDisplayed');
  474. li.find('a.element_edit_link').hide();
  475. li.find('a.element_remove_link').hide();
  476. }
  477. });
  478. elements_edited = new Array();
  479. // Ouverture du formulaire de modification
  480. $('a.element_edit_link').live('click', function(){
  481. link = $(this);
  482. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  483. // On garde en mémoire l'élément édité en cas d'annulation
  484. elements_edited[li.attr('id')] = li.html();
  485. div_loader = li.find('div.loader');
  486. li.html(div_loader);
  487. li.find('img.element_loader').show();
  488. $.getJSON($(this).attr('href'), function(response) {
  489. if (response.status == 'mustbeconnected')
  490. {
  491. $(location).attr('href', url_index);
  492. }
  493. // On prépare le tagBox
  494. li.html(response.html);
  495. // Pour le click sur l'input de saisie de tag
  496. li.find('ul.tagbox li.input input[type="text"]').formDefaults();
  497. var options = new Array();
  498. options.form_name = response.form_name;
  499. options.tag_init = response.tags;
  500. ajax_query_timestamp = null;
  501. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  502. // On rend ce formulaire ajaxFormable
  503. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  504. li.prepend(div_loader);
  505. li.find('img.element_loader').show();
  506. });
  507. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  508. if (response.status == 'mustbeconnected')
  509. {
  510. $(location).attr('href', url_index);
  511. }
  512. if (response.status == 'success')
  513. {
  514. li.html(response.html);
  515. delete(elements_edited[li.attr('id')]);
  516. }
  517. else if (response.status == 'error')
  518. {
  519. li.find('img.element_loader').hide();
  520. li.find('ul.error_list').remove();
  521. ul_errors = $('<ul>').addClass('error_list');
  522. for (i in response.errors)
  523. {
  524. ul_errors.append($('<li>').append(response.errors[i]));
  525. }
  526. li.prepend(ul_errors);
  527. }
  528. });
  529. });
  530. return false;
  531. });
  532. // Annulation d'un formulaire de modification d'élément
  533. $('form.edit_element input.cancel_edit').live('click', function(){
  534. var li = $(this).parent('form').parent('li');
  535. li.html(elements_edited[li.attr('id')]);
  536. delete(elements_edited[li.attr('id')]);
  537. });
  538. ////////////////// TAG PROMPT ///////////////
  539. ajax_query_timestamp = null;
  540. tag_text_help = $('input.tag_text_help').val();
  541. // Les deux clicks ci-dessous permettent de faire disparaitre
  542. // la div de tags lorsque l'on clique ailleurs
  543. $('html').click(function() {
  544. if ($("div.search_tag_list").is(':visible'))
  545. {
  546. $("div.search_tag_list").hide();
  547. }
  548. });
  549. $("div.search_tag_list").live('click', function(event){
  550. event.stopPropagation();
  551. });
  552. function autocomplete_tag(input, form_name)
  553. {
  554. // Il doit y avoir au moin un caractère
  555. if (input.val().length > 0)
  556. {
  557. // on met en variable l'input
  558. inputTag = input;
  559. // On récupére la div de tags
  560. divtags = $("#search_tag_"+form_name);
  561. // Si la fenêtre de tags est caché
  562. if (!divtags.is(':visible'))
  563. {
  564. // On la replace
  565. position = input.position();
  566. divtags.css('left', Math.round(position.left) + 5);
  567. divtags.css('top', Math.round(position.top) + 28);
  568. // Et on l'affiche
  569. divtags.show();
  570. }
  571. // On affiche le loader
  572. $('#tag_loader_'+form_name).show();
  573. // On cache la liste de tags
  574. search_tag_list = divtags.find('ul.search_tag_list');
  575. // On supprime les anciens li
  576. search_tag_list.find('li').remove();
  577. search_tag_list.hide();
  578. // Et on affiche une info
  579. span_info = divtags.find('span.info');
  580. span_info.show();
  581. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  582. // C'est en fonction du nb de resultats qu'il sera affiché
  583. divtags.find('a.more').hide();
  584. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  585. ajax_query_timestamp = new Date().getTime();
  586. // Récupération des tags correspondants
  587. $.getJSON(url_search_tag+'/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  588. if (data.status == 'mustbeconnected')
  589. {
  590. $(location).attr('href', url_index);
  591. }
  592. // Ce contrôle permet de ne pas continuer si une requete
  593. // ajax a été faite depuis.
  594. if (data.timestamp == ajax_query_timestamp)
  595. {
  596. status = data.status;
  597. tags = data.data;
  598. // Si on spécifie une erreur
  599. if (status == 'error')
  600. {
  601. // On l'affiche a l'utilisateur
  602. span_info.text(data.error);
  603. }
  604. // Si c'est un succés
  605. else if (status == 'success')
  606. {
  607. if (tags.length > 0)
  608. {
  609. more = false;
  610. // Pour chaque tags retournés
  611. for (i in tags)
  612. {
  613. var tag_name = tags[i]['name'];
  614. var tag_id = tags[i]['id'];
  615. var t_string = tag_name
  616. // On construit un li
  617. string_exploded = explode(' ', $.trim(input.val()));
  618. for (n in string_exploded)
  619. {
  620. r_string = string_exploded[n];
  621. var re = new RegExp(r_string, "i");
  622. t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
  623. }
  624. li_tag =
  625. $('<li>').append(
  626. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  627. // qui réagit quand on clique dessus
  628. .click(function(e){
  629. // On récupère le nom du tag
  630. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  631. name = name.substr(strpos(name, '#')+1, name.length);
  632. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  633. id = str_replace(name, '', id);
  634. id = str_replace('#', '', id);
  635. $('input#tags_selected_tag_'+form_name).val(id);
  636. inputTag.val(name);
  637. // Et on execute l'évènement selectTag de l'input
  638. inputTag.trigger("selectTag");
  639. // On cache la liste puisque le choix vient d'être fait
  640. divtags.hide();
  641. inputTag.val(tag_text_help);
  642. return false;
  643. })
  644. .append(t_string)
  645. );
  646. // Si on depasse les 30 tags
  647. if (i > 30)
  648. {
  649. more = true;
  650. // On le cache
  651. li_tag.hide();
  652. }
  653. // On ajout ce li a la liste
  654. search_tag_list.append(li_tag);
  655. }
  656. if (more)
  657. {
  658. divtags.find('a.more').show();
  659. }
  660. // On cache l'info
  661. span_info.hide();
  662. // Et on affiche la liste
  663. search_tag_list.show();
  664. }
  665. else
  666. {
  667. span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  668. }
  669. }
  670. // On cache le loader
  671. $('#tag_loader_'+form_name).hide();
  672. }
  673. });
  674. }
  675. }
  676. last_keypress = 0;
  677. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  678. {
  679. if (!timed)
  680. {
  681. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  682. // et on met a jour l'id de la dernière pressé
  683. last_keypress = new Date().getTime();
  684. var this_time_id = last_keypress;
  685. }
  686. else
  687. {
  688. // Si elle a été redirigé, on met son id dans cette variable
  689. var this_time_id = time_id;
  690. }
  691. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  692. if (time_id != last_keypress && timed)
  693. {
  694. // elle disparait
  695. }
  696. else
  697. {
  698. //
  699. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  700. {
  701. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  702. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  703. // elle doit attendre au cas ou soit pressé.
  704. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  705. }
  706. else
  707. {
  708. // il n'y a plus a attendre, on envoie la demande de tag.
  709. autocomplete_tag(input, form_name);
  710. }
  711. }
  712. }
  713. // Autocompletion de tags
  714. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  715. var form_name = $(this).parent('li').parent('ul.tagbox')
  716. .parent('div.tags_prompt').parent('form').attr('name')
  717. ;
  718. var code = (e.keyCode ? e.keyCode : e.which);
  719. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  720. {
  721. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  722. }
  723. });
  724. // Un click sur ce lien affiche tout les tags cachés de la liste
  725. $('div.search_tag_list a.more').live('click', function(){
  726. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  727. $(this).show();
  728. });
  729. return false;
  730. });
  731. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  732. $('ul.tagbox li.input input[type="text"]').formDefaults();
  733. ////////////////// FIN TAG PROMPT ///////////////
  734. // Suppression d'un element
  735. $('a.group_remove_link').jConfirmAction({
  736. question : "Supprimer ce groupe ?",
  737. yesAnswer : "Oui",
  738. cancelAnswer : "Non",
  739. onYes: function(link){
  740. window.location = link.attr('href');
  741. return false;
  742. },
  743. onOpen: function(){},
  744. onClose: function(){}
  745. });
  746. // Selection Réseau global / Mon réseau
  747. $('div.select_network a').live('click', function(){
  748. divSelect = $(this).parent('div');
  749. if ($(this).hasClass('all_network'))
  750. {
  751. divSelect.find('a.all_network').addClass('active');
  752. divSelect.find('a.my_network').removeClass('active');
  753. divSelect.find('select').val('network_public');
  754. }
  755. else
  756. {
  757. divSelect.find('a.my_network').addClass('active');
  758. divSelect.find('a.all_network').removeClass('active');
  759. divSelect.find('select').val('network_personal');
  760. }
  761. });
  762. // Ajout d'un element
  763. $('form[name="add"] input[type="submit"]').live('click', function(){
  764. $('form[name="add"]').find('img.tag_loader').show();
  765. });
  766. $('form[name="add"]').ajaxForm(function(response) {
  767. if (response.status == 'mustbeconnected')
  768. {
  769. $(location).attr('href', url_index);
  770. }
  771. $('form[name="add"] img.tag_loader').hide();
  772. if (response.status == 'success')
  773. {
  774. $('form[name="add"]').find('ul.error_list').remove();
  775. $('ul.elements').prepend(response.html);
  776. $('form[name="add"] input[type="text"]').val('');
  777. $('div#element_add_box').slideUp();
  778. $('a#element_add_link').show();
  779. if ($('form[name="search"]').length)
  780. {
  781. $('form[name="search"]').slideDown();
  782. }
  783. remove_tags('add');
  784. }
  785. else if (response.status == 'error')
  786. {
  787. $('form[name="add"]').find('ul.error_list').remove();
  788. ul_errors = $('<ul>').addClass('error_list');
  789. for (i in response.errors)
  790. {
  791. ul_errors.append($('<li>').append(response.errors[i]));
  792. }
  793. $('form[name="add"]').prepend(ul_errors);
  794. }
  795. return false;
  796. });
  797. // Check périodique
  798. // TODO.
  799. /////////////////////
  800. // Filtre par tags (show, favorite)
  801. function refresh_elements_with_tags_selected(link)
  802. {
  803. // Puis on fait notre rekékéte ajax.
  804. $('ul.elements').html('');
  805. $('div.no_elements').hide();
  806. $('img.elements_more_loader').show();
  807. $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
  808. if (response.status == 'mustbeconnected')
  809. {
  810. $(location).attr('href', url_index);
  811. }
  812. $('ul.elements').html(response.html);
  813. if (response.count)
  814. {
  815. $('img.elements_more_loader').hide();
  816. $('span.elements_more').show();
  817. $('a.elements_more').show();
  818. }
  819. });
  820. return false;
  821. }
  822. function list_tag_clicked(link, erease)
  823. {
  824. if (erease)
  825. {
  826. $('ul#favorite_tags a.tag').removeClass('active');
  827. }
  828. // Ensuite on l'active ou le désactive
  829. if (link.hasClass('active'))
  830. {
  831. link.removeClass('active');
  832. }
  833. else
  834. {
  835. link.addClass('active');
  836. }
  837. // On construit notre liste de tags
  838. tags_ids = new Array();
  839. $('ul#favorite_tags a.tag.active').each(function(index){
  840. id = str_replace('#', '', link.attr('href'));
  841. tags_ids[id] = id;
  842. });
  843. // On adapte le lien afficher plus de résultats
  844. a_more = $('a.elements_more');
  845. a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
  846. return check_timelaps_and_find_with_tags(link, new Date().getTime(), false);
  847. }
  848. $('ul#favorite_tags a.tag').click(function(){
  849. list_tag_clicked();
  850. });
  851. last_keypress = 0;
  852. function check_timelaps_and_find_with_tags(link, time_id, timed)
  853. {
  854. if (!timed)
  855. {
  856. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  857. // et on met a jour l'id de la dernière pressé
  858. last_keypress = new Date().getTime();
  859. var this_time_id = last_keypress;
  860. }
  861. else
  862. {
  863. // Si elle a été redirigé, on met son id dans cette variable
  864. var this_time_id = time_id;
  865. }
  866. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  867. if (time_id != last_keypress && timed)
  868. {
  869. // elle disparait
  870. }
  871. else
  872. {
  873. //
  874. if ((new Date().getTime() - last_keypress) < 800 || timed == false)
  875. {
  876. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  877. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  878. // elle doit attendre au cas ou soit pressé.
  879. setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
  880. }
  881. else
  882. {
  883. // il n'y a plus a attendre, on envoie la demande de tag.
  884. return refresh_elements_with_tags_selected(link);
  885. }
  886. }
  887. return null;
  888. }
  889. });