var sankoreLang = { display: "Afficher", edit: "Modifier", short_desc: "Combien de signaux entendez-vous?", add: "Nouveau bloc", enter: "Saisir votre description ici ...", wgt_name: "Associer le son", reload: "Recharger", slate: "Bois", pad: "Pad" }; //main function function start(){ $("#wgt_display").text(sankoreLang.display); $("#wgt_edit").text(sankoreLang.edit); $("#wgt_name").text(sankoreLang.wgt_name); $("#wgt_reload").text(sankoreLang.reload); $(".style_select option[value='1']").text(sankoreLang.slate); $(".style_select option[value='2']").text(sankoreLang.pad); if(window.sankore){ if(sankore.preference("associer_sound","")){ var data = jQuery.parseJSON(sankore.preference("associer_sound","")); importData(data); } else showExample(); if(sankore.preference("as_snd_style","")){ changeStyle(sankore.preference("as_snd_style","")); $(".style_select").val(sankore.preference("as_snd_style","")); } else changeStyle(1) } else showExample(); //events if (window.widget) { window.widget.onleave = function(){ exportData(); sankore.setPreference("as_snd_style", $(".style_select").find("option:selected").val()); } } $("#wgt_reload").click(function(){ window.location.reload(); }); $(".style_select").change(function (event){ changeStyle($(this).find("option:selected").val()); }) $("#wgt_display, #wgt_edit").click(function(event){ if(this.id == "wgt_display"){ if(!$(this).hasClass("selected")){ if(window.sankore) sankore.enableDropOnWidget(false); $(this).addClass("selected"); $("#wgt_edit").removeClass("selected"); $(".style_select").css("display","none"); $(".add_block").remove(); $(".cont").each(function(){ var container = $(this); var tmp_i = 0; var tmp_array = []; container.find(".text_cont .audio_desc").removeAttr("contenteditable"); container.find(".text_cont").removeAttr("ondragenter") .removeAttr("ondragleave") .removeAttr("ondragover") .removeAttr("ondrop") container.find(".add_img").remove(); container.find(".close_cont").remove(); container.find(".img_block").each(function(){ if($(this).find("img").attr("src") != "img/drop_img.png"){ $(this).find(".close_img").remove(); $(this).find(".true_img").remove(); $(this).find(".false_img").remove(); $(this).removeAttr("ondragenter") .removeAttr("ondragleave") .removeAttr("ondragover") .removeAttr("ondrop") .addClass("img_gray"); } else $(this).remove(); }); var img_answers = $("
").insertAfter(container.find(".sub_cont")); container.find(".img_block").each(function(){ $(this).css("float",""); tmp_array.push($(this)); }); tmp_array = shuffle(tmp_array); for(var i = 0; i"); var answers = ""; $(this).parent().find(".imgs_answers .img_block").each(function(){ answers += $(this).find("input").val(); }); if(tmp_cont.children()[0].tagName == "IMG") tmp_cont.removeClass("imgs_answers_green") .removeClass("imgs_answers_red") .addClass("imgs_answers_gray"); else if(tmp_cont.children().length == 1){ if(tmp_cont.children().find("input").val() == "1") tmp_cont.removeClass("imgs_answers_gray") .removeClass("imgs_answers_red") .addClass("imgs_answers_green"); else tmp_cont.removeClass("imgs_answers_gray") .removeClass("imgs_answers_green") .addClass("imgs_answers_red"); } } } } }); }); $(this).css("display", "none"); $("#wgt_edit").css("display", "block"); } } else { if(!$(this).hasClass("selected")){ if(window.sankore) sankore.enableDropOnWidget(true); $(this).addClass("selected"); $("#wgt_display").removeClass("selected"); $(".style_select").css("display","block"); $(".cont").each(function(){ var container = $(this); container.find(".imgs_answers").find(".img_block").each(function(){ $(this).appendTo(container.find(".imgs_cont")) }); container.find(".imgs_answers").remove(); $("
").appendTo(container); container.find(".imgs_cont").css("background-color", ""); container.find(".text_cont .audio_desc").attr("contenteditable","true"); container.find(".text_cont").attr("ondragenter", "return false;") .attr("ondragleave", "$(this).removeClass('gray'); return false;") .attr("ondragover", "$(this).addClass('gray'); return false;") .attr("ondrop", "$(this).removeClass('gray'); return onDropAudio(this,event);"); var add_img = $("
"); container.find(".img_block").each(function(){ $(this).draggable("destroy"); $(this).attr("ondragenter", "return false;") .attr("ondragleave", "$(this).css(\"background-color\",\"\"); return false;") .attr("ondragover", "$(this).css(\"background-color\",\"#ccc\"); return false;") .attr("ondrop", "$(this).css(\"background-color\",\"\"); return onDropTarget(this,event);") .css("float","left"); $("
").appendTo($(this)); if($(this).find("input").val() == "1"){ $("
").appendTo($(this)); } else{ $("
").appendTo($(this)); } }); container.find(".imgs_cont").append(add_img) }); $("
" + sankoreLang.add + "
").appendTo("#data"); $(this).css("display", "none"); $("#wgt_display").css("display", "block"); } } $("audio").each(function(){ this.pause(); $(this).parent().find(":first-child").removeClass("stop").addClass("play"); }); }); //add new block $(".add_block").live("click", function(){ addContainer(); }); //adding new img $(".add_img").live("click", function(){ addImgBlock($(this)); }); //deleting a block $(".close_cont").live("click",function(){ $(this).parent().remove(); refreshBlockNumbers(); }); //deleting the img block $(".close_img").live("click", function(){ $(this).parent().remove(); }); //correct image $(".true_img").live("click",function(){ $(this).parent().parent().find(".img_block").each(function(){ $(this).find("input").val(0); var tmp_div = $(this).find(".false_img"); if(tmp_div) tmp_div.removeClass("false_img").addClass("true_img"); }) $(this).parent().find("input").val(1); $(this).removeClass("true_img").addClass("false_img"); }); //wrong image $(".false_img").live("click",function(){ $(this).parent().find("input").val(0); $(this).removeClass("false_img").addClass("true_img"); }); //play/pause event $(".play, .stop").live("click", function(){ var tmp_audio = $(this); var audio = tmp_audio.parent().find("audio").get(0); if($(this).hasClass("play")){ if(tmp_audio.parent().find("source").attr("src")){ tmp_audio.removeClass("play").addClass("stop"); var id = setInterval(function(){ if(audio.currentTime == audio.duration){ clearInterval(id); tmp_audio.removeClass("stop").addClass("play"); } }, 10); tmp_audio.parent().find("input").val(id); audio.play(); } } else { $(this).removeClass("stop").addClass("play"); clearInterval( tmp_audio.parent().find("input").val()) audio.pause(); } }); $(".replay").live("click", function(){ var tmp_audio = $(this).prev(); var audio = $(this).parent().find("audio").get(0); if(tmp_audio.parent().find("source").attr("src")){ $(this).prev().removeClass("play").addClass("stop"); clearInterval($(this).parent().find("input").val()); var id = setInterval(function(){ if(audio.currentTime == audio.duration){ clearInterval(id); tmp_audio.removeClass("stop").addClass("play"); } }, 10); tmp_audio.parent().find("input").val(id); audio.currentTime = 0; audio.play(); } }); } //export function exportData(){ var array_to_export = []; if($("#wgt_edit").hasClass("selected")){ $(".cont").each(function(){ var cont_obj = new Object(); cont_obj.text = $(this).find(".audio_desc").text(); cont_obj.audio = $(this).find("source").attr("src").replace("../../",""); cont_obj.mode = "edit"; cont_obj.imgs = []; $(this).find(".img_block").each(function(){ var img_obj = new Object(); img_obj.value = $(this).find("input").val(); img_obj.link = $(this).find("img").attr("src").replace("../../",""); img_obj.ht = $(this).find("img").height(); img_obj.wd = $(this).find("img").width(); cont_obj.imgs.push(img_obj); }); array_to_export.push(cont_obj); }); } else { $(".cont").each(function(){ var cont_obj = new Object(); cont_obj.text = $(this).find(".audio_desc").text(); cont_obj.audio = $(this).find("source").attr("src").replace("../../",""); cont_obj.mode = "display"; cont_obj.imgs = []; if($(this).find(".imgs_answers").children()[0].tagName == "DIV"){ var tmp_cont = $(this).find(".imgs_answers").find(".img_block"); var img_obj = new Object(); img_obj.value = tmp_cont.find("input").val(); img_obj.link = tmp_cont.find("img").attr("src").replace("../../",""); img_obj.ht = tmp_cont.find("img").height(); img_obj.wd = tmp_cont.find("img").width(); img_obj.cont = "answers"; cont_obj.imgs.push(img_obj); } $(this).find(".imgs_cont .img_block").each(function(){ var img_obj = new Object(); img_obj.value = $(this).find("input").val(); img_obj.link = $(this).find("img").attr("src").replace("../../",""); img_obj.ht = $(this).find("img").height(); img_obj.wd = $(this).find("img").width(); img_obj.cont = "cont"; cont_obj.imgs.push(img_obj); }); array_to_export.push(cont_obj); }); } sankore.setPreference("associer_sound", JSON.stringify(array_to_export)); } //import function importData(data){ var tmp = 0; for(var i in data){ if(data[i].mode == "edit"){ var tmp_array = []; var container = $("
"); var sub_container = $("
").appendTo(container); var imgs_answers = $("
").appendTo(container); var imgs_container = $("
").appendTo(container); var number = $("
"+ (++tmp) +"
").appendTo(sub_container); var text = $("
").appendTo(sub_container); var audio_block = $("
").appendTo(text); $("
").appendTo(audio_block); $("
").appendTo(audio_block); var source = $("").attr("src","../../" + data[i].audio); var audio = $("