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";
var onRefreshBroadcast = Util.mkEvent();
var getBroadcastForm = function ($form, key) {
$form.empty();
var getData = function () {
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();
}
});
var broadcast = {
getData: function () { return false; },
reset: function () {},
handlers: {}
};
$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 () {
broadcast.handlers.custom = function ($form, button, send, preview, onPreview) {
// Custom message
var container = h('div.cp-broadcast-container');
var $container = $(container);
@ -1115,7 +1051,7 @@ define([
});
// Extract form data
getData = function () {
broadcast.getData = function () {
var map = {};
var defaultLanguage;
var error = false;
@ -1144,7 +1080,7 @@ define([
};
};
// Clear all the textarea when sent
reset = function () {
broadcast.reset = function () {
$container.find('.cp-broadcast-lang textarea').each(function (i, el) {
$(el).val('');
});
@ -1167,11 +1103,8 @@ define([
button
])
]);
})();
return;
}
if (key === 'maintenance') {
(function () {
};
broadcast.handlers.maintenance = function ($form, button, send, preview) {
// Maintenance message
// Start and end date pickers
@ -1192,7 +1125,7 @@ define([
});
// Extract form data
getData = function () {
broadcast.getData = function () {
var start = +new Date($start.val());
var end = +new Date($end.val());
if (isNaN(start) || isNaN(end)) {
@ -1206,7 +1139,7 @@ define([
};
// Clear when sent
reset = function () {
broadcast.reset = function () {
$start.val('');
$end.val('');
};
@ -1221,11 +1154,9 @@ define([
preview
])
]);
})();
return;
}
if (key === 'version') {
(function () {
};
broadcast.handlers.version = function ($form, button, send, preview) {
// New version available message
// This checkbox can be used to trigger a fake "reconnect" event on the clients
@ -1236,12 +1167,12 @@ define([
var $checkbox = $cbox.find('input');
// Extract the data and make the form
getData = function () {
broadcast.getData = function () {
return {
reload: $checkbox.is(':checked')
};
};
reset = function () {
broadcast.reset = function () {
$checkbox[0].checked = false;
};
$form.append([
@ -1252,17 +1183,14 @@ define([
preview
])
]);
})();
return;
}
if (key === 'survey') {
(function () {
};
broadcast.handlers.survey = function ($form, button, send, preview) {
// New survey message
// TODO send different URLs for other languages?
var label = h('label', Messages.broadcast_surveyURL);
var input = h('input');
var $input = $(input);
getData = function () {
broadcast.getData = function () {
var url = $input.val();
if (!Util.isValidURL(url)) {
console.error('Invalid URL');
@ -1272,11 +1200,11 @@ define([
url: url
};
};
reset = function () {
broadcast.reset = function () {
$input.val('');
};
$(button).off('click').click(function () {
var data = getData();
var data = broadcast.getData();
var val = $input.val() || '';
// Invalid url: abort
@ -1290,7 +1218,6 @@ define([
cmd: 'ADMIN_DECREE',
data: ['SET_SURVEY_URL', [url]]
}, function (e) {
console.error(e);
if (e) {
UI.warn(Messages.error); console.error(e);
return;
@ -1309,12 +1236,8 @@ define([
preview
])
]);
})();
return;
}
if (key === 'delete') {
// Delete form
};
broadcast.handlers.delete = function ($form, button, send) {
require(['/api/broadcast?'+ (+new Date())], function (BCast) {
// Always display the messages from the instance "lastBroadcastHash"
@ -1368,11 +1291,11 @@ define([
classes: 'btn-danger',
multiple: true
}, function () {
getData = function () {
broadcast.getData = function () {
if (!uid) { return false; }
return { uid: uid };
};
reset = function () {
broadcast.reset = function () {
$(deleteBtn).prop('disabled', 'disabled').text(Messages.deleted);
};
send();
@ -1387,16 +1310,17 @@ define([
classes: 'btn-danger',
multiple: true
}, function () {
getData = function () {
broadcast.getData = function () {
return { all: true };
};
reset = function () {};
broadcast.reset = function () {};
// Send a message to all users telling them to wipe the broadcast mailbox
// and on success, send an admin decree to update /api/broadcast
send(function (err, obj) {
if (err) { return; }
if (!obj || !obj.hash) { return; }
/*
sFrameChan.query('Q_ADMIN_RPC', {
cmd: 'ADMIN_DECREE',
data: ['SET_LAST_BROADCAST_HASH', [obj.hash]]
@ -1408,6 +1332,7 @@ define([
// On success, reload the "delete" tab
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;
}
// 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 () {
var key = 'broadcast';

Loading…
Cancel
Save