muzich.js 9.6KB

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