Finishing up the initial functionality for the addmedia (tinyMCE) plugin

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-03-05 02:04:41 -08:00
parent 554d279795
commit c55bbb32ce
6 changed files with 32 additions and 21 deletions

View File

@@ -168,7 +168,7 @@ namespace Orchard.Media.Controllers {
return Content(string.Format("<script type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", T("ERROR: You don't have permission to upload media files")));
if (Request.Files.Count < 1 || Request.Files[0].ContentLength == 0)
return Content(string.Format("<scipt type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", T("HEY: You didn't give me a file to upload")));
return Content(string.Format("<script type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", T("HEY: You didn't give me a file to upload")));
try {
_mediaService.GetMediaFiles(viewModel.MediaPath);
@@ -181,7 +181,7 @@ namespace Orchard.Media.Controllers {
var file = Request.Files[0];
_mediaService.UploadMediaFile(viewModel.MediaPath, file);
return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\" }};</script>", Path.Combine(_mediaService.GetRootUrl(), string.Format("{0}/{1}", viewModel.MediaPath, Path.GetFileName(file.FileName))).Replace("\\", "/")));
return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\" }};</script>", Path.Combine(_mediaService.GetRootUrl(), string.Format("{0}/{1}", viewModel.MediaPath, Path.GetFileName(file.FileName)).Replace("//", "/")).Replace("\\", "/")));
}
catch (Exception exception) {
return Content(string.Format("<script type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", T("ERROR: Uploading media file failed: {0}", exception.Message)));

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Core.Common.Models;
@@ -17,8 +16,7 @@ namespace TinyMce.Extensions {
var currentModule = htmlHelper.Resolve<IExtensionManager>().ActiveExtensions().FirstOrDefault(ee => ee.Descriptor.ExtensionType == "Module" && ee.Assembly == currentDriver.GetType().Assembly);
var routable = body.ContentItem.Has<RoutableAspect>() ? body.ContentItem.As<RoutableAspect>() : null;
//TODO: (erikpo) Get the value for the media folder from settings instead of hardcoding it
return ((Control)htmlHelper.ViewDataContainer).ResolveUrl(string.Format("{0}/{1}{2}", "~/Media", currentModule.Descriptor.Name, routable != null && !string.IsNullOrEmpty(routable.ContainerPath) ? "/" + routable.ContainerPath : ""));
return string.Format("{0}{1}", currentModule.Descriptor.Name, routable != null && !string.IsNullOrEmpty(routable.ContainerPath) ? "/" + routable.ContainerPath : "");
}
}
}

View File

@@ -12,7 +12,7 @@
* @param {string} url Absolute URL to where the plugin is located.
*/
init: function(ed, url) {
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceExample');
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceAddMedia');
ed.addCommand('mceAddMedia', function() {
ed.windowManager.open({
file: url + '/addmedia.htm',
@@ -24,7 +24,7 @@
});
});
// Register example button
// Register addmedia button
ed.addButton('addmedia', {
title: ed.getLang('addmedia.title'),
cmd: 'mceAddMedia',

View File

@@ -10,44 +10,53 @@ var AddMediaDialog = {
},
addMedia: function(form) {
var callback = AddMediaDialog.insertMediaAndClose;
var iframeName = "addmedia__" + (new Date()).getTime()
var iframe = document.createElement("iframe");
iframe.name = iframeName;
iframe.src = "about:blank";
tinymce.DOM.setStyles(iframe, { display: "none" });
tinymce.dom.Event.add(iframe, 'load', function(ev) {
var result = window.frames[iframeName].result;
var iframeLoadHandler = tinymce.dom.Event.add(iframe, 'load', function(ev) {
var result = window.frames[iframeName].result, close = 0;
if (result && result.url) {
return callback(result.url);
} else if (false && result && result.error) {
alert("Wait, there was an error:\n\r\n\r" + result.error);
window.parent.AddMediaDialog.insertMedia(result.url);
close = 1;
} else if (result && result.error) {
alert(tinyMCEPopup.getLang("addmedia_dlg.msg_error") + "\n\r\n\r" + result.error);
} else {
var somethingPotentiallyHorrible = window.frames[iframeName].document.getElementsByTagName("body")[0].innerHTML;
var somethingPotentiallyHorrible = "";
try {
somethingPotentiallyHorrible = window.frames[iframeName].document.getElementsByTagName("body")[0].innerHTML;
} catch (ex) { // some browsers flip out trying to access anything in the iframe when there's an error. best we can do is guess at this point
somethingPotentiallyHorrible = tinyMCEPopup.getLang("addmedia_dlg.msg_error_unknown");
}
if (somethingPotentiallyHorrible) {
alert("Something unexpected happened:\n\r\n\r" + somethingPotentiallyHorrible);
alert(tinyMCEPopup.getLang("addmedia_dlg.msg_error_unexpected") + "\n\r\n\r" + somethingPotentiallyHorrible);
}
}
//cleanup
setTimeout(function() { callback = null; tinymce.DOM.remove(iframe); }, 123);
setTimeout(function() {
tinymce.dom.Event.remove(iframe, 'load', iframeLoadHandler);
tinymce.DOM.remove(iframe);
iframe = null;
if (close) window.parent.tinyMCEPopup.close();
},
123);
});
form.target = iframeName;
tinymce.DOM.add(document.body, iframe);
},
insertMediaAndClose: function(url) {
insertMedia: function(url) {
if (!url) return;
//todo: (heskew) needs more awesome
var markup = /\.(jpe?g|png|gif)$/i.test(url)
? "<img src=\"" + url + "\" />"
: "<a href=\"" + url + "\">" + url + "</a>";
tinyMCE.activeEditor.execCommand("mceInsertContent", false, markup);
tinyMCEPopup.close();
}
};

View File

@@ -3,7 +3,10 @@ tinyMCE.addI18n({ en: {
button_title: "Add Media",
title: "Add Media",
path_label: "Media File Path",
browse_button_text: "Browse"
browse_button_text: "Browse",
msg_error: "Wait, there was an error:",
msg_error_unexpected: "Something unexpected happened:",
msg_error_unknown: "Can't get the error from the response. This can happen when the server throws an error. Is the file size larger than what the server is configured to accept?"
}
}
});

View File

@@ -17,6 +17,7 @@ using (this.Capture("end-of-page-scripts")) {%>
theme_advanced_buttons1: "search,replace,|,cut,copy,paste,|,undo,redo,|,image,addmedia,|,link,unlink,charmap,emoticon,codeblock,|,bold,italic,|,numlist,bullist,formatselect,|,code,fullscreen",
theme_advanced_buttons2: "",
theme_advanced_buttons3: "",
convert_urls: false,
addmedia_action: "<%=Url.Action("AddFromClient", "Admin", new {area = "Orchard.Media"}) %>",
addmedia_path: "<%=Html.GetCurrentMediaPath() %>",
request_verification_token: "<%=Html.AntiForgeryTokenValueOrchard() %>"