muzich.js 31KB

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