erreur thunmbail4
[aidenligne_francais_universite.git] / plugins / forms_1_9_1 / forms_lang.js
1 var forms_containers={},forms_fields={},forms_forms,forms_menu_lang;
2 var match_multi = /\[([a-z_]+)\](.*?)(?=\[[a-z_]+\]|$)/mig;
3 var forms_cur_lang,forms_css_link,forms_css_cur_link={};
4 forms_css_link = {"cursor":"pointer","margin":"2px 5px","float":"left"};
5 $.extend(forms_css_cur_link,forms_css_link);
6 $.extend(forms_css_cur_link,{fontWeight:"bold"});
7
8 function forms_init_lang() {
9 //Detect if we're on the right page and if multilinguism is activated. If not return.
10 if(window.location.search.indexOf("exec=forms_edit")==-1 || forms_avail_langs.length<=1) return;
11 forms_cur_lang = forms_def_lang;
12 forms_containers = $("#forms_lang");
13 //create menu lang template
14 forms_menu_lang =$("<div>");
15 $.each(forms_avail_langs,function() {
16 forms_menu_lang.append($("<a>").html("["+this+"]").css(this==forms_def_lang?forms_css_cur_link:forms_css_link)[0]);
17 });
18 //create menu lang for the global form
19 forms_make_menu_lang(forms_containers);
20 //init fields
21 forms_init_multi();
22 }
23
24 function forms_make_menu_lang(container,target) {
25 $(forms_menu_lang).clone().find("a").click(function() {forms_change_lang(this,container,target)}).end().
26 append("<div style='clear:left'></div>").appendTo(container);
27 }
28
29 function forms_change_lang(el,container,target) {
30 var lang = el.innerHTML;
31 container = container || forms_containers;
32 //update lang menu with current selection
33 container.find("a").each(function(){
34 $(this).css("fontWeight",lang==this.innerHTML?"bold":"normal");
35 }).end();
36 lang = lang.slice(1,-1);
37 //store the fields inputs for later use (usefull for select)
38 if(!forms_fields[target]) forms_fields[target] = $('input[@id][@id^="nom_"]',target);
39 //save the current values
40 forms_fields[target].each(function(){forms_save_lang(this,forms_cur_lang)});
41 //change current lang
42 forms_cur_lang = lang;
43 //reinit fields to current lang
44 forms_fields[target].each(function(){forms_set_lang(this,lang)});
45 }
46
47 function forms_init_multi() {
48 //store all the fields forms
49 forms_forms = $("form.forms_champ").submit(forms_multi_submit);
50 //init the value of the field to current lang
51 //bug of jquery 1.0.1 Error if attribute is not defined and matching its value
52 forms_fields["undefined"] = $('input[@id][@id^="nom_"]',forms_forms).each(function() {forms_init_field(this,forms_def_lang)});
53 //create menu for each form. The menu is just before the form
54 forms_forms.prev().each(function() {
55 var id = "#"+this.id;
56 //store all form containers to allow menu lang update on each container
57 //when it is triggered by global menu
58 forms_containers.add(id);
59 forms_make_menu_lang($(id),$(this).next())
60 }).end();
61 }
62
63 function forms_init_field(el,lang) {
64 //Retrieves the following data
65 //1)the title element of the field
66 //2)boolean multi = the fields has a multi value
67 //3)various lang string
68 var langs;
69 var m = el.value.match(/<multi>(.*?)<\/multi>/m);
70 el.field_lang = {};
71 el.titre_el = $("#titre_"+el.id);
72 if(m!=null) {
73 el.multi = true;
74 match_multi.lastIndex=0;
75 while((langs=match_multi.exec(m[1]))!=null) {
76 el.field_lang[langs[1]] = langs[2];
77 }
78 //Put the current lang string only in the field
79 forms_set_lang(el,lang);
80 } else {
81 el.multi = false;
82 el.field_lang[lang] = el.value;
83 }
84 }
85
86 function forms_set_lang(el,lang) {
87 //if current lang is not setted use default lang value
88 if(el.field_lang[lang]==undefined)
89 el.field_lang[lang] = el.field_lang[forms_def_lang];
90 el.value = el.field_lang[lang];
91 el.titre_el.html(el.value);
92 }
93
94 function forms_save_lang(el,lang) {
95 //if the lang value is equal to the def lang do nothing
96 //else save value but if the filed is not empty, delete lang value
97 if(el.field_lang[forms_def_lang]!=el.value) {
98 if(!el.value) {
99 delete el.field_lang[lang];
100 return;
101 }
102 el.multi = true;
103 el.field_lang[lang] = el.value;
104 }
105 }
106
107 //This func receives the forms that is going to be submitted
108 function forms_multi_submit() {
109 $('input[@id][@id^="nom_"]',this).each(function(){
110 //save data before submit
111 forms_save_lang(this,forms_cur_lang);
112 //build the string value
113 var def_value = this.field_lang[forms_def_lang];
114 if(!this.multi) this.value = def_value;
115 else {
116 var value="",count=0;
117 $.each(this.field_lang,function(name){
118 //save default lang value and other lang values if different from
119 //the default one
120 if(this!=def_value || name==forms_def_lang) {
121 value += "["+name+"]"+this;
122 count++;
123 }
124 });
125 this.value = count!=1?"<multi>"+value+"</multi>":value.replace(/^\[[a-z_]+\]/,'');
126 }
127 });
128 }