Commit 888db56f authored by Aurélien Béliard's avatar Aurélien Béliard Committed by Mikaël Salson

app.js, main.js, database.js, notification.js: refactor code into Notification

See #2152, #2772...
Refactor done by @aurelBZH in 2017.
parent 93c0fcf0
...@@ -65,6 +65,7 @@ function loadAfterConf() { ...@@ -65,6 +65,7 @@ function loadAfterConf() {
"../crossDomain", "../crossDomain",
"../database", "../database",
"../shortcut", "../shortcut",
"../notification",
"../export", "../export",
"../similarity", "../similarity",
"../tools", "../tools",
......
...@@ -996,29 +996,7 @@ Database.prototype = { ...@@ -996,29 +996,7 @@ Database.prototype = {
xhrFields: {withCredentials: true}, xhrFields: {withCredentials: true},
timeout: DB_TIMEOUT_CALL, timeout: DB_TIMEOUT_CALL,
success: function (result) { success: function (result) {
var messages; notification.parse_notification(result)
try {
messages = JSON.parse(result);
var header_messages = [];
var login_messages = [];
for (var i = 0; i < messages.length; ++i) {
if (messages[i].notification.message_type == 'header') {
header_messages.push(messages[i]);
} else if (messages[i].notification.message_type == 'login') {
login_messages.push(messages[i]);
}
}
//TODO see if we can remove this hard coupling to classes
var hm = $('#header_messages');
self.integrateMessages(hm, header_messages);
var lm = $('#login_messages');
self.integrateMessages(lm, login_messages);
} catch (err) {
console.log("ERROR: " + err);
}
}, },
error: function (request, status, error) { error: function (request, status, error) {
...@@ -1035,42 +1013,6 @@ Database.prototype = { ...@@ -1035,42 +1013,6 @@ Database.prototype = {
} }
}, },
// takes a jQuery elem
integrateMessages: function(elem, messages, classNames) {
var message, preformat;
// empty container because prototype is destined to be called periodically
elem.empty();
//set default classes if they are undefined
classNames = classNames === undefined ? {'urgent': 'urgent_message', 'info': 'info_message'} : classNames;
if (messages.length > 0) {
for (var i=0; i < messages.length; ++i) {
display_title = ""
if (messages[i].notification.message_type == 'login')
{
display_title += messages[i].notification.creation_datetime.split(' ')[0] + ' : '
}
display_title += messages[i].notification.title
message = document.createElement('div');
message.className = classNames[messages[i].notification.priority] + " notification";
$(message).attr('onclick', "db.call('notification/index', {'id': '" + messages[i].notification.id + "'})");
$(message).append(
// message is sanitized by the server so we unescape the string to include links and formatting
document.createTextNode(unescape(display_title))
);
elem.append(message);
}
elem.fadeIn();
} else {
// No messages to display so hide message container
elem.fadeOut();
}
},
//affiche la fenetre de dialogue avec le serveur et affiche ses réponses //affiche la fenetre de dialogue avec le serveur et affiche ses réponses
display: function (msg) { display: function (msg) {
this.div.style.display = "block"; this.div.style.display = "block";
......
...@@ -46,6 +46,7 @@ setCrossDomainModel(m); ...@@ -46,6 +46,7 @@ setCrossDomainModel(m);
* This links the model to a patient database (possibly the one defined in config.js) * This links the model to a patient database (possibly the one defined in config.js)
*/ */
var db = new Database(m); var db = new Database(m);
var notification = new Notification(m)
try { try {
/* Views /* Views
......
function Notification(m) {
this.m = m;
}
Notification.prototype = {
parse_notification: function (result) {
var messages;
try {
messages = JSON.parse(result);
var header_messages = [];
var login_messages = [];
for (var i = 0; i < messages.length; ++i) {
if (messages[i].notification.message_type == 'header') {
header_messages.push(messages[i]);
} else if (messages[i].notification.message_type == 'login') {
login_messages.push(messages[i]);
}
}
//TODO see if we can remove this hard coupling to classes
var hm = $('#header_messages');
this.integrateMessages(hm, header_messages);
var lm = $('#login_messages');
this.integrateMessages(lm, login_messages);
} catch (err) {
console.log("Notification Error: " + err);
}
},
integrateMessages: function(elem, messages, classNames) {
var message, preformat;
// empty container because prototype is destined to be called periodically
elem.empty();
//set default classes if they are undefined
classNames = classNames === undefined ? {'urgent': 'urgent_message', 'info': 'info_message'} : classNames;
if (messages.length > 0) {
for (var i=0; i < messages.length; ++i) {
display_title = ""
if (messages[i].notification.message_type == 'login')
{
display_title += messages[i].notification.creation_datetime.split(' ')[0] + ' : '
}
display_title += messages[i].notification.title
message = document.createElement('div');
message.className = classNames[messages[i].notification.priority] + " notification";
$(message).attr('onclick', "db.call('notification/index', {'id': '" + messages[i].notification.id + "'})");
// message is sanitized by the server so we unescape the string to include links and formatting
$(message).append(document.createTextNode(unescape(display_title)));
elem.append(message);
}
elem.fadeIn();
} else {
// No messages to display so hide message container
elem.fadeOut();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment