147 lines
4.2 KiB
JavaScript
147 lines
4.2 KiB
JavaScript
|
/*
|
||
|
* This file is part of Linode Manager Classic.
|
||
|
*
|
||
|
* Linode Manager Classic is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* Linode Manager Classic is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
import { settings, elements, apiGet, parseParams, setupHeader } from "/global.js";
|
||
|
|
||
|
(function()
|
||
|
{
|
||
|
// Element names specific to this page
|
||
|
elements.lmcRow = "lmc-tr1";
|
||
|
elements.lmcRowAlt = "lmc-tr2";
|
||
|
elements.loading = "loading";
|
||
|
elements.noPayments = "no-payments";
|
||
|
elements.paymentsBody = "payments-body";
|
||
|
elements.subnav = "subnav-link";
|
||
|
elements.subnavActive = "subnav-link-active";
|
||
|
elements.total = "total";
|
||
|
elements.year = "year";
|
||
|
|
||
|
// Data received from API calls
|
||
|
var data = {};
|
||
|
data.payments = [];
|
||
|
|
||
|
// Static references to UI elements
|
||
|
var ui = {};
|
||
|
ui.loading = {};
|
||
|
ui.noPayments = {};
|
||
|
ui.paymentsBody = {};
|
||
|
ui.total = {};
|
||
|
ui.year = {};
|
||
|
|
||
|
// Generate a payment table row
|
||
|
var createPaymentRow = function(payment, alt)
|
||
|
{
|
||
|
var row = document.createElement("tr");
|
||
|
if (alt)
|
||
|
row.className = elements.lmcRowAlt;
|
||
|
else
|
||
|
row.className = elements.lmcRow;
|
||
|
|
||
|
var description = document.createElement("td");
|
||
|
description.innerHTML = "Payment. Thank you.";
|
||
|
row.appendChild(description);
|
||
|
|
||
|
var date = document.createElement("td");
|
||
|
var paymentDate = new Date(payment.date + "Z");
|
||
|
date.innerHTML = paymentDate.toLocaleDateString();
|
||
|
row.appendChild(date);
|
||
|
|
||
|
var amount = document.createElement("td");
|
||
|
amount.innerHTML = "$" + payment.usd.toFixed(2);
|
||
|
row.appendChild(amount);
|
||
|
|
||
|
return row;
|
||
|
};
|
||
|
|
||
|
// Callback for payments API call
|
||
|
var displayPayments = function(response)
|
||
|
{
|
||
|
// Only add payments from the specified year
|
||
|
var year = parseInt(data.params.year);
|
||
|
for (var i = 0; i < response.data.length; i++) {
|
||
|
var paymentDate = new Date(response.data[i].date + "Z");
|
||
|
if (paymentDate.getFullYear() == year)
|
||
|
data.payments.push(response.data[i]);
|
||
|
}
|
||
|
|
||
|
// Request the next page if there are more
|
||
|
if (response.page != response.pages) {
|
||
|
apiGet("/account/payments?page=" + (response.page + 1), displayPayments, null);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
ui.loading.remove();
|
||
|
if (!data.payments.length) {
|
||
|
ui.noPayments.style.display = "table-row";
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Insert payments into table
|
||
|
var total = 0.0;
|
||
|
for (var i = 0; i < data.payments.length; i++) {
|
||
|
total += data.payments[i].usd;
|
||
|
ui.paymentsBody.appendChild(createPaymentRow(data.payments[i], i % 2));
|
||
|
}
|
||
|
|
||
|
ui.total.innerHTML = "$" + total.toFixed(2);
|
||
|
};
|
||
|
|
||
|
// Initial setup
|
||
|
var setup = function()
|
||
|
{
|
||
|
// Parse URL parameters
|
||
|
data.params = parseParams();
|
||
|
|
||
|
// We need a year, so die if we don't have it
|
||
|
if (!data.params.year) {
|
||
|
alert("No payment ID supplied!");
|
||
|
return;
|
||
|
} else if (!parseInt(data.params.year)) {
|
||
|
alert("Supplied year is invalid!");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
setupHeader();
|
||
|
|
||
|
// Highlight the remote access subnav link
|
||
|
var subnavLinks = document.getElementsByClassName(elements.subnav);
|
||
|
for (var i = 0; i < subnavLinks.length; i++) {
|
||
|
if (subnavLinks[i].pathname == "/account/billing_history")
|
||
|
subnavLinks[i].className = elements.subnav + " " + elements.subnavActive;
|
||
|
else
|
||
|
subnavLinks[i].className = elements.subnav;
|
||
|
}
|
||
|
|
||
|
// Get element references
|
||
|
ui.loading = document.getElementById(elements.loading);
|
||
|
ui.noPayments = document.getElementById(elements.noPayments);
|
||
|
ui.paymentsBody = document.getElementById(elements.paymentsBody);
|
||
|
ui.total = document.getElementById(elements.total);
|
||
|
ui.year = document.getElementById(elements.year);
|
||
|
|
||
|
// Set year
|
||
|
var year = parseInt(data.params.year);
|
||
|
ui.year.innerHTML = year;
|
||
|
|
||
|
// Get data from API
|
||
|
apiGet("/account/payments", displayPayments, null);
|
||
|
};
|
||
|
|
||
|
// Attach onload handler
|
||
|
window.addEventListener("load", setup);
|
||
|
})();
|