muzich.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Scripts de Muzi.ch
  3. * Rédigé et propriété de Sevajol Bastien (http://www.bux.fr)
  4. *
  5. */
  6. // Messages flashs
  7. var myMessages = ['info','warning','error','success']; // define the messages types
  8. function hideAllMessages()
  9. {
  10. var messagesHeights = new Array(); // this array will store height for each
  11. for (i=0; i<myMessages.length; i++)
  12. {
  13. messagesHeights[i] = $('.' + myMessages[i]).outerHeight();
  14. $('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport
  15. }
  16. }
  17. $(document).ready(function(){
  18. // Initially, hide them all
  19. hideAllMessages();
  20. $('.message').animate({top:"0"}, 500);
  21. // When message is clicked, hide it
  22. $('.message a.message-close').click(function(){
  23. $(this).parent('.message').animate({top: -$(this).outerHeight()-50}, 700);
  24. return false;
  25. });
  26. });
  27. function findKeyWithValue(arrayt, value)
  28. {
  29. for(i in arrayt)
  30. {
  31. if (arrayt[i] == value)
  32. {
  33. return i;
  34. }
  35. }
  36. return "";
  37. }
  38. function json_to_array(json_string)
  39. {
  40. if (json_string.length)
  41. {
  42. return eval("(" + json_string + ")");
  43. }
  44. return new Array();
  45. }
  46. function strpos (haystack, needle, offset) {
  47. // Finds position of first occurrence of a string within another
  48. //
  49. // version: 1109.2015
  50. // discuss at: http://phpjs.org/functions/strpos // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  51. // + improved by: Onno Marsman
  52. // + bugfixed by: Daniel Esteban
  53. // + improved by: Brett Zamir (http://brett-zamir.me)
  54. // * example 1: strpos('Kevin van Zonneveld', 'e', 5); // * returns 1: 14
  55. var i = (haystack + '').indexOf(needle, (offset || 0));
  56. return i === -1 ? false : i;
  57. }
  58. /**
  59. * Converts the given data structure to a JSON string.
  60. * Argument: arr - The data structure that must be converted to JSON
  61. * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
  62. * var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
  63. * http://www.openjs.com/scripts/data/json_encode.php
  64. */
  65. function array2json(arr) {
  66. var parts = [];
  67. var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
  68. for(var key in arr) {
  69. var value = arr[key];
  70. if(typeof value == "object") { //Custom handling for arrays
  71. if(is_list) parts.push(array2json(value)); /* :RECURSION: */
  72. else parts[key] = array2json(value); /* :RECURSION: */
  73. } else {
  74. var str = "";
  75. if(!is_list) str = '"' + key + '":';
  76. //Custom handling for multiple data types
  77. if(typeof value == "number") str += value; //Numbers
  78. else if(value === false) str += 'false'; //The booleans
  79. else if(value === true) str += 'true';
  80. else str += '"' + value + '"'; //All other things
  81. // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
  82. parts.push(str);
  83. }
  84. }
  85. var json = parts.join(",");
  86. if(is_list) return '[' + json + ']';//Return numerical JSON
  87. return '{' + json + '}';//Return associative JSON
  88. }
  89. function isInteger(s) {
  90. return (s.toString().search(/^-?[0-9]+$/) == 0);
  91. }
  92. function inArray(array, p_val) {
  93. var l = array.length;
  94. for(var i = 0; i < l; i++) {
  95. if(array[i] == p_val) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. if(typeof(String.prototype.trim) === "undefined")
  102. {
  103. String.prototype.trim = function()
  104. {
  105. return String(this).replace(/^\s+|\s+$/g, '');
  106. };
  107. }
  108. function str_replace (search, replace, subject, count) {
  109. // Replaces all occurrences of search in haystack with replace
  110. //
  111. // version: 1109.2015
  112. // discuss at: http://phpjs.org/functions/str_replace // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  113. // + improved by: Gabriel Paderni
  114. // + improved by: Philip Peterson
  115. // + improved by: Simon Willison (http://simonwillison.net)
  116. // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfixed by: Anton Ongson
  117. // + input by: Onno Marsman
  118. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  119. // + tweaked by: Onno Marsman
  120. // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  121. // + input by: Oleg Eremeev
  122. // + improved by: Brett Zamir (http://brett-zamir.me)
  123. // + bugfixed by: Oleg Eremeev
  124. // % 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
  125. // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
  126. // * returns 1: 'Kevin.van.Zonneveld'
  127. // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
  128. // * returns 2: 'hemmo, mars' var i = 0,
  129. j = 0,
  130. temp = '',
  131. repl = '',
  132. sl = 0, fl = 0,
  133. f = [].concat(search),
  134. r = [].concat(replace),
  135. s = subject,
  136. ra = Object.prototype.toString.call(r) === '[object Array]', sa = Object.prototype.toString.call(s) === '[object Array]';
  137. s = [].concat(s);
  138. if (count) {
  139. this.window[count] = 0;
  140. }
  141. for (i = 0, sl = s.length; i < sl; i++) {
  142. if (s[i] === '') {
  143. continue;
  144. }for (j = 0, fl = f.length; j < fl; j++) {
  145. temp = s[i] + '';
  146. repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
  147. s[i] = (temp).split(f[j]).join(repl);
  148. if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
  149. }
  150. }
  151. }
  152. return sa ? s : s[0];
  153. }
  154. $(document).ready(function(){
  155. // Bouton de personalisation du filtre
  156. // pour le moment ce ne sotn que des redirection vers des actions
  157. $('.tags_prompt input.clear, a.filter_clear_url').live("click", function(){
  158. $(location).attr('href', $('input.filter_clear_url').val());
  159. });
  160. $('.tags_prompt input.mytags').live("click", function(){
  161. $(location).attr('href', $('input.filter_mytags_url').val());
  162. });
  163. // Affichage un/des embed
  164. $('a.element_embed_open_link').live("click", function(){
  165. $(this).parent().parent('li.element').find('a.element_embed_open_link').hide();
  166. $(this).parent().parent('li.element').find('a.element_embed_close_link').show();
  167. $(this).parent().parent('li.element').find('div.element_embed').show();
  168. return false;
  169. });
  170. // Fermeture du embed si demandé
  171. $('a.element_embed_close_link').live("click", function(){
  172. $(this).parent().parent('li.element').find('a.element_embed_open_link').show();
  173. $(this).parent().parent('li.element').find('a.element_embed_close_link').hide();
  174. $(this).parent().parent('li.element').find('div.element_embed').hide();
  175. return false;
  176. });
  177. // Mise en favoris
  178. $('a.favorite_link').live("click", function(){
  179. link = $(this);
  180. $.getJSON($(this).attr('href'), function(response) {
  181. img = link.find('img');
  182. link.attr('href', response.link_new_url);
  183. img.attr('src', response.img_new_src);
  184. img.attr('title', response.img_new_title);
  185. });
  186. return false;
  187. });
  188. // Affichage du bouton Modifier et Supprimer
  189. $('ul.elements li.element').live({
  190. mouseenter:
  191. function()
  192. {
  193. $(this).find('a.element_edit_link').show();
  194. $(this).find('a.element_remove_link').show();
  195. },
  196. mouseleave:
  197. function()
  198. {
  199. if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
  200. {
  201. $(this).find('a.element_edit_link').hide();
  202. }
  203. if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
  204. {
  205. $(this).find('a.element_remove_link').hide();
  206. }
  207. }
  208. }
  209. );
  210. // Plus d'éléments
  211. last_id = null;
  212. $('a.elements_more').click(function(){
  213. link = $(this);
  214. last_element = $('ul.elements li.element:last-child');
  215. id_last = str_replace('element_', '', last_element.attr('id'));
  216. invertcolor = 0;
  217. if (last_element.hasClass('even'))
  218. {
  219. invertcolor = 1;
  220. }
  221. $('img.elements_more_loader').show();
  222. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  223. if (response.count)
  224. {
  225. $('ul.elements').append(response.html);
  226. $('img.elements_more_loader').hide();
  227. }
  228. if (response.end || response.count < 1)
  229. {
  230. $('img.elements_more_loader').hide();
  231. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  232. response.message+'</p></div>');
  233. link.hide();
  234. }
  235. });
  236. return false;
  237. });
  238. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  239. // Filtre et affichage éléments ajax
  240. $('form[name="search"] input[type="submit"]').click(function(){
  241. $('ul.elements').html('');
  242. $('div.no_elements').hide();
  243. $('img.elements_more_loader').show();
  244. });
  245. $('form[name="search"]').ajaxForm(function(response) {
  246. $('ul.elements').html(response.html);
  247. if (response.count)
  248. {
  249. $('img.elements_more_loader').hide();
  250. $('span.elements_more').show();
  251. $('a.elements_more').show();
  252. }
  253. if (response.count < 1)
  254. {
  255. $('img.elements_more_loader').hide();
  256. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  257. response.message+'</p></div>');
  258. $('a.elements_more').hide()
  259. ;
  260. }
  261. $('ul.tagbox input[type="text"]').val(tag_box_input_value);
  262. });
  263. // Suppression d'un element
  264. $('a.element_remove_link').jConfirmAction({
  265. question : "Vraiment supprimer ?",
  266. yesAnswer : "Oui",
  267. cancelAnswer : "Non",
  268. onYes: function(link){
  269. li = link.parent('li.element');
  270. li.find('img.element_loader').show();
  271. $.getJSON(link.attr('href'), function(response){
  272. if (response.status == 'success')
  273. {
  274. li.remove();
  275. }
  276. else
  277. {
  278. li.find('img.element_loader').hide();
  279. }
  280. });
  281. return false;
  282. },
  283. onOpen: function(link){
  284. li = link.parent('li.element');
  285. li.find('a.element_edit_link').addClass('mustBeDisplayed');
  286. li.find('a.element_remove_link').addClass('mustBeDisplayed');
  287. },
  288. onClose: function(link){
  289. li = link.parent('li.element');
  290. li.find('a.element_edit_link').removeClass('mustBeDisplayed');
  291. li.find('a.element_remove_link').removeClass('mustBeDisplayed');
  292. li.find('a.element_edit_link').hide();
  293. li.find('a.element_remove_link').hide();
  294. }
  295. });
  296. elements_edited = new Array();
  297. // Ouverture du formulaire de modification
  298. $('a.element_edit_link').live('click', function(){
  299. link = $(this);
  300. li = link.parent('li.element');
  301. // On garde en mémoire l'élément édité en cas d'annulation
  302. elements_edited[li.attr('id')] = li.html();
  303. div_loader = li.find('div.loader');
  304. li.html(div_loader);
  305. li.find('img.element_loader').show();
  306. $.getJSON($(this).attr('href'), function(response) {
  307. // On prépare le tagBox
  308. li.html(response.html);
  309. var options = new Array();
  310. options.form_name = response.form_name;
  311. options.tag_init = response.tags;
  312. ajax_query_timestamp = null;
  313. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  314. // On rend ce formulaire ajaxFormable
  315. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  316. li.prepend(div_loader);
  317. li.find('img.element_loader').show();
  318. });
  319. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  320. if (response.status == 'success')
  321. {
  322. li.html(response.html);
  323. delete(elements_edited[li.attr('id')]);
  324. }
  325. else if (response.status == 'error')
  326. {
  327. li.find('img.element_loader').hide();
  328. li.find('ul.error_list').remove();
  329. ul_errors = $('<ul>').addClass('error_list');
  330. for (i in response.errors)
  331. {
  332. ul_errors.append($('<li>').append(response.errors[i]));
  333. }
  334. li.prepend(ul_errors);
  335. }
  336. });
  337. });
  338. return false;
  339. });
  340. // Annulation d'un formulaire de modification d'élément
  341. $('form.edit_element input.cancel_edit').live('click', function(){
  342. var li = $(this).parent('form').parent('li');
  343. li.html(elements_edited[li.attr('id')]);
  344. delete(elements_edited[li.attr('id')]);
  345. });
  346. ////////////////// TAG PROMPT ///////////////
  347. ajax_query_timestamp = null;
  348. tag_text_help = $('input.tag_text_help').val();
  349. // Les deux clicks ci-dessous permettent de faire disparaitre
  350. // la div de tags lorsque l'on clique ailleurs
  351. $('html').click(function() {
  352. if ($("div.search_tag_list").is(':visible'))
  353. {
  354. $("div.search_tag_list").hide();
  355. }
  356. });
  357. $("div.search_tag_list").live('click', function(event){
  358. event.stopPropagation();
  359. });
  360. function autocomplete_tag(input, form_name)
  361. {
  362. // Il doit y avoir au moin un caractère
  363. if (input.val().length > 0)
  364. {
  365. // on met en variable l'input
  366. inputTag = input;
  367. // On récupére la div de tags
  368. divtags = $("#search_tag_"+form_name);
  369. // Si la fenêtre de tags est caché
  370. if (!divtags.is(':visible'))
  371. {
  372. // On la replace
  373. position = input.position();
  374. divtags.css('left', Math.round(position.left) + 5);
  375. divtags.css('top', Math.round(position.top) + 28);
  376. // Et on l'affiche
  377. divtags.show();
  378. }
  379. // On affiche le loader
  380. $('#tag_loader_'+form_name).show();
  381. // On cache la liste de tags
  382. search_tag_list = divtags.find('ul.search_tag_list');
  383. // On supprime les anciens li
  384. search_tag_list.find('li').remove();
  385. search_tag_list.hide();
  386. // Et on affiche une info
  387. span_info = divtags.find('span.info');
  388. span_info.show();
  389. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  390. // C'est en fonction du nb de resultats qu'il sera affiché
  391. divtags.find('a.more').hide();
  392. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  393. ajax_query_timestamp = new Date().getTime();
  394. // Récupération des tags correspondants
  395. $.getJSON('/app_dev.php/fr/search/tag/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  396. // Ce contrôle permet de ne pas continuer si une requete
  397. // ajax a été faite depuis.
  398. if (data.timestamp == ajax_query_timestamp)
  399. {
  400. status = data.status;
  401. tags = data.data;
  402. // Si on spécifie une erreur
  403. if (status == 'error')
  404. {
  405. // On l'affiche a l'utilisateur
  406. span_info.text(data.error);
  407. }
  408. // Si c'est un succés
  409. else if (status == 'success')
  410. {
  411. if (tags.length > 0)
  412. {
  413. more = false;
  414. // Pour chaque tags retournés
  415. for (i in tags)
  416. {
  417. var tag_name = tags[i]['name'];
  418. var tag_id = tags[i]['id'];
  419. // On construit un li
  420. var sstr = $.trim(input.val());
  421. var re = new RegExp(sstr, "i") ;
  422. var t_string = tag_name.replace(re,"<strong>" + sstr + "</strong>");
  423. li_tag =
  424. $('<li>').append(
  425. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  426. // qui réagit quand on clique dessus
  427. .click(function(e){
  428. // On récupère le nom du tag
  429. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  430. name = name.substr(strpos(name, '#')+1, name.length);
  431. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  432. id = str_replace(name, '', id);
  433. id = str_replace('#', '', id);
  434. $('input#tags_selected_tag_'+form_name).val(id);
  435. inputTag.val(name);
  436. // Et on execute l'évènement selectTag de l'input
  437. inputTag.trigger("selectTag");
  438. // On cache la liste puisque le choix vient d'être fait
  439. divtags.hide();
  440. inputTag.val(tag_text_help);
  441. return false;
  442. })
  443. .append(t_string)
  444. );
  445. // Si on depasse les 30 tags
  446. if (i > 30)
  447. {
  448. more = true;
  449. // On le cache
  450. li_tag.hide();
  451. }
  452. // On ajout ce li a la liste
  453. search_tag_list.append(li_tag);
  454. }
  455. if (more)
  456. {
  457. divtags.find('a.more').show();
  458. }
  459. // On cache l'info
  460. span_info.hide();
  461. // Et on affiche la liste
  462. search_tag_list.show();
  463. }
  464. else
  465. {
  466. span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  467. }
  468. }
  469. // On cache le loader
  470. $('#tag_loader_'+form_name).hide();
  471. }
  472. });
  473. }
  474. }
  475. last_keypress = 0;
  476. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  477. {
  478. if (!timed)
  479. {
  480. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  481. // et on met a jour l'id de la dernière pressé
  482. last_keypress = new Date().getTime();
  483. var this_time_id = last_keypress;
  484. }
  485. else
  486. {
  487. // Si elle a été redirigé, on met son id dans cette variable
  488. var this_time_id = time_id;
  489. }
  490. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  491. if (time_id != last_keypress && timed)
  492. {
  493. // elle disparait
  494. }
  495. else
  496. {
  497. //
  498. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  499. {
  500. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  501. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  502. // elle doit attendre au cas ou soit pressé.
  503. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  504. }
  505. else
  506. {
  507. // il n'y a plus a attendre, on envoie la demande de tag.
  508. autocomplete_tag(input, form_name);
  509. }
  510. }
  511. }
  512. // Autocompletion de tags
  513. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  514. var form_name = $(this).parent('li').parent('ul.tagbox')
  515. .parent('div.tags_prompt').parent('form').attr('name')
  516. ;
  517. var code = (e.keyCode ? e.keyCode : e.which);
  518. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  519. {
  520. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  521. }
  522. });
  523. // Un click sur ce lien affiche tout les tags cachés de la liste
  524. $('div.search_tag_list a.more').live('click', function(){
  525. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  526. $(this).show();
  527. });
  528. return false;
  529. });
  530. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  531. $('ul.tagbox li.input input[type="text"]').formDefaults();
  532. ////////////////// FIN TAG PROMPT ///////////////
  533. });