This repository was archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathleft-menu.js
211 lines (184 loc) · 7.98 KB
/
left-menu.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { html, render } from 'https://unpkg.com/[email protected]/lit-html.js';
import { getTemplates, getTemplateByName, onStoreUpdate } from './template-store.js';
import { CONTAINER_ID } from './platform-window.js';
const CHART_URL = 'https://cdn.openfin.co/embed-web/chart.html';
const LAYOUT_STORE_KEY = 'LayoutForm';
const SNAPSHOT_STORE_KEY = 'SnapshotForm';
//Our Left Menu element
class LeftMenu extends HTMLElement {
constructor() {
super();
this.onclick = this.clickHandler;
//List of apps available in the menu.
this.appList = [
{
// A JSON manifest can be used for View Options starting in runtime v16.83.53.*+
// Any properties in the manifest will overwrite other properties given here
manifestUrl: window.location.href.replace('platform-window.html', 'view-manifest.json'),
printName: 'OF Chart',
},
{
url: 'https://www.tradingview.com/chart/?symbol=NASDAQ:AAPL',
printName: 'TradeView',
processAffinity: 'tv_1'
},
{
url: 'https://www.google.com/search?q=INDEXDJX:+.DJI&stick=H4sIAAAAAAAAAONgecRozC3w8sc9YSmtSWtOXmNU4eIKzsgvd80rySypFBLjYoOyeKS4uDj0c_UNkgsry3kWsfJ5-rm4Rrh4RVgp6Ll4eQIAqJT5uUkAAAA&source=lnms&sa=X&ved=0ahUKEwii_NWT9fzoAhU3mHIEHWy3AWIQ_AUIDSgA&biw=1280&bih=1366&dpr=1',
printName: 'News',
processAffinity: 'mw_1'
},
{
url: window.location.href.replace('platform-window', 'color-view'),
printName: 'Colors',
processAffinity: 'cv_1'
},
{
url: `https://cdn.openfin.co/docs/javascript/${fin.desktop.getVersion()}`,
printName: "Documentation",
processAffinity: 'ps_1'
}
];
this.snapshotForm = document.querySelector('snapshot-form');
this.layoutForm = document.querySelector('layout-form');
this.layoutContainer = document.querySelector(`#${CONTAINER_ID}`);
this.render();
//Whenever the store updates we will want to render any new elements.
onStoreUpdate(() => { this.render(); });
}
clickHandler = (e) => {
const target = e.target;
if (target.className === 'snapshot-button' || target.className === 'layout-button') {
if (!this.layoutContainer.classList.contains('hidden')) {
this.layoutContainer.classList.toggle('hidden');
}
}
if (target.className === 'snapshot-button') {
this.snapshotForm.showElement();
this.layoutForm.hideElement();
} else if (target.className === 'layout-button') {
this.layoutForm.showElement();
this.snapshotForm.hideElement();
} else {
this.layoutForm.hideElement();
this.snapshotForm.hideElement();
if (this.layoutContainer.classList.contains('hidden')) {
this.layoutContainer.classList.toggle('hidden');
}
}
}
render = async () => {
const layoutTemplates = getTemplates(LAYOUT_STORE_KEY);
const snapshotTemplates = getTemplates(SNAPSHOT_STORE_KEY);
const menuItems = html`
<span>Applications</span>
<ul>
${this.appList.map((item) => html`<li>
<button @click=${() => this.addView(item.printName)}>${item.printName}</button>
</li>`)}
</ul>
<span>Windows</span>
<ul>
<li><button @click=${() => this.layoutWindow().catch(console.error)}>Platform Window</button></li>
<li><button @click=${() => this.nonLayoutWindow().catch(console.error)}>OF Window</button></li>
</ul>
<span>Layouts</span>
<ul>
<li><button @click=${() => this.toGrid().catch(console.error)}>Grid</button></li>
<li><button @click=${() => this.toTabbed().catch(console.error)}>Tab</button></li>
${layoutTemplates.map((item) => html`<li>
<button @click=${() => this.replaceLayoutFromTemplate(item.name)}>${item.name}</button>
</li>`)}
<li><button @click=${() => this.cloneWindow().catch(console.error)}>Clone</button></li>
<li><button class="layout-button">Save Layout</button></li>
</ul>
<span>Snapshots</span>
<ul>
${snapshotTemplates.map((item) => html`<li><button @click=${() => this.applySnapshotFromTemplate(item.name)}>${item.name}</button></li>`)}
<li><button class="snapshot-button">Save Snapshot</button></li>
<li><button @click=${() => this.share()}>Share</button></li>
</ul>`;
return render(menuItems, this);
}
applySnapshotFromTemplate = async (templateName) => {
const template = getTemplateByName(SNAPSHOT_STORE_KEY, templateName);
return fin.Platform.getCurrentSync().applySnapshot(template.snapshot, {
closeExistingWindows: template.close
});
}
replaceLayoutFromTemplate = async (templateName) => {
const templates = getTemplates(LAYOUT_STORE_KEY);
const templateToUse = templates.find(i => i.name === templateName);
fin.Platform.Layout.getCurrentSync().replace(templateToUse.layout);
}
addView = async (printName) => {
const viewOptions = this.appList.find(i => i.printName === printName);
return fin.Platform.getCurrentSync().createView(viewOptions, fin.me.identity);
}
toGrid = async () => {
await fin.Platform.Layout.getCurrentSync().applyPreset({
presetType: 'grid'
});
}
toTabbed = async () => {
await fin.Platform.Layout.getCurrentSync().applyPreset({
presetType: 'tabs'
});
}
toRows = async () => {
await fin.Platform.Layout.getCurrentSync().applyPreset({
presetType: 'rows'
});
}
cloneWindow = async () => {
const bounds = await fin.me.getBounds();
const layout = await fin.Platform.Layout.getCurrentSync().getConfig();
const customContext = await fin.Platform.getCurrentSync().getWindowContext();
const snapshot = {
windows: [
{
defaultWidth: bounds.width,
defaultHeight: bounds.height,
layout,
//getWindowContext actually returns the customContext option.
customContext
}
]
};
return fin.Platform.getCurrentSync().applySnapshot(snapshot);
}
nonLayoutWindow = async () => {
return fin.Platform.getCurrentSync().applySnapshot({
windows: [{
defaultWidth: 600,
defaultHeight: 600,
defaultLeft: 200,
defaultTop: 200,
saveWindowState: false,
url: CHART_URL,
contextMenu: true
}]
});
}
layoutWindow = async () => {
const viewOptions = {
url: CHART_URL
};
return fin.Platform.getCurrentSync().createView(viewOptions);
}
share = async () => {
const { windows } = await fin.Platform.getCurrentSync().getSnapshot();
const contentConfig = {snapshot: { windows } };
const res = await fetch('https://jsonblob.com/api/jsonBlob', {
method: 'POST', // or 'PUT'
body: JSON.stringify(contentConfig), // data can be `string` or {object}!
headers: {
'Content-Type': 'application/json'
}
});
const contentUrl = res.headers.get('Location');
const { manifestUrl } = await fin.Application.getCurrentSync().getInfo();
const startUrl = `https://openfin.github.io/start/?manifest=${encodeURIComponent(`${manifestUrl}?$$appManifestUrl=${contentUrl}`)}`;
fin.System.openUrlWithBrowser(startUrl);
}
}
customElements.define('left-menu', LeftMenu);