123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- /*
- * Scripts de Muzi.ch
- * Rédigé et propriété de Sevajol Bastien (http://www.bux.fr)
- *
- */
-
- // Messages flashs
-
- var myMessages = ['info','warning','error','success']; // define the messages types
-
- function hideAllMessages()
- {
- var messagesHeights = new Array(); // this array will store height for each
-
- for (i=0; i<myMessages.length; i++)
- {
- messagesHeights[i] = $('.' + myMessages[i]).outerHeight();
- $('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport
- }
- }
-
- $(document).ready(function(){
-
- // Initially, hide them all
- hideAllMessages();
-
- $('.message').animate({top:"0"}, 500);
-
- // When message is clicked, hide it
- $('.message a.message-close').click(function(){
- $(this).parent('.message').animate({top: -$(this).outerHeight()-50}, 700);
- return false;
- });
-
- });
-
- function findKeyWithValue(arrayt, value)
- {
- for(i in arrayt)
- {
- if (arrayt[i] == value)
- {
- return i;
- }
- }
- return "";
- }
-
- function json_to_array(json_string)
- {
- if (json_string.length)
- {
- return eval("(" + json_string + ")");
- }
- return new Array();
- }
-
- function strpos (haystack, needle, offset) {
- // Finds position of first occurrence of a string within another
- //
- // version: 1109.2015
- // discuss at: http://phpjs.org/functions/strpos // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Onno Marsman
- // + bugfixed by: Daniel Esteban
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // * example 1: strpos('Kevin van Zonneveld', 'e', 5); // * returns 1: 14
- var i = (haystack + '').indexOf(needle, (offset || 0));
- return i === -1 ? false : i;
- }
-
- /**
- * Converts the given data structure to a JSON string.
- * Argument: arr - The data structure that must be converted to JSON
- * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
- * var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
- * http://www.openjs.com/scripts/data/json_encode.php
- */
- function array2json(arr) {
- var parts = [];
- var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
-
- for(var key in arr) {
- var value = arr[key];
- if(typeof value == "object") { //Custom handling for arrays
- if(is_list) parts.push(array2json(value)); /* :RECURSION: */
- else parts[key] = array2json(value); /* :RECURSION: */
- } else {
- var str = "";
- if(!is_list) str = '"' + key + '":';
-
- //Custom handling for multiple data types
- if(typeof value == "number") str += value; //Numbers
- else if(value === false) str += 'false'; //The booleans
- else if(value === true) str += 'true';
- else str += '"' + value + '"'; //All other things
- // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
-
- parts.push(str);
- }
- }
- var json = parts.join(",");
-
- if(is_list) return '[' + json + ']';//Return numerical JSON
- return '{' + json + '}';//Return associative JSON
- }
-
- function isInteger(s) {
- return (s.toString().search(/^-?[0-9]+$/) == 0);
- }
-
- function inArray(array, p_val) {
- var l = array.length;
- for(var i = 0; i < l; i++) {
- if(array[i] == p_val) {
- return true;
- }
- }
- return false;
- }
-
- if(typeof(String.prototype.trim) === "undefined")
- {
- String.prototype.trim = function()
- {
- return String(this).replace(/^\s+|\s+$/g, '');
- };
- }
-
- function str_replace (search, replace, subject, count) {
- // Replaces all occurrences of search in haystack with replace
- //
- // version: 1109.2015
- // discuss at: http://phpjs.org/functions/str_replace // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Gabriel Paderni
- // + improved by: Philip Peterson
- // + improved by: Simon Willison (http://simonwillison.net)
- // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfixed by: Anton Ongson
- // + input by: Onno Marsman
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + tweaked by: Onno Marsman
- // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Oleg Eremeev
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Oleg Eremeev
- // % 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
- // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
- // * returns 1: 'Kevin.van.Zonneveld'
- // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
- // * returns 2: 'hemmo, mars' var i = 0,
- j = 0,
- temp = '',
- repl = '',
- sl = 0, fl = 0,
- f = [].concat(search),
- r = [].concat(replace),
- s = subject,
- ra = Object.prototype.toString.call(r) === '[object Array]', sa = Object.prototype.toString.call(s) === '[object Array]';
- s = [].concat(s);
- if (count) {
- this.window[count] = 0;
- }
- for (i = 0, sl = s.length; i < sl; i++) {
- if (s[i] === '') {
- continue;
- }for (j = 0, fl = f.length; j < fl; j++) {
- temp = s[i] + '';
- repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
- s[i] = (temp).split(f[j]).join(repl);
- if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
- }
- }
- }
- return sa ? s : s[0];
- }
-
- $(document).ready(function(){
-
-
- // Bouton de personalisation du filtre
- // pour le moment ce ne sotn que des redirection vers des actions
- $('.tags_prompt input.clear, a.filter_clear_url').live("click", function(){
- $(location).attr('href', $('input.filter_clear_url').val());
- });
- $('.tags_prompt input.mytags').live("click", function(){
- $(location).attr('href', $('input.filter_mytags_url').val());
- });
-
- // Affichage un/des embed
- $('a.element_embed_open_link').live("click", function(){
- $(this).parent().parent('li.element').find('a.element_embed_open_link').hide();
- $(this).parent().parent('li.element').find('a.element_embed_close_link').show();
- $(this).parent().parent('li.element').find('div.element_embed').show();
- return false;
- });
-
- // Fermeture du embed si demandé
- $('a.element_embed_close_link').live("click", function(){
- $(this).parent().parent('li.element').find('a.element_embed_open_link').show();
- $(this).parent().parent('li.element').find('a.element_embed_close_link').hide();
- $(this).parent().parent('li.element').find('div.element_embed').hide();
- return false;
- });
-
- // Mise en favoris
- $('a.favorite_link').live("click", function(){
- link = $(this);
- $.getJSON($(this).attr('href'), function(response) {
- img = link.find('img');
- link.attr('href', response.link_new_url);
- img.attr('src', response.img_new_src);
- img.attr('title', response.img_new_title);
- });
- return false;
- });
-
- // Affichage du bouton Modifier et Supprimer
- $('ul.elements li.element').live({
- mouseenter:
- function()
- {
- $(this).find('a.element_edit_link').show();
- $(this).find('a.element_remove_link').show();
- },
- mouseleave:
- function()
- {
- if (!$(this).find('a.element_edit_link').hasClass('mustBeDisplayed'))
- {
- $(this).find('a.element_edit_link').hide();
- }
- if (!$(this).find('a.element_remove_link').hasClass('mustBeDisplayed'))
- {
- $(this).find('a.element_remove_link').hide();
- }
- }
- }
- );
-
- // Plus d'éléments
- last_id = null;
- $('a.elements_more').click(function(){
- link = $(this);
- last_element = $('ul.elements li.element:last-child');
- id_last = str_replace('element_', '', last_element.attr('id'));
- invertcolor = 0;
- if (last_element.hasClass('even'))
- {
- invertcolor = 1;
- }
- $('img.elements_more_loader').show();
- $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
- if (response.count)
- {
- $('ul.elements').append(response.html);
- $('img.elements_more_loader').hide();
- }
-
- if (response.end || response.count < 1)
- {
- $('img.elements_more_loader').hide();
- $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
- response.message+'</p></div>');
- link.hide();
- }
- });
- return false;
- });
-
- tag_box_input_value = $('ul.tagbox input[type="text"]').val();
-
- // Filtre et affichage éléments ajax
- $('form[name="search"] input[type="submit"]').click(function(){
- $('ul.elements').html('');
- $('div.no_elements').hide();
- $('img.elements_more_loader').show();
- });
-
- $('form[name="search"]').ajaxForm(function(response) {
-
- $('ul.elements').html(response.html);
-
- if (response.count)
- {
- $('img.elements_more_loader').hide();
- $('span.elements_more').show();
- $('a.elements_more').show();
- }
-
- if (response.count < 1)
- {
- $('img.elements_more_loader').hide();
- $('ul.elements').after('<div class="no_elements"><p class="no-elements">'+
- response.message+'</p></div>');
- $('a.elements_more').hide()
- ;
- }
-
- $('ul.tagbox input[type="text"]').val(tag_box_input_value);
-
- });
-
- // Suppression d'un element
- $('a.element_remove_link').jConfirmAction({
- question : "Vraiment supprimer ?",
- yesAnswer : "Oui",
- cancelAnswer : "Non",
- onYes: function(link){
-
- li = link.parent('li.element');
- li.find('img.element_loader').show();
- $.getJSON(link.attr('href'), function(response){
- if (response.status == 'success')
- {
- li.remove();
- }
- else
- {
- li.find('img.element_loader').hide();
- }
- });
-
- return false;
- },
- onOpen: function(link){
- li = link.parent('li.element');
- li.find('a.element_edit_link').addClass('mustBeDisplayed');
- li.find('a.element_remove_link').addClass('mustBeDisplayed');
- },
- onClose: function(link){
- li = link.parent('li.element');
- li.find('a.element_edit_link').removeClass('mustBeDisplayed');
- li.find('a.element_remove_link').removeClass('mustBeDisplayed');
- li.find('a.element_edit_link').hide();
- li.find('a.element_remove_link').hide();
- }
- });
-
- elements_edited = new Array();
- // Ouverture du formulaire de modification
- $('a.element_edit_link').live('click', function(){
-
- link = $(this);
- li = link.parent('li.element');
- // On garde en mémoire l'élément édité en cas d'annulation
- elements_edited[li.attr('id')] = li.html();
- div_loader = li.find('div.loader');
- li.html(div_loader);
- li.find('img.element_loader').show();
-
- $.getJSON($(this).attr('href'), function(response) {
-
- // On prépare le tagBox
- li.html(response.html);
-
- var options = new Array();
- options.form_name = response.form_name;
- options.tag_init = response.tags;
-
- ajax_query_timestamp = null;
-
- $("#tags_prompt_list_"+response.form_name).tagBox(options);
-
- // On rend ce formulaire ajaxFormable
- $('form[name="'+response.form_name+'"] input[type="submit"]').live('click', function(){
- li.prepend(div_loader);
- li.find('img.element_loader').show();
- });
- $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
-
- if (response.status == 'success')
- {
- li.html(response.html);
- delete(elements_edited[li.attr('id')]);
- }
- else if (response.status == 'error')
- {
- li.find('img.element_loader').hide();
- li.find('ul.error_list').remove();
- ul_errors = $('<ul>').addClass('error_list');
-
- for (i in response.errors)
- {
- ul_errors.append($('<li>').append(response.errors[i]));
- }
-
- li.prepend(ul_errors);
- }
- });
-
- });
- return false;
- });
-
- // Annulation d'un formulaire de modification d'élément
- $('form.edit_element input.cancel_edit').live('click', function(){
- var li = $(this).parent('form').parent('li');
- li.html(elements_edited[li.attr('id')]);
- delete(elements_edited[li.attr('id')]);
- });
-
- ////////////////// TAG PROMPT ///////////////
-
- ajax_query_timestamp = null;
- tag_text_help = $('input.tag_text_help').val();
-
- // Les deux clicks ci-dessous permettent de faire disparaitre
- // la div de tags lorsque l'on clique ailleurs
- $('html').click(function() {
- if ($("div.search_tag_list").is(':visible'))
- {
- $("div.search_tag_list").hide();
- }
- });
-
- $("div.search_tag_list").live('click', function(event){
- event.stopPropagation();
- });
-
- function autocomplete_tag(input, form_name)
- {
- // Il doit y avoir au moin un caractère
- if (input.val().length > 0)
- {
-
- // on met en variable l'input
- inputTag = input;
-
- // On récupére la div de tags
- divtags = $("#search_tag_"+form_name);
-
- // Si la fenêtre de tags est caché
- if (!divtags.is(':visible'))
- {
- // On la replace
- position = input.position();
- divtags.css('left', Math.round(position.left) + 5);
- divtags.css('top', Math.round(position.top) + 28);
- // Et on l'affiche
- divtags.show();
- }
- // On affiche le loader
- $('#tag_loader_'+form_name).show();
- // On cache la liste de tags
- search_tag_list = divtags.find('ul.search_tag_list');
- // On supprime les anciens li
- search_tag_list.find('li').remove();
- search_tag_list.hide();
- // Et on affiche une info
- span_info = divtags.find('span.info');
- span_info.show();
- span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
-
- // C'est en fonction du nb de resultats qu'il sera affiché
- divtags.find('a.more').hide();
-
- // On récupère le timestamp pour reconnaitre la dernière requête effectué
- ajax_query_timestamp = new Date().getTime();
-
- // Récupération des tags correspondants
- $.getJSON('/app_dev.php/fr/search/tag/'+input.val()+'/'+ajax_query_timestamp, function(data) {
- // Ce contrôle permet de ne pas continuer si une requete
- // ajax a été faite depuis.
- if (data.timestamp == ajax_query_timestamp)
- {
- status = data.status;
- tags = data.data;
-
- // Si on spécifie une erreur
- if (status == 'error')
- {
- // On l'affiche a l'utilisateur
- span_info.text(data.error);
- }
- // Si c'est un succés
- else if (status == 'success')
- {
- if (tags.length > 0)
- {
- more = false;
- // Pour chaque tags retournés
- for (i in tags)
- {
- var tag_name = tags[i]['name'];
- var tag_id = tags[i]['id'];
- // On construit un li
- var sstr = $.trim(input.val());
- var re = new RegExp(sstr, "i") ;
- var t_string = tag_name.replace(re,"<strong>" + sstr + "</strong>");
-
- li_tag =
- $('<li>').append(
- $('<a>').attr('href','#'+tag_id+'#'+tag_name)
- // qui réagit quand on clique dessus
- .click(function(e){
- // On récupère le nom du tag
- name = $(this).attr('href').substr(1,$(this).attr('href').length);
- name = name.substr(strpos(name, '#')+1, name.length);
-
- id = $(this).attr('href').substr(1,$(this).attr('href').length);
- id = str_replace(name, '', id);
- id = str_replace('#', '', id);
-
- $('input#tags_selected_tag_'+form_name).val(id);
- inputTag.val(name);
- // Et on execute l'évènement selectTag de l'input
- inputTag.trigger("selectTag");
- // On cache la liste puisque le choix vient d'être fait
- divtags.hide();
- inputTag.val(tag_text_help);
- return false;
- })
- .append(t_string)
- );
-
- // Si on depasse les 30 tags
- if (i > 30)
- {
- more = true;
- // On le cache
- li_tag.hide();
- }
-
- // On ajout ce li a la liste
- search_tag_list.append(li_tag);
- }
-
- if (more)
- {
- divtags.find('a.more').show();
- }
-
- // On cache l'info
- span_info.hide();
- // Et on affiche la liste
- search_tag_list.show();
- }
- else
- {
- span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
- }
-
- }
-
- // On cache le loader
- $('#tag_loader_'+form_name).hide();
- }
- });
-
- }
- }
-
-
- last_keypress = 0;
-
- function check_timelaps_and_search(input, form_name, time_id, timed, info)
- {
- if (!timed)
- {
- // C'est une nouvelle touche (pas redirigé) on lui donne un id
- // et on met a jour l'id de la dernière pressé
- last_keypress = new Date().getTime();
- var this_time_id = last_keypress;
- }
- else
- {
- // Si elle a été redirigé, on met son id dans cette variable
- var this_time_id = time_id;
- }
-
- // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
- if (time_id != last_keypress && timed)
- {
- // elle disparait
- }
- else
- {
- //
- if ((new Date().getTime() - last_keypress) < 600 || timed == false)
- {
- // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
- // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
- // elle doit attendre au cas ou soit pressé.
- setTimeout(function(){check_timelaps_and_search(input, form_name, this_time_id, true, info)}, 700);
- }
- else
- {
- // il n'y a plus a attendre, on envoie la demande de tag.
- autocomplete_tag(input, form_name);
- }
- }
- }
-
- // Autocompletion de tags
- $("div.tags_prompt ul.tagbox li.input input").live('keypress', function(e){
-
- var form_name = $(this).parent('li').parent('ul.tagbox')
- .parent('div.tags_prompt').parent('form').attr('name')
- ;
- var code = (e.keyCode ? e.keyCode : e.which);
-
- if ((e.which !== 0 && e.charCode !== 0) || (code == 8 || code == 46))
- {
- check_timelaps_and_search($(this), form_name, new Date().getTime(), false, $(this).val());
- }
-
- });
-
- // Un click sur ce lien affiche tout les tags cachés de la liste
- $('div.search_tag_list a.more').live('click', function(){
- jQuery.each( $(this).parent('div').find('ul.search_tag_list li') , function(){
- $(this).show();
- });
- return false;
- });
-
- $('ul.tagbox li.input input[type="text"]').val(tag_text_help);
- $('ul.tagbox li.input input[type="text"]').formDefaults();
-
- ////////////////// FIN TAG PROMPT ///////////////
-
-
- });
|