Skip to content

Commit bf3b045

Browse files
committed
Add plugin technology to add header buttons.
1 parent 499c867 commit bf3b045

File tree

7 files changed

+75
-37
lines changed

7 files changed

+75
-37
lines changed

.gitignore

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
build/*
2-
website_meta_data.txt
3-
website_analytics_data.txt
4-
website_script_data.txt
5-
website_intro_data.txt
2+
plugins/*
63
node_modules
74
__pycache__

source/website/index.js

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { SetExternalLibLocation } from '../engine/io/externallibs.js';
2-
import { CreateDomElement } from '../engine/viewer/domutils.js';
3-
import { AddSvgIconElement, InstallTooltip } from './utils.js';
4-
import { SetEventHandler } from './eventhandler.js';
52
import { Embed } from './embed.js';
63
import { Website } from './website.js';
74

8-
export function SetWebsiteEventHandler (eventHandler)
9-
{
10-
SetEventHandler (eventHandler);
11-
}
5+
import { SetEventHandler } from './eventhandler.js';
6+
export { SetEventHandler };
7+
import { PluginType, RegisterPlugin } from './pluginregistry.js';
8+
export { PluginType, RegisterPlugin };
129

1310
export function StartWebsite (externalLibLocation)
1411
{
1512
SetExternalLibLocation (externalLibLocation);
1613
window.addEventListener ('load', () => {
1714
let website = new Website ({
1815
headerDiv : document.getElementById ('header'),
16+
headerButtonsDiv : document.getElementById ('header_buttons'),
1917
toolbarDiv : document.getElementById ('toolbar'),
2018
mainDiv : document.getElementById ('main'),
2119
introDiv : document.getElementById ('intro'),
@@ -42,15 +40,3 @@ export function StartEmbed (externalLibLocation)
4240
embed.Load ();
4341
});
4442
}
45-
46-
export function CreateHeaderButton (parentElement, iconName, title, link)
47-
{
48-
let buttonLink = CreateDomElement ('a');
49-
buttonLink.setAttribute ('href', link);
50-
buttonLink.setAttribute ('target', '_blank');
51-
buttonLink.setAttribute ('rel', 'noopener noreferrer');
52-
InstallTooltip (buttonLink, title);
53-
AddSvgIconElement (buttonLink, iconName, 'header_button');
54-
parentElement.appendChild (buttonLink);
55-
return buttonLink;
56-
}

source/website/pluginregistry.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let plugins = new Map ();
2+
3+
export const PluginType =
4+
{
5+
Header : 1
6+
};
7+
8+
export function RegisterPlugin (type, plugin)
9+
{
10+
if (!plugins.has (type)) {
11+
plugins.set (type, []);
12+
}
13+
let typedPlugins = plugins.get (type);
14+
typedPlugins.push (plugin);
15+
}
16+
17+
export function EnumeratePlugins (type, onPlugin)
18+
{
19+
if (!plugins.has (type)) {
20+
return;
21+
}
22+
let typedPlugins = plugins.get (type);
23+
for (let typedPlugin of typedPlugins) {
24+
onPlugin (typedPlugin);
25+
}
26+
}

source/website/website.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { GetFileExtension, TransformFileHostUrls } from '../engine/io/fileutils.
22
import { InputFilesFromFileObjects, InputFilesFromUrls } from '../engine/import/importerfiles.js';
33
import { ImportErrorCode, ImportSettings } from '../engine/import/importer.js';
44
import { CameraMode, Viewer } from '../engine/viewer/viewer.js';
5-
import { AddDiv, AddDomElement, ShowDomElement, SetDomElementOuterHeight } from '../engine/viewer/domutils.js';
5+
import { AddDiv, AddDomElement, ShowDomElement, SetDomElementOuterHeight, CreateDomElement } from '../engine/viewer/domutils.js';
66
import { CalculatePopupPositionToScreen, ShowListPopup } from './dialogs.js';
77
import { HandleEvent } from './eventhandler.js';
88
import { HashHandler } from './hashhandler.js';
@@ -14,7 +14,7 @@ import { ThreeModelLoaderUI } from './threemodelloaderui.js';
1414
import { Toolbar } from './toolbar.js';
1515
import { ShowExportDialog } from './exportdialog.js';
1616
import { ShowSnapshotDialog } from './snapshotdialog.js';
17-
import { AddSmallWidthChangeEventListener, GetFilesFromDataTransfer, IsSmallWidth } from './utils.js';
17+
import { AddSmallWidthChangeEventListener, AddSvgIconElement, GetFilesFromDataTransfer, InstallTooltip, IsSmallWidth } from './utils.js';
1818
import { ShowOpenUrlDialog } from './openurldialog.js';
1919
import { ShowSharingDialog } from './sharingdialog.js';
2020
import { HasDefaultMaterial, ReplaceDefaultMaterialColor } from '../engine/model/modelutils.js';
@@ -24,6 +24,7 @@ import { MeasureTool } from './measuretool.js';
2424
import { CloseAllDialogs } from './dialog.js';
2525

2626
import * as THREE from 'three';
27+
import { EnumeratePlugins, PluginType } from './pluginregistry.js';
2728

2829
export const WebsiteUIState =
2930
{
@@ -58,6 +59,14 @@ export class Website
5859
this.SwitchTheme (this.settings.themeId, false);
5960
HandleEvent ('theme_on_load', this.settings.themeId === Theme.Light ? 'light' : 'dark');
6061

62+
EnumeratePlugins (PluginType.Header, (plugin) => {
63+
plugin.registerButtons ({
64+
createHeaderButton : (icon, title, link) => {
65+
this.CreateHeaderButton (icon, title, link);
66+
}
67+
});
68+
});
69+
6170
this.InitViewer ();
6271
this.InitToolbar ();
6372
this.InitDragAndDrop ();
@@ -776,6 +785,18 @@ export class Website
776785
this.sidebar.ShowPanels (showSidebar);
777786
}
778787

788+
CreateHeaderButton (icon, title, link)
789+
{
790+
let buttonLink = CreateDomElement ('a');
791+
buttonLink.setAttribute ('href', link);
792+
buttonLink.setAttribute ('target', '_blank');
793+
buttonLink.setAttribute ('rel', 'noopener noreferrer');
794+
InstallTooltip (buttonLink, title);
795+
AddSvgIconElement (buttonLink, icon, 'header_button');
796+
this.parameters.headerButtonsDiv.appendChild (buttonLink);
797+
return buttonLink;
798+
}
799+
779800
InitCookieConsent ()
780801
{
781802
let accepted = CookieGetBoolVal ('ov_cookie_consent', false);

tools/create_package.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ def CreateDestinationDir (config, rootDir, websiteDir, version, testBuild):
3636
shutil.copytree (os.path.join (rootDir, 'website', 'css', 'O3DVIcons'), os.path.join (websiteDir, 'o3dv', 'O3DVIcons'))
3737
shutil.copytree (os.path.join (rootDir, 'website', 'info'), os.path.join (websiteDir, 'info'))
3838

39+
pluginFiles = []
40+
pluginsDir = os.path.join (rootDir, 'plugins')
41+
if os.path.exists (pluginsDir):
42+
for pluginFile in os.listdir (pluginsDir):
43+
if os.path.splitext (pluginFile)[1] != '.js':
44+
continue
45+
websitePluginsDir = os.path.join (websiteDir, 'plugins');
46+
if not os.path.exists (websitePluginsDir):
47+
os.makedirs (websitePluginsDir)
48+
shutil.copy2 (os.path.join (pluginsDir, pluginFile), os.path.join (websitePluginsDir, pluginFile))
49+
pluginFiles.append ('plugins/' + pluginFile)
50+
3951
websiteLibFiles = config['website_lib_files']
4052
websiteFiles = [
4153
'o3dv/o3dv.website.min.css',
@@ -54,6 +66,7 @@ def CreateDestinationDir (config, rootDir, websiteDir, version, testBuild):
5466
replacer = Tools.TokenReplacer (htmlFilePath, False)
5567
replacer.ReplaceTokenFileLinks ('<!-- website libs start -->', '<!-- website libs end -->', websiteLibFiles, version)
5668
replacer.ReplaceTokenFileLinks ('<!-- website start -->', '<!-- website end -->', websiteFiles, version)
69+
replacer.ReplaceTokenFileLinks ('<!-- plugins start -->', '<!-- plugins end -->', pluginFiles, version)
5770
initScriptContent = ''
5871
initScriptContent += '<script type="text/javascript">' + replacer.eolChar
5972
initScriptContent += ' OV.StartWebsite (\'libs\');' + replacer.eolChar
@@ -64,19 +77,15 @@ def CreateDestinationDir (config, rootDir, websiteDir, version, testBuild):
6477
embedInitScriptContent += '</script>'
6578
replacer.ReplaceTokenContent ('<!-- website init start -->', '<!-- website init end -->', initScriptContent)
6679
replacer.ReplaceTokenContent ('<!-- embed init start -->', '<!-- embed init end -->', embedInitScriptContent)
67-
metaFile = os.path.join (rootDir, 'tools', 'website_meta_data.txt')
80+
metaFile = os.path.join (rootDir, 'plugins', 'website_meta_data.txt')
6881
if os.path.exists (metaFile):
6982
metaContent = Tools.GetFileContent (metaFile)
7083
replacer.ReplaceTokenContent ('<!-- meta start -->', '<!-- meta end -->', metaContent)
71-
analyticsFile = os.path.join (rootDir, 'tools', 'website_analytics_data.txt')
84+
analyticsFile = os.path.join (rootDir, 'plugins', 'website_analytics_data.txt')
7285
if os.path.exists (analyticsFile) and not testBuild:
7386
analyticsContent = Tools.GetFileContent (analyticsFile)
7487
replacer.ReplaceTokenContent ('<!-- analytics start -->', '<!-- analytics end -->', analyticsContent)
75-
scriptFile = os.path.join (rootDir, 'tools', 'website_script_data.txt')
76-
if os.path.exists (scriptFile):
77-
scriptContent = Tools.GetFileContent (scriptFile)
78-
replacer.ReplaceTokenContent ('<!-- script start -->', '<!-- script end -->', scriptContent)
79-
introFile = os.path.join (rootDir, 'tools', 'website_intro_data.txt')
88+
introFile = os.path.join (rootDir, 'plugins', 'website_intro_data.txt')
8089
if os.path.exists (introFile):
8190
introContent = Tools.GetFileContent (introFile)
8291
replacer.ReplaceTokenContent ('<!-- intro start -->', '<!-- intro end -->', introContent)

website/embed.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<!-- analytics start -->
3030
<script type="text/javascript">
31-
OV.SetWebsiteEventHandler ((eventName, eventLabel, eventParams) => {
31+
OV.SetEventHandler ((eventName, eventLabel, eventParams) => {
3232
console.log ({
3333
eventName : eventName,
3434
eventLabel : eventLabel,

website/index.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
<link rel="stylesheet" type="text/css" href="css/embed.css">
3131
<script type="text/javascript" src="../build/o3dv.website.min-dev.js"></script>
3232
<!-- website end -->
33+
<!-- plugins start -->
34+
<!-- plugins end -->
3335

3436
<!-- analytics start -->
3537
<script type="text/javascript">
36-
OV.SetWebsiteEventHandler ((eventName, eventLabel, eventParams) => {
38+
OV.SetEventHandler ((eventName, eventLabel, eventParams) => {
3739
console.log ({
3840
eventName : eventName,
3941
eventLabel : eventLabel,
@@ -49,9 +51,6 @@
4951
</script>
5052
<!-- website init end -->
5153

52-
<!-- script start -->
53-
<!-- script end -->
54-
5554
</head>
5655

5756
<body>

0 commit comments

Comments
 (0)