muzich.js 29KB

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