muzich.js 30KB

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