lazy load scripts

This commit is contained in:
Luke Pulverenti 2015-05-16 15:09:02 -04:00
parent 477a182efd
commit c8a38c139f
6 changed files with 180 additions and 13 deletions

View file

@ -56,6 +56,8 @@ namespace MediaBrowser.Api
[ApiMember(Name = "IsAdult", Description = "Optional. Filter by package that contain adult content.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? IsAdult { get; set; }
public bool? IsAppStoreEnabled { get; set; }
}
/// <summary>
@ -207,6 +209,11 @@ namespace MediaBrowser.Api
packages = packages.Where(p => p.adult == request.IsAdult.Value);
}
if (request.IsAppStoreEnabled.HasValue)
{
packages = packages.Where(p => p.enableInAppStore == request.IsAppStoreEnabled.Value);
}
return ToOptimizedResult(packages.ToList());
}

View file

@ -25,6 +25,7 @@ namespace MediaBrowser.Api
[Authenticated]
public class GetPlugins : IReturn<List<PluginInfo>>
{
public bool? IsAppStoreEnabled { get; set; }
}
/// <summary>
@ -181,6 +182,7 @@ namespace MediaBrowser.Api
public async Task<object> Get(GetPlugins request)
{
var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
// Don't fail just on account of image url's
try
@ -197,10 +199,26 @@ namespace MediaBrowser.Api
plugin.ImageUrl = pkg.thumbImage;
}
}
if (requireAppStoreEnabled)
{
result = result
.Where(plugin =>
{
var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
return pkg != null && pkg.enableInAppStore;
})
.ToList();
}
}
catch
{
// Play it safe here
if (requireAppStoreEnabled)
{
result = new List<PluginInfo>();
}
}
return ToOptimizedSerializedResultUsingCache(result);

View file

@ -153,6 +153,12 @@ namespace MediaBrowser.Model.Updates
/// <value>The versions.</value>
public List<PackageVersionInfo> versions { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [enable in application store].
/// </summary>
/// <value><c>true</c> if [enable in application store]; otherwise, <c>false</c>.</value>
public bool enableInAppStore { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="PackageInfo"/> class.
/// </summary>

View file

@ -295,6 +295,14 @@ namespace MediaBrowser.WebDashboard.Api
var mode = request.Mode;
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
// Overwrite certain files with cordova specific versions
var cordovaVersion = Path.Combine(path, "thirdparty", "cordova", "registrationservices.js");
File.Copy(cordovaVersion, Path.Combine(path, "scripts", "registrationservices.js"), true);
File.Delete(cordovaVersion);
}
await DumpHtml(creator.DashboardUIPath, path, mode, culture, appVersion);
await DumpJs(creator.DashboardUIPath, path, mode, culture, appVersion);

View file

@ -38,17 +38,17 @@ namespace MediaBrowser.WebDashboard.Api
string appVersion,
bool enableMinification)
{
var isHtml = IsHtml(path);
Stream resourceStream;
if (path.Equals("scripts/all.js", StringComparison.OrdinalIgnoreCase))
{
resourceStream = await GetAllJavascript(mode, localizationCulture, appVersion, enableMinification).ConfigureAwait(false);
enableMinification = false;
}
else if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase))
{
resourceStream = await GetAllCss(enableMinification).ConfigureAwait(false);
enableMinification = false;
}
else
{
@ -59,7 +59,7 @@ namespace MediaBrowser.WebDashboard.Api
{
// Don't apply any caching for html pages
// jQuery ajax doesn't seem to handle if-modified-since correctly
if (isHtml && path.IndexOf("cordovaindex.html", StringComparison.OrdinalIgnoreCase) == -1)
if (IsHtml(path) && path.IndexOf("cordovaindex.html", StringComparison.OrdinalIgnoreCase) == -1)
{
resourceStream = await ModifyHtml(resourceStream, mode, localizationCulture, enableMinification).ConfigureAwait(false);
}
@ -115,6 +115,86 @@ namespace MediaBrowser.WebDashboard.Api
return fullPath;
}
public async Task<Stream> ModifyCss(Stream sourceStream, bool enableMinification)
{
using (sourceStream)
{
string content;
using (var memoryStream = new MemoryStream())
{
await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);
content = Encoding.UTF8.GetString(memoryStream.ToArray());
if (enableMinification)
{
try
{
var result = new KristensenCssMinifier().Minify(content, false, Encoding.UTF8);
if (result.Errors.Count > 0)
{
_logger.Error("Error minifying css: " + result.Errors[0].Message);
}
else
{
content = result.MinifiedContent;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error minifying css", ex);
}
}
}
var bytes = Encoding.UTF8.GetBytes(content);
return new MemoryStream(bytes);
}
}
public async Task<Stream> ModifyJs(Stream sourceStream, bool enableMinification)
{
using (sourceStream)
{
string content;
using (var memoryStream = new MemoryStream())
{
await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);
content = Encoding.UTF8.GetString(memoryStream.ToArray());
if (enableMinification)
{
try
{
var result = new CrockfordJsMinifier().Minify(content, false, Encoding.UTF8);
if (result.Errors.Count > 0)
{
_logger.Error("Error minifying javascript: " + result.Errors[0].Message);
}
else
{
content = result.MinifiedContent;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error minifying javascript", ex);
}
}
}
var bytes = Encoding.UTF8.GetBytes(content);
return new MemoryStream(bytes);
}
}
/// <summary>
/// Modifies the HTML by adding common meta tags, css and js.
/// </summary>
@ -135,6 +215,11 @@ namespace MediaBrowser.WebDashboard.Api
html = Encoding.UTF8.GetString(memoryStream.ToArray());
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
html = ModifyForCordova(html);
}
if (!string.IsNullOrWhiteSpace(localizationCulture))
{
var lang = localizationCulture.Split('-').FirstOrDefault();
@ -182,6 +267,22 @@ namespace MediaBrowser.WebDashboard.Api
}
}
private string ModifyForCordova(string html)
{
// Strip everything between CORDOVA_EXCLUDE_START and CORDOVA_EXCLUDE_END
html = ReplaceBetween(html, "CORDOVA_EXCLUDE_START", "CORDOVA_EXCLUDE_END", string.Empty);
// Replace CORDOVA_REPLACE_SUPPORTER_SUBMIT_START
html = ReplaceBetween(html, "CORDOVA_REPLACE_SUPPORTER_SUBMIT_START", "CORDOVA_REPLACE_SUPPORTER_SUBMIT_END", "<i class=\"fa fa-check\"></i><span>${ButtonDonate}</span>");
return html;
}
private string ReplaceBetween(string html, string startToken, string endToken, string newHtml)
{
return html;
}
private string GetLocalizationToken(string phrase)
{
return "${" + phrase + "}";
@ -293,7 +394,14 @@ namespace MediaBrowser.WebDashboard.Api
await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);
await AppendLocalization(memoryStream, culture).ConfigureAwait(false);
var excludePhrases = new List<string>();
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
excludePhrases.Add("paypal");
}
await AppendLocalization(memoryStream, culture, excludePhrases).ConfigureAwait(false);
await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(mode))
@ -424,13 +532,11 @@ namespace MediaBrowser.WebDashboard.Api
"taskbutton.js",
"ratingdialog.js",
"aboutpage.js",
"alphapicker.js",
"addpluginpage.js",
"metadataadvanced.js",
"autoorganizetv.js",
"autoorganizelog.js",
"channels.js",
"channelslatest.js",
"channelitems.js",
"channelsettings.js",
@ -489,10 +595,6 @@ namespace MediaBrowser.WebDashboard.Api
"moviepeople.js",
"moviestudios.js",
"movietrailers.js",
"musicalbums.js",
"musicalbumartists.js",
"musicartists.js",
"musicrecommended.js",
"mypreferencesdisplay.js",
"mypreferenceslanguages.js",
@ -538,9 +640,29 @@ namespace MediaBrowser.WebDashboard.Api
};
}
private async Task AppendLocalization(Stream stream, string culture)
private async Task AppendLocalization(Stream stream, string culture, List<string> excludePhrases)
{
var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(_localization.GetJavaScriptLocalizationDictionary(culture));
var dictionary = _localization.GetJavaScriptLocalizationDictionary(culture);
if (excludePhrases.Count > 0)
{
var removes = new List<string>();
foreach (var pair in dictionary)
{
if (excludePhrases.Any(i => pair.Key.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1 || pair.Value.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
{
removes.Add(pair.Key);
}
}
foreach (var remove in removes)
{
dictionary.Remove(remove);
}
}
var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(dictionary);
var bytes = Encoding.UTF8.GetBytes(js);
await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

View file

@ -156,6 +156,9 @@
<Content Include="dashboard-ui\scripts\photos.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\scripts\registrationservices.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\scripts\reports.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@ -222,6 +225,9 @@
<Content Include="dashboard-ui\thirdparty\cordova\imagestore.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\thirdparty\cordova\registrationservices.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\thirdparty\cordova\remotecontrols.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>