houdini/client/js/nonprofits/button/page.js
Bradley M. Kuhn fc77ee76d6 Relicense Javascript code in accordance with project's new license
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

The Additional Permission is designed to permit publicly distributed
Javascript code to be relicensed under LGPL-3.0-or-later, but not server-side
Javascript code.  As such, we've relicensed here static Javscript files under
LGPL-3.0-or-later, and those that run as part of build and/or server side
under AGPL-3.0-or-later.

Note that in future, Javascript files may be updated to be stronger copyleft
license with the Additional Permission, particularly if they adapted to run
on server side and/or turned into templates.  Of course, we'd seek public
discussion with the contributor community about such changes.

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

196 lines
5.3 KiB
JavaScript

// License: LGPL-3.0-or-later
var view = require("vvvview")
var flyd = require("flyd")
flyd.scanmerge = require("flyd/module/scanmerge")
var h = require("virtual-dom/h")
var setStateFromValue = require('../../components/set-state-from-value')
var appearance = require('./appearance')
var designations = require('./designations')
var amounts = require('./amounts')
var type = require('./type')
var hideDedication = require('./hide-dedication')
var thankYou = require('./thank-you')
var preview = require('./preview')
var $footer = require('./footer').stream
var state = {
page: window.location.hash.replace('#', '')
? window.location.hash.replace('#', '')
: 'appearance',
settings: {
appearance: {
name: 'default',
customText: 'Donate'
},
designations: {count: 1, multiples: {}},
amounts: {
name: 'multiple',
single: 30,
multiples: {0: 10, 1: 20, 2: 30, 3: 70, 4: 100, 5: 200, 6: 1000 }
},
type: { name: 'both'},
thankYou: {}
}
}
function root(state) {
return h('div', [
menu(state),
pages(state)
])
}
var $page = flyd.stream()
var $pageClick = flyd.stream()
flyd.map(function(ev){
if(ev.target.data.page === 'preview') {
appendScript()
} else {
removeScript()
}
}, $pageClick)
$page = flyd.merge($page,
flyd.map(function(ev) {
return ev.target.data.page
}, $pageClick))
function appendScript(){
var script = document.createElement('script')
script.id = 'commitchange-donation-script'
script.setAttribute('data-npo-id', app.nonprofit_id)
script.setAttribute('src', app.host_with_port + '/js/donate-button.v2.js')
document.body.appendChild(script)
}
function removeScript(){
if(document.getElementById('commitchange-donation-script')){
document.getElementById('commitchange-donation-script').remove()
}
removeButtonContent()
}
function removeButtonContent(){
var donateButton = document.querySelector('.commitchange-donate')
while(donateButton.lastChild){
donateButton.removeChild(donateButton.lastChild)
}
}
function appendButtonCode(){
document.getElementById('choose-role-modal').classList.add('inView')
document.body.classList.add('is-showingModal')
var buttonWrapper = document.getElementById('js-donateButtonWrapper').cloneNode(true)
while(buttonWrapper.querySelector('iframe')) {
buttonWrapper.querySelector('iframe').remove()
}
while(buttonWrapper.querySelector('div')){
buttonWrapper.querySelector('div').remove()
}
var code = buttonWrapper.innerHTML.replace(/"/g, "'")
document.getElementById('js-donateButtonAnchor').value = code
document.querySelector('#send-code-modal input[name="code"]').value = code
}
function menu(state){
var menuItems = [
{name: 'appearance', text: 'Appearance'},
{name: 'designations', text: 'Designations'},
{name: 'amounts', text: 'Preset amounts'},
{name: 'type', text: 'Preset recurring or one-time'},
{name: 'hideDedication', text: 'Hide dedication' },
{name: 'thankYou', text: 'Thank-you page'},
{name: 'preview', text: 'Live preview'}]
var lis =[]
var button = h('div.u-paddingX-10',
h('a.button--large.orange.u-width--full', {onclick: appendButtonCode}, 'Finish'))
menuItems.map(function(item) {
var liClass = state.page === item.name ? '.active' : ''
lis.push(h('li' + liClass, {data: {page: item.name}, onclick: $pageClick}, item.text))
})
return h('aside.stepsMenu', [h('ul', lis), button])
}
function pageWrapper(state, pageName, content){
return h('section.step.' + pageName, {
style: {display: state.page === pageName ? 'block' : 'none'}
}, content)
}
function pages(state){
return [
pageWrapper(state, 'appearance', appearance.root(state)),
pageWrapper(state, 'designations', designations.root(state)),
pageWrapper(state, 'amounts', amounts.root(state)),
pageWrapper(state, 'type', type.root(state)),
pageWrapper(state, 'hideDedication', hideDedication.root(state)),
pageWrapper(state, 'thankYou', thankYou.root(state)),
pageWrapper(state, 'preview', preview.root(state))
]
}
var donateFormBuilder = view(root, document.getElementById('js-donateFormBuilder'), state)
var nameStreams = [appearance.stream, designations.streams.name, amounts.stream, type.stream, hideDedication.stream, thankYou.stream]
.map(function(stream) { return [stream, setStateFromValue]})
window.state = state
var scanPairs = [
[$page, setPage],
[$footer, advancePage],
[designations.streams.count, addDesignation]
].concat(nameStreams)
var $state = flyd.immediate(flyd.scanmerge(scanPairs, state))
// rerenders the view based on state changes
// takes the view and state stream
flyd.map(donateFormBuilder, $state)
function setPage(state, pageName){
state.page = window.location.hash = pageName
return state
}
function addDesignation(state, ev) {
if(state.settings.designations.count < 20) {
state.settings.designations.count++
}
return state
}
function advancePage(state, ev) {
state.page = ev.target.data.next
return state
}
// // Send email to webmaster
$('#send-code-modal form').on('submit', function(e) {
var self = this
e.preventDefault()
var data = $(this).serializeObject()
$(this).find('button').loading('Sending...')
$.post('/nonprofits/' + app.nonprofit_id + '/button/send_code.json', data)
.done(function() {
notification('Email sent!')
appl.close_modal()
})
.complete(function() {
$(self).find('button').disableLoading()
})
.fail(function(d) {
notification('Error: ' + utils.print_error(d))
})
})