[SPIP] ~2.1.12 -->2.1.25
[velocampus/web/www.git] / www / prive / javascript / async_upload.js
1 // JavaScript Document
2 jQuery.fn.async_upload = function(add_function) {
3 return this.ajaxForm({
4 beforeSubmit:async_upload_before_submit,
5 success:add_function,
6 iframe:true
7 });
8 }
9
10 // Safari plante quand on utilise clone() -> on utilise html()
11 // Mais FF a un bug sur les urls contenant ~ quand on utilise html() -> on utilise clone()
12 jQuery.fn.clone2 = jQuery.browser.mozilla ? jQuery.fn.clone : jQuery.fn.html;
13
14 var iframeHandler = function(data,jForm,success) {
15 //remove the previous message
16 jQuery("div.upload_message",$(jForm).parent()).remove();
17 var res = jQuery(data).filter(".upload_answer");
18 //possible classes
19 //upload_document_added
20 if(res.is(".upload_document_added")) {
21 return res;
22 }
23 //upload_error
24 if(res.is(".upload_error")) {
25 var msg = jQuery("<div class='upload_message'>")
26 .append(res.html())
27 jForm.after(msg[0]);
28 return false;
29 }
30 //upload_zip_list
31 if(res.is(".upload_zip_list")) {
32 var zip_form = jQuery("<div class='upload_message'>").append(res.html());
33 zip_form
34 .find("form")
35 .async_upload(function(res,s){
36 success(res,s,'',jForm);
37 });
38 jForm.after(zip_form[0]);
39 return false;
40 }
41 };
42
43
44 function async_upload_before_submit(data,form) {
45 form.before(jQuery("<div class='upload_message' style='height:1%'>").append(ajax_image_searching)[0]);
46 //if not present add the iframe input
47 if(!form.find("input[name=iframe]").length)
48 form.append("<input type='hidden' name='iframe' value='iframe'>");
49 //reset the redirect input
50 form
51 .find("input[name='redirect']")
52 .val("");
53 };
54
55 function async_upload_article_edit(res,s,xhr,jForm){
56 res = iframeHandler(res,jForm,async_upload_article_edit);
57 if(!res) return true;
58 var cont;
59 //verify if a new document or a customized vignette
60 var bloc = jQuery(res.find(">div:first[id^=document]"));
61 if(jQuery("#"+bloc.attr('id')).size()) {
62 cont = jQuery("#"+bloc.attr('id')).html(bloc.html());
63 } else {
64 //add a class to new documents
65 res.
66 find(">div[class]")
67 .addClass("documents_added")
68 .css("display","none");
69 if (jForm.find("input[name='arg']").val().search("/0/image")!=-1){
70 cont = jQuery("#liste_images");
71 // cas de l'interface document unifiee
72 if (!cont.length)
73 cont = jQuery("#liste_documents");
74 }
75 else
76 cont = jQuery("#liste_documents");
77 cont
78 .prepend(res.clone2());
79 //find added documents, remove label and show them nicely
80 cont = cont.
81 find("div.documents_added")
82 .removeClass("documents_added")
83 .show("slow",function(){
84 var anim = jQuery(this).css("height","");
85 //bug explorer-opera-safari
86 if(!jQuery.browser.mozilla)
87 anim.css('width', jQuery(this).width()-2);
88 a = jQuery(anim).find("img[onclick]")
89 if (a.length) a.get(0).onclick();
90 })
91 .css('overflow','');
92 }
93 jQuery("form.form_upload",cont).async_upload(async_upload_article_edit);
94 verifForm(cont);
95 return true;
96 }
97
98 function async_upload_icon(res,s,xhr,jForm) {
99 res = iframeHandler(res,jForm);
100 if(!res) return true;
101 res.find(">div").each(function(){
102 var cont = jQuery("#"+this.id);
103 verifForm(cont.html(jQuery(this).html()));
104 jQuery("form.form_upload_icon",cont).async_upload(async_upload_icon);
105 cont.find("img[onclick]").each(function(){this.onclick();});
106 });
107 return true;
108 }
109
110 function async_upload_portfolio_documents(res,s,xhr,jForm){
111 res = iframeHandler(res,jForm,async_upload_portfolio_documents);
112
113 if(!res) return true;
114
115 // on dirait que ca passe mieux sur Safari avec un setTimeout cf #1408
116 setTimeout(function() {
117 res.find(">div").each(function(){
118 // this.id = documenter--id_article ou documenter-id_article
119 var cont = jQuery("#"+this.id);
120 var self = jQuery(this);
121 if(!cont.size()) {
122 cont = jQuery(this.id.search(/--/)!=-1 ? "#portfolio":"#documents")
123 .append(self.clone2());
124 }
125 verifForm(cont.html(self.html()));
126 jQuery("form.form_upload",cont).async_upload(async_upload_portfolio_documents);
127 });
128 }, 50);
129 return true;
130 }