-
Notifications
You must be signed in to change notification settings - Fork 368
/
JavascriptUtilities.cs
46 lines (40 loc) · 1.56 KB
/
JavascriptUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
namespace Microsoft.DotNet.Interactive.Http
{
public static class JavascriptUtilities
{
public static string GetCodeForEnsureRequireJs(Uri requireJsUri = null, string onRequirejsLoadedCallBackName = null)
{
requireJsUri??= new Uri("https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js");
string GenerateOnload()
{
return !string.IsNullOrWhiteSpace(onRequirejsLoadedCallBackName)
? $@"
require_script.onload = function() {{
{onRequirejsLoadedCallBackName}();
}};"
: string.Empty;
}
string GenerateElseBranch()
{
return !string.IsNullOrWhiteSpace(onRequirejsLoadedCallBackName)
? $@"else {{
{onRequirejsLoadedCallBackName}();
}}"
: string.Empty;
}
return $@"// ensure `require` is available globally
if ((typeof(require) !== typeof(Function)) || (typeof(require.config) !== typeof(Function))) {{
let require_script = document.createElement('script');
require_script.setAttribute('src', '{requireJsUri.AbsoluteUri}');
require_script.setAttribute('type', 'text/javascript');
{GenerateOnload()}
document.getElementsByTagName('head')[0].appendChild(require_script);
}}
{GenerateElseBranch()}
";
}
}
}