muzich.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Scripts de Muzi.ch
  3. * Rédigé et propriété de Sevajol Bastien (http://www.bux.fr)
  4. *
  5. */
  6. // Messages flashs
  7. var myMessages = ['info','warning','error','success']; // define the messages types
  8. function hideAllMessages()
  9. {
  10. var messagesHeights = new Array(); // this array will store height for each
  11. for (i=0; i<myMessages.length; i++)
  12. {
  13. messagesHeights[i] = $('.' + myMessages[i]).outerHeight();
  14. $('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport
  15. }
  16. }
  17. $(document).ready(function(){
  18. // Initially, hide them all
  19. hideAllMessages();
  20. $('.message').animate({top:"0"}, 500);
  21. // When message is clicked, hide it
  22. $('.message a.message-close').click(function(){
  23. $(this).parent('.message').animate({top: -$(this).outerHeight()-50}, 700);
  24. return false;
  25. });
  26. });
  27. function findKeyWithValue(arrayt, value)
  28. {
  29. for(i in arrayt)
  30. {
  31. if (arrayt[i] == value)
  32. {
  33. return i;
  34. }
  35. }
  36. return "";
  37. }
  38. function json_to_array(json_string)
  39. {
  40. if (json_string.length)
  41. {
  42. return eval("(" + json_string + ")");
  43. }
  44. return new Array();
  45. }
  46. /**
  47. * Converts the given data structure to a JSON string.
  48. * Argument: arr - The data structure that must be converted to JSON
  49. * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
  50. * var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
  51. * http://www.openjs.com/scripts/data/json_encode.php
  52. */
  53. function array2json(arr) {
  54. var parts = [];
  55. var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
  56. for(var key in arr) {
  57. var value = arr[key];
  58. if(typeof value == "object") { //Custom handling for arrays
  59. if(is_list) parts.push(array2json(value)); /* :RECURSION: */
  60. else parts[key] = array2json(value); /* :RECURSION: */
  61. } else {
  62. var str = "";
  63. if(!is_list) str = '"' + key + '":';
  64. //Custom handling for multiple data types
  65. if(typeof value == "number") str += value; //Numbers
  66. else if(value === false) str += 'false'; //The booleans
  67. else if(value === true) str += 'true';
  68. else str += '"' + value + '"'; //All other things
  69. // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
  70. parts.push(str);
  71. }
  72. }
  73. var json = parts.join(",");
  74. if(is_list) return '[' + json + ']';//Return numerical JSON
  75. return '{' + json + '}';//Return associative JSON
  76. }
  77. function isInteger(s) {
  78. return (s.toString().search(/^-?[0-9]+$/) == 0);
  79. }
  80. function inArray(array, p_val) {
  81. var l = array.length;
  82. for(var i = 0; i < l; i++) {
  83. if(array[i] == p_val) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. if(typeof(String.prototype.trim) === "undefined")
  90. {
  91. String.prototype.trim = function()
  92. {
  93. return String(this).replace(/^\s+|\s+$/g, '');
  94. };
  95. }
  96. function str_replace (search, replace, subject, count) {
  97. // Replaces all occurrences of search in haystack with replace
  98. //
  99. // version: 1109.2015
  100. // discuss at: http://phpjs.org/functions/str_replace // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  101. // + improved by: Gabriel Paderni
  102. // + improved by: Philip Peterson
  103. // + improved by: Simon Willison (http://simonwillison.net)
  104. // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfixed by: Anton Ongson
  105. // + input by: Onno Marsman
  106. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  107. // + tweaked by: Onno Marsman
  108. // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  109. // + input by: Oleg Eremeev
  110. // + improved by: Brett Zamir (http://brett-zamir.me)
  111. // + bugfixed by: Oleg Eremeev
  112. // % 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
  113. // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
  114. // * returns 1: 'Kevin.van.Zonneveld'
  115. // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
  116. // * returns 2: 'hemmo, mars' var i = 0,
  117. j = 0,
  118. temp = '',
  119. repl = '',
  120. sl = 0, fl = 0,
  121. f = [].concat(search),
  122. r = [].concat(replace),
  123. s = subject,
  124. ra = Object.prototype.toString.call(r) === '[object Array]', sa = Object.prototype.toString.call(s) === '[object Array]';
  125. s = [].concat(s);
  126. if (count) {
  127. this.window[count] = 0;
  128. }
  129. for (i = 0, sl = s.length; i < sl; i++) {
  130. if (s[i] === '') {
  131. continue;
  132. }for (j = 0, fl = f.length; j < fl; j++) {
  133. temp = s[i] + '';
  134. repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
  135. s[i] = (temp).split(f[j]).join(repl);
  136. if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
  137. }
  138. }
  139. }
  140. return sa ? s : s[0];
  141. }
  142. $(document).ready(function(){
  143. // Bouton de personalisation du filtre
  144. // pour le moment ce ne sotn que des redirection vers des actions
  145. $('.tags_prompt input.clear, .filter_clear_url').click(function(){
  146. $(location).attr('href', $('input.filter_clear_url').val());
  147. });
  148. $('.tags_prompt input.mytags').click(function(){
  149. $(location).attr('href', $('input.filter_mytags_url').val());
  150. });
  151. // On met les listeners lié aux éléments de façon a pouvoir écouter
  152. // les événenements des éléments chargés en ajax en appelelant
  153. // cette fonction après un ptit coup d'ajax
  154. function init_elements()
  155. {
  156. // Affichage un/des embed
  157. $('a.element_embed_open_link').click(function(){
  158. $(this).parent().parent('li.element').find('a.element_embed_open_link').hide();
  159. $(this).parent().parent('li.element').find('a.element_embed_close_link').show();
  160. $(this).parent().parent('li.element').find('div.element_embed').show();
  161. return false;
  162. });
  163. // Fermeture du embed si demandé
  164. $('a.element_embed_close_link').click(function(){
  165. $(this).parent().parent('li.element').find('a.element_embed_open_link').show();
  166. $(this).parent().parent('li.element').find('a.element_embed_close_link').hide();
  167. $(this).parent().parent('li.element').find('div.element_embed').hide();
  168. return false;
  169. });
  170. // Mise en favoris
  171. $('a.favorite_link').click(function(){
  172. link = $(this);
  173. $.getJSON($(this).attr('href'), function(response) {
  174. img = link.find('img');
  175. link.attr('href', response.link_new_url);
  176. img.attr('src', response.img_new_src);
  177. img.attr('title', response.img_new_title);
  178. });
  179. return false;
  180. });
  181. }
  182. init_elements();
  183. // Plus d'éléments
  184. last_id = null;
  185. $('a.elements_more').click(function(){
  186. link = $(this);
  187. last_element = $('ul.elements li.element:last-child');
  188. id_last = str_replace('element_', '', last_element.attr('id'));
  189. invertcolor = 0;
  190. if (last_element.hasClass('even'))
  191. {
  192. invertcolor = 1;
  193. }
  194. $('img.elements_more_loader').show();
  195. $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
  196. if (response.count)
  197. {
  198. $('ul.elements').append(response.html);
  199. $('img.elements_more_loader').hide();
  200. init_elements();
  201. }
  202. if (response.end || response.count < 1)
  203. {
  204. $('img.elements_more_loader').hide();
  205. $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
  206. response.message+'</p></div>');
  207. link.hide();
  208. }
  209. });
  210. return false;
  211. });
  212. });