/** * yuiImgUploader * variables: * rte: The YAHOO.widget.Editor instance * upload_url: the url to post the file to * upload_image_name: the name of the post parameter to send the file as * * Your server must handle the posted image. You must return a JSON object * with the result url that the image can be viewed at on your server. If * the upload fails, you can return an error message. For successful * uploads, the status must be set to UPLOADED. All other status messages, * or the lack of a status message is interpreted as an error. IE will * try to open a new document window when the response is returned if your * content-type header on your response is not set to 'text/javascript' * * Example Success: * {status:'UPLOADED', image_url:'/somedirectory/filename'} * Example Failure: * {status:'We only allow JPEG Images.'} */ function yuiImgUploader(rte, upload_url, upload_image_name) { // customize the editor img button YAHOO.log("Adding Click Listener" ,"debug"); rte.addListener("toolbarLoaded",function() { rte.toolbar.addListener("insertimageClick", function(o) { try { var imgPanel = new YAHOO.util.Element("yui-editor-panel"); imgPanel.on("contentReady", function() { try { var Dom = YAHOO.util.Dom; var div = document.createElement("div"); div.id = "img_uploader"; div.innerHTML = '<label for="insertimage_upload">Upload:</label>'+ '<input id="insertimage_upload" type="file" name="'+upload_image_name+'" size="20">'+ '<a href="#" id="insertimage_upload_btn">Upload Image</a>'; var img_elem = Dom.get("insertimage_url"); Dom.getAncestorByTagName(img_elem, "form").encoding = "multipart/form-data"; Dom.insertAfter(div, img_elem.parentNode); var uploadButton = new YAHOO.widget.Button("insertimage_upload_btn"); uploadButton.addListener("click", function(e) { YAHOO.util.Event.preventDefault(e); //alert ( "Upload Click" ); var lessonEditorForm = img_elem.form; YAHOO.util.Connect.setForm(lessonEditorForm, true, true); var c = YAHOO.util.Connect.asyncRequest("POST", upload_url, {upload:function(o){ try { // trying to parse the json string try { var image = YAHOO.lang.JSON.parse(o.responseText); } catch (error4) { alert("Invalid image data"); } if (image.status == "UPLOADED") { Dom.get("insertimage_upload").value = ''; Dom.get("insertimage_url").value = image.image_url; // tell the image panel the url changed // hack instead of fireEvent('blur') // which for some reason isn't working Dom.get("insertimage_url").focus(); Dom.get("insertimage_upload").focus(); } else { alert("Upload Failed: "+o.status+" - "+image.status); } } catch (error3) { YAHOO.log(error3.message, "error"); } } }); return false; }); } catch (error2) { YAHOO.log(error2.message, "error") } }); } catch (error1) { YAHOO.log(error1.message, "error") } }); }); }