-
Notifications
You must be signed in to change notification settings - Fork 20
/
example.html
128 lines (112 loc) · 4.83 KB
/
example.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Wealthica Example Add-on</title>
<style>
body { margin: 0; padding: 0; }
</style>
<script type="text/javascript" src="https://wealthica.github.io/wealthica.js/dist/addon.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
<h3>This is the Wealthica Example Add-on.</h3>
<p>Try some of the actions below:</p>
<p>
<button type="button" disabled="disabled" id="getTransactions">List Transactions</button>
<button type="button" disabled="disabled" id="getPositions">List Positions</button>
<button type="button" disabled="disabled" id="addTransaction">Add Transaction</button>
<button type="button" disabled="disabled" id="saveData">Save Addon Data</button>
</p>
<textarea id="data" rows="8" cols="100"></textarea>
<div id="result"></div>
<script type="text/javascript">
var addon, addonOptions;
$(function () {
addon = new Addon();
addon.on('init', function (options) {
// Dashboard is ready and is signaling to the add-on that it should
// render using the passed in options (filters, language, etc.)
addonOptions = options;
$('button').removeAttr('disabled');
showAddonData(addonOptions.data, true);
}).on('update', function (options) {
// Filters have been updated and Dashboard is passing in updated options
addonOptions = _.extend(addonOptions, options);
showAddonData(addonOptions.data);
});
$('#addTransaction').on('click', function () {
$(this).attr('disabled', 'disabled');
addon.addTransaction({
description: "New transaction from Example Add-on"
}).then(function (transaction) {
if(transaction) {
$('#result').html('New transaction:<br><code>' + JSON.stringify(transaction) + '</code>');
}
}).catch(function (err) {
$('#result').html('Error:<br><code>' + err + '</code>');
}).finally(function () {
$('#addTransaction').removeAttr('disabled');
});
});
$('#getTransactions').on('click', function () {
$(this).attr('disabled', 'disabled');
addon.api.getTransactions(getQueryFromOptions(addonOptions)).then(function (response) {
$('#result').html('List Transactions Result:<br><code>' + JSON.stringify(response) + '</code>');
}).catch(function (err) {
$('#result').html('Error:<br><code>' + err + '</code>');
}).finally(function () {
$('#getTransactions').removeAttr('disabled');
});
});
$('#getPositions').on('click', function () {
$(this).attr('disabled', 'disabled');
addon.api.getPositions(getQueryFromOptions(addonOptions)).then(function (response) {
$('#result').html('List Positions Result:<br><code>' + JSON.stringify(response) + '</code>');
}).catch(function (err) {
$('#result').html('Error:<br><code>' + err + '</code>');
}).finally(function () {
$('#getPositions').removeAttr('disabled');
});
});
$('#saveData').on('click', function () {
$(this).attr('disabled', 'disabled');
var newData = $('#data').val();
// Try parsing textarea value to a JSON object before passing to addon.saveData(data)
try { newData = JSON.parse(newData) }
catch (e) {
$('#result').html('Error:<br><code>Data must be JSON</code>');
$('#saveData').removeAttr('disabled');
return;
}
addon.saveData(newData).catch(function (err) {
$('#result').html('Error:<br><code>' + err + '</code>');
}).finally(function () {
$('#saveData').removeAttr('disabled');
});
});
// Show addon data in result box and optionally update the text input.
function showAddonData(data, updateInput) {
if (data) {
$('#result').html('Addon data:<br><code>' + JSON.stringify(data) + '</code>');
}
if (updateInput && data) {
$('#data').val(JSON.stringify(data));
}
}
// Compose a query object from the addon options to pass to the API calls.
function getQueryFromOptions (options) {
return {
from: options.dateRangeFilter && options.dateRangeFilter[0],
to: options.dateRangeFilter && options.dateRangeFilter[1],
groups: options.groupsFilter,
institutions: options.institutionsFilter,
investments: options.investmentsFilter === 'all' ? null: options.investmentsFilter,
}
}
});
</script>
</body>
</html>