-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from SumOfUs/development
Toggleable funding table
- Loading branch information
Showing
18 changed files
with
277 additions
and
203 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
source/javascripts/sumofus/backbone/funding/conversion_rates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const conversionRates = { | ||
AUD: { | ||
_2014: 0.8666, | ||
_2015: 0.7435, | ||
_2016: 0.7143, | ||
}, | ||
CAD: { | ||
_2014: 0.8703, | ||
_2015: 0.7524, | ||
_2016: 0.7252, | ||
}, | ||
EUR: { | ||
_2014: 1.2755, | ||
_2015: 1.0672, | ||
_2016: 1.0638, | ||
}, | ||
GBP: { | ||
_2014: 1.5823, | ||
_2015: 1.4684, | ||
_2016: 1.2987, | ||
}, | ||
USD: { | ||
_2014: 1, | ||
_2015: 1, | ||
_2016: 1, | ||
} | ||
}; | ||
|
||
module.exports = conversionRates; |
62 changes: 62 additions & 0 deletions
62
source/javascripts/sumofus/backbone/funding/expenses_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const conversionRates = require('./conversion_rates.js'); | ||
|
||
const TableTemplate = require('./table_template.js'); | ||
const Expenses = Backbone.Model.extend({ | ||
defaults: { | ||
_2014: { | ||
total: 3230875, | ||
campaigns: 2855182, | ||
ops: 280345, | ||
fundraising: 95348, | ||
}, | ||
_2015: { | ||
total: 4765579, | ||
campaigns: 4304197, | ||
ops: 328136, | ||
fundraising: 133246, | ||
}, | ||
_2016: { | ||
total: 4977788, | ||
campaigns: 4341557, | ||
ops: 467838, | ||
fundraising: 168848, | ||
} | ||
} | ||
}); | ||
|
||
const ExpensesTable = Backbone.View.extend({ | ||
events: { | ||
'change #expenses-currency-select': 'changeCurrency', | ||
}, | ||
el: '.expenses-table-container', | ||
model: new Expenses(), | ||
|
||
categories: ['total', 'campaigns', 'ops', 'fundraising'], | ||
|
||
initialize: function() { | ||
// render initial format - default to USD | ||
this.$el.html(this.template('expenses', this.categories, this.model.attributes, 'USD')); | ||
return this; | ||
}, | ||
|
||
conversionRates: conversionRates, | ||
|
||
changeCurrency(e) { | ||
const currency = $('#expenses-currency-select option:selected').val() | ||
const rates = this.conversionRates[currency]; | ||
var newAttributes = {}; | ||
_.each(this.model.attributes, function(sources, year){ | ||
newAttributes[year] = {}; | ||
_.each(sources, function(amount, source){ | ||
newAttributes[year][source] = Math.round(amount / rates[year]) | ||
}); | ||
}); | ||
const convertedModel = new Expenses(newAttributes); | ||
this.$el.html(this.template('expenses', this.categories, convertedModel.attributes, currency)); | ||
return this; | ||
}, | ||
|
||
template: TableTemplate, | ||
}); | ||
|
||
module.exports = ExpensesTable; |
15 changes: 15 additions & 0 deletions
15
source/javascripts/sumofus/backbone/funding/format_currency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const AccountingJs = require('accounting-js'); | ||
|
||
const formatCurrency = function(money, currency) { | ||
// make money a string and format it appropriately to the currency | ||
switch (currency) { | ||
case 'EUR': | ||
return AccountingJs.formatMoney(money, { symbol: "€", precision: 0 }); | ||
case 'GBP': | ||
return AccountingJs.formatMoney(money, { symbol: "£", precision: 0 }); | ||
default: | ||
return AccountingJs.formatMoney(money, { precision: 0}); | ||
} | ||
} | ||
|
||
module.exports = formatCurrency; |
62 changes: 62 additions & 0 deletions
62
source/javascripts/sumofus/backbone/funding/funding_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const conversionRates = require('./conversion_rates.js'); | ||
const TableTemplate = require('./table_template.js'); | ||
|
||
const Funding = Backbone.Model.extend({ | ||
defaults: { | ||
_2014: { | ||
total: 4426005, | ||
individuals: 3655474, | ||
foundations: 748712, | ||
other: 21819, | ||
}, | ||
_2015: { | ||
total: 4857359, | ||
individuals: 4080182, | ||
foundations: 761250, | ||
other: 15927, | ||
}, | ||
_2016: { | ||
total: 4637815, | ||
individuals: 3869976, | ||
foundations: 612728, | ||
other: 155111, | ||
} | ||
} | ||
}); | ||
|
||
const FundingTable = Backbone.View.extend({ | ||
events: { | ||
'change #funding-currency-select': 'changeCurrency', | ||
}, | ||
el: '.funding-table-container', | ||
model: new Funding(), | ||
|
||
categories: ['total', 'individuals', 'foundations', 'other'], | ||
|
||
conversionRates: conversionRates, | ||
|
||
initialize: function() { | ||
// render initial format - default to USD | ||
this.$el.html(this.template('funding', this.categories, this.model.attributes, 'USD')); | ||
return this; | ||
}, | ||
|
||
changeCurrency(e) { | ||
const currency = $('#funding-currency-select option:selected').val() | ||
const rates = this.conversionRates[currency]; | ||
var newAttributes = {}; | ||
_.each(this.model.attributes, function(sources, year){ | ||
newAttributes[year] = {}; | ||
_.each(sources, function(amount, source){ | ||
newAttributes[year][source] = Math.round(amount / rates[year]) | ||
}); | ||
}); | ||
const convertedModel = new Funding(newAttributes); | ||
this.$el.html(this.template('funding', this.categories, convertedModel.attributes, currency)); | ||
return this; | ||
}, | ||
|
||
template: TableTemplate, | ||
}); | ||
|
||
module.exports = FundingTable; |
6 changes: 6 additions & 0 deletions
6
source/javascripts/sumofus/backbone/funding/round_percentile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const roundPercentile = function(x, y, decimals) { | ||
const value = 100 * (x / y); | ||
return Number(Math.round(value+'e'+decimals)+'e-'+decimals); | ||
} | ||
|
||
module.exports = roundPercentile; |
12 changes: 12 additions & 0 deletions
12
source/javascripts/sumofus/backbone/funding/select_template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const conversionRates = require('./conversion_rates.js'); | ||
|
||
const selectTemplate = function(type, selectCurrency){ | ||
var openTag = `<select id="${type}-currency-select" class="currency-select" name="currency">`; | ||
_.each(Object.keys(conversionRates), function(currency){ | ||
const selected = currency === selectCurrency ? " selected" : ""; | ||
openTag = openTag.concat(`<option${selected}>${currency}</option>`); | ||
}) | ||
return openTag.concat(`</select>`); | ||
} | ||
|
||
module.exports = selectTemplate; |
Oops, something went wrong.