freechat/freechat-client/src/html.js

54 lines
1.4 KiB
JavaScript

const auth = require('./auth.js')
/**
* @param {String} id HTML ID
* @param {String} type Base tag type => examples include 'button' 'div'
* @param {Object} attrs Config object with the following valid fields and their types
*
* @returns HTMLElement
*/
exports.new_node = function new_node(id, type, attrs) {
let node = document.createElement(type)
if(id) { node.id = id; }
for(const key in attrs) {
if(key == 'classList') {
attrs['classList'].forEach(cls => node.classList.add(cls))
continue
}
node[key] = attrs[key]
}
return node
}
/**
* {Config} config | configuration object passed from the ipc
* {String} dom_id | element container for the server list itself
* */
exports.build_server_list = function (config, dom_id) {
const channels = require('./channels.js')
if(!config['servers']) { return; }
let container = document.getElementById(dom_id)
for(let server of config['servers']) {
// Request a new token so that further functions don't accidently try to use
// an old token
if(!('jwt' in server)) {
auth.request_new_token(server)
}
const id = server['domain'] || server['ip']
let child = html.new_node(id, 'button', {
'classList': ['btn', 'btn-outline-secondary', 'btn-nav-settings'],
'textContent': server['name'],
'type': 'button',
})
child.addEventListener('click', () => channels.get_channels(server))
container.appendChild(child)
}
}