muzich.js 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. do_action_body_focused();
  207. }
  208. if (/*@cc_on!@*/false) { // check for Internet Explorer
  209. document.onfocusin = onFocus;
  210. document.onfocusout = onBlur;
  211. } else {
  212. window.onfocus = onFocus;
  213. window.onblur = onBlur;
  214. }
  215. // Bouton de personalisation du filtre
  216. // Aucun tags
  217. $('.tags_prompt input.clear, a.filter_clear_url').live("click", function(){
  218. $('img.elements_more_loader').show();
  219. $('ul.elements').html('');
  220. form = $('form[name="search"]');
  221. remove_tags(form.attr('name'));
  222. form.submit();
  223. });
  224. // tags préférés
  225. $('.tags_prompt input.mytags').live("click", function(){
  226. $('img.elements_more_loader').show();
  227. $('ul.elements').html('');
  228. form = $(this).parent('div').parent('form');
  229. $.getJSON(url_get_favorites_tags, function(response) {
  230. if (response.status == 'mustbeconnected')
  231. {
  232. $(location).attr('href', url_index);
  233. }
  234. remove_tags(form.attr('name'));
  235. // if (tags.length)
  236. // {
  237. inputTag = $("div#tags_prompt_"+form.attr('name')+" input.form-default-value-processed");
  238. for (i in response.tags)
  239. {
  240. $('input#tags_selected_tag_'+form.attr('name')).val(i);
  241. inputTag.val(response.tags[i]);
  242. // Et on execute l'évènement selectTag de l'input
  243. inputTag.trigger("selectTag");
  244. }
  245. form.submit();
  246. //}
  247. });
  248. });
  249. // Tag cliqué dans la liste d'éléments
  250. $('ul.element_tags li a.element_tag').live('click', function(){
  251. // Si il y a une liste de tags (comme sur la page favoris, profil)
  252. if ($('ul#favorite_tags').length)
  253. {
  254. id = str_replace('#', '', $(this).attr('href'));
  255. link = $('ul#favorite_tags li a[href="#'+id+'"]');
  256. list_tag_clicked(link, true);
  257. }
  258. if ($('form[name="search"]').length)
  259. {
  260. $('img.elements_more_loader').show();
  261. $('ul.elements').html('');
  262. form = $('form[name="search"]');
  263. id = str_replace('#', '', $(this).attr('href'));
  264. remove_tags('search');
  265. inputTag = $("div#tags_prompt_search input.form-default-value-processed");
  266. $('input#tags_selected_tag_search').val(id);
  267. inputTag.val($(this).html());
  268. inputTag.trigger("selectTag");
  269. form.submit();
  270. }
  271. });
  272. // Affichage un/des embed
  273. // 1328283150_media-playback-start.png
  274. // 1328283201_emblem-symbolic-link.png
  275. $('a.element_embed_open_link').live("click", function(){
  276. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  277. li.find('a.element_embed_close_link').show();
  278. li.find('a.element_embed_open_link_text').hide();
  279. li.find('div.element_embed').show();
  280. return false;
  281. });
  282. $('a.element_name_embed_open_link').live("click", function(){
  283. li = $(this).parent('span').parent('td').parent('tr').parent().parent().parent('li.element');
  284. li.find('a.element_embed_close_link').show();
  285. li.find('a.element_embed_open_link_text').hide();
  286. li.find('div.element_embed').show();
  287. return false;
  288. });
  289. // Fermeture du embed si demandé
  290. $('a.element_embed_close_link').live("click", function(){
  291. li = $(this).parent('td').parent('tr').parent().parent().parent('li.element');
  292. li.find('div.element_embed').hide();
  293. li.find('a.element_embed_open_link_text').show();
  294. $(this).hide();
  295. return false;
  296. });
  297. // Affichage du "play" ou du "open" (image png)
  298. $('li.element a.a_thumbnail, li.element img.open, li.element img.play').live({
  299. mouseenter:
  300. function()
  301. {
  302. td = $(this).parent('td');
  303. a = td.find('a.a_thumbnail');
  304. if (a.hasClass('embed'))
  305. {
  306. td.find('img.play').show();
  307. }
  308. else
  309. {
  310. td.find('img.open').show();
  311. }
  312. },
  313. mouseleave:
  314. function()
  315. {
  316. td = $(this).parent('td');
  317. a = td.find('a.a_thumbnail');
  318. if (a.hasClass('embed'))
  319. {
  320. td.find('img.play').hide();
  321. }
  322. else
  323. {
  324. td.find('img.open').hide();
  325. }
  326. }
  327. }
  328. );
  329. // Mise en favoris
  330. $('a.favorite_link').live("click", function(){
  331. link = $(this);
  332. // Pour ne pas attendre la fin du chargement ajax:
  333. img = link.find('img');
  334. if (!link.hasClass('loading'))
  335. {
  336. if (img.attr('src') == '/bundles/muzichcore/img/favorite_bw.png')
  337. {
  338. img.attr('src', '/bundles/muzichcore/img/favorite.png');
  339. }
  340. else
  341. {
  342. img.attr('src', '/bundles/muzichcore/img/favorite_bw.png');
  343. }
  344. }
  345. link.addClass('loading');
  346. $.getJSON($(this).attr('href'), function(response) {
  347. if (response.status == 'mustbeconnected')
  348. {
  349. $(location).attr('href', url_index);
  350. }
  351. img = link.find('img');
  352. link.attr('href', response.link_new_url);
  353. img.attr('src', response.img_new_src);
  354. img.attr('title', response.img_new_title);
  355. link.removeClass('loading');
  356. });
  357. return false;
  358. });
  359. // Affichage du bouton Modifier et Supprimer
  360. $('ul.elements li.element').live({
  361. mouseenter:
  362. function()
  363. {
  364. $(this).find('a.element_edit_link').show();
  365. $(this).find('a.element_remove_link').show();
  366. },
  367. mouseleave:
  368. function()
  369. {
  370. if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
  371. {
  372. $(this).find('a.element_edit_link').hide();
  373. }
  374. if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
  375. {
  376. $(this).find('a.element_remove_link').hide();
  377. }
  378. }
  379. }
  380. );
  381. // Plus d'éléments
  382. last_id = null;
  383. $('a.elements_more').click(function(){
  384. link = $(this);
  385. last_element = $('ul.elements li.element:last-child');
  386. id_last = str_replace('element_', '', last_element.attr('id'));
  387. invertcolor = 0;
  388. if (last_element.hasClass('even'))
  389. {
  390. invertcolor = 1;
  391. }
  392. $('img.elements_more_loader').show();
  393. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  394. if (response.status == 'mustbeconnected')
  395. {
  396. $(location).attr('href', url_index);
  397. }
  398. if (response.count)
  399. {
  400. $('ul.elements').append(response.html);
  401. $('img.elements_more_loader').hide();
  402. }
  403. if (response.end || response.count < 1)
  404. {
  405. $('img.elements_more_loader').hide();
  406. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  407. response.message+'</p></div>');
  408. link.hide();
  409. }
  410. });
  411. return false;
  412. });
  413. tag_box_input_value = $('ul.tagbox input[type="text"]').val();
  414. // Filtre et affichage éléments ajax
  415. $('form[name="search"] input[type="submit"]').click(function(){
  416. $('ul.elements').html('');
  417. $('div.no_elements').hide();
  418. $('img.elements_more_loader').show();
  419. });
  420. $('form[name="search"]').ajaxForm(function(response) {
  421. if (response.status == 'mustbeconnected')
  422. {
  423. $(location).attr('href', url_index);
  424. }
  425. $('ul.elements').html(response.html);
  426. if (response.count)
  427. {
  428. $('img.elements_more_loader').hide();
  429. $('span.elements_more').show();
  430. $('a.elements_more').show();
  431. }
  432. if (response.count < 1)
  433. {
  434. $('img.elements_more_loader').hide();
  435. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  436. response.message+'</p></div>');
  437. $('a.elements_more').hide()
  438. ;
  439. }
  440. $('ul.tagbox input[type="text"]').val($('ul.tagbox input[type="text"]').val());
  441. });
  442. // Suppression d'un element
  443. $('a.element_remove_link').jConfirmAction({
  444. question : "Vraiment supprimer ?",
  445. yesAnswer : "Oui",
  446. cancelAnswer : "Non",
  447. onYes: function(link){
  448. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  449. li.find('img.element_loader').show();
  450. $.getJSON(link.attr('href'), function(response){
  451. if (response.status == 'mustbeconnected')
  452. {
  453. $(location).attr('href', url_index);
  454. }
  455. if (response.status == 'success')
  456. {
  457. li.remove();
  458. }
  459. else
  460. {
  461. li.find('img.element_loader').hide();
  462. }
  463. });
  464. return false;
  465. },
  466. onOpen: function(link){
  467. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  468. li.find('a.element_edit_link').addClass('mustBeDisplayed');
  469. li.find('a.element_remove_link').addClass('mustBeDisplayed');
  470. },
  471. onClose: function(link){
  472. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  473. li.find('a.element_edit_link').removeClass('mustBeDisplayed');
  474. li.find('a.element_remove_link').removeClass('mustBeDisplayed');
  475. li.find('a.element_edit_link').hide();
  476. li.find('a.element_remove_link').hide();
  477. }
  478. });
  479. elements_edited = new Array();
  480. // Ouverture du formulaire de modification
  481. $('a.element_edit_link').live('click', function(){
  482. link = $(this);
  483. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  484. // On garde en mémoire l'élément édité en cas d'annulation
  485. elements_edited[li.attr('id')] = li.html();
  486. div_loader = li.find('div.loader');
  487. li.html(div_loader);
  488. li.find('img.element_loader').show();
  489. $.getJSON($(this).attr('href'), function(response) {
  490. if (response.status == 'mustbeconnected')
  491. {
  492. $(location).attr('href', url_index);
  493. }
  494. // On prépare le tagBox
  495. li.html(response.html);
  496. // Pour le click sur l'input de saisie de tag
  497. li.find('ul.tagbox li.input input[type="text"]').formDefaults();
  498. var options = new Array();
  499. options.form_name = response.form_name;
  500. options.tag_init = response.tags;
  501. ajax_query_timestamp = null;
  502. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  503. // On rend ce formulaire ajaxFormable
  504. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  505. li.prepend(div_loader);
  506. li.find('img.element_loader').show();
  507. });
  508. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  509. if (response.status == 'mustbeconnected')
  510. {
  511. $(location).attr('href', url_index);
  512. }
  513. if (response.status == 'success')
  514. {
  515. li.html(response.html);
  516. delete(elements_edited[li.attr('id')]);
  517. }
  518. else if (response.status == 'error')
  519. {
  520. li.find('img.element_loader').hide();
  521. li.find('ul.error_list').remove();
  522. ul_errors = $('<ul>').addClass('error_list');
  523. for (i in response.errors)
  524. {
  525. ul_errors.append($('<li>').append(response.errors[i]));
  526. }
  527. li.prepend(ul_errors);
  528. }
  529. });
  530. });
  531. return false;
  532. });
  533. // Annulation d'un formulaire de modification d'élément
  534. $('form.edit_element input.cancel_edit').live('click', function(){
  535. var li = $(this).parent('form').parent('li');
  536. li.html(elements_edited[li.attr('id')]);
  537. delete(elements_edited[li.attr('id')]);
  538. });
  539. ////////////////// TAG PROMPT ///////////////
  540. ajax_query_timestamp = null;
  541. tag_text_help = $('input.tag_text_help').val();
  542. // Les deux clicks ci-dessous permettent de faire disparaitre
  543. // la div de tags lorsque l'on clique ailleurs
  544. $('html').click(function() {
  545. if ($("div.search_tag_list").is(':visible'))
  546. {
  547. $("div.search_tag_list").hide();
  548. }
  549. });
  550. $("div.search_tag_list, div.search_tag_list a.more").live('click', function(event){
  551. event.stopPropagation();
  552. $("div.search_tag_list").show();
  553. });
  554. function autocomplete_tag(input, form_name)
  555. {
  556. // Il doit y avoir au moin un caractère
  557. if ((input.val().length > 0) && (input.val() != string_tag_prompt_input_help))
  558. {
  559. // on met en variable l'input
  560. inputTag = input;
  561. // On récupére la div de tags
  562. divtags = $("#search_tag_"+form_name);
  563. // Si la fenêtre de tags est caché
  564. if (!divtags.is(':visible'))
  565. {
  566. // On la replace
  567. position = input.position();
  568. divtags.css('left', Math.round(position.left) + 5);
  569. divtags.css('top', Math.round(position.top) + 28);
  570. // Et on l'affiche
  571. divtags.show();
  572. }
  573. // On affiche le loader
  574. $('#tag_loader_'+form_name).show();
  575. // On cache la liste de tags
  576. search_tag_list = divtags.find('ul.search_tag_list');
  577. // On supprime les anciens li
  578. search_tag_list.find('li').remove();
  579. search_tag_list.hide();
  580. // Et on affiche une info
  581. span_info = divtags.find('span.info');
  582. span_info.show();
  583. // TODO: multilingue !
  584. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  585. // C'est en fonction du nb de resultats qu'il sera affiché
  586. divtags.find('a.more').hide();
  587. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  588. ajax_query_timestamp = new Date().getTime();
  589. // Récupération des tags correspondants
  590. $.getJSON(url_search_tag+'/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  591. if (data.status == 'mustbeconnected')
  592. {
  593. $(location).attr('href', url_index);
  594. }
  595. // Ce contrôle permet de ne pas continuer si une requete
  596. // ajax a été faite depuis.
  597. if (data.timestamp == ajax_query_timestamp)
  598. {
  599. status = data.status;
  600. tags = data.data;
  601. // Si on spécifie une erreur
  602. if (status == 'error')
  603. {
  604. // On l'affiche a l'utilisateur
  605. span_info.text(data.error);
  606. }
  607. // Si c'est un succés
  608. else if (status == 'success')
  609. {
  610. if (tags.length > 0)
  611. {
  612. more = false;
  613. // Pour chaque tags retournés
  614. for (i in tags)
  615. {
  616. var tag_name = tags[i]['name'];
  617. var tag_id = tags[i]['id'];
  618. var t_string = tag_name
  619. // On construit un li
  620. r_string = $.trim(input.val());
  621. var re = new RegExp(r_string, "i");
  622. t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
  623. li_tag =
  624. $('<li>').append(
  625. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  626. // qui réagit quand on clique dessus
  627. .click(function(e){
  628. // On récupère le nom du tag
  629. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  630. name = name.substr(strpos(name, '#')+1, name.length);
  631. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  632. id = str_replace(name, '', id);
  633. id = str_replace('#', '', id);
  634. $('input#tags_selected_tag_'+form_name).val(id);
  635. inputTag.val(name);
  636. // Et on execute l'évènement selectTag de l'input
  637. inputTag.trigger("selectTag");
  638. // On cache la liste puisque le choix vient d'être fait
  639. divtags.hide();
  640. inputTag.val(tag_text_help);
  641. return false;
  642. })
  643. .append(t_string)
  644. );
  645. // Si on depasse les 30 tags
  646. if (i > 30)
  647. {
  648. more = true;
  649. // On le cache
  650. li_tag.hide();
  651. }
  652. // On ajout ce li a la liste
  653. search_tag_list.append(li_tag);
  654. }
  655. if (more)
  656. {
  657. divtags.find('a.more').show();
  658. }
  659. span_info.show();
  660. span_info.text(data.message);
  661. // Et on affiche la liste
  662. search_tag_list.show();
  663. }
  664. else
  665. {
  666. span_info.show();
  667. span_info.text(data.message);
  668. search_tag_list.show();
  669. // Dans ce cas ou aucun tag n'a été trouvé, la proposition
  670. // d'ajout s'affichecf en dessous
  671. //span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  672. }
  673. // Si le tag ne semble pas connu en base
  674. if (!data.same_found)
  675. {
  676. // Cette variable nous permettra de stocker le lien nouveau tag
  677. link_add_tag = null;
  678. li_tag =
  679. $('<li>').addClass('new').append(
  680. $('<a>').attr('href','#new#'+$.trim(input.val()))
  681. // qui réagit quand on clique dessus
  682. .click(function(e){
  683. // Effet fade-in du fond opaque
  684. $('body').append($('<div>').attr('id', 'fade'));
  685. //Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) pour corriger les bogues de IE
  686. $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
  687. // On met le lien cliqué dans la variabke prévu
  688. link_add_tag = $(this);
  689. // En premier lieux on fait apparaître la fenêtre de confirmation
  690. popup = $('<div>')
  691. .attr('id', 'add_tag')
  692. .addClass('popin_block')
  693. .css('width', '400px')
  694. //.append($('<h2>').append(string_tag_add_title))
  695. .append($('<div>').addClass('tag')
  696. .append($('<ul>')
  697. .append($('<li>').addClass('button')
  698. .append($(this).text()))))
  699. .append($('<p>').append(string_tag_add_text))
  700. .append($('<p>').append(string_tag_add_argument))
  701. .append($('<textarea>').attr('name', 'argument'))
  702. .append($('<div>').addClass('inputs')
  703. .append($('<input>')
  704. .attr('type', 'button')
  705. .attr('value', string_tag_add_inputs_cancel)
  706. .addClass('button')
  707. .click(function(){
  708. $('#fade').fadeOut(1000, function(){$('#fade').remove();});
  709. $('#add_tag').remove();
  710. return false;
  711. })
  712. )
  713. .append($('<input>')
  714. .attr('type', 'button')
  715. .attr('value', string_tag_add_inputs_submit)
  716. .addClass('button')
  717. .click(function(){
  718. var arguments = $('#add_tag textarea').val();
  719. $('#fade').fadeOut(400, function(){$('#fade').remove();});
  720. $('#add_tag').remove();
  721. // On récupère le nom du tag
  722. name = link_add_tag.attr('href').substr(1,link_add_tag.attr('href').length);
  723. name = name.substr(strpos(name, '#')+1, name.length);
  724. link_add_tag.parent('li').parent('ul').parent('div').find('img.tag_loader').show();
  725. var url;
  726. if (arguments)
  727. {
  728. url = url_add_tag+'/'+name+'/'+arguments;
  729. }
  730. else
  731. {
  732. url = url_add_tag+'/'+name;
  733. }
  734. // La on fait l'ajout en base en tant que nouveau tag
  735. $.getJSON(url, function(response){
  736. if (response.status == 'mustbeconnected')
  737. {
  738. $(location).attr('href', url_index);
  739. }
  740. tag_id = response.tag_id;
  741. tag_name = response.tag_name;
  742. $('input#tags_selected_tag_'+form_name).val(tag_id);
  743. inputTag.val(tag_name);
  744. // Et on execute l'évènement selectTag de l'input
  745. inputTag.trigger("selectTag");
  746. // On cache la liste puisque le choix vient d'être fait
  747. divtags.hide();
  748. inputTag.val(tag_text_help);
  749. link_add_tag.parent('li').parent('ul').parent('div').find('img.tag_loader').hide();
  750. });
  751. return false;
  752. })
  753. )
  754. )
  755. ;
  756. // Il faut ajouter le popup au dom avant de le positionner en css
  757. // Sinon la valeur height n'est pas encore calculable
  758. $('body').prepend(popup);
  759. //Récupération du margin, qui permettra de centrer la fenêtre - on ajuste de 80px en conformité avec le CSS
  760. var popMargTop = (popup.height() + 50) / 2;
  761. var popMargLeft = (popup.width() + 50) / 2;
  762. //On affecte le margin
  763. $(popup).css({
  764. 'margin-top' : -popMargTop,
  765. 'margin-left' : -popMargLeft
  766. });
  767. return false;
  768. })
  769. .append($.trim(input.val()))
  770. );
  771. search_tag_list.append(li_tag);
  772. }
  773. }
  774. // On cache le loader
  775. $('#tag_loader_'+form_name).hide();
  776. }
  777. });
  778. }
  779. }
  780. last_keypress = 0;
  781. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  782. {
  783. if (!timed)
  784. {
  785. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  786. // et on met a jour l'id de la dernière pressé
  787. last_keypress = new Date().getTime();
  788. var this_time_id = last_keypress;
  789. }
  790. else
  791. {
  792. // Si elle a été redirigé, on met son id dans cette variable
  793. var this_time_id = time_id;
  794. }
  795. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  796. if (time_id != last_keypress && timed)
  797. {
  798. // elle disparait
  799. }
  800. else
  801. {
  802. //
  803. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  804. {
  805. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  806. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  807. // elle doit attendre au cas ou soit pressé.
  808. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  809. }
  810. else
  811. {
  812. // il n'y a plus a attendre, on envoie la demande de tag.
  813. autocomplete_tag(input, form_name);
  814. }
  815. }
  816. }
  817. // Autocompletion de tags
  818. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  819. var form_name = $(this).parent('li').parent('ul.tagbox')
  820. .parent('div.tags_prompt').parent('form').attr('name')
  821. ;
  822. var code = (e.keyCode ? e.keyCode : e.which);
  823. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  824. {
  825. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  826. }
  827. });
  828. // Un click sur ce lien affiche tout les tags cachés de la liste
  829. $('div.search_tag_list a.more').live('click', function(){
  830. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  831. $(this).show();
  832. });
  833. $(this).hide();
  834. return false;
  835. });
  836. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  837. $('ul.tagbox li.input input[type="text"]').formDefaults();
  838. ////////////////// FIN TAG PROMPT ///////////////
  839. // Suppression d'un element
  840. $('a.group_remove_link').jConfirmAction({
  841. question : "Supprimer ce groupe ?",
  842. yesAnswer : "Oui",
  843. cancelAnswer : "Non",
  844. onYes: function(link){
  845. window.location = link.attr('href');
  846. return false;
  847. },
  848. onOpen: function(){},
  849. onClose: function(){}
  850. });
  851. // Selection Réseau global / Mon réseau
  852. $('div.select_network a').live('click', function(){
  853. divSelect = $(this).parent('div');
  854. if ($(this).hasClass('all_network'))
  855. {
  856. divSelect.find('a.all_network').addClass('active');
  857. divSelect.find('a.my_network').removeClass('active');
  858. divSelect.find('select').val('network_public');
  859. }
  860. else
  861. {
  862. divSelect.find('a.my_network').addClass('active');
  863. divSelect.find('a.all_network').removeClass('active');
  864. divSelect.find('select').val('network_personal');
  865. }
  866. });
  867. // Ajout d'un element
  868. $('form[name="add"] input[type="submit"]').live('click', function(){
  869. $('form[name="add"]').find('img.tag_loader').show();
  870. });
  871. $('form[name="add"]').ajaxForm(function(response) {
  872. if (response.status == 'mustbeconnected')
  873. {
  874. $(location).attr('href', url_index);
  875. }
  876. $('form[name="add"] img.tag_loader').hide();
  877. if (response.status == 'success')
  878. {
  879. $('form[name="add"]').find('ul.error_list').remove();
  880. $('ul.elements').prepend(response.html);
  881. $('form[name="add"] input[type="text"]').val('');
  882. $('div#element_add_box').slideUp();
  883. $('a#element_add_link').show();
  884. if ($('form[name="search"]').length)
  885. {
  886. $('form[name="search"]').slideDown();
  887. }
  888. remove_tags('add');
  889. recolorize_element_list();
  890. }
  891. else if (response.status == 'error')
  892. {
  893. $('form[name="add"]').find('ul.error_list').remove();
  894. ul_errors = $('<ul>').addClass('error_list');
  895. for (i in response.errors)
  896. {
  897. ul_errors.append($('<li>').append(response.errors[i]));
  898. }
  899. $('form[name="add"]').prepend(ul_errors);
  900. }
  901. return false;
  902. });
  903. // Check périodique
  904. // TODO.
  905. /////////////////////
  906. // Filtre par tags (show, favorite)
  907. function refresh_elements_with_tags_selected(link)
  908. {
  909. // Puis on fait notre rekékéte ajax.
  910. $('ul.elements').html('');
  911. $('div.no_elements').hide();
  912. $('img.elements_more_loader').show();
  913. $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
  914. if (response.status == 'mustbeconnected')
  915. {
  916. $(location).attr('href', url_index);
  917. }
  918. $('ul.elements').html(response.html);
  919. if (response.count)
  920. {
  921. $('img.elements_more_loader').hide();
  922. $('span.elements_more').show();
  923. $('a.elements_more').show();
  924. }
  925. });
  926. return false;
  927. }
  928. function list_tag_clicked(link, erease)
  929. {
  930. if (erease)
  931. {
  932. $('ul#favorite_tags a.tag').removeClass('active');
  933. }
  934. // Ensuite on l'active ou le désactive
  935. if (link.hasClass('active'))
  936. {
  937. link.removeClass('active');
  938. }
  939. else
  940. {
  941. link.addClass('active');
  942. }
  943. // On construit notre liste de tags
  944. tags_ids = new Array();
  945. $('ul#favorite_tags a.tag.active').each(function(index){
  946. id = str_replace('#', '', $(this).attr('href'));
  947. tags_ids[id] = id;
  948. });
  949. // On adapte le lien afficher plus de résultats
  950. a_more = $('a.elements_more');
  951. a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
  952. return check_timelaps_and_find_with_tags(link, new Date().getTime(), false);
  953. }
  954. $('ul#favorite_tags a.tag').click(function(){
  955. list_tag_clicked($(this));
  956. });
  957. last_keypress = 0;
  958. function check_timelaps_and_find_with_tags(link, time_id, timed)
  959. {
  960. if (!timed)
  961. {
  962. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  963. // et on met a jour l'id de la dernière pressé
  964. last_keypress = new Date().getTime();
  965. var this_time_id = last_keypress;
  966. }
  967. else
  968. {
  969. // Si elle a été redirigé, on met son id dans cette variable
  970. var this_time_id = time_id;
  971. }
  972. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  973. if (time_id != last_keypress && timed)
  974. {
  975. // elle disparait
  976. }
  977. else
  978. {
  979. //
  980. if ((new Date().getTime() - last_keypress) < 800 || timed == false)
  981. {
  982. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  983. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  984. // elle doit attendre au cas ou soit pressé.
  985. setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
  986. }
  987. else
  988. {
  989. // il n'y a plus a attendre, on envoie la demande de tag.
  990. return refresh_elements_with_tags_selected(link);
  991. }
  992. }
  993. return null;
  994. }
  995. ////////////////////////////////////////
  996. /// Gestion de nouveaux éléments
  997. do_check_new_elements = false;
  998. function check_new_elements()
  999. {
  1000. if ($('ul.elements li').length)
  1001. {
  1002. console.log('?');
  1003. // Si l'utilisateur a quitté la page on reporte le check
  1004. if ($('body.blurred').length)
  1005. {
  1006. // on passe la variable a vrai de façon a ce que lorsque la page
  1007. // et ré affiché on lance le check
  1008. do_check_new_elements = true;
  1009. console.log('on repassera');
  1010. }
  1011. else
  1012. {
  1013. console.log('on fait');
  1014. var url = url_element_new_count
  1015. +'/'
  1016. +str_replace('element_', '', $('ul.elements li:first').attr('id'))
  1017. ;
  1018. $.getJSON(url, function(response){
  1019. if (response.status == 'mustbeconnected')
  1020. {
  1021. $(location).attr('href', url_index);
  1022. }
  1023. if (response.status == 'success' && response.count)
  1024. {
  1025. $('div.display_more_elements').show();
  1026. $('div.display_more_elements span').html(response.message);
  1027. }
  1028. setTimeout(check_new_elements, 150000);
  1029. });
  1030. }
  1031. }
  1032. }
  1033. if ($('div.display_more_elements').length)
  1034. {
  1035. setTimeout(check_new_elements, 150000);
  1036. }
  1037. $('a.show_new_elements').live('click', function(){
  1038. var url = url_element_new_get
  1039. +'/'
  1040. +str_replace('element_', '', $('ul.elements li:first').attr('id'))
  1041. ;
  1042. $('img.elements_new_loader').show();
  1043. $.getJSON(url, function(response){
  1044. if (response.status == 'mustbeconnected')
  1045. {
  1046. $(location).attr('href', url_index);
  1047. }
  1048. if (response.status == 'success')
  1049. {
  1050. if (response.count)
  1051. {
  1052. $('div.display_more_elements').show();
  1053. $('div.display_more_elements span').html(response.message);
  1054. }
  1055. else
  1056. {
  1057. $('div.display_more_elements').hide();
  1058. }
  1059. $('ul.elements').prepend(response.html);
  1060. recolorize_element_list();
  1061. }
  1062. $('img.elements_new_loader').hide();
  1063. });
  1064. });
  1065. function recolorize_element_list()
  1066. {
  1067. $('ul.elements li.element').each(function(index){
  1068. if ((index & 1) == 1)
  1069. {
  1070. $(this).removeClass('even');
  1071. $(this).removeClass('odd');
  1072. $(this).addClass('odd');
  1073. }
  1074. else
  1075. {
  1076. $(this).removeClass('odd');
  1077. $(this).removeClass('even');
  1078. $(this).addClass('even');
  1079. }
  1080. });
  1081. }
  1082. /*
  1083. * Action a effectuer lorsque l'utilisateur met le focus sur la page
  1084. */
  1085. function do_action_body_focused()
  1086. {
  1087. if (do_check_new_elements)
  1088. {
  1089. check_new_elements();
  1090. }
  1091. }
  1092. });