muzich.js 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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 : string_element_delete_confirm_sentence,
  445. yesAnswer : string_element_delete_confirm_yes,
  446. cancelAnswer : string_element_delete_confirm_no,
  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 = $(this).parent('form').parent('li');
  506. li.prepend(div_loader);
  507. li.find('img.element_loader').show();
  508. });
  509. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  510. if (response.status == 'mustbeconnected')
  511. {
  512. $(location).attr('href', url_index);
  513. }
  514. li = $('li#'+response.dom_id);
  515. if (response.status == 'success')
  516. {
  517. li.html(response.html);
  518. delete(elements_edited[li.attr('id')]);
  519. }
  520. else if (response.status == 'error')
  521. {
  522. li.find('img.element_loader').hide();
  523. li.find('ul.error_list').remove();
  524. ul_errors = $('<ul>').addClass('error_list');
  525. for (i in response.errors)
  526. {
  527. ul_errors.append($('<li>').append(response.errors[i]));
  528. }
  529. li.prepend(ul_errors);
  530. }
  531. });
  532. });
  533. return false;
  534. });
  535. // Annulation d'un formulaire de modification d'élément
  536. $('form.edit_element input.cancel_edit').live('click', function(){
  537. var li = $(this).parent('form').parent('li');
  538. li.html(elements_edited[li.attr('id')]);
  539. delete(elements_edited[li.attr('id')]);
  540. });
  541. ////////////////// TAG PROMPT ///////////////
  542. ajax_query_timestamp = null;
  543. tag_text_help = $('input.tag_text_help').val();
  544. // Les deux clicks ci-dessous permettent de faire disparaitre
  545. // la div de tags lorsque l'on clique ailleurs
  546. $('html').click(function() {
  547. if ($("div.search_tag_list").is(':visible'))
  548. {
  549. $("div.search_tag_list").hide();
  550. }
  551. });
  552. $("div.search_tag_list, div.search_tag_list a.more").live('click', function(event){
  553. event.stopPropagation();
  554. $("div.search_tag_list").show();
  555. });
  556. function autocomplete_tag(input, form_name)
  557. {
  558. // Il doit y avoir au moin un caractère
  559. if ((input.val().length > 0) && (input.val() != string_tag_prompt_input_help))
  560. {
  561. // on met en variable l'input
  562. inputTag = input;
  563. // On récupére la div de tags
  564. divtags = $("#search_tag_"+form_name);
  565. // Si la fenêtre de tags est caché
  566. if (!divtags.is(':visible'))
  567. {
  568. // On la replace
  569. position = input.position();
  570. divtags.css('left', Math.round(position.left) + 5);
  571. divtags.css('top', Math.round(position.top) + 28);
  572. // Et on l'affiche
  573. divtags.show();
  574. }
  575. // On affiche le loader
  576. $('#tag_loader_'+form_name).show();
  577. // On cache la liste de tags
  578. search_tag_list = divtags.find('ul.search_tag_list');
  579. // On supprime les anciens li
  580. search_tag_list.find('li').remove();
  581. search_tag_list.hide();
  582. // Et on affiche une info
  583. span_info = divtags.find('span.info');
  584. span_info.show();
  585. // TODO: multilingue !
  586. span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
  587. // C'est en fonction du nb de resultats qu'il sera affiché
  588. divtags.find('a.more').hide();
  589. // On récupère le timestamp pour reconnaitre la dernière requête effectué
  590. ajax_query_timestamp = new Date().getTime();
  591. // Récupération des tags correspondants
  592. $.getJSON(url_search_tag+'/'+input.val()+'/'+ajax_query_timestamp, function(data) {
  593. if (data.status == 'mustbeconnected')
  594. {
  595. $(location).attr('href', url_index);
  596. }
  597. // Ce contrôle permet de ne pas continuer si une requete
  598. // ajax a été faite depuis.
  599. if (data.timestamp == ajax_query_timestamp)
  600. {
  601. status = data.status;
  602. tags = data.data;
  603. // Si on spécifie une erreur
  604. if (status == 'error')
  605. {
  606. // On l'affiche a l'utilisateur
  607. span_info.text(data.error);
  608. }
  609. // Si c'est un succés
  610. else if (status == 'success')
  611. {
  612. if (tags.length > 0)
  613. {
  614. more = false;
  615. // Pour chaque tags retournés
  616. for (i in tags)
  617. {
  618. var tag_name = tags[i]['name'];
  619. var tag_id = tags[i]['id'];
  620. var t_string = tag_name
  621. // On construit un li
  622. r_string = $.trim(input.val());
  623. var re = new RegExp(r_string, "i");
  624. t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
  625. li_tag =
  626. $('<li>').append(
  627. $('<a>').attr('href','#'+tag_id+'#'+tag_name)
  628. // qui réagit quand on clique dessus
  629. .click(function(e){
  630. // On récupère le nom du tag
  631. name = $(this).attr('href').substr(1,$(this).attr('href').length);
  632. name = name.substr(strpos(name, '#')+1, name.length);
  633. id = $(this).attr('href').substr(1,$(this).attr('href').length);
  634. id = str_replace(name, '', id);
  635. id = str_replace('#', '', id);
  636. $('input#tags_selected_tag_'+form_name).val(id);
  637. inputTag.val(name);
  638. // Et on execute l'évènement selectTag de l'input
  639. inputTag.trigger("selectTag");
  640. // On cache la liste puisque le choix vient d'être fait
  641. divtags.hide();
  642. inputTag.val(tag_text_help);
  643. return false;
  644. })
  645. .append(t_string)
  646. );
  647. // Si on depasse les 30 tags
  648. if (i > 30)
  649. {
  650. more = true;
  651. // On le cache
  652. li_tag.hide();
  653. }
  654. // On ajout ce li a la liste
  655. search_tag_list.append(li_tag);
  656. }
  657. if (more)
  658. {
  659. divtags.find('a.more').show();
  660. }
  661. span_info.show();
  662. span_info.text(data.message);
  663. // Et on affiche la liste
  664. search_tag_list.show();
  665. }
  666. else
  667. {
  668. span_info.show();
  669. span_info.text(data.message);
  670. search_tag_list.show();
  671. // Dans ce cas ou aucun tag n'a été trouvé, la proposition
  672. // d'ajout s'affichecf en dessous
  673. //span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
  674. }
  675. // Si le tag ne semble pas connu en base
  676. if (!data.same_found)
  677. {
  678. // Cette variable nous permettra de stocker le lien nouveau tag
  679. link_add_tag = null;
  680. li_tag =
  681. $('<li>').addClass('new').append(
  682. $('<a>').attr('href','#new#'+$.trim(input.val()))
  683. // qui réagit quand on clique dessus
  684. .click(function(e){
  685. // Effet fade-in du fond opaque
  686. $('body').append($('<div>').attr('id', 'fade'));
  687. //Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) pour corriger les bogues de IE
  688. $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
  689. // On met le lien cliqué dans la variabke prévu
  690. link_add_tag = $(this);
  691. // En premier lieux on fait apparaître la fenêtre de confirmation
  692. popup = $('<div>')
  693. .attr('id', 'add_tag')
  694. .addClass('popin_block')
  695. .css('width', '400px')
  696. //.append($('<h2>').append(string_tag_add_title))
  697. .append($('<div>').addClass('tag')
  698. .append($('<ul>')
  699. .append($('<li>').addClass('button')
  700. .append($(this).text()))))
  701. .append($('<p>').append(string_tag_add_text))
  702. .append($('<p>').append(string_tag_add_argument))
  703. .append($('<textarea>').attr('name', 'argument'))
  704. .append($('<div>').addClass('inputs')
  705. .append($('<input>')
  706. .attr('type', 'button')
  707. .attr('value', string_tag_add_inputs_cancel)
  708. .addClass('button')
  709. .click(function(){
  710. $('#fade').fadeOut(1000, function(){$('#fade').remove();});
  711. $('#add_tag').remove();
  712. return false;
  713. })
  714. )
  715. .append($('<input>')
  716. .attr('type', 'button')
  717. .attr('value', string_tag_add_inputs_submit)
  718. .addClass('button')
  719. .click(function(){
  720. var arguments = $('#add_tag textarea').val();
  721. $('#fade').fadeOut(400, function(){$('#fade').remove();});
  722. $('#add_tag').remove();
  723. // On récupère le nom du tag
  724. name = link_add_tag.attr('href').substr(1,link_add_tag.attr('href').length);
  725. name = name.substr(strpos(name, '#')+1, name.length);
  726. link_add_tag.parent('li').parent('ul').parent('div').find('img.tag_loader').show();
  727. var url;
  728. if (arguments)
  729. {
  730. url = url_add_tag+'/'+name+'/'+arguments;
  731. }
  732. else
  733. {
  734. url = url_add_tag+'/'+name;
  735. }
  736. // La on fait l'ajout en base en tant que nouveau tag
  737. $.getJSON(url, function(response){
  738. if (response.status == 'mustbeconnected')
  739. {
  740. $(location).attr('href', url_index);
  741. }
  742. tag_id = response.tag_id;
  743. tag_name = response.tag_name;
  744. $('input#tags_selected_tag_'+form_name).val(tag_id);
  745. inputTag.val(tag_name);
  746. // Et on execute l'évènement selectTag de l'input
  747. inputTag.trigger("selectTag");
  748. // On cache la liste puisque le choix vient d'être fait
  749. divtags.hide();
  750. inputTag.val(tag_text_help);
  751. link_add_tag.parent('li').parent('ul').parent('div').find('img.tag_loader').hide();
  752. });
  753. return false;
  754. })
  755. )
  756. )
  757. ;
  758. // Il faut ajouter le popup au dom avant de le positionner en css
  759. // Sinon la valeur height n'est pas encore calculable
  760. $('body').prepend(popup);
  761. //Récupération du margin, qui permettra de centrer la fenêtre - on ajuste de 80px en conformité avec le CSS
  762. var popMargTop = (popup.height() + 50) / 2;
  763. var popMargLeft = (popup.width() + 50) / 2;
  764. //On affecte le margin
  765. $(popup).css({
  766. 'margin-top' : -popMargTop,
  767. 'margin-left' : -popMargLeft
  768. });
  769. return false;
  770. })
  771. .append($.trim(input.val()))
  772. );
  773. search_tag_list.append(li_tag);
  774. }
  775. }
  776. // On cache le loader
  777. $('#tag_loader_'+form_name).hide();
  778. }
  779. });
  780. }
  781. }
  782. last_keypress = 0;
  783. function check_timelaps_and_search(input, form_name, time_id, timed, info)
  784. {
  785. if (!timed)
  786. {
  787. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  788. // et on met a jour l'id de la dernière pressé
  789. last_keypress = new Date().getTime();
  790. var this_time_id = last_keypress;
  791. }
  792. else
  793. {
  794. // Si elle a été redirigé, on met son id dans cette variable
  795. var this_time_id = time_id;
  796. }
  797. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  798. if (time_id != last_keypress && timed)
  799. {
  800. // elle disparait
  801. }
  802. else
  803. {
  804. //
  805. if ((new Date().getTime() - last_keypress) < 600 || timed == false)
  806. {
  807. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  808. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  809. // elle doit attendre au cas ou soit pressé.
  810. setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
  811. }
  812. else
  813. {
  814. // il n'y a plus a attendre, on envoie la demande de tag.
  815. autocomplete_tag(input, form_name);
  816. }
  817. }
  818. }
  819. // Autocompletion de tags
  820. $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
  821. var form_name = $(this).parent('li').parent('ul.tagbox')
  822. .parent('div.tags_prompt').parent('form').attr('name')
  823. ;
  824. var code = (e.keyCode ? e.keyCode : e.which);
  825. if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
  826. {
  827. check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
  828. }
  829. });
  830. // Un click sur ce lien affiche tout les tags cachés de la liste
  831. $('div.search_tag_list a.more').live('click', function(){
  832. jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
  833. $(this).show();
  834. });
  835. $(this).hide();
  836. return false;
  837. });
  838. $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
  839. $('ul.tagbox li.input input[type="text"]').formDefaults();
  840. ////////////////// FIN TAG PROMPT ///////////////
  841. // Suppression d'un element
  842. $('a.group_remove_link').jConfirmAction({
  843. question : "Supprimer ce groupe ?",
  844. yesAnswer : "Oui",
  845. cancelAnswer : "Non",
  846. onYes: function(link){
  847. window.location = link.attr('href');
  848. return false;
  849. },
  850. onOpen: function(){},
  851. onClose: function(){}
  852. });
  853. // Selection Réseau global / Mon réseau
  854. $('div.select_network a').live('click', function(){
  855. divSelect = $(this).parent('div');
  856. if ($(this).hasClass('all_network'))
  857. {
  858. divSelect.find('a.all_network').addClass('active');
  859. divSelect.find('a.my_network').removeClass('active');
  860. divSelect.find('select').val('network_public');
  861. }
  862. else
  863. {
  864. divSelect.find('a.my_network').addClass('active');
  865. divSelect.find('a.all_network').removeClass('active');
  866. divSelect.find('select').val('network_personal');
  867. }
  868. });
  869. // Ajout d'un element #ajouter
  870. $('form[name="add"] input[type="submit"]').live('click', function(){
  871. $('form[name="add"]').find('img.tag_loader').show();
  872. });
  873. $('form[name="add"]').ajaxForm(function(response) {
  874. if (response.status == 'mustbeconnected')
  875. {
  876. $(location).attr('href', url_index);
  877. }
  878. $('form[name="add"] img.tag_loader').hide();
  879. if (response.status == 'success')
  880. {
  881. $('form[name="add"]').find('ul.error_list').remove();
  882. $('ul.elements').prepend(response.html);
  883. $('form[name="add"] input[type="text"]').val('');
  884. if ($('form[name="search"]').length)
  885. {
  886. $('form[name="search"]').slideDown();
  887. }
  888. remove_tags('add');
  889. recolorize_element_list();
  890. $('div#element_add_box').slideUp();
  891. if (response.groups.length)
  892. {
  893. // Des groupes sont proposés pour diffuser cet élément
  894. $('div#added_element_to_group').slideDown();
  895. for (i in response.groups)
  896. {
  897. var group = response.groups[i];
  898. $('ul#groups_to_add_element').html('');
  899. $('ul#groups_to_add_element')
  900. .append($('<li>')
  901. .append($('<a>')
  902. .addClass('added_element_add_to_group')
  903. .attr('href', group.url)
  904. .append(group.name)
  905. )
  906. )
  907. ;
  908. }
  909. }
  910. else
  911. {
  912. $('a#element_add_link').show();
  913. }
  914. }
  915. else if (response.status == 'error')
  916. {
  917. $('form[name="add"]').find('ul.error_list').remove();
  918. ul_errors = $('<ul>').addClass('error_list');
  919. for (i in response.errors)
  920. {
  921. ul_errors.append($('<li>').append(response.errors[i]));
  922. }
  923. $('form[name="add"]').prepend(ul_errors);
  924. }
  925. return false;
  926. });
  927. // Check périodique
  928. // TODO.
  929. /////////////////////
  930. // Filtre par tags (show, favorite)
  931. function refresh_elements_with_tags_selected(link)
  932. {
  933. // Puis on fait notre rekékéte ajax.
  934. $('ul.elements').html('');
  935. $('div.no_elements').hide();
  936. $('img.elements_more_loader').show();
  937. $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
  938. if (response.status == 'mustbeconnected')
  939. {
  940. $(location).attr('href', url_index);
  941. }
  942. $('ul.elements').html(response.html);
  943. if (response.count)
  944. {
  945. $('img.elements_more_loader').hide();
  946. $('span.elements_more').show();
  947. $('a.elements_more').show();
  948. }
  949. });
  950. return false;
  951. }
  952. function list_tag_clicked(link, erease)
  953. {
  954. if (erease)
  955. {
  956. $('ul#favorite_tags a.tag').removeClass('active');
  957. }
  958. // Ensuite on l'active ou le désactive
  959. if (link.hasClass('active'))
  960. {
  961. link.removeClass('active');
  962. }
  963. else
  964. {
  965. link.addClass('active');
  966. }
  967. // On construit notre liste de tags
  968. tags_ids = new Array();
  969. $('ul#favorite_tags a.tag.active').each(function(index){
  970. id = str_replace('#', '', $(this).attr('href'));
  971. tags_ids[id] = id;
  972. });
  973. // On adapte le lien afficher plus de résultats
  974. a_more = $('a.elements_more');
  975. a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
  976. return check_timelaps_and_find_with_tags(link, new Date().getTime(), false);
  977. }
  978. $('ul#favorite_tags a.tag').click(function(){
  979. list_tag_clicked($(this));
  980. });
  981. last_keypress = 0;
  982. function check_timelaps_and_find_with_tags(link, time_id, timed)
  983. {
  984. if (!timed)
  985. {
  986. // C'est une nouvelle touche (pas redirigé) on lui donne un id
  987. // et on met a jour l'id de la dernière pressé
  988. last_keypress = new Date().getTime();
  989. var this_time_id = last_keypress;
  990. }
  991. else
  992. {
  993. // Si elle a été redirigé, on met son id dans cette variable
  994. var this_time_id = time_id;
  995. }
  996. // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
  997. if (time_id != last_keypress && timed)
  998. {
  999. // elle disparait
  1000. }
  1001. else
  1002. {
  1003. //
  1004. if ((new Date().getTime() - last_keypress) < 800 || timed == false)
  1005. {
  1006. // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
  1007. // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
  1008. // elle doit attendre au cas ou soit pressé.
  1009. setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
  1010. }
  1011. else
  1012. {
  1013. // il n'y a plus a attendre, on envoie la demande de tag.
  1014. return refresh_elements_with_tags_selected(link);
  1015. }
  1016. }
  1017. return null;
  1018. }
  1019. ////////////////////////////////////////
  1020. /// Gestion de nouveaux éléments
  1021. do_check_new_elements = false;
  1022. function check_new_elements()
  1023. {
  1024. if ($('ul.elements li').length)
  1025. {
  1026. // Si l'utilisateur a quitté la page on reporte le check
  1027. if ($('body.blurred').length)
  1028. {
  1029. // on passe la variable a vrai de façon a ce que lorsque la page
  1030. // et ré affiché on lance le check
  1031. do_check_new_elements = true;
  1032. }
  1033. else
  1034. {
  1035. var url = url_element_new_count
  1036. +'/'
  1037. +str_replace('element_', '', $('ul.elements li:first').attr('id'))
  1038. ;
  1039. $.getJSON(url, function(response){
  1040. if (response.status == 'mustbeconnected')
  1041. {
  1042. $(location).attr('href', url_index);
  1043. }
  1044. if (response.status == 'success' && response.count)
  1045. {
  1046. $('div.display_more_elements').show();
  1047. $('div.display_more_elements span').html(response.message);
  1048. }
  1049. setTimeout(check_new_elements, 150000);
  1050. });
  1051. do_check_new_elements = false;
  1052. }
  1053. }
  1054. }
  1055. if ($('div.display_more_elements').length)
  1056. {
  1057. setTimeout(check_new_elements, 150000);
  1058. }
  1059. $('a.show_new_elements').live('click', function(){
  1060. var url = url_element_new_get
  1061. +'/'
  1062. +str_replace('element_', '', $('ul.elements li:first').attr('id'))
  1063. ;
  1064. $('img.elements_new_loader').show();
  1065. $.getJSON(url, function(response){
  1066. if (response.status == 'mustbeconnected')
  1067. {
  1068. $(location).attr('href', url_index);
  1069. }
  1070. if (response.status == 'success')
  1071. {
  1072. if (response.count)
  1073. {
  1074. $('div.display_more_elements').show();
  1075. $('div.display_more_elements span').html(response.message);
  1076. }
  1077. else
  1078. {
  1079. $('div.display_more_elements').hide();
  1080. }
  1081. $('ul.elements').prepend(response.html);
  1082. recolorize_element_list();
  1083. }
  1084. $('img.elements_new_loader').hide();
  1085. });
  1086. });
  1087. function recolorize_element_list()
  1088. {
  1089. $('ul.elements li.element').each(function(index){
  1090. if ((index & 1) == 1)
  1091. {
  1092. $(this).removeClass('even');
  1093. $(this).removeClass('odd');
  1094. $(this).addClass('odd');
  1095. }
  1096. else
  1097. {
  1098. $(this).removeClass('odd');
  1099. $(this).removeClass('even');
  1100. $(this).addClass('even');
  1101. }
  1102. });
  1103. }
  1104. /*
  1105. * Action a effectuer lorsque l'utilisateur met le focus sur la page
  1106. */
  1107. function do_action_body_focused()
  1108. {
  1109. if (do_check_new_elements)
  1110. {
  1111. check_new_elements();
  1112. }
  1113. }
  1114. /*
  1115. * Commentaires d'élément
  1116. */
  1117. // Afficher les commentaires
  1118. $('td.element_content a.display_comments').live('click', function(){
  1119. display_comments(
  1120. $('li#element_'+
  1121. str_replace('#comments_', '', $(this).attr('href'))
  1122. )
  1123. );
  1124. });
  1125. $('td.element_content a.hide_comments').live('click', function(){
  1126. hide_comments(
  1127. $('li#element_'+
  1128. str_replace('#hide_comments_', '', $(this).attr('href'))
  1129. )
  1130. );
  1131. });
  1132. function display_comments(li_element)
  1133. {
  1134. li_element.find('div.comments').slideDown();
  1135. li_element.find('a.display_comments').hide();
  1136. li_element.find('a.hide_comments').show();
  1137. }
  1138. function hide_comments(li_element)
  1139. {
  1140. li_element.find('div.comments').slideUp();
  1141. li_element.find('a.display_comments').show();
  1142. li_element.find('a.hide_comments').hide();
  1143. }
  1144. // Ajouter un commentaire
  1145. $('li.element a.add_comment').live('click', function(){
  1146. display_add_comment($('li#element_'+
  1147. str_replace('#add_comment_', '', $(this).attr('href'))
  1148. ));
  1149. });
  1150. $('form.add_comment input[type="submit"]').live('click', function(){
  1151. $(this).parent('div').parent('form').parent('div.comments').find('img.comments_loader').show();
  1152. });
  1153. function display_add_comment(li_element)
  1154. {
  1155. display_comments(li_element);
  1156. li_element.find('a.add_comment').hide();
  1157. li_element.find('form.add_comment').show();
  1158. li_element.find('form.add_comment').ajaxForm(function(response) {
  1159. if (response.status == 'mustbeconnected')
  1160. {
  1161. $(location).attr('href', url_index);
  1162. }
  1163. li_element.find('img.comments_loader').hide();
  1164. if (response.status == 'success')
  1165. {
  1166. li_element.find('form.add_comment').find('ul.error_list').remove();
  1167. li_element.find('div.comments ul.comments').append(response.html);
  1168. hide_add_comment(li_element);
  1169. }
  1170. else if (response.status == 'error')
  1171. {
  1172. li_element.find('form.add_comment').find('ul.error_list').remove();
  1173. ul_errors = $('<ul>').addClass('error_list');
  1174. for (i in response.errors)
  1175. {
  1176. ul_errors.append($('<li>').append(response.errors[i]));
  1177. }
  1178. li_element.find('form.add_comment').prepend(ul_errors);
  1179. }
  1180. return false;
  1181. });
  1182. }
  1183. $('form.add_comment input.cancel').live('click', function(){
  1184. li_element = $(this).parent('div').parent('form').parent('div.comments').parent('li.element');
  1185. hide_add_comment(li_element);
  1186. });
  1187. function hide_add_comment(li_element)
  1188. {
  1189. li_element.find('a.add_comment').show();
  1190. li_element.find('form.add_comment').hide();
  1191. li_element.find('form.add_comment textarea').val('');
  1192. }
  1193. // Modifier et supprimer
  1194. // Affichage du bouton Modifier et Supprimer
  1195. $('ul.comments li.comment').live({
  1196. mouseenter:
  1197. function()
  1198. {
  1199. $(this).find('a.comment_edit_link').show();
  1200. $(this).find('a.comment_remove_link').show();
  1201. },
  1202. mouseleave:
  1203. function()
  1204. {
  1205. if (!$(this).find('a.comment_edit_link').hasClass('mustBeDisplayed'))
  1206. {
  1207. $(this).find('a.comment_edit_link').hide();
  1208. }
  1209. if (!$(this).find('a.comment_remove_link').hasClass('mustBeDisplayed'))
  1210. {
  1211. $(this).find('a.comment_remove_link').hide();
  1212. }
  1213. }
  1214. }
  1215. );
  1216. // Supprimer
  1217. $('a.comment_remove_link').jConfirmAction({
  1218. question : string_comment_delete_confirm_sentence,
  1219. yesAnswer : string_comment_delete_confirm_yes,
  1220. cancelAnswer : string_comment_delete_confirm_no,
  1221. onYes: function(link){
  1222. li = link.parent('li.comment');
  1223. li.find('img.comment_loader').show();
  1224. $.getJSON(link.attr('href'), function(response){
  1225. li.find('img.comment_loader').hide();
  1226. if (response.status == 'mustbeconnected')
  1227. {
  1228. $(location).attr('href', url_index);
  1229. }
  1230. if (response.status == 'success')
  1231. {
  1232. li.remove();
  1233. }
  1234. });
  1235. return false;
  1236. },
  1237. onOpen: function(link){
  1238. li = link.parent('li.comment');
  1239. li.find('a.comment_edit_link').addClass('mustBeDisplayed');
  1240. li.find('a.comment_remove_link').addClass('mustBeDisplayed');
  1241. },
  1242. onClose: function(link){
  1243. li = link.parent('li.comment');
  1244. li.find('a.comment_edit_link').removeClass('mustBeDisplayed');
  1245. li.find('a.comment_remove_link').removeClass('mustBeDisplayed');
  1246. li.find('a.comment_edit_link').hide();
  1247. li.find('a.comment_remove_link').hide();
  1248. }
  1249. });
  1250. comments_edited = new Array();
  1251. // Modification
  1252. // Ouverture du formulaire de modification
  1253. $('a.comment_edit_link').live('click', function(){
  1254. link = $(this);
  1255. li = link.parent('li.comment');
  1256. // On garde en mémoire l'élément édité en cas d'annulation
  1257. comments_edited[li.attr('id')] = li.html();
  1258. loader = li.find('img.comment_loader');
  1259. li.html(loader);
  1260. li.find('img.comment_loader').show();
  1261. $.getJSON($(this).attr('href'), function(response) {
  1262. if (response.status == 'mustbeconnected')
  1263. {
  1264. $(location).attr('href', url_index);
  1265. }
  1266. li.html(response.html);
  1267. // On rend ce formulaire ajaxFormable
  1268. $('li#'+li.attr('id')+' form.edit_comment input[type="submit"]').live('click', function(){
  1269. li_current = $(this).parent('div').parent('form').parent('li');
  1270. li_current.prepend(loader);
  1271. li_current.find('img.comment_loader').show();
  1272. });
  1273. li.find('form.edit_comment').ajaxForm(function(response){
  1274. li = $('li#'+response.dom_id);
  1275. li.find('img.comment_loader').hide();
  1276. if (response.status == 'mustbeconnected')
  1277. {
  1278. $(location).attr('href', url_index);
  1279. }
  1280. if (response.status == 'success')
  1281. {
  1282. li.html(response.html);
  1283. delete(comments_edited[li.attr('id')]);
  1284. }
  1285. else if (response.status == 'error')
  1286. {
  1287. li.find('ul.error_list').remove();
  1288. ul_errors = $('<ul>').addClass('error_list');
  1289. for (i in response.errors)
  1290. {
  1291. ul_errors.append($('<li>').append(response.errors[i]));
  1292. }
  1293. li.prepend(ul_errors);
  1294. }
  1295. });
  1296. });
  1297. return false;
  1298. });
  1299. // Annulation d'un formulaire de modification d'un comment
  1300. $('form.edit_comment input.cancel').live('click', function(){
  1301. var li = $(this).parent('div').parent('form').parent('li');
  1302. li.html(comments_edited[li.attr('id')]);
  1303. delete(comments_edited[li.attr('id')]);
  1304. });
  1305. /*
  1306. * Ajout d'un tag en favoris a partir d'un élément
  1307. */
  1308. $('li.element_tag').live({
  1309. mouseenter:
  1310. function()
  1311. {
  1312. $(this).find('a.tag_to_favorites').show();
  1313. $(this).find('a.element_tag').addClass('element_tag_large_for_fav');
  1314. },
  1315. mouseleave:
  1316. function()
  1317. {
  1318. if (!$(this).find('a.tag_to_favorites').hasClass('mustBeDisplayed'))
  1319. {
  1320. $(this).find('a.tag_to_favorites').hide();
  1321. $(this).find('a.element_tag').removeClass('element_tag_large_for_fav');
  1322. }
  1323. }
  1324. }
  1325. );
  1326. $('a.tag_to_favorites').jConfirmAction({
  1327. question : string_tag_addtofav_confirm_sentence,
  1328. yesAnswer : string_tag_addtofav_confirm_yes,
  1329. cancelAnswer : string_tag_addtofav_confirm_no,
  1330. onYes: function(link){
  1331. $.getJSON(link.attr('href'), function(response){
  1332. if (response.status == 'mustbeconnected')
  1333. {
  1334. $(location).attr('href', url_index);
  1335. }
  1336. });
  1337. $('div.question').fadeOut();
  1338. return false;
  1339. },
  1340. onOpen: function(link){
  1341. li = link.parent('li.element_tag');
  1342. li.find('a.tag_to_favorites').addClass('mustBeDisplayed');
  1343. },
  1344. onClose: function(link){
  1345. li = link.parent('li.element_tag');
  1346. li.find('a.tag_to_favorites').removeClass('mustBeDisplayed');
  1347. li.find('a.element_tag').removeClass('element_tag_large_for_fav');
  1348. li.find('a.tag_to_favorites').hide();
  1349. }
  1350. });
  1351. /*
  1352. * Ajout dans un groupe de l'élément envoyé
  1353. */
  1354. $('a.added_element_add_to_group').live('click', function(){
  1355. div = $(this).parent('li').parent('ul').parent('div');
  1356. div.find('img.loader').show();
  1357. $.getJSON($(this).attr('href'), function(response) {
  1358. div.find('img.loader').hide();
  1359. if (response.status == 'mustbeconnected')
  1360. {
  1361. $(location).attr('href', url_index);
  1362. }
  1363. if (response.status == 'success')
  1364. {
  1365. $('li#'+response.dom_id).html(response.html);
  1366. }
  1367. $('div#added_element_to_group').slideUp();
  1368. $('a#element_add_link').show();
  1369. });
  1370. return false;
  1371. });
  1372. $('div#added_element_to_group a.cancel').live('click', function(){
  1373. $('div#added_element_to_group').slideUp();
  1374. $('a#element_add_link').show();
  1375. return false;
  1376. });
  1377. /*
  1378. * Report / signalement d'un élément
  1379. */
  1380. $('a.element_report').jConfirmAction({
  1381. question : string_elementreport_confirm_sentence,
  1382. yesAnswer : string_elementreport_confirm_yes,
  1383. cancelAnswer : string_elementreport_confirm_no,
  1384. onYes: function(link){
  1385. $.getJSON(link.attr('href'), function(response){
  1386. if (response.status == 'mustbeconnected')
  1387. {
  1388. $(location).attr('href', url_index);
  1389. }
  1390. });
  1391. $('div.question').fadeOut();
  1392. return false;
  1393. },
  1394. onOpen: function(link){
  1395. },
  1396. onClose: function(link){
  1397. }
  1398. });
  1399. /*
  1400. * Vote sur element
  1401. */
  1402. $('li.element a.vote').live('click', function(){
  1403. img = $(this).find('img');
  1404. link = $(this);
  1405. img.attr('src', url_img_ajax_loader);
  1406. $.getJSON(link.attr('href'), function(response){
  1407. if (response.status == 'mustbeconnected')
  1408. {
  1409. $(location).attr('href', url_index);
  1410. }
  1411. if (response.status == 'success')
  1412. {
  1413. link.attr('href', response.data.a.href);
  1414. img.attr('src', response.data.img.src);
  1415. link.parent('li').parent('ul').find('li.score span.score').html(response.data.element.points);
  1416. }
  1417. });
  1418. return false;
  1419. });
  1420. // Enlever les ids du ElementSearch
  1421. $('div.more_filters a.new_comments, div.more_filters a.new_favorites').live('click', function(){
  1422. $('img.elements_more_loader').show();
  1423. $('ul.elements').html('');
  1424. link = $(this);
  1425. $.getJSON(link.attr('href'), function(response){
  1426. if (response.status == 'mustbeconnected')
  1427. {
  1428. $(location).attr('href', url_index);
  1429. }
  1430. if (response.status == 'success')
  1431. {
  1432. $('form[name="search"]').submit();
  1433. $('div.more_filters a.new_comments').hide();
  1434. $('div.more_filters a.new_favorites').hide();
  1435. }
  1436. });
  1437. return false;
  1438. });
  1439. /*
  1440. *
  1441. * Proposition de tags sur un élément
  1442. *
  1443. */
  1444. // Ouverture du formulaire de modification
  1445. $('a.element_propose_tags').live('click', function(){
  1446. link = $(this);
  1447. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  1448. li.find('img.element_loader').show();
  1449. $.getJSON($(this).attr('href'), function(response) {
  1450. if (response.status == 'mustbeconnected')
  1451. {
  1452. $(location).attr('href', url_index);
  1453. }
  1454. li.find('img.element_loader').hide();
  1455. if (response.status == 'success')
  1456. {
  1457. // On prépare le tagBox
  1458. table = li.find('table:first');
  1459. li.find('div.tag_proposition').remove();
  1460. table.after(response.html);
  1461. // Pour le click sur l'input de saisie de tag
  1462. li.find('ul.tagbox li.input input[type="text"]').formDefaults();
  1463. var options = new Array();
  1464. options.form_name = response.form_name;
  1465. options.tag_init = response.tags;
  1466. ajax_query_timestamp = null;
  1467. $("#tags_prompt_list_"+response.form_name).tagBox(options);
  1468. // On rend ce formulaire ajaxFormable
  1469. $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
  1470. li = $(this).parent('form').parent('div').parent('li');
  1471. li.find('img.element_loader').show();
  1472. });
  1473. $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
  1474. if (response.status == 'mustbeconnected')
  1475. {
  1476. $(location).attr('href', url_index);
  1477. }
  1478. if (response.status == 'success')
  1479. {
  1480. li = $('li#'+response.dom_id);
  1481. li.find('img.element_loader').hide();
  1482. li.find('form')
  1483. li.find('div.tag_proposition').remove();
  1484. }
  1485. });
  1486. }
  1487. });
  1488. return false;
  1489. });
  1490. // Annulation d'un formulaire de modification d'élément
  1491. //
  1492. // TODO TODO TODO
  1493. // ca en dessous la: TODO neuu
  1494. //
  1495. $('form.edit_element input.cancel_edit').live('click', function(){
  1496. var li = $(this).parent('form').parent('li');
  1497. li.html(elements_edited[li.attr('id')]);
  1498. delete(elements_edited[li.attr('id')]);
  1499. });
  1500. ///////
  1501. $('a.element_view_propositions_link').live('click', function(){
  1502. link = $(this);
  1503. li = link.parent('td').parent('tr').parent().parent().parent('li.element');
  1504. li.find('img.element_loader').show();
  1505. $.getJSON($(this).attr('href'), function(response) {
  1506. if (response.status == 'mustbeconnected')
  1507. {
  1508. $(location).attr('href', url_index);
  1509. }
  1510. li.find('img.element_loader').hide();
  1511. if (response.status == 'success')
  1512. {
  1513. table = li.find('table:first');
  1514. li.find('div.tags_proposition_view').remove();
  1515. table.after(response.html);
  1516. }
  1517. });
  1518. return false;
  1519. });
  1520. $('a.accept_tag_propotision').live('click', function(){
  1521. link = $(this);
  1522. li = link.parent('li.tag_proposition').parent('ul.tag_propositions')
  1523. .parent('div.tags_proposition_view').parent('li.element');
  1524. li.find('img.element_loader').show();
  1525. $.getJSON($(this).attr('href'), function(response) {
  1526. if (response.status == 'mustbeconnected')
  1527. {
  1528. $(location).attr('href', url_index);
  1529. }
  1530. li.find('img.element_loader').hide();
  1531. if (response.status == 'success')
  1532. {
  1533. li.html(response.html);
  1534. }
  1535. });
  1536. return false;
  1537. });
  1538. //
  1539. $('a.refuse_tag_propositions').live('click', function(){
  1540. link = $(this);
  1541. li = link.parent('div.tags_proposition_view').parent('li.element');
  1542. li.find('img.element_loader').show();
  1543. $.getJSON($(this).attr('href'), function(response) {
  1544. if (response.status == 'mustbeconnected')
  1545. {
  1546. $(location).attr('href', url_index);
  1547. }
  1548. li.find('img.element_loader').hide();
  1549. if (response.status == 'success')
  1550. {
  1551. li.find('div.tags_proposition_view').remove();
  1552. }
  1553. });
  1554. return false;
  1555. });
  1556. /*
  1557. * Proposition de tag sur un élément FIN
  1558. */
  1559. });