muzich.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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
  189. $('ul.elements li.element').live({
  190. mouseenter:
  191. function()
  192. {
  193. $(this).find('a.element_edit_link').show();
  194. },
  195. mouseleave:
  196. function()
  197. {
  198. $(this).find('a.element_edit_link').hide();
  199. }
  200. }
  201. );
  202. // Plus d'éléments
  203. last_id = null;
  204. $('a.elements_more').click(function(){
  205. link = $(this);
  206. last_element = $('ul.elements li.element:last-child');
  207. id_last = str_replace('element_', '', last_element.attr('id'));
  208. invertcolor = 0;
  209. if (last_element.hasClass('even'))
  210. {
  211. invertcolor = 1;
  212. }
  213. $('img.elements_more_loader').show();
  214. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  215. if (response.count)
  216. {
  217. $('ul.elements').append(response.html);
  218. $('img.elements_more_loader').hide();
  219. }
  220. if (response.end || response.count < 1)
  221. {
  222. $('img.elements_more_loader').hide();
  223. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  224. response.message+'</p></div>');
  225. link.hide();
  226. }
  227. });
  228. return false;
  229. });
  230. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  231. // Filtre et affichage éléments ajax
  232. $('form[name="search"] input[type="submit"]').click(function(){
  233. $('ul.elements').html('');
  234. $('div.no_elements').hide();
  235. $('img.elements_more_loader').show();
  236. });
  237. $('form[name="search"]').ajaxForm(function(response) {
  238. $('ul.elements').html(response.html);
  239. if (response.count)
  240. {
  241. $('img.elements_more_loader').hide();
  242. $('span.elements_more').show();
  243. $('a.elements_more').show();
  244. }
  245. if (response.count < 1)
  246. {
  247. $('img.elements_more_loader').hide();
  248. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  249. response.message+'</p></div>');
  250. $('a.elements_more').hide()
  251. ;
  252. }
  253. $('ul.tagbox input[type="text"]').val(tag_box_input_value);
  254. });
  255. // Ouverture du formulaire de modification
  256. $('a.element_edit_link').live('click', function(){
  257. link = $(this);
  258. li = link.parent('li.element');
  259. div_loader = li.find('div.loader');
  260. li.html(div_loader);
  261. li.find('img.element_loader').show();
  262. $.getJSON($(this).attr('href'), function(response) {
  263. // On prépare le tagBox
  264. li.html(response.html);
  265. var options = new Array();
  266. options.form_name = response.form_name;
  267. options.tag_init = response.tags;
  268. ajax_query_timestamp = null;
  269. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  270. // On rend ce formulaire ajaxFormable
  271. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  272. li.prepend(div_loader);
  273. li.find('img.element_loader').show();
  274. });
  275. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  276. if (response.status == 'success')
  277. {
  278. li.html(response.html);
  279. }
  280. else if (response.status == 'error')
  281. {
  282. li.find('img.element_loader').hide();
  283. li.find('ul.error_list').remove();
  284. ul_errors = $('<ul>').addClass('error_list');
  285. for (i in response.errors)
  286. {
  287. ul_errors.append($('<li>').append(response.errors[i]));
  288. }
  289. li.prepend(ul_errors);
  290. }
  291. });
  292. });
  293. return false;
  294. });
  295. ////////////////// TAG PROMPT ///////////////
  296. ajax_query_timestamp = null;
  297. tag_text_help = $('input.tag_text_help').val();
  298. // Les deux clicks ci-dessous permettent de faire disparaitre
  299. // la div de tags lorsque l'on clique ailleurs
  300. $('html').click(function() {
  301. if ($("div.search_tag_list").is(':visible'))
  302. {
  303. $("div.search_tag_list").hide();
  304. }
  305. });
  306. $("div.search_tag_list").live('click', function(event){
  307. event.stopPropagation();
  308. });
  309. function autocomplete_tag(input, form_name)
  310. {
  311. // Il doit y avoir au moin un caractère
  312. if (input.val().length > 0)
  313. {
  314. // on met en variable l'input
  315. inputTag = input;
  316. // On récupére la div de tags
  317. divtags = $("#search_tag_"+form_name);
  318. // Si la fenêtre de tags est caché
  319. if (!divtags.is(':visible'))
  320. {
  321. // On la replace
  322. position = input.position();
  323. divtags.css('left', Math.round(position.left) + 5);
  324. divtags.css('top', Math.round(position.top) + 28);
  325. // Et on l'affiche
  326. divtags.show();
  327. }
  328. // On affiche le loader
  329. $('#tag_loader_'+form_name).show();
  330. // On cache la liste de tags
  331. search_tag_list = divtags.find('ul.search_tag_list');
  332. // On supprime les anciens li
  333. search_tag_list.find('li').remove();
  334. search_tag_list.hide();
  335. // Et on affiche une info
  336. span_info = divtags.find('span.info');
  337. span_info.show();
  338. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  339. // C'est en fonction du nb de resultats qu'il sera affiché
  340. divtags.find('a.more').hide();
  341. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  342. ajax_query_timestamp = new Date().getTime();
  343. // Récupération des tags correspondants
  344. $.getJSON('/app_dev.php/fr/search/tag/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  345. // Ce contrôle permet de ne pas continuer si une requete
  346. // ajax a été faite depuis.
  347. if (data.timestamp == ajax_query_timestamp)
  348. {
  349. status = data.status;
  350. tags = data.data;
  351. // Si on spécifie une erreur
  352. if (status == 'error')
  353. {
  354. // On l'affiche a l'utilisateur
  355. span_info.text(data.error);
  356. }
  357. // Si c'est un succés
  358. else if (status == 'success')
  359. {
  360. if (tags.length > 0)
  361. {
  362. more = false;
  363. // Pour chaque tags retournés
  364. for (i in tags)
  365. {
  366. var tag_name = tags[i]['name'];
  367. var tag_id = tags[i]['id'];
  368. // On construit un li
  369. var sstr = $.trim(input.val());
  370. var re = new RegExp(sstr, "i") ;
  371. var t_string = tag_name.replace(re,"<strong>" + sstr + "</strong>");
  372. li_tag =
  373. $('<li>').append(
  374. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  375. // qui réagit quand on clique dessus
  376. .click(function(e){
  377. // On récupère le nom du tag
  378. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  379. name = name.substr(strpos(name, '#')+1, name.length);
  380. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  381. id = str_replace(name, '', id);
  382. id = str_replace('#', '', id);
  383. $('input#tags_selected_tag_'+form_name).val(id);
  384. inputTag.val(name);
  385. // Et on execute l'évènement selectTag de l'input
  386. inputTag.trigger("selectTag");
  387. // On cache la liste puisque le choix vient d'être fait
  388. divtags.hide();
  389. inputTag.val(tag_text_help);
  390. return false;
  391. })
  392. .append(t_string)
  393. );
  394. // Si on depasse les 30 tags
  395. if (i > 30)
  396. {
  397. more = true;
  398. // On le cache
  399. li_tag.hide();
  400. }
  401. // On ajout ce li a la liste
  402. search_tag_list.append(li_tag);
  403. }
  404. if (more)
  405. {
  406. divtags.find('a.more').show();
  407. }
  408. // On cache l'info
  409. span_info.hide();
  410. // Et on affiche la liste
  411. search_tag_list.show();
  412. }
  413. else
  414. {
  415. span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  416. }
  417. }
  418. // On cache le loader
  419. $('#tag_loader_'+form_name).hide();
  420. }
  421. });
  422. }
  423. }
  424. last_keypress = 0;
  425. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  426. {
  427. if (!timed)
  428. {
  429. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  430. // et on met a jour l'id de la dernière pressé
  431. last_keypress = new Date().getTime();
  432. var this_time_id = last_keypress;
  433. }
  434. else
  435. {
  436. // Si elle a été redirigé, on met son id dans cette variable
  437. var this_time_id = time_id;
  438. }
  439. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  440. if (time_id != last_keypress && timed)
  441. {
  442. // elle disparait
  443. }
  444. else
  445. {
  446. //
  447. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  448. {
  449. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  450. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  451. // elle doit attendre au cas ou soit pressé.
  452. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  453. }
  454. else
  455. {
  456. // il n'y a plus a attendre, on envoie la demande de tag.
  457. autocomplete_tag(input, form_name);
  458. }
  459. }
  460. }
  461. // Autocompletion de tags
  462. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  463. var form_name = $(this).parent('li').parent('ul.tagbox')
  464. .parent('div.tags_prompt').parent('form').attr('name')
  465. ;
  466. var code = (e.keyCode ? e.keyCode : e.which);
  467. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  468. {
  469. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  470. }
  471. });
  472. // Un click sur ce lien affiche tout les tags cachés de la liste
  473. $('div.search_tag_list a.more').live('click', function(){
  474. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  475. $(this).show();
  476. });
  477. return false;
  478. });
  479. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  480. $('ul.tagbox li.input input[type="text"]').formDefaults();
  481. ////////////////// FIN TAG PROMPT ///////////////
  482. });