houdini/client/js/campaigns/peer_to_peer/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

116 lines
3.1 KiB
JavaScript

// License: LGPL-3.0-or-later
require('../new/wizard')
var request = require("../../common/client")
require('../../common/onboard')
// setting up some default values
appl.def('is_signing_up', true)
.def('selected_result_index', -1)
appl.def('search_nonprofits', function(value){
// keyCode 13 is the return key.
// this conditional just clears the dropdown
if(event.keyCode === 13) {
appl.def('search_results', [])
return
}
// when the user starts typing,
// it sets the selected_results key to false
appl.def('selected_result', false)
// if the the input is empty, it clears the dropdown
if (!value) {
appl.def('search_results', [])
return
}
// logic for controlling the dropdown options with up
// and down arrows
if (returnUpOrDownArrow() && appl.search_results && appl.search_results.length) {
event.preventDefault()
setIndexWithArrows(returnUpOrDownArrow())
return
}
// if the input is not an up or down arrow or an empty string
// or a return key, then it searches for nonprofits
utils.delay(300, function(){ajax_nonprofit_search(value)})
})
function ajax_nonprofit_search(value){
request.get('/nonprofits/search?npo_name=' + value).end(function(err, resp){
if(!resp.body) {
appl.def('search_results', [])
appl.notify("Sorry, we couldn't find any nonprofits containing the word '" + value + "'")
} else {
appl.def('selected_result_index', -1)
appl.def('search_results', resp.body)
}
})
}
function returnUpOrDownArrow() {
var keyCode = event.keyCode
if(keyCode === 38)
return 'up'
if(keyCode === 40)
return 'down'
}
function setIndexWithArrows(dir) {
if(dir === 'down') {
var search_length = appl.search_results.length -1
appl.def('selected_result_index', appl.selected_result_index === search_length
? search_length
: appl.selected_result_index += 1)
} else {
appl.def('selected_result_index', appl.selected_result_index === 0
? 0
: appl.selected_result_index -= 1)
}
}
appl.def('select_result', {
with_arrows: function(i, node) {
addSelectedClass(appl.prev_elem(node))
var selected = appl.search_results[appl.selected_result_index]
app.nonprofit_id = selected.id
appl.def('selected_result', selected)
utils.change_url_param('npo_id', selected.id, '/peer-to-peer')
},
with_click: function(i, node) {
appl.def('selected_result_index', i)
addSelectedClass(appl.prev_elem(node))
var selected = appl.search_results[i]
app.nonprofit_id = selected.id
appl.def('selected_result', selected)
appl.def('search_results', [])
utils.change_url_param('npo_id', selected.id, '/peer-to-peer')
}
})
function addSelectedClass(node) {
if(!node || !node.parentElement) return
var siblings = node.parentElement.querySelectorAll('li')
var len = siblings.length
while(len--){siblings[len].className=''}
node.className = 'is-selected'
}
// this is for clearing the dropdown
var main = document.querySelector('main')
main.onclick = function(ev) {
var node = ev.target.nodeName
if(node === 'INPUT' || node === 'BUTTON') {
return
}
appl.def('search_results', [])
}