1
0
mirror of https://github.com/bigchaindb/site.git synced 2024-11-25 11:08:29 +01:00
site/_src/_assets/javascripts/page-getstarted.js

172 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-06-26 22:38:25 +02:00
//=require gumshoe/dist/js/gumshoe.js
//=include bigchain/tab.js
//=include bigchain/newsletter.js
2016-02-10 01:04:57 +01:00
2018-02-05 10:40:52 +01:00
const bigchaindbUrl = 'https://test.bigchaindb.com'
2018-02-06 14:56:14 +01:00
const proxyUrl = 'https://getstarted.bigchaindb.com'
2017-12-20 18:45:22 +01:00
const apiPath = '/api/v1/'
2017-07-18 11:47:58 +02:00
2016-02-10 01:04:57 +01:00
jQuery(function($) {
2017-06-15 19:16:45 +02:00
//
// init modules
//
Newsletter.init()
2017-06-26 22:38:25 +02:00
2017-06-15 19:16:45 +02:00
})
2017-06-22 17:03:05 +02:00
2017-06-26 22:38:25 +02:00
//
2017-06-28 17:13:20 +02:00
// Scrollspy
2017-06-26 22:38:25 +02:00
//
2017-06-28 17:13:20 +02:00
gumshoe.init()
2017-06-26 22:38:25 +02:00
//
2017-06-28 17:13:20 +02:00
// Sticky nav
2017-06-26 22:38:25 +02:00
//
2017-06-28 17:13:20 +02:00
function stickyNav() {
const menu = document.getElementsByClassName('menu--sub')[0]
if ( window.innerWidth >= 768 ) {
const offset = menu.offsetTop
window.addEventListener('scroll', function() {
if (offset < window.pageYOffset) {
menu.classList.add('sticky')
} else {
menu.classList.remove('sticky')
}
}, false)
}
}
stickyNav()
2017-06-26 22:38:25 +02:00
2018-04-10 15:43:38 +02:00
//
// Test network version
//
function testNetworkVersion() {
const versionOutput = document.getElementById('network-version')
fetch(bigchaindbUrl)
.then(function(response) {
return response.json()
})
.then(function(data) {
const version = data.version
const titleOrig = versionOutput.getAttribute('title')
versionOutput.innerText = version
versionOutput.setAttribute('title', titleOrig + version)
})
.catch(function(error) {
console.log(error)
})
}
testNetworkVersion()
2017-06-26 22:38:25 +02:00
//
// BigchainDB transaction tool
//
// makes this file huuuuge, consider loading this as additional request
2017-06-23 12:47:58 +02:00
//=include bigchaindb-driver/dist/browser/bigchaindb-driver.window.min.js
2017-06-22 17:23:48 +02:00
2017-06-26 22:38:25 +02:00
window.addEventListener('DOMContentLoaded', function domload(event) {
2017-06-22 17:23:48 +02:00
window.removeEventListener('DOMContentLoaded', domload, false)
2017-06-22 17:03:05 +02:00
const driver = window.BigchainDB
2017-12-20 18:45:22 +01:00
const API_PATH = proxyUrl + apiPath
2017-06-22 17:03:05 +02:00
2017-06-22 17:23:48 +02:00
const form = document.getElementById('form-transaction')
2017-06-22 17:03:05 +02:00
const postButton = document.getElementById('post')
2017-06-23 12:47:58 +02:00
const messageInput = document.getElementById('message')
// nasty jquery inside of here, YOLO
$(".highlight code:contains('Blockchain all the things!')").html(function(_, html) {
return html.replace(/(Blockchain all the things!)/g, '<strong class="code-example__message">$1</strong>');
})
const codeMessages = document.querySelectorAll('.code-example__message')
function updateMessage(content) {
2017-07-06 17:37:58 +02:00
const escapedContent = content.replace(/'/g, "\\'")
for (var codeMessage of codeMessages) {
2017-07-06 17:37:58 +02:00
codeMessage.textContent = escapedContent
}
}
// empty default message
updateMessage('')
// quick form validation, live update code example with user input
2017-06-23 12:47:58 +02:00
messageInput.addEventListener('input', function() {
if (messageInput.value === '') {
postButton.setAttribute('disabled', '')
// empty message again
updateMessage('')
2017-06-23 12:47:58 +02:00
} else {
postButton.removeAttribute('disabled')
// update code message
updateMessage(messageInput.value)
2017-06-23 12:47:58 +02:00
}
})
2017-06-22 17:03:05 +02:00
postButton.addEventListener('click', function(e) {
e.preventDefault()
2017-06-23 12:47:58 +02:00
const message = messageInput.value
2018-02-05 17:46:30 +01:00
postButton.classList.add('disabled')
2017-06-22 17:03:05 +02:00
2017-06-22 17:23:48 +02:00
const alice = new driver.Ed25519Keypair()
2017-07-11 11:00:01 +02:00
const tx = driver.Transaction.makeCreateTransaction(
{ message: message },
null,
[driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))],
alice.publicKey)
2017-06-22 17:03:05 +02:00
const txSigned = driver.Transaction.signTransaction(tx, alice.privateKey)
2017-07-11 11:00:01 +02:00
const conn = new driver.Connection(API_PATH)
2017-06-22 17:03:05 +02:00
2017-06-22 18:12:56 +02:00
const waiting = document.getElementsByClassName('waiting')[0]
2017-06-23 12:47:58 +02:00
const responseArea = document.getElementsByClassName('response')[0]
2017-06-22 18:12:56 +02:00
const output = document.getElementsByClassName('output')[0]
2017-06-28 00:45:26 +02:00
const messageInitial = document.getElementsByClassName('message')[0]
2017-06-22 18:23:47 +02:00
const messageSuccess = document.getElementsByClassName('message--success')[0]
const messageFail = document.getElementsByClassName('message--fail')[0]
const transactionLink = document.getElementsByClassName('transaction-link')[0]
2017-06-22 18:12:56 +02:00
2018-04-09 14:42:18 +02:00
conn.postTransactionSync(txSigned).then((response) => {
2017-06-26 22:38:25 +02:00
waiting.classList.add('hide')
2017-06-28 00:45:26 +02:00
messageInitial.classList.add('hide')
2017-06-26 22:38:25 +02:00
responseArea.classList.remove('hide')
messageSuccess.classList.remove('hide')
2017-06-22 18:12:56 +02:00
2017-06-26 22:38:25 +02:00
console.log(response)
2017-06-23 12:47:58 +02:00
2017-06-27 16:16:51 +02:00
const outputContent = JSON.stringify(response, null, 2) // indented with 2 spaces
2017-06-26 22:38:25 +02:00
output.textContent = outputContent
2017-06-23 12:47:58 +02:00
2017-12-20 18:45:22 +01:00
transactionLink.href = bigchaindbUrl + apiPath + 'transactions/' + response.id
2017-06-27 16:16:51 +02:00
postButton.style.opacity = 0
2017-06-26 22:38:25 +02:00
}, reason => { // Error!
console.log(reason)
2017-06-22 18:12:56 +02:00
2017-06-26 22:38:25 +02:00
waiting.classList.add('hide')
responseArea.classList.remove('hide')
messageFail.classList.remove('hide')
2017-06-22 17:03:05 +02:00
2017-06-26 22:38:25 +02:00
const outputContent = reason.status + ' ' + reason.statusText
output.textContent = outputContent
}).then((res) => console.log('Transaction status:', res.status))
2017-06-22 17:03:05 +02:00
}, false)
}, false)