Upgrade clingBottom to handle removals and be easier to use

Co-authored-by: Stamates <stamates@hotmail.com>
pull/949/head
jimmay5469 6 years ago committed by Stamates
parent 0eab4537d9
commit 914a6fe8b7
  1. 14
      apps/block_scout_web/assets/js/pages/address.js
  2. 8
      apps/block_scout_web/assets/js/pages/block.js
  3. 8
      apps/block_scout_web/assets/js/pages/transaction.js
  4. 65
      apps/block_scout_web/assets/js/utils.js

@ -4,7 +4,7 @@ import URI from 'urijs'
import humps from 'humps' import humps from 'humps'
import numeral from 'numeral' import numeral from 'numeral'
import socket from '../socket' import socket from '../socket'
import { batchChannel, initRedux, prependWithClingBottom } from '../utils' import { batchChannel, initRedux, slideDownPrepend, clingBottom } from '../utils'
import { updateAllAges } from '../lib/from_now' import { updateAllAges } from '../lib/from_now'
import { updateAllCalculatedUsdValues } from '../lib/currency.js' import { updateAllCalculatedUsdValues } from '../lib/currency.js'
import { loadTokenBalanceDropdown } from '../lib/token_balance_dropdown' import { loadTokenBalanceDropdown } from '../lib/token_balance_dropdown'
@ -209,14 +209,16 @@ if ($addressDetailsPage.length) {
$channelBatching.hide() $channelBatching.hide()
} }
if (oldState.newInternalTransactions !== state.newInternalTransactions && $internalTransactionsList.length) { if (oldState.newInternalTransactions !== state.newInternalTransactions && $internalTransactionsList.length) {
prependWithClingBottom($internalTransactionsList, state.newInternalTransactions.slice(oldState.newInternalTransactions.length).reverse().join('')) clingBottom()
slideDownPrepend($internalTransactionsList, state.newInternalTransactions.slice(oldState.newInternalTransactions.length).reverse().join(''))
updateAllAges() updateAllAges()
} }
if (oldState.pendingTransactionHashes.length !== state.pendingTransactionHashes.length && $pendingTransactionsCount.length) { if (oldState.pendingTransactionHashes.length !== state.pendingTransactionHashes.length && $pendingTransactionsCount.length) {
$pendingTransactionsCount[0].innerHTML = numeral(state.pendingTransactionHashes.length).format() $pendingTransactionsCount[0].innerHTML = numeral(state.pendingTransactionHashes.length).format()
} }
if (oldState.newPendingTransactions !== state.newPendingTransactions && $pendingTransactionsList.length) { if (oldState.newPendingTransactions !== state.newPendingTransactions && $pendingTransactionsList.length) {
prependWithClingBottom($pendingTransactionsList, state.newPendingTransactions.slice(oldState.newPendingTransactions.length).reverse().join('')) clingBottom()
slideDownPrepend($pendingTransactionsList, state.newPendingTransactions.slice(oldState.newPendingTransactions.length).reverse().join(''))
updateAllAges() updateAllAges()
} }
if (oldState.newTransactions !== state.newTransactions && $transactionsList.length) { if (oldState.newTransactions !== state.newTransactions && $transactionsList.length) {
@ -227,15 +229,17 @@ if ($addressDetailsPage.length) {
setTimeout(() => { setTimeout(() => {
$transaction.addClass('shrink-out') $transaction.addClass('shrink-out')
setTimeout(() => { setTimeout(() => {
clingBottom()
$transaction.slideUp({ complete: () => $transaction.remove() }) $transaction.slideUp({ complete: () => $transaction.remove() })
prependWithClingBottom($transactionsList, transactionHtml) slideDownPrepend($transactionsList, transactionHtml)
}, 400) }, 400)
}, 1000) }, 1000)
}) })
updateAllAges() updateAllAges()
} }
if (oldState.newBlock !== state.newBlock) { if (oldState.newBlock !== state.newBlock) {
prependWithClingBottom($validationsList, state.newBlock) clingBottom()
slideDownPrepend($validationsList, state.newBlock)
updateAllAges() updateAllAges()
} }
} }

@ -4,7 +4,7 @@ import URI from 'urijs'
import humps from 'humps' import humps from 'humps'
import socket from '../socket' import socket from '../socket'
import { updateAllAges } from '../lib/from_now' import { updateAllAges } from '../lib/from_now'
import { buildFullBlockList, initRedux, beforeWithClingBottom, skippedBlockListBuilder } from '../utils' import { buildFullBlockList, initRedux, slideDownBefore, skippedBlockListBuilder, clingBottom } from '../utils'
export const initialState = { export const initialState = {
blockNumbers: [], blockNumbers: [],
@ -97,10 +97,12 @@ if ($blockListPage.length) {
} else { } else {
if (skippedBlockNumbers.length) { if (skippedBlockNumbers.length) {
_.forEachRight(skippedBlockNumbers, (skippedBlockNumber) => { _.forEachRight(skippedBlockNumbers, (skippedBlockNumber) => {
beforeWithClingBottom($(`[data-block-number="${skippedBlockNumber - 1}"]`), placeHolderBlock(skippedBlockNumber)) clingBottom()
slideDownBefore($(`[data-block-number="${skippedBlockNumber - 1}"]`), placeHolderBlock(skippedBlockNumber))
}) })
} }
beforeWithClingBottom($(`[data-block-number="${state.blockNumbers[0] - 1}"]`), state.newBlock) clingBottom()
slideDownBefore($(`[data-block-number="${state.blockNumbers[0] - 1}"]`), state.newBlock)
} }
updateAllAges() updateAllAges()
} }

@ -5,7 +5,7 @@ import humps from 'humps'
import numeral from 'numeral' import numeral from 'numeral'
import socket from '../socket' import socket from '../socket'
import { updateAllAges } from '../lib/from_now' import { updateAllAges } from '../lib/from_now'
import { batchChannel, initRedux, prependWithClingBottom } from '../utils' import { batchChannel, initRedux, slideDownPrepend, clingBottom } from '../utils'
const BATCH_THRESHOLD = 10 const BATCH_THRESHOLD = 10
@ -189,7 +189,8 @@ if ($transactionPendingListPage.length) {
} }
if (oldState.newPendingTransactions !== state.newPendingTransactions) { if (oldState.newPendingTransactions !== state.newPendingTransactions) {
const newTransactionsToInsert = state.newPendingTransactions.slice(oldState.newPendingTransactions.length) const newTransactionsToInsert = state.newPendingTransactions.slice(oldState.newPendingTransactions.length)
prependWithClingBottom($pendingTransactionsList, newTransactionsToInsert.reverse().join('')) clingBottom()
slideDownPrepend($pendingTransactionsList, newTransactionsToInsert.reverse().join(''))
updateAllAges() updateAllAges()
} }
@ -230,7 +231,8 @@ if ($transactionListPage.length) {
} }
if (oldState.newTransactions !== state.newTransactions) { if (oldState.newTransactions !== state.newTransactions) {
const newTransactionsToInsert = state.newTransactions.slice(oldState.newTransactions.length) const newTransactionsToInsert = state.newTransactions.slice(oldState.newTransactions.length)
prependWithClingBottom($transactionsList, newTransactionsToInsert.reverse().join('')) clingBottom()
slideDownPrepend($transactionsList, newTransactionsToInsert.reverse().join(''))
updateAllAges() updateAllAges()
} }

@ -45,61 +45,54 @@ export function skippedBlockListBuilder (skippedBlockNumbers, newestBlock, oldes
return skippedBlockNumbers return skippedBlockNumbers
} }
export function slideDownPrepend ($el, content, callback) { export function slideDownPrepend ($el, content) {
const $content = $(content) const $content = $(content)
$el.prepend($content.hide()) $el.prepend($content.hide())
$content.slideDown({ complete: callback }) return $content.slideDown()
} }
export function slideDownBefore ($el, content, callback) { export function slideDownBefore ($el, content) {
const $content = $(content) const $content = $(content)
$el.before($content.hide()) $el.before($content.hide())
$content.slideDown({ complete: callback }) return $content.slideDown()
}
export function prependWithClingBottom ($el, content) {
return slideDownPrepend($el, content, clingBottom($el))
}
export function beforeWithClingBottom ($el, content) {
return slideDownBefore($el, content, clingBottom($el))
} }
function clingBottom ($el) { let clingBottomLoop
export function clingBottom ($el, content) {
if (clingBottomLoop) window.cancelAnimationFrame(clingBottomLoop)
function userAtTop () { function userAtTop () {
return window.scrollY < $('[data-selector="navbar"]').outerHeight() return window.scrollY < $('[data-selector="navbar"]').outerHeight()
} }
if (userAtTop()) return true if (userAtTop()) return
let isAnimating
function setIsAnimating () {
isAnimating = true
}
$el.on('animationstart', setIsAnimating)
let pageHeight = document.body.scrollHeight
let startingScrollPosition = window.scrollY let startingScrollPosition = window.scrollY
let endingScrollPosition = window.scrollY let endingScrollPosition = window.scrollY
function userIsScrolling () { function userIsScrolling () {
return window.scrollY < startingScrollPosition || endingScrollPosition < window.scrollY const pageHeightDiff = Math.abs(document.body.scrollHeight - pageHeight)
const minScrollPosition = _.min([
startingScrollPosition,
endingScrollPosition
]) - pageHeightDiff
const maxScrollPosition = _.max([
startingScrollPosition,
endingScrollPosition
]) + pageHeightDiff
return window.scrollY < minScrollPosition || maxScrollPosition < window.scrollY
} }
const clingDistanceFromBottom = document.body.scrollHeight - window.scrollY const clingDistanceFromBottom = document.body.scrollHeight - window.scrollY
let clingBottomLoop = window.requestAnimationFrame(function clingBottom () { clingBottomLoop = window.requestAnimationFrame(function clingBottomFrame () {
if (userIsScrolling()) return stopClinging() if (userIsScrolling()) {
window.cancelAnimationFrame(clingBottomLoop)
clingBottomLoop = null
return
}
pageHeight = document.body.scrollHeight
startingScrollPosition = window.scrollY startingScrollPosition = window.scrollY
endingScrollPosition = document.body.scrollHeight - clingDistanceFromBottom endingScrollPosition = pageHeight - clingDistanceFromBottom
$(window).scrollTop(endingScrollPosition) $(window).scrollTop(endingScrollPosition)
clingBottomLoop = window.requestAnimationFrame(clingBottom) clingBottomLoop = window.requestAnimationFrame(clingBottomFrame)
}) })
function stopClinging () {
window.cancelAnimationFrame(clingBottomLoop)
$el.off('animationstart', setIsAnimating)
$el.off('animationend animationcancel', stopClinging)
}
return {
function () {
$el.on('animationend animationcancel', stopClinging)
setTimeout(() => !isAnimating && stopClinging(), 100)
}
}
} }

Loading…
Cancel
Save