Write better code

pull/1/head
yflory 4 years ago
parent d9b13ca3b0
commit d390f9c995

@ -967,76 +967,12 @@ define([
Messages.broadcast_noFallback = "Don't fallback to a default language"; Messages.broadcast_noFallback = "Don't fallback to a default language";
var onRefreshBroadcast = Util.mkEvent(); var onRefreshBroadcast = Util.mkEvent();
var getBroadcastForm = function ($form, key) { var broadcast = {
$form.empty(); getData: function () { return false; },
reset: function () {},
var getData = function () { handlers: {}
return false;
};
var reset = function () {};
var button = h('button.btn.btn-primary', Messages.support_formButton);
var $button = $(button);
var send = function (_cb) {
var cb = Util.once(_cb || function () {});
var data = getData();
if (data === false) {
cb('NODATA');
return void UI.warn(Messages.error);
}
$button.prop('disabled', 'disabled');
data.time = +new Date();
common.mailbox.sendTo('BROADCAST_'+key.toUpperCase(), data, {}, function (err, data) {
$button.prop('disabled', '');
cb(err, data);
if (err) {
console.error(err);
return UI.warn(Messages.error);
}
// Only print success if there is no callback
if (!_cb) {
UI.log(Messages.saved);
// Clear the UI
reset();
onRefreshBroadcast.fire();
}
});
}; };
broadcast.handlers.custom = function ($form, button, send, preview, onPreview) {
$button.click(function () {
send();
});
var onPreview = function (l) {
var data = getData();
if (data === false) { return void UI.warn(Messages.error); }
var msg = {
uid: Util.uid(),
type: 'BROADCAST_'+key.toUpperCase(),
content: data
};
common.mailbox.onMessage({
lang: l,
type: 'broadcast',
content: {
msg: msg,
hash: 'LOCAL|' + JSON.stringify(msg).slice(0,58)
}
}, function () {
UI.log(Messages.saved);
});
};
var preview = h('button.cp-broadcast-preview.btn.btn-secondary', Messages.broadcast_preview);
$(preview).click(function () {
onPreview();
});
if (key === 'custom') {
(function () {
// Custom message // Custom message
var container = h('div.cp-broadcast-container'); var container = h('div.cp-broadcast-container');
var $container = $(container); var $container = $(container);
@ -1115,7 +1051,7 @@ define([
}); });
// Extract form data // Extract form data
getData = function () { broadcast.getData = function () {
var map = {}; var map = {};
var defaultLanguage; var defaultLanguage;
var error = false; var error = false;
@ -1144,7 +1080,7 @@ define([
}; };
}; };
// Clear all the textarea when sent // Clear all the textarea when sent
reset = function () { broadcast.reset = function () {
$container.find('.cp-broadcast-lang textarea').each(function (i, el) { $container.find('.cp-broadcast-lang textarea').each(function (i, el) {
$(el).val(''); $(el).val('');
}); });
@ -1167,11 +1103,8 @@ define([
button button
]) ])
]); ]);
})(); };
return; broadcast.handlers.maintenance = function ($form, button, send, preview) {
}
if (key === 'maintenance') {
(function () {
// Maintenance message // Maintenance message
// Start and end date pickers // Start and end date pickers
@ -1192,7 +1125,7 @@ define([
}); });
// Extract form data // Extract form data
getData = function () { broadcast.getData = function () {
var start = +new Date($start.val()); var start = +new Date($start.val());
var end = +new Date($end.val()); var end = +new Date($end.val());
if (isNaN(start) || isNaN(end)) { if (isNaN(start) || isNaN(end)) {
@ -1206,7 +1139,7 @@ define([
}; };
// Clear when sent // Clear when sent
reset = function () { broadcast.reset = function () {
$start.val(''); $start.val('');
$end.val(''); $end.val('');
}; };
@ -1221,11 +1154,9 @@ define([
preview preview
]) ])
]); ]);
})();
return; };
} broadcast.handlers.version = function ($form, button, send, preview) {
if (key === 'version') {
(function () {
// New version available message // New version available message
// This checkbox can be used to trigger a fake "reconnect" event on the clients // This checkbox can be used to trigger a fake "reconnect" event on the clients
@ -1236,12 +1167,12 @@ define([
var $checkbox = $cbox.find('input'); var $checkbox = $cbox.find('input');
// Extract the data and make the form // Extract the data and make the form
getData = function () { broadcast.getData = function () {
return { return {
reload: $checkbox.is(':checked') reload: $checkbox.is(':checked')
}; };
}; };
reset = function () { broadcast.reset = function () {
$checkbox[0].checked = false; $checkbox[0].checked = false;
}; };
$form.append([ $form.append([
@ -1252,17 +1183,14 @@ define([
preview preview
]) ])
]); ]);
})(); };
return; broadcast.handlers.survey = function ($form, button, send, preview) {
}
if (key === 'survey') {
(function () {
// New survey message // New survey message
// TODO send different URLs for other languages? // TODO send different URLs for other languages?
var label = h('label', Messages.broadcast_surveyURL); var label = h('label', Messages.broadcast_surveyURL);
var input = h('input'); var input = h('input');
var $input = $(input); var $input = $(input);
getData = function () { broadcast.getData = function () {
var url = $input.val(); var url = $input.val();
if (!Util.isValidURL(url)) { if (!Util.isValidURL(url)) {
console.error('Invalid URL'); console.error('Invalid URL');
@ -1272,11 +1200,11 @@ define([
url: url url: url
}; };
}; };
reset = function () { broadcast.reset = function () {
$input.val(''); $input.val('');
}; };
$(button).off('click').click(function () { $(button).off('click').click(function () {
var data = getData(); var data = broadcast.getData();
var val = $input.val() || ''; var val = $input.val() || '';
// Invalid url: abort // Invalid url: abort
@ -1290,7 +1218,6 @@ define([
cmd: 'ADMIN_DECREE', cmd: 'ADMIN_DECREE',
data: ['SET_SURVEY_URL', [url]] data: ['SET_SURVEY_URL', [url]]
}, function (e) { }, function (e) {
console.error(e);
if (e) { if (e) {
UI.warn(Messages.error); console.error(e); UI.warn(Messages.error); console.error(e);
return; return;
@ -1309,12 +1236,8 @@ define([
preview preview
]) ])
]); ]);
})(); };
return; broadcast.handlers.delete = function ($form, button, send) {
}
if (key === 'delete') {
// Delete form
require(['/api/broadcast?'+ (+new Date())], function (BCast) { require(['/api/broadcast?'+ (+new Date())], function (BCast) {
// Always display the messages from the instance "lastBroadcastHash" // Always display the messages from the instance "lastBroadcastHash"
@ -1368,11 +1291,11 @@ define([
classes: 'btn-danger', classes: 'btn-danger',
multiple: true multiple: true
}, function () { }, function () {
getData = function () { broadcast.getData = function () {
if (!uid) { return false; } if (!uid) { return false; }
return { uid: uid }; return { uid: uid };
}; };
reset = function () { broadcast.reset = function () {
$(deleteBtn).prop('disabled', 'disabled').text(Messages.deleted); $(deleteBtn).prop('disabled', 'disabled').text(Messages.deleted);
}; };
send(); send();
@ -1387,16 +1310,17 @@ define([
classes: 'btn-danger', classes: 'btn-danger',
multiple: true multiple: true
}, function () { }, function () {
getData = function () { broadcast.getData = function () {
return { all: true }; return { all: true };
}; };
reset = function () {}; broadcast.reset = function () {};
// Send a message to all users telling them to wipe the broadcast mailbox // Send a message to all users telling them to wipe the broadcast mailbox
// and on success, send an admin decree to update /api/broadcast // and on success, send an admin decree to update /api/broadcast
send(function (err, obj) { send(function (err, obj) {
if (err) { return; } if (err) { return; }
if (!obj || !obj.hash) { return; } if (!obj || !obj.hash) { return; }
/*
sFrameChan.query('Q_ADMIN_RPC', { sFrameChan.query('Q_ADMIN_RPC', {
cmd: 'ADMIN_DECREE', cmd: 'ADMIN_DECREE',
data: ['SET_LAST_BROADCAST_HASH', [obj.hash]] data: ['SET_LAST_BROADCAST_HASH', [obj.hash]]
@ -1408,6 +1332,7 @@ define([
// On success, reload the "delete" tab // On success, reload the "delete" tab
onRefreshBroadcast.fire(); onRefreshBroadcast.fire();
}); });
*/
}); });
}); });
@ -1417,9 +1342,86 @@ define([
]); ]);
}); });
}); });
};
var getBroadcastForm = function ($form, key) {
$form.empty();
var button = h('button.btn.btn-primary', Messages.support_formButton);
var $button = $(button);
var send = function (_cb) {
var cb = Util.once(_cb || function () {});
var data = broadcast.getData();
if (data === false) {
cb('NODATA');
return void UI.warn(Messages.error);
}
$button.prop('disabled', 'disabled');
data.time = +new Date();
common.mailbox.sendTo('BROADCAST_'+key.toUpperCase(), data, {}, function (err, data) {
$button.prop('disabled', '');
cb(err, data);
if (err) {
console.error(err);
return UI.warn(Messages.error);
}
sFrameChan.query('Q_ADMIN_RPC', {
cmd: 'ADMIN_DECREE',
data: ['SET_LAST_BROADCAST_HASH', [data.hash]]
}, function (e) {
if (e) {
UI.warn(Messages.error); console.error(e);
return; return;
} }
// On success, reload the "delete" tab
onRefreshBroadcast.fire();
});
// Only print success if there is no callback
if (!_cb) {
UI.log(Messages.saved);
// Clear the UI
broadcast.reset();
onRefreshBroadcast.fire();
}
});
};
$button.click(function () {
send();
});
var onPreview = function (l) {
var data = broadcast.getData();
if (data === false) { return void UI.warn(Messages.error); }
var msg = {
uid: Util.uid(),
type: 'BROADCAST_'+key.toUpperCase(),
content: data
};
common.mailbox.onMessage({
lang: l,
type: 'broadcast',
content: {
msg: msg,
hash: 'LOCAL|' + JSON.stringify(msg).slice(0,58)
}
}, function () {
UI.log(Messages.saved);
});
};
var preview = h('button.cp-broadcast-preview.btn.btn-secondary', Messages.broadcast_preview);
$(preview).click(function () {
onPreview();
});
var handler = broadcast.handlers[key];
if (!handler) { return; } // XXX
handler($form, button, send, preview, onPreview);
}; };
create['broadcast'] = function () { create['broadcast'] = function () {
var key = 'broadcast'; var key = 'broadcast';

Loading…
Cancel
Save