site/_src/_assets/javascripts/page-getstarted.js

189 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-05-21 14:15:11 +02:00
//=include gumshoejs/dist/gumshoe.js
2017-06-26 22:38:25 +02:00
//=include bigchain/tab.js
2016-02-10 01:04:57 +01:00
const bigchaindbUrl = 'https://test.bigchaindb.com'
2017-12-20 18:45:22 +01:00
const apiPath = '/api/v1/'
2017-07-18 11:47:58 +02:00
// jQuery(function ($) {
2016-02-10 01:04:57 +01: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
//
2020-05-25 11:18:32 +02:00
var spy = new Gumshoe('#gumshoe a');
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]
2018-06-13 14:25:33 +02:00
if (window.innerWidth >= 768) {
2017-06-28 17:13:20 +02:00
const offset = menu.offsetTop
2018-06-13 14:25:33 +02:00
window.addEventListener('scroll', function () {
2017-06-28 17:13:20 +02:00
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)
2018-06-13 14:25:33 +02:00
.then(function (response) {
2018-04-10 15:43:38 +02:00
return response.json()
})
2018-06-13 14:25:33 +02:00
.then(function (data) {
2018-04-10 15:43:38 +02:00
const version = data.version
const titleOrig = versionOutput.getAttribute('title')
versionOutput.innerText = version
versionOutput.setAttribute('title', titleOrig + version)
})
2018-06-13 14:25:33 +02:00
.catch(function (error) {
2018-04-10 15:43:38 +02:00
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
const API_PATH = bigchaindbUrl + apiPath
2017-06-22 17:03:05 +02:00
const postButton = document.getElementById('post')
const postButtonText = postButton.innerText
2017-06-23 12:47:58 +02:00
const messageInput = document.getElementById('message')
// put a wrapper around original message, and empty it
function addMessageWrapper() {
const codeExamples = document.querySelectorAll('.highlight code')
codeExamples.forEach(function (codeExample) {
if (codeExample.innerText.includes('Blockchain all the things!')) {
let html = codeExample.innerHTML
html = html.replace('Blockchain all the things!', '<strong class="code-example__message"></strong>')
codeExample.innerHTML = html
}
})
}
addMessageWrapper()
// update message helper function
function updateMessage(content) {
const codeMessages = document.querySelectorAll('.code-example__message')
2017-07-06 17:37:58 +02:00
const escapedContent = content.replace(/'/g, "\\'")
2018-06-13 14:25:33 +02:00
codeMessages.forEach(function (codeMessage) {
2017-07-06 17:37:58 +02:00
codeMessage.textContent = escapedContent
})
}
// quick form validation, live update code example with user input
2018-06-13 14:25:33 +02:00
messageInput.addEventListener('input', function () {
2017-06-23 12:47:58 +02:00
if (messageInput.value === '') {
postButton.setAttribute('disabled', '')
// empty message
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.style.width = `${postButton.offsetWidth}px`
function buttonStateLoading() {
postButton.classList.add('disabled')
postButton.innerHTML = '<span class="loader loader--dark"></span>'
}
function buttonStateSuccess() {
postButton.style.opacity = 0
}
function buttonStateFail() {
postButton.classList.remove('disabled')
postButton.removeAttribute('disabled')
postButton.innerHTML = postButtonText
}
2018-06-13 14:25:33 +02:00
postButton.addEventListener('click', function (e) {
2017-06-22 17:03:05 +02:00
e.preventDefault()
buttonStateLoading()
2017-06-22 17:03:05 +02:00
const message = messageInput.value
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-05-02 18:09:02 +02:00
conn.postTransactionCommit(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-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-12-20 18:45:22 +01:00
transactionLink.href = bigchaindbUrl + apiPath + 'transactions/' + response.id
buttonStateSuccess()
2017-06-27 16:16:51 +02:00
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
buttonStateFail()
2017-06-26 22:38:25 +02:00
}).then((res) => console.log('Transaction status:', res.status))
2017-06-22 17:03:05 +02:00
}, false)
}, false)