From 1d3c25fdccf656ef0c785b04a22a5bd04ee6301a Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 31 Mar 2014 14:49:48 -0400 Subject: [PATCH] moved alert to its own requirejs module --- public/src/app.js | 97 +++--------------------------------- public/src/modules/alerts.js | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 89 deletions(-) create mode 100644 public/src/modules/alerts.js diff --git a/public/src/app.js b/public/src/app.js index a2219dfe24..f9a352a1a9 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -166,114 +166,33 @@ var socket, }); }; - // use unique alert_id to have multiple alerts visible at a time, use the same alert_id to fade out the current instance - // type : error, success, info, warning/notify - // title = bolded title text - // message = alert message content - // timeout default = permanent - // location : alert_window (default) or content app.alert = function (params) { - var alert_id = 'alert_button_' + ((params.alert_id) ? params.alert_id : new Date().getTime()); - - var alert = $('#' + alert_id); - var title = params.title || ''; - - function fadeOut() { - alert.fadeOut(500, function () { - $(this).remove(); - }); - } - - function startTimeout(timeout) { - var timeoutId = setTimeout(function () { - fadeOut(); - }, timeout); - - alert.attr('timeoutId', timeoutId); - } - - if (alert.length > 0) { - alert.find('strong').html(title); - alert.find('p').html(params.message); - alert.attr('class', 'alert alert-dismissable alert-' + params.type); - - clearTimeout(alert.attr('timeoutId')); - startTimeout(params.timeout); - - alert.children().fadeOut('100'); - translator.translate(alert.html(), function(translatedHTML) { - alert.children().fadeIn('100'); - alert.html(translatedHTML); - }); - } else { - alert = $('
'); - - alert.append($('')) - .append($('' + title + '')); - - if (params.message) { - alert.append($('

' + params.message + '

')); - } - - if (!params.location) { - params.location = 'alert_window'; - } - - translator.translate(alert.html(), function(translatedHTML) { - alert.html(translatedHTML); - $('#' + params.location).prepend(alert.fadeIn('100')); - - if(typeof params.closefn === 'function') { - alert.find('button').on('click', function() { - params.closefn(); - fadeOut(); - return false; - }); - } - }); - - if (params.timeout) { - startTimeout(params.timeout); - } - - if (typeof params.clickfn === 'function') { - alert.on('click', function (e) { - if(!$(e.target).is('.close')) { - params.clickfn(); - } - fadeOut(); - }); - } - } + require(['alerts'], function(alerts) { + alerts.alert(params); + }); }; app.removeAlert = function(id) { - $('#' + 'alert_button_' + id).remove(); + require(['alerts'], function(alerts) { + alerts.remove(id); + }); }; app.alertSuccess = function (message, timeout) { - if (!timeout) { - timeout = 2000; - } - app.alert({ title: '[[global:alert.success]]', message: message, type: 'success', - timeout: timeout + timeout: timeout ? timeout : 2000 }); }; app.alertError = function (message, timeout) { - if (!timeout) { - timeout = 2000; - } - app.alert({ title: '[[global:alert.error]]', message: message, type: 'danger', - timeout: timeout + timeout: timeout ? timeout : 2000 }); }; diff --git a/public/src/modules/alerts.js b/public/src/modules/alerts.js new file mode 100644 index 0000000000..9efa3298a0 --- /dev/null +++ b/public/src/modules/alerts.js @@ -0,0 +1,94 @@ +'use strict'; +/* globals define, translator, templates */ + +define(function() { + + var module = {}; + + // use unique alert_id to have multiple alerts visible at a time, use the same alert_id to fade out the current instance + // type : error, success, info, warning/notify + // title = bolded title text + // message = alert message content + // timeout default = permanent + // location : alert_window (default) or content + module.alert = function (params) { + var alert_id = 'alert_button_' + ((params.alert_id) ? params.alert_id : new Date().getTime()); + + var alert = $('#' + alert_id); + var title = params.title || ''; + + function fadeOut() { + alert.fadeOut(500, function () { + $(this).remove(); + }); + } + + function startTimeout(timeout) { + var timeoutId = setTimeout(function () { + fadeOut(); + }, timeout); + + alert.attr('timeoutId', timeoutId); + } + + if (alert.length) { + alert.find('strong').html(title); + alert.find('p').html(params.message); + alert.attr('class', 'alert alert-dismissable alert-' + params.type); + + clearTimeout(alert.attr('timeoutId')); + startTimeout(params.timeout); + + alert.children().fadeOut('100'); + translator.translate(alert.html(), function(translatedHTML) { + alert.children().fadeIn('100'); + alert.html(translatedHTML); + }); + } else { + alert = $('
'); + + alert.append($('')) + .append($('' + title + '')); + + if (params.message) { + alert.append($('

' + params.message + '

')); + } + + if (!params.location) { + params.location = 'alert_window'; + } + + translator.translate(alert.html(), function(translatedHTML) { + alert.html(translatedHTML); + $('#' + params.location).prepend(alert.fadeIn('100')); + + if(typeof params.closefn === 'function') { + alert.find('button').on('click', function() { + params.closefn(); + fadeOut(); + return false; + }); + } + }); + + if (params.timeout) { + startTimeout(params.timeout); + } + + if (typeof params.clickfn === 'function') { + alert.on('click', function (e) { + if(!$(e.target).is('.close')) { + params.clickfn(); + } + fadeOut(); + }); + } + } + }; + + module.remove = function(id) { + $('#alert_button_' + id).remove(); + }; + + return module; +}); \ No newline at end of file