muzich.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. $(document).ready(function(){
  190. // Controle du focus sur la page
  191. function onBlur() {
  192. document.body.className = 'blurred';
  193. }
  194. function onFocus(){
  195. document.body.className = 'focused';
  196. }
  197. if (/*@cc_on!@*/false) { // check for Internet Explorer
  198. document.onfocusin = onFocus;
  199. document.onfocusout = onBlur;
  200. } else {
  201. window.onfocus = onFocus;
  202. window.onblur = onBlur;
  203. }
  204. // Bouton de personalisation du filtre
  205. // pour le moment ce ne sotn que des redirection vers des actions
  206. $('.tags_prompt input.clear, a.filter_clear_url').live("click", function(){
  207. $(location).attr('href', $('input.filter_clear_url').val());
  208. });
  209. $('.tags_prompt input.mytags').live("click", function(){
  210. $(location).attr('href', $('input.filter_mytags_url').val());
  211. });
  212. // Affichage un/des embed
  213. // 1328283150_media-playback-start.png
  214. // 1328283201_emblem-symbolic-link.png
  215. $('a.element_embed_open_link').live("click", function(){
  216. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  217. li.find('a.element_embed_close_link').show();
  218. li.find('a.element_embed_open_link_text').hide();
  219. li.find('div.element_embed').show();
  220. return false;
  221. });
  222. $('a.element_name_embed_open_link').live("click", function(){
  223. li = $(this).parent('span').parent('td').parent('tr').parent().parent().parent('li.element');
  224. li.find('a.element_embed_close_link').show();
  225. li.find('a.element_embed_open_link_text').hide();
  226. li.find('div.element_embed').show();
  227. return false;
  228. });
  229. // Fermeture du embed si demandé
  230. $('a.element_embed_close_link').live("click", function(){
  231. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  232. li.find('div.element_embed').hide();
  233. li.find('a.element_embed_open_link_text').show();
  234. $(this).hide();
  235. return false;
  236. });
  237. // Affichage du "play" ou du "open" (image png)
  238. $('li.element a.a_thumbnail, li.element img.open, li.element img.play').live({
  239. mouseenter:
  240. function()
  241. {
  242. td = $(this).parent('td');
  243. a = td.find('a.a_thumbnail');
  244. if (a.hasClass('embed'))
  245. {
  246. td.find('img.play').show();
  247. }
  248. else
  249. {
  250. td.find('img.open').show();
  251. }
  252. },
  253. mouseleave:
  254. function()
  255. {
  256. td = $(this).parent('td');
  257. a = td.find('a.a_thumbnail');
  258. if (a.hasClass('embed'))
  259. {
  260. td.find('img.play').hide();
  261. }
  262. else
  263. {
  264. td.find('img.open').hide();
  265. }
  266. }
  267. }
  268. );
  269. // Mise en favoris
  270. $('a.favorite_link').live("click", function(){
  271. link = $(this);
  272. $.getJSON($(this).attr('href'), function(response) {
  273. if (response.status == 'mustbeconnected')
  274. {
  275. $(location).attr('href', url_index);
  276. }
  277. img = link.find('img');
  278. link.attr('href', response.link_new_url);
  279. img.attr('src', response.img_new_src);
  280. img.attr('title', response.img_new_title);
  281. });
  282. return false;
  283. });
  284. // Affichage du bouton Modifier et Supprimer
  285. $('ul.elements li.element').live({
  286. mouseenter:
  287. function()
  288. {
  289. $(this).find('a.element_edit_link').show();
  290. $(this).find('a.element_remove_link').show();
  291. },
  292. mouseleave:
  293. function()
  294. {
  295. if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
  296. {
  297. $(this).find('a.element_edit_link').hide();
  298. }
  299. if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
  300. {
  301. $(this).find('a.element_remove_link').hide();
  302. }
  303. }
  304. }
  305. );
  306. // Plus d'éléments
  307. last_id = null;
  308. $('a.elements_more').click(function(){
  309. link = $(this);
  310. last_element = $('ul.elements li.element:last-child');
  311. id_last = str_replace('element_', '', last_element.attr('id'));
  312. invertcolor = 0;
  313. if (last_element.hasClass('even'))
  314. {
  315. invertcolor = 1;
  316. }
  317. $('img.elements_more_loader').show();
  318. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  319. if (response.status == 'mustbeconnected')
  320. {
  321. $(location).attr('href', url_index);
  322. }
  323. if (response.count)
  324. {
  325. $('ul.elements').append(response.html);
  326. $('img.elements_more_loader').hide();
  327. }
  328. if (response.end || response.count < 1)
  329. {
  330. $('img.elements_more_loader').hide();
  331. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  332. response.message+'</p></div>');
  333. link.hide();
  334. }
  335. });
  336. return false;
  337. });
  338. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  339. // Filtre et affichage éléments ajax
  340. $('form[name="search"] input[type="submit"]').click(function(){
  341. $('ul.elements').html('');
  342. $('div.no_elements').hide();
  343. $('img.elements_more_loader').show();
  344. });
  345. $('form[name="search"]').ajaxForm(function(response) {
  346. if (response.status == 'mustbeconnected')
  347. {
  348. $(location).attr('href', url_index);
  349. }
  350. $('ul.elements').html(response.html);
  351. if (response.count)
  352. {
  353. $('img.elements_more_loader').hide();
  354. $('span.elements_more').show();
  355. $('a.elements_more').show();
  356. }
  357. if (response.count < 1)
  358. {
  359. $('img.elements_more_loader').hide();
  360. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  361. response.message+'</p></div>');
  362. $('a.elements_more').hide()
  363. ;
  364. }
  365. $('ul.tagbox input[type="text"]').val($('ul.tagbox input[type="text"]').val());
  366. });
  367. // Suppression d'un element
  368. $('a.element_remove_link').jConfirmAction({
  369. question : "Vraiment supprimer ?",
  370. yesAnswer : "Oui",
  371. cancelAnswer : "Non",
  372. onYes: function(link){
  373. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  374. li.find('img.element_loader').show();
  375. $.getJSON(link.attr('href'), function(response){
  376. if (response.status == 'mustbeconnected')
  377. {
  378. $(location).attr('href', url_index);
  379. }
  380. if (response.status == 'success')
  381. {
  382. li.remove();
  383. }
  384. else
  385. {
  386. li.find('img.element_loader').hide();
  387. }
  388. });
  389. return false;
  390. },
  391. onOpen: function(link){
  392. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  393. li.find('a.element_edit_link').addClass('mustBeDisplayed');
  394. li.find('a.element_remove_link').addClass('mustBeDisplayed');
  395. },
  396. onClose: function(link){
  397. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  398. li.find('a.element_edit_link').removeClass('mustBeDisplayed');
  399. li.find('a.element_remove_link').removeClass('mustBeDisplayed');
  400. li.find('a.element_edit_link').hide();
  401. li.find('a.element_remove_link').hide();
  402. }
  403. });
  404. elements_edited = new Array();
  405. // Ouverture du formulaire de modification
  406. $('a.element_edit_link').live('click', function(){
  407. link = $(this);
  408. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  409. // On garde en mémoire l'élément édité en cas d'annulation
  410. elements_edited[li.attr('id')] = li.html();
  411. div_loader = li.find('div.loader');
  412. li.html(div_loader);
  413. li.find('img.element_loader').show();
  414. $.getJSON($(this).attr('href'), function(response) {
  415. if (response.status == 'mustbeconnected')
  416. {
  417. $(location).attr('href', url_index);
  418. }
  419. // On prépare le tagBox
  420. li.html(response.html);
  421. var options = new Array();
  422. options.form_name = response.form_name;
  423. options.tag_init = response.tags;
  424. ajax_query_timestamp = null;
  425. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  426. // On rend ce formulaire ajaxFormable
  427. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  428. li.prepend(div_loader);
  429. li.find('img.element_loader').show();
  430. });
  431. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  432. if (response.status == 'mustbeconnected')
  433. {
  434. $(location).attr('href', url_index);
  435. }
  436. if (response.status == 'success')
  437. {
  438. li.html(response.html);
  439. delete(elements_edited[li.attr('id')]);
  440. }
  441. else if (response.status == 'error')
  442. {
  443. li.find('img.element_loader').hide();
  444. li.find('ul.error_list').remove();
  445. ul_errors = $('<ul>').addClass('error_list');
  446. for (i in response.errors)
  447. {
  448. ul_errors.append($('<li>').append(response.errors[i]));
  449. }
  450. li.prepend(ul_errors);
  451. }
  452. });
  453. });
  454. return false;
  455. });
  456. // Annulation d'un formulaire de modification d'élément
  457. $('form.edit_element input.cancel_edit').live('click', function(){
  458. var li = $(this).parent('form').parent('li');
  459. li.html(elements_edited[li.attr('id')]);
  460. delete(elements_edited[li.attr('id')]);
  461. });
  462. ////////////////// TAG PROMPT ///////////////
  463. ajax_query_timestamp = null;
  464. tag_text_help = $('input.tag_text_help').val();
  465. // Les deux clicks ci-dessous permettent de faire disparaitre
  466. // la div de tags lorsque l'on clique ailleurs
  467. $('html').click(function() {
  468. if ($("div.search_tag_list").is(':visible'))
  469. {
  470. $("div.search_tag_list").hide();
  471. }
  472. });
  473. $("div.search_tag_list").live('click', function(event){
  474. event.stopPropagation();
  475. });
  476. function autocomplete_tag(input, form_name)
  477. {
  478. // Il doit y avoir au moin un caractère
  479. if (input.val().length > 0)
  480. {
  481. // on met en variable l'input
  482. inputTag = input;
  483. // On récupére la div de tags
  484. divtags = $("#search_tag_"+form_name);
  485. // Si la fenêtre de tags est caché
  486. if (!divtags.is(':visible'))
  487. {
  488. // On la replace
  489. position = input.position();
  490. divtags.css('left', Math.round(position.left) + 5);
  491. divtags.css('top', Math.round(position.top) + 28);
  492. // Et on l'affiche
  493. divtags.show();
  494. }
  495. // On affiche le loader
  496. $('#tag_loader_'+form_name).show();
  497. // On cache la liste de tags
  498. search_tag_list = divtags.find('ul.search_tag_list');
  499. // On supprime les anciens li
  500. search_tag_list.find('li').remove();
  501. search_tag_list.hide();
  502. // Et on affiche une info
  503. span_info = divtags.find('span.info');
  504. span_info.show();
  505. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  506. // C'est en fonction du nb de resultats qu'il sera affiché
  507. divtags.find('a.more').hide();
  508. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  509. ajax_query_timestamp = new Date().getTime();
  510. // Récupération des tags correspondants
  511. $.getJSON(url_search_tag+'/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  512. if (data.status == 'mustbeconnected')
  513. {
  514. $(location).attr('href', url_index);
  515. }
  516. // Ce contrôle permet de ne pas continuer si une requete
  517. // ajax a été faite depuis.
  518. if (data.timestamp == ajax_query_timestamp)
  519. {
  520. status = data.status;
  521. tags = data.data;
  522. // Si on spécifie une erreur
  523. if (status == 'error')
  524. {
  525. // On l'affiche a l'utilisateur
  526. span_info.text(data.error);
  527. }
  528. // Si c'est un succés
  529. else if (status == 'success')
  530. {
  531. if (tags.length > 0)
  532. {
  533. more = false;
  534. // Pour chaque tags retournés
  535. for (i in tags)
  536. {
  537. var tag_name = tags[i]['name'];
  538. var tag_id = tags[i]['id'];
  539. var t_string = tag_name
  540. // On construit un li
  541. string_exploded = explode(' ', $.trim(input.val()));
  542. for (n in string_exploded)
  543. {
  544. r_string = string_exploded[n];
  545. var re = new RegExp(r_string, "i");
  546. t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
  547. }
  548. li_tag =
  549. $('<li>').append(
  550. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  551. // qui réagit quand on clique dessus
  552. .click(function(e){
  553. // On récupère le nom du tag
  554. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  555. name = name.substr(strpos(name, '#')+1, name.length);
  556. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  557. id = str_replace(name, '', id);
  558. id = str_replace('#', '', id);
  559. $('input#tags_selected_tag_'+form_name).val(id);
  560. inputTag.val(name);
  561. // Et on execute l'évènement selectTag de l'input
  562. inputTag.trigger("selectTag");
  563. // On cache la liste puisque le choix vient d'être fait
  564. divtags.hide();
  565. inputTag.val(tag_text_help);
  566. return false;
  567. })
  568. .append(t_string)
  569. );
  570. // Si on depasse les 30 tags
  571. if (i > 30)
  572. {
  573. more = true;
  574. // On le cache
  575. li_tag.hide();
  576. }
  577. // On ajout ce li a la liste
  578. search_tag_list.append(li_tag);
  579. }
  580. if (more)
  581. {
  582. divtags.find('a.more').show();
  583. }
  584. // On cache l'info
  585. span_info.hide();
  586. // Et on affiche la liste
  587. search_tag_list.show();
  588. }
  589. else
  590. {
  591. span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  592. }
  593. }
  594. // On cache le loader
  595. $('#tag_loader_'+form_name).hide();
  596. }
  597. });
  598. }
  599. }
  600. last_keypress = 0;
  601. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  602. {
  603. if (!timed)
  604. {
  605. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  606. // et on met a jour l'id de la dernière pressé
  607. last_keypress = new Date().getTime();
  608. var this_time_id = last_keypress;
  609. }
  610. else
  611. {
  612. // Si elle a été redirigé, on met son id dans cette variable
  613. var this_time_id = time_id;
  614. }
  615. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  616. if (time_id != last_keypress && timed)
  617. {
  618. // elle disparait
  619. }
  620. else
  621. {
  622. //
  623. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  624. {
  625. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  626. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  627. // elle doit attendre au cas ou soit pressé.
  628. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  629. }
  630. else
  631. {
  632. // il n'y a plus a attendre, on envoie la demande de tag.
  633. autocomplete_tag(input, form_name);
  634. }
  635. }
  636. }
  637. // Autocompletion de tags
  638. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  639. var form_name = $(this).parent('li').parent('ul.tagbox')
  640. .parent('div.tags_prompt').parent('form').attr('name')
  641. ;
  642. var code = (e.keyCode ? e.keyCode : e.which);
  643. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  644. {
  645. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  646. }
  647. });
  648. // Un click sur ce lien affiche tout les tags cachés de la liste
  649. $('div.search_tag_list a.more').live('click', function(){
  650. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  651. $(this).show();
  652. });
  653. return false;
  654. });
  655. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  656. $('ul.tagbox li.input input[type="text"]').formDefaults();
  657. ////////////////// FIN TAG PROMPT ///////////////
  658. // Suppression d'un element
  659. $('a.group_remove_link').jConfirmAction({
  660. question : "Supprimer ce groupe ?",
  661. yesAnswer : "Oui",
  662. cancelAnswer : "Non",
  663. onYes: function(link){
  664. window.location = link.attr('href');
  665. return false;
  666. },
  667. onOpen: function(){},
  668. onClose: function(){}
  669. });
  670. // Selection Réseau global / Mon réseau
  671. $('div.select_network a').live('click', function(){
  672. divSelect = $(this).parent('div');
  673. if ($(this).hasClass('all_network'))
  674. {
  675. divSelect.find('a.all_network').addClass('active');
  676. divSelect.find('a.my_network').removeClass('active');
  677. divSelect.find('select').val('network_public');
  678. }
  679. else
  680. {
  681. divSelect.find('a.my_network').addClass('active');
  682. divSelect.find('a.all_network').removeClass('active');
  683. divSelect.find('select').val('network_personal');
  684. }
  685. });
  686. // Ajout d'un element
  687. $('form[name="add"] input[type="submit"]').live('click', function(){
  688. $('form[name="add"]').find('img.tag_loader').show();
  689. });
  690. $('form[name="add"]').ajaxForm(function(response) {
  691. if (response.status == 'mustbeconnected')
  692. {
  693. $(location).attr('href', url_index);
  694. }
  695. $('form[name="add"] img.tag_loader').hide();
  696. if (response.status == 'success')
  697. {
  698. $('form[name="add"]').find('ul.error_list').remove();
  699. $('ul.elements').prepend(response.html);
  700. $('form[name="add"] input[type="text"]').val('');
  701. $('div#element_add_box').slideUp();
  702. $('a#element_add_link').show();
  703. if ($('form[name="search"]').length)
  704. {
  705. $('form[name="search"]').slideDown();
  706. }
  707. }
  708. else if (response.status == 'error')
  709. {
  710. $('form[name="add"]').find('ul.error_list').remove();
  711. ul_errors = $('<ul>').addClass('error_list');
  712. for (i in response.errors)
  713. {
  714. ul_errors.append($('<li>').append(response.errors[i]));
  715. }
  716. $('form[name="add"]').prepend(ul_errors);
  717. }
  718. return false;
  719. });
  720. // Check périodique
  721. // TODO.
  722. /////////////////////
  723. // Filtre par tags (show, favorite)
  724. function refresh_elements_with_tags_selected(link)
  725. {
  726. // Puis on fait notre rekékéte ajax.
  727. $('ul.elements').html('');
  728. $('div.no_elements').hide();
  729. $('img.elements_more_loader').show();
  730. $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
  731. if (response.status == 'mustbeconnected')
  732. {
  733. $(location).attr('href', url_index);
  734. }
  735. $('ul.elements').html(response.html);
  736. if (response.count)
  737. {
  738. $('img.elements_more_loader').hide();
  739. $('span.elements_more').show();
  740. $('a.elements_more').show();
  741. }
  742. });
  743. return false;
  744. }
  745. $('ul#favorite_tags a.tag').click(function(){
  746. // Ensuite on l'active ou le désactive'
  747. if ($(this).hasClass('active'))
  748. {
  749. $(this).removeClass('active');
  750. }
  751. else
  752. {
  753. $(this).addClass('active');
  754. }
  755. // On construit notre liste de tags
  756. tags_ids = new Array();
  757. $('ul#favorite_tags a.tag.active').each(function(index){
  758. id = str_replace('#', '', $(this).attr('href'));
  759. tags_ids[id] = id;
  760. });
  761. // On adapte le lien afficher plus de résultats
  762. a_more = $('a.elements_more');
  763. a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
  764. return check_timelaps_and_find_with_tags($(this), new Date().getTime(), false);
  765. });
  766. last_keypress = 0;
  767. function check_timelaps_and_find_with_tags(link, time_id, timed)
  768. {
  769. if (!timed)
  770. {
  771. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  772. // et on met a jour l'id de la dernière pressé
  773. last_keypress = new Date().getTime();
  774. var this_time_id = last_keypress;
  775. }
  776. else
  777. {
  778. // Si elle a été redirigé, on met son id dans cette variable
  779. var this_time_id = time_id;
  780. }
  781. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  782. if (time_id != last_keypress && timed)
  783. {
  784. // elle disparait
  785. }
  786. else
  787. {
  788. //
  789. if ((new Date().getTime() - last_keypress) < 800 || timed == false)
  790. {
  791. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  792. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  793. // elle doit attendre au cas ou soit pressé.
  794. setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
  795. }
  796. else
  797. {
  798. // il n'y a plus a attendre, on envoie la demande de tag.
  799. return refresh_elements_with_tags_selected(link);
  800. }
  801. }
  802. return null;
  803. }
  804. });