Changeset 1153 in main


Ignore:
Timestamp:
06/01/12 13:11:07 (7 years ago)
Author:
zali
Message:

templates File : while uploading files the user can download files already linked to the document but can not delete these files

Location:
trunk/openPLM
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/media/js/file.js

    r1140 r1153  
     1/****************************************************** 
     2Author : Zahariri ALI 
     3Contact : zahariri.ali@gmail.com 
     4******************************************************/ 
     5 
     6 
     7 
     8 
     9 
    110/***************************************************************** 
    211**              Add or delete files in queue                                    ** 
     
    615var nbr_files=0; 
    716//count for input added 
    8 var num_file=0; 
     17var num_input=0; 
    918 
    1019//create a new input file, called when a new file is added to the queue for upload 
    1120function new_input_file(){ 
    1221    var table = $("#fileupload > table.Content"); 
    13     var num = num_file+1; 
     22    var num = num_input+1; 
    1423    var input = $("<input type='file' name='filename"+num+"' id='id_filename"+num+"'/ >"); 
    1524    input.change(function(){ 
     
    3140        $("span.warning").remove(); 
    3241    }); 
    33     if(($.browser.msie!=true)&&($.browser.opera!=true)){ 
     42    if(($.browser.msie!=true)&&($.browser.opera!=true)&&(xhr!=null)){ 
    3443        input.attr("multiple","multiple"); 
    3544    } 
     
    3948    tr_input.append(td_input); 
    4049    table.prepend(tr_input); 
    41     num_file++; 
     50    num_input++; 
    4251} 
    4352 
     
    107116        var xhr_nat= getXHR(); 
    108117        var url=location.href; 
    109         url=url.replace("/files/","/files/add/?file_name="+file_name); 
     118        url +="add/?file_name="+file_name; 
    110119        xhr_nat.open("GET",url,false); 
    111120        xhr_nat.onreadystatechange=function(){ 
     
    158167} 
    159168 
     169//add the file f from the input to the queue 
    160170function add_f_file(input,f){ 
    161     //test if the file f can be added 
    162     if(can_add_file(f.name,true)){ 
    163         //test if the file can be added even if it is a native file 
    164         if((can_add_native(f.name,true))||(doc_type.toLowerCase()!="document3d")){ 
    165  
    166             var key = f.name.replace(".","_"); 
    167             key = key.replace(" ","_"); 
    168             var size=f.size; 
    169  
    170             //create line for file progress 
    171             var file_line = $("<div id='"+key+"' class='file_p'>"+f.name+"("+render_size(size)+") "+"</div>"); 
    172             var progress = $("<span class='progress'><span class='text'></span></span>"); 
    173             file_line.append(progress); 
    174  
    175             //create link to delete this file from the queue for upload 
    176             var link = $("<span class='del_link'> </span>"); 
    177             link.click(function(){ 
    178                 del_file(file_line,input); 
    179             }); 
    180             var del_img = "<img src='/media/img/trash_can1.png' alt='delete' title='"+trans["remove the file from the queue"]+"'>"; 
    181             link.append(del_img); 
    182             file_line.append(link); 
    183  
    184             //add the file in the list of files to upload 
    185             $("#div_files").append(file_line); 
    186  
    187             var f_id = gen_uuid(); 
    188              
    189             if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
    190                 /*var data_key = "filename"; 
    191                 if(nbr_files!=0){ 
    192                     data_key+=nbr_files; 
    193                 } 
    194                 data.append(data_key,f);*/ 
    195                 //create an object related to the file in the files_info array-like 
    196                 files_info[key]={"field_name":" ", "f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
    197             }else{ 
    198                 files_info[key]={"f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
    199             } 
    200              
    201              
    202             nbr_files+=1; 
    203         } 
    204     } 
    205 } 
    206 //add a file in the queue for uploading 
     171 
     172        var key = f.name.replace(".","_"); 
     173        key = key.replace(" ","_"); 
     174        var size=f.size; 
     175 
     176        //create line for file progress 
     177        var file_line = $("<div id='"+key+"' class='file_p'>"+f.name+"("+render_size(size)+") "+"</div>"); 
     178        var progress = $("<span class='progress'><span class='text'></span></span>"); 
     179        file_line.append(progress); 
     180 
     181        //create link to delete this file from the queue for upload 
     182        var link = $("<span class='del_link'> </span>"); 
     183        link.click(function(){ 
     184            del_file(file_line,input); 
     185        }); 
     186        var del_img = "<img src='/media/img/trash_can1.png' alt='delete' title='"+trans["remove the file from the queue"]+"'>"; 
     187        link.append(del_img); 
     188        file_line.append(link); 
     189 
     190        //add the file in the list of files to upload 
     191        $("#div_files").append(file_line); 
     192 
     193        //create an object related to the file in the files_info array-like 
     194        var f_id = gen_uuid(); 
     195        if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
     196            files_info[key]={"field_name":" ", "f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
     197        }else{ 
     198            files_info[key]={"f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
     199        } 
     200                
     201        nbr_files+=1; 
     202} 
     203 
     204//add an input and the files selected in the queue for uploading 
    207205function add_file(input){ 
    208206    if (nbr_files==0){ 
    209207        $("#div_files > div.file_p").remove(); 
    210208    } 
     209     
    211210    $.each(input.files,function(ind,f){ 
    212         add_f_file(input,f); 
    213     }) 
     211        //test if the file f can be added 
     212        if((can_add_file(f.name,true))&&(can_add_native(f.name,true))){ 
     213            add_f_file(input,f); 
     214        } 
     215    }); 
     216     
     217    //the file is not added in the queue form when 
     218    // checking in for a given file 
    214219    if($("#fileupload").attr("action")=="."){ 
    215220        add_in_queue_form(input); 
     
    218223} 
    219224 
     225//add the file selected for check-in to the upload queue 
    220226function check_in_file(input){ 
     227 
     228    //check-in consider only the first file selected 
    221229    var f = input.files[0]; 
    222     var key = f.name.replace(".","_"); 
    223     key = key.replace(" ","_"); 
    224     var size=f.size; 
    225  
    226     //create line for file progress 
    227     var file_line = $("<div id='"+key+"' class='file_p'>"+f.name+"("+render_size(size)+") "+"</div>"); 
    228     var progress = $("<span class='progress'><span class='text'></span></span>"); 
    229     file_line.append(progress); 
    230  
    231     //create link to delete this file from the queue for upload 
    232     var link = $("<span class='del_link'> </span>"); 
    233     link.click(function(){ 
    234         del_file(file_line,input); 
    235     }); 
    236     var del_img = "<img src='/media/img/trash_can1.png' alt='delete' title='"+trans["remove the file from the queue"]+"'>"; 
    237     link.append(del_img); 
    238     file_line.append(link); 
    239  
    240     //add the file in the list of files to upload 
    241     $("#div_files").append(file_line); 
    242  
    243     var f_id = gen_uuid(); 
    244              
    245     if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
    246     /*var data_key = "filename"; 
    247     if(nbr_files!=0){ 
    248         data_key+=nbr_files; 
    249     } 
    250     data.append(data_key,f);*/ 
    251       
    252         //create an object related to the file in the files_info array-like 
    253         files_info[key]={"field_name":" ", "f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
    254     }else{ 
    255         files_info[key]={"f_name":f.name, "p_id":f_id, "size":size, "uploaded":0, "status":"waiting"}; 
    256     } 
    257                    
    258     nbr_files+=1; 
     230    add_f_file(input,f); 
    259231     
    260232} 
     
    266238 
    267239//delete a file from list and form which contains files to upload 
    268  
    269240function del_file(item,input){ 
    270241    var key = item.attr("id"); 
     
    273244    nbr_files--; 
    274245    $(".up_fail").remove(); 
     246     
     247    //input file's multiple selection is not enable for opera and IE 
    275248    if(($.browser.opera)||($.browser.msie)){ 
    276249        $(input).remove(); 
     
    291264 
    292265//Array that contains path and size of eache files selected for upload 
    293 var files_info=new Array(); 
    294 files_info.features=['features','size','key','remove','removeAll']; 
    295  
     266var files_info=[]; 
     267files_info.features=['features','size','key','remove']; 
     268 
     269//returns all the key except those in files_info.features 
    296270files_info.key=function(){ 
    297     var ret=new Array(); 
     271    var ret=[]; 
    298272    for(key in files_info){ 
    299273        if($.inArray(key,files_info.features)==-1) 
     
    303277} 
    304278 
     279//returns the number of file in files_info 
    305280files_info.size=function(){ 
    306281    var ret = 0; 
     
    312287} 
    313288 
     289//remove an element identified by key from files_info 
    314290files_info.remove=function(key){ 
    315    var keys = files_info.key(); 
    316    if($.inArray(key,keys)==-1){ 
    317         return; 
    318    }else{ 
    319         var tmp_f_info=new Array(); 
    320         $.each(files_info.features, function (ind,fe){ 
    321             tmp_f_info[fe]=files_info[fe]; 
    322         }); 
    323         $.each(keys, function(index,val){ 
    324             if(val!==key) 
    325                 tmp_f_info[val]=files_info[val]; 
    326         }); 
    327         return tmp_f_info; 
    328    } 
    329 } 
    330  
    331 files_info.removeAll=function(){ 
    332    var ret = new Array(); 
    333    $.each(files_info.features, function (ind,fe){ 
    334        ret[fe]=files_info[fe]; 
    335    }); 
    336    return ret; 
    337 } 
    338  
     291    var keys = files_info.key(); 
     292    
     293    if($.inArray(key,keys)==-1){ 
     294        return; 
     295    }else{ 
     296     
     297        var tmp_f_info=[]; 
     298         
     299            $.each(files_info.features, function (ind,fe){ 
     300                tmp_f_info[fe]=files_info[fe]; 
     301            }); 
     302            $.each(keys, function(index,val){ 
     303                if(val!==key){ 
     304                    tmp_f_info[val]=files_info[val]; 
     305                } 
     306            }); 
     307            return tmp_f_info; 
     308    } 
     309} 
     310 
     311//return the total size of all selected files 
    339312function getSizeSelected(){ 
    340313    var ret=0; 
     
    346319} 
    347320 
     321//return the uploaded size for all selected files 
    348322function getUpSize(){ 
    349323    var ret = 0; 
     
    359333    return (files_info[key]!=undefined); 
    360334} 
    361 var files_linked= new Array(); 
     335 
     336//list of files linked to the document 
     337var files_linked= []; 
    362338 
    363339function file_linked(file_name){ 
     
    370346} 
    371347 
     348//test if the document has step file linked 
    372349function has_stp_file(){ 
    373350    var ret = false; 
    374351    $.each(files_linked, function(ind,val){ 
    375         if(is_stp_file(val)){ 
    376             ret = true; 
    377         } 
     352        if(is_stp_file(val)){ 
     353                ret = true; 
     354            } 
    378355    }); 
    379356    return ret; 
     
    390367var go_to =""; 
    391368 
     369//size of all files selected for upload 
    392370var totalF_Size =0; 
    393371 
     372//size of files uploaded 
    394373var totalUpSize =  0; 
     374 
    395375//xmlhttprequest use to launch the upload 
    396376var xhr = getXHR(); 
     
    407387//return percentage of the progress 
    408388function progress_bar(uploaded, total){ 
    409     var progress_w = uploaded * 100/total; 
     389    var progress_w = uploaded/total; 
     390    progress_w=Math.round(progress_w*10000)/100; 
    410391    return progress_w; 
    411392} 
     
    417398    var keys = files_info.key(); 
    418399    $.each(keys,function(index,val){ 
    419         ret += files_info[val].field_name+"="+files_info[val].p_id+"&"; 
     400            ret += files_info[val].field_name+"="+files_info[val].p_id+"&"; 
    420401    }); 
    421402    ret = ret.substr(0,ret.length-1); 
     
    424405 
    425406 
    426 //indicate wether or not the upload is complete for a given file name 
     407//indicate wether or not the upload is complete for a given file name/key 
    427408function upload_end(key){ 
    428409    var file_key = files_info[key]; 
     
    433414function update_progress(key){ 
    434415    var xhr2=getXHR(); 
     416     
    435417    var action = location.href; 
    436     action = action.split("/files")[0]; 
    437     //action=action.replace("/files*","/files/_up"); 
    438     action+="/files/_up/"; 
     418    action+="_up/"; 
    439419    action+="?X-Progress-ID="+files_info[key].p_id; 
    440420    action+="&f_size="+files_info[key].size; 
     421     
    441422    xhr2.open("GET",action,true); 
     423     
    442424    xhr2.onreadystatechange = function() { 
    443425        if((xhr2.readyState == 4)&&(xhr2.status==200)) { 
    444                 var response=xhr2.responseText; 
     426 
     427                var response = xhr2.responseText; 
    445428                var uploaded = parseInt(response.split(":")[0]); 
    446                 var totalsize = files_info[key].size; 
    447             var f_status = response.split(":")[1]; 
    448             files_info[key].status= f_status; 
    449             var percent = progress_bar(uploaded, totalsize); 
    450             if(isNaN(percent)){ 
    451                         $("#"+key+" .progress .text").text("0% ("+files_info[key].status+")"); 
    452             }else{ 
    453                         var aux_per=""+percent; 
    454                         if (aux_per.split(".").length>1){ 
    455                             aux_per=aux_per.split(".")[0]+"."+aux_per.split(".")[1].substr(0,2); 
    456                         } 
    457                         $("#"+key+" .progress .text").text(aux_per+"%"); 
     429                var f_status = response.split(":")[1]; 
     430                 
     431            if(files_info[key].status != f_status){ 
     432                files_info[key].status = f_status; 
    458433            } 
    459434            if(f_status=="linking"){ 
     
    462437                        if (f_status!="waiting"){ 
    463438                            files_info[key].uploaded=parseInt(response.split(":")[0]); 
    464                             $("#"+key).find("progress").attr("value",percent); 
    465439                        } 
    466440            } 
     441             
     442            var percent = progress_bar(uploaded, files_info[key].size); 
     443            if(!isNaN(percent)){ 
     444                        $("#"+key+" .progress .text").text(percent+"%"); 
     445                        $("#"+key).find("progress").attr("value",percent); 
     446            } 
     447             
    467448        } 
    468449    } 
     450     
    469451    xhr2.send(null); 
    470452} 
    471453 
     454//intialize data before the upload is sent 
     455//data : formData object or form element (opera, IE) 
    472456function init_files_data(f_form){ 
     457 
    473458    //initialize field_name for each file selected 
    474459    var keys = files_info.key(); 
     
    479464        } 
    480465    }); 
     466     
    481467    var inputs_file = $(f_form).find("input[type='file']"); 
    482468    if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
     
    497483} 
    498484 
     485//Handle the failure of upload 
    499486function upload_failed(){ 
    500487    clearTimeout(t); 
     488     
     489    //display a message 
    501490    var fail_div = $("<div class='up_fail'></div>"); 
    502491    var span_text=trans["Your upload(s) failed"]+"!<br>"+trans["Try again"]; 
     
    508497    fail_div.append(span_text); 
    509498    $("#fileupload").after(fail_div); 
     499     
     500    //remove all the progress info 
    510501    $(".progress .text").empty(); 
    511502    $("progress").remove(); 
    512503    $(".del_link").show(); 
    513504    $("#global").remove(); 
     505     
     506    //reset the file upload form 
    514507    $("#fileupload").find("input[type='file']").removeAttr("disabled"); 
    515508    $("#_up").show(); 
     
    518511    $(files_form[0]).find("a").removeAttr("target"); 
    519512    $(files_form[0]).find("input[type='submit']").removeAttr("disabled"); 
     513    $(files_form[0]).next("div").find("a").removeAttr("target"); 
    520514    $("#up_message").hide(); 
    521515} 
     516 
    522517//launch and track progress of upload file in the form f_form 
    523518function up_file(f_form){ 
    524519    totalF_Size=getSizeSelected(); 
    525520    totalUpSize=getUpSize(); 
     521     
    526522    var new_action = location.href; 
    527523    var form_action=""; 
     
    531527        new_action+="get_"+form_action; 
    532528    }else{ 
    533         //new_action = new_action.replace("/files*","/files/up"); 
    534         new_action = new_action.split("/files")[0]+"/files/up/"; 
     529        new_action+="up/"; 
    535530    } 
    536531    init_files_data(f_form); 
    537532    var ids_list = ids_to_data(); 
    538533    new_action +="?"+ids_list; 
     534     
    539535    function update_progress_info() { 
    540536        var keys = files_info.key(); 
     
    546542            } 
    547543        }); 
     544         
    548545        totalUpSize=getUpSize(); 
    549546        var totalPercent = progress_bar(totalUpSize, totalF_Size); 
    550547        $("#global").find("progress").attr("value",totalPercent); 
    551         var textTot = totalPercent.toString().split(".")[0]; 
    552         if(totalPercent.toString().split(".").length > 1){ 
    553             textTot+= "."+totalPercent.toString().split(".")[1].substr(0,2); 
    554         } 
    555         $("#global .progress .text").text(textTot+"%"); 
    556         t=window.setTimeout(update_progress_info, 1000) 
     548        $("#global .progress .text").text(totalPercent+"%"); 
     549         
     550        t=window.setTimeout(update_progress_info, 1000); 
     551         
    557552        if(($.browser.opera)||($.browser.msie)){ 
    558553            var x=document.getElementById("hidden_frame"); 
     
    569564            } 
    570565        } 
     566         
    571567        if(go_to!=""){ 
    572568            location.href=go_to; 
    573569        } 
    574570    } 
     571     
    575572    t=window.setTimeout(update_progress_info, 1000); 
     573     
    576574    if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
    577575        xhr.onreadystatechange = function() { 
     
    579577                if(xhr.status==200) { 
    580578                    go_to = location.href; 
    581                     //go_to = go_to.replace("/files*","/files"); 
    582                     go_to = go_to.split("/files")[0]+"/files/"; 
    583579                }else{ 
    584580                    upload_failed(); 
     
    586582            } 
    587583        } 
     584         
    588585        xhr.open("POST", new_action,true); 
    589         //var data = new FormData(f_form); 
     586         
    590587        xhr.send(data); 
    591588    }else{ 
     589        //opera and IE do not support FormData object 
    592590        opera_up_file(f_form,new_action,go_to); 
    593591    } 
     
    596594/*reset the upload: 
    597595* - delete all files selected and hidden input files 
     596* - set the upload form to check-in for a given file 
    598597*/ 
    599598function reset_upload(){ 
    600599    $(".del_link").click(); 
    601600    nbr_files=0; 
    602     num_file=0; 
    603     //files_info = files_info.removeAll(); 
     601    num_input=0; 
     602     
     603    $(".up_fail").remove(); 
    604604    var table = $("#fileupload > table.Content"); 
     605     
    605606    $("#fileupload > table.Content tr:first").remove(); 
     607     
    606608    var input = $("<input type='file' name='filename' id='id_filename'/ >"); 
    607609    input.change(function(){ 
     
    650652} 
    651653 
     654//contains translation needed for different message/text 
    652655var trans=[]; 
     656 
     657//sets data according to the browser used 
    653658var data; 
    654659if(($.browser.opera!=true)&&($.browser.msie!=true)){ 
     
    656661} 
    657662 
     663 
    658664$(function(){ 
     665    //sets the message that will be displayed while uploading files 
     666    var up_msg = $("<div style='margin:1%;' id='up_message'></div>"); 
     667    up_msg.text(trans["Do not leave the current page until the upload ends"]); 
     668    $("form.hidden:first").after(up_msg); 
    659669    $("#up_message").hide(); 
     670     
    660671    $("input[type='submit'][name='_validate']").hide(); 
    661     if(($.browser.msie!=true)&&($.browser.opera!=true)){ 
    662         $("input[type='file']").attr('multiple','multiple'); 
    663     } 
     672     
    664673    $("#add_file_container").toggleClass("hidden"); 
    665674    if(files_linked.length!=0){ 
     
    670679    $("#add_text").attr("title",trans["Show/Hide the upload form"]); 
    671680 
    672     $.each($(".check-in"),function(ind,val){ 
    673         var ref =$(val).parent().attr("href"); 
    674         $(val).parent().removeAttr("href"); 
    675         $(val).attr("action",ref); 
    676     }); 
    677  
    678681    $("#add_text").click(function(){ 
    679682        $("#add_form_file").toggleClass("hidden"); 
    680683    }); 
    681684 
    682     $(".check-in").click(function(){ 
     685    $(".check-in").click(function(e){ 
     686        e.preventDefault(); 
    683687        if($("#fileupload").find("input[type='file']").attr("disabled")!="disabled"){ 
    684             $("#fileupload").attr("action",$(this).attr("action")); 
     688            $("#fileupload").attr("action",$(this).parent().attr("href")); 
    685689            reset_upload(); 
    686690            $("#add_form_file").removeClass("hidden"); 
     
    709713    }); 
    710714 
     715    //remove all the selected files and hidden inputs files 
    711716    $("#_delete").click(function(){ 
    712717        $(".del_link").click(); 
     718        $("form.hidden > input[type='file']").remove(); 
    713719        $("#_up").addClass("hidden"); 
    714720        $("#_delete").addClass("hidden"); 
     
    717723    if(xhr!=null){ 
    718724    //if the browser supports xmlhttprequest object 
     725     
     726        if(($.browser.msie!=true)&&($.browser.opera!=true)){ 
     727            $("input[type='file']").attr('multiple','multiple'); 
     728        } 
     729         
    719730        $("input[name='_undo']").click(function(){ 
    720731            if(xhr.readyState!=0){ 
     
    747758            $("#_up").hide(); 
    748759            var files_form = $(".archive_form").next(); 
     760             
     761            //disable actions in the file display form 
     762            $(files_form[0]).find("input[type!='hidden']").attr("disabled","disabled"); 
     763             
     764            //set all links so a new tab is opened when they are clicked on 
    749765            $(files_form[0]).find("a").attr("target","_blank"); 
    750             $(files_form[0]).find("input[type='submit']").attr("disabled","disabled"); 
     766            $(files_form[0]).next("div").find("a").attr("target","_blank"); 
     767             
    751768            $("#up_message").show(); 
    752769        }); 
    753770    }else{ 
    754         //the browser does not support xmlhttprequest object 
     771        //the browser does not support xmlhttprequest object but has javascript enabled 
    755772        //if javascript is disabled, noscript tag will handle that 
    756773        $("#fileupload").insertAfter("<span class='warning'>"+trans["You can only upload files, but you wont get progress of this upload"]+"</span>"); 
     
    760777                return; 
    761778            } 
     779             
     780            opera_prepare_form($("form.hidden:first > input[type='file']")); 
    762781            $('form.hidden')[0].action=location.href+"add/"; 
    763782            $('form.hidden')[0].method="post"; 
  • trunk/openPLM/templates/documents/files_add.html

    r1137 r1153  
    1717    </noscript> 
    1818    <form class="hidden" action="" target="hidden_frame" method="post" enctype="multipart/form-data"></form> 
    19     <div style="margin:1%;" id="up_message"> 
    20         {% trans "Don't leave the current page until the upload ends" %} 
    21     </div> 
    2219    <div style="margin:1%;"> 
    2320        <input type="button" class="{{"Button"|button}} hidden" value="{% trans "Upload" %}" id="_up"/> 
     
    4946    trans["Check-in for file "]="{% trans 'Check-in for file ' %}"; 
    5047    trans["You can only upload files, but you wont get progress of this upload"]="{% trans 'You can only upload files, but you wont get progress of this upload' %}"; 
    51     /*trans[""]="{% trans '' %}"; 
    52     trans[""]="{% trans '' %}";*/ 
     48    trans["Do not leave the current page until the upload ends"]="{% trans 'Do not leave the current page before the upload ends' %}"; 
     49    /*trans[""]="{% trans '' %}";*/ 
    5350</script> 
    5451<script type="text/javascript" src="/media/js/opera_upload.js"></script> 
Note: See TracChangeset for help on using the changeset viewer.