Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Algorithem adjustments, remove tab index for links
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 25, 2017
1 parent a404201 commit 863d582
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
40 changes: 31 additions & 9 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const doAction = (action) => {
} else if (action.key === 'ledgerPinPercentage') {
if (!synopsis.publishers[publisher]) break
synopsis.publishers[publisher].pinPercentage = action.value
updatePublisherInfo()
updatePublisherInfo(publisher)
}
break

Expand Down Expand Up @@ -940,7 +940,7 @@ var publisherInfo = {
}
}

var updatePublisherInfo = () => {
var updatePublisherInfo = (changedPublisher) => {
var data = {}
var then = underscore.now() - msecs.week

Expand All @@ -961,7 +961,7 @@ var updatePublisherInfo = () => {
atomicWriter(pathName(synopsisPath), synopsis, () => {})
if (!publisherInfo._internal.enabled) return

publisherInfo.synopsis = synopsisNormalizer()
publisherInfo.synopsis = synopsisNormalizer(changedPublisher)
publisherInfo.synopsisOptions = synopsis.options

if (publisherInfo._internal.debugP) {
Expand Down Expand Up @@ -1021,7 +1021,7 @@ var contributeP = (publisher) => {
(!blockedP(publisher)))
}

var synopsisNormalizer = () => {
var synopsisNormalizer = (changedPublisher) => {
// courtesy of https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100#13485888
const roundToTarget = (l, target, property) => {
let off = target - underscore.reduce(l, (acc, x) => { return acc + Math.round(x[property]) }, 0)
Expand All @@ -1033,11 +1033,19 @@ var synopsisNormalizer = () => {
})
}

const normalizePinned = (dataPinned, total, target) => dataPinned.map((publisher) => {
const floatNumber = (publisher.pinPercentage / total) * target
let newPer = Math.floor(floatNumber)
if (newPer < 1) {
const normalizePinned = (dataPinned, total, target, setOne) => dataPinned.map((publisher) => {
let newPer
let floatNumber

if (setOne) {
newPer = 1
floatNumber = 1
} else {
floatNumber = (publisher.pinPercentage / total) * target
newPer = Math.floor(floatNumber)
if (newPer < 1) {
newPer = 1
}
}

publisher.weight = floatNumber
Expand Down Expand Up @@ -1118,14 +1126,28 @@ var synopsisNormalizer = () => {

// round if over 100% of pinned publishers
if (pinnedTotal > 100) {
dataPinned = roundToTarget(normalizePinned(dataPinned, pinnedTotal, 100), 100, 'pinPercentage')
const changedObject = dataPinned.filter(publisher => publisher.site === changedPublisher)[0]
const setOne = changedObject.pinPercentage > (100 - dataPinned.length - 1)

if (setOne) {
changedObject.pinPercentage = 100 - dataPinned.length + 1
changedObject.weight = changedObject.pinPercentage
}

const pinnedRestTotal = pinnedTotal - changedObject.pinPercentage
dataPinned = dataPinned.filter(publisher => publisher.site !== changedPublisher)
dataPinned = normalizePinned(dataPinned, pinnedRestTotal, (100 - changedObject.pinPercentage), setOne)
dataPinned = roundToTarget(dataPinned, (100 - changedObject.pinPercentage), 'pinPercentage')

dataUnPinned = dataUnPinned.map((result) => {
let publisher = getPublisherData(result)
publisher.percentage = 0
publisher.weight = 0
return publisher
})

dataPinned.push(changedObject)

// sync app store
appActions.changeLedgerPinnedPercentages(dataPinned)
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/renderer/components/preferences/payment/ledgerTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class LedgerTable extends ImmutableComponent {
},
{
html: <div>
<a className={css(styles.siteData)} href={publisherURL} target='_blank'>
<a className={css(styles.siteData)} href={publisherURL} target='_blank' tabIndex={-1}>
{
faviconURL
? <img className={css(styles.favicon)} src={faviconURL} alt={site} />
Expand Down
1 change: 1 addition & 0 deletions app/renderer/components/preferences/payment/pinnedInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PinnedInput extends ImmutableComponent {
}

aboutActions.changeSiteSetting(hostPattern, 'ledgerPinPercentage', value)
this.forceUpdate()
}
render () {
return <input type='text'
Expand Down
2 changes: 1 addition & 1 deletion js/components/sortableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SortableTable extends React.Component {
return this.sortTable
}

componentWillUpdate () {
componentDidUpdate () {
this.sortTable.refresh()
}
/**
Expand Down
25 changes: 7 additions & 18 deletions test/about/ledgerTableTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,11 @@ describe('Ledger table', function () {
.click(`${secondTableThirdRow} [data-test-pinned="false"]`)
.waitForElementCount(`${firstTable} tr`, 3)
.click(`${firstTableFirstRow} [data-test-id="pinnedInput"]`)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '40'])
.waitForInputText(`${firstTableFirstRow} [data-test-id="pinnedInput"]`, '40')
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '40', Brave.keys.ENTER])
.click(`${firstTableSecondRow} [data-test-id="pinnedInput"]`)
.pause(100)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '30'])
.waitForInputText(`${firstTableSecondRow} [data-test-id="pinnedInput"]`, '30')
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '30', Brave.keys.ENTER])
.click(`${firstTableThirdRow} [data-test-id="pinnedInput"]`)
.pause(100)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '20', Brave.keys.ENTER])
.waitForInputText(`${firstTableThirdRow} [data-test-id="pinnedInput"]`, '20')
.waitForTextValue(`${secondTableForthRow} [data-test-id="percentageValue"]`, '10')
})

Expand All @@ -238,22 +233,16 @@ describe('Ledger table', function () {
.click(`${secondTableThirdRow} [data-test-pinned="false"]`)
.waitForElementCount(`${firstTable} tr`, 3)
.click(`${firstTableFirstRow} [data-test-id="pinnedInput"]`)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '40'])
.waitForInputText(`${firstTableFirstRow} [data-test-id="pinnedInput"]`, '40')
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '40', Brave.keys.ENTER])
.click(`${firstTableSecondRow} [data-test-id="pinnedInput"]`)
.pause(100)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '30'])
.waitForInputText(`${firstTableSecondRow} [data-test-id="pinnedInput"]`, '30')
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '30', Brave.keys.ENTER])
.click(`${firstTableThirdRow} [data-test-id="pinnedInput"]`)
.pause(100)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '20', Brave.keys.ENTER])
.waitForInputText(`${firstTableThirdRow} [data-test-id="pinnedInput"]`, '20')
.click(`${firstTableThirdRow} [data-test-id="pinnedInput"]`)
.pause(100)
.keys([Brave.keys.DELETE, Brave.keys.DELETE, '150', Brave.keys.ENTER])
.waitForInputText(`${firstTableFirstRow} [data-test-id="pinnedInput"]`, '19')
.waitForInputText(`${firstTableSecondRow} [data-test-id="pinnedInput"]`, '13')
.waitForInputText(`${firstTableThirdRow} [data-test-id="pinnedInput"]`, '68')
.waitForInputText(`${firstTableFirstRow} [data-test-id="pinnedInput"]`, '1')
.waitForInputText(`${firstTableSecondRow} [data-test-id="pinnedInput"]`, '1')
.waitForInputText(`${firstTableThirdRow} [data-test-id="pinnedInput"]`, '98')
.waitForTextValue(`${secondTableForthRow} [data-test-id="percentageValue"]`, '0')
})
})
Expand Down

0 comments on commit 863d582

Please sign in to comment.