web/static/ldt/js/projectscontents.js
changeset 71 8a881c9593d0
parent 66 8a7530e28185
child 103 5578dcb54f4d
--- a/web/static/ldt/js/projectscontents.js	Thu Apr 21 18:07:59 2011 +0200
+++ b/web/static/ldt/js/projectscontents.js	Fri Apr 29 10:58:24 2011 +0200
@@ -399,3 +399,179 @@
     });
     
 }
+
+//
+// Functions used in the create content view.
+//
+function onCreateContentReady(url_upload, media_prefix, post_added_params, btn_label, success_label) {
+	
+    var upload_from_local_done = false;
+    
+    $("#close_button").click(function (e) {
+        e.preventDefault();
+        parent.$.nmTop().close();
+    });
+    $("#submit_button_write").click(function(e) {
+        $(".submitcontent-loader-content").show();
+    });
+    $(".media_fields").hide();
+    $("#media_field_"+$("#id_content-media_input_type").val()).show();
+    $("#id_content-media_input_type").change(function(e) {
+        $(".media_fields").hide();
+        $("#media_field_"+$(e.target).val()).show();
+    });
+    // The textinput's id for external url is id_media-src
+    $('#id_media-src').bind('textchange', function(e) { testUrlValue(e); });
+    
+    // We disable the default submit
+    $('#my_form').submit(function() {
+        if($('#id_content-media_input_type').val()=="upload" && upload_from_local_done==false){
+            //alert("1. " + $('#id_content-media_input_type').val() + ", upload_done = " + upload_from_local_done);
+            startLocalUpload();
+            return false;
+        }
+        else{
+            //alert("2. " + $('#id_content-media_input_type').val());
+            return true;
+        }
+    });
+    
+    $("#upload_progress_bar").css({ width:"90%", height:"10" });
+    
+    $('#media_fields_div').height(80);
+    
+    // We init the swfupload object
+    swfupload = new SWFUpload({
+        debug: false,
+        
+        upload_url: url_upload,
+        flash_url: media_prefix+"swf/swfupload.swf",
+        
+        post_params: post_added_params,
+        
+        button_placeholder_id: "upload_btn",
+        button_width: "60",
+        button_height: "16",
+        button_cursor: SWFUpload.CURSOR.HAND,
+        button_text : '<span class="btnText">' + btn_label + '</span>',
+        button_text_style : ".btnText { font-size: 12; font-family: Arial; }",
+        
+        file_types : "*.flv;*.f4v;*.mp4;*.mov;*.mp3",
+        file_types_description : "Media Files (flv, f4v, mov H264, mp4, mp3)",
+        file_upload_limit : "1",
+        file_queue_limit : "1",
+        
+        upload_progress_handler : uploadProgress,
+        upload_error_handler : uploadError,
+        upload_success_handler : function() {
+                try {
+                    if($('#upload_progress_info').children().size()>0){
+                        $('#progress_info').remove();
+                    }
+                    $('#upload_progress_info').append('<p id="progress_info">' + success_label + '.</p>');
+                    upload_from_local_done = true;
+                    // Now that the file is uploaded, we submit the form
+                    $('#my_form').submit();
+                }
+                catch (ex) {
+                    //this.debug(ex);
+                }
+            },
+        
+        file_queued_handler : displayUploadPath,
+        //file_dialog_complete_handler: function() { this.startUpload(); },
+        //upload_complete_handler: function() { this.startUpload(); },
+    });
+}
+function displayUploadPath(file) {
+    try {
+        $('#id_media-local_file_name').val(file.name);
+    }
+    catch (ex) {
+        //this.debug(ex);
+    }
+}
+function startLocalUpload(){
+    swfupload.startUpload();
+}
+function uploadProgress(file, bytesLoaded, bytesTotal) {
+    try {
+        var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
+        $("#upload_progress_bar").progressbar({ value: percent });
+    }
+    catch (ex) {
+        //this.debug(ex);
+    }
+}
+function uploadSuccess(success_label) {
+    try {
+        if($('#upload_progress_info').children().size()>0){
+            $('#progress_info').remove();
+        }
+        $('#upload_progress_info').append('<p id="progress_info">' + success_label + '.</p>');
+        upload_from_local_done = true;
+        // Now that the file is uploaded, we submit the form
+        $('#my_form').submit();
+    }
+    catch (ex) {
+        //this.debug(ex);
+    }
+}
+function uploadError(file, errorCode, message) {
+    try {
+        if($('#upload_progress_info').children().size()>0){
+            $('#progress_info').remove();
+        }
+        switch (errorCode) {
+        case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: HTTP Error, File name: " + file.name + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.IO_ERROR:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: IO Error, File name: " + file.name + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: Security Error, File name: " + file.name + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
+            $('#upload_progress_info').append('<p id="progress_info">' + "Error Code: FILE_CANCELLED" + '</p>');
+            break;
+        case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
+            $('#upload_progress_info').append('<p id="progress_info">' + "STOPPED" + '</p>');
+            break;
+        default:
+            $('#upload_progress_info').append('<p id="progress_info">' + "unhandled error: File name: " + file.name + ", File size: " + file.size + ", Message: " + message + '</p>');
+            break;
+        }
+    } catch (ex) {
+        //this.debug(ex);
+    }
+}
+
+// Test the value of the URL from the form to load a picture in case it is a youtube video
+function testUrlValue(e){
+    // First, we remove the current thumbnail if there is one.
+    if($('#media_field_create').children().size()>2){
+        $('#external_thumbnail').remove();
+        $('#media_fields_div').height(80);
+    }
+    // If the pasted text is a youtube url, we get the default thumbnail of the video and display it.
+    url = $('#id_media-src').val();
+    if(url.match("youtube") && url.match("[\\?&]v=([^&#]*)")){
+        results = url.match("[\\?&]v=([^&#]*)");
+        vid = ( results === null ) ? url : results[1].substring(0,11);
+        if(vid.length===11){
+            //$('#id_content-description').val($('#media_field_create') + '<img src="http://img.youtube.com/vi/'+vid+'/default.jpg" alt="Youtube Thumbnail"/>');
+            $('#media_field_create').append('<img id="external_thumbnail" src="http://img.youtube.com/vi/'+vid+'/default.jpg" alt="Youtube Thumbnail"/>');
+            $('#media_fields_div').height(150);
+        }
+    }
+}