##// END OF EJS Templates
Allow timeout and click callback
Jessica B. Hamrick -
Show More
@@ -16,11 +16,8 define([
16 this.style();
16 this.style();
17 }
17 }
18 this.element.hide();
18 this.element.hide();
19 var that = this;
20
21 this.inner = $('<span/>');
19 this.inner = $('<span/>');
22 this.element.append(this.inner);
20 this.element.append(this.inner);
23
24 };
21 };
25
22
26 NotificationWidget.prototype.style = function () {
23 NotificationWidget.prototype.style = function () {
@@ -34,9 +31,8 define([
34 // click_callback : function called if user click on notification
31 // click_callback : function called if user click on notification
35 // could return false to prevent the notification to be dismissed
32 // could return false to prevent the notification to be dismissed
36 NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
33 NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
37 var options = options || {};
34 options = options || {};
38 var callback = click_callback || function() {return true;};
35
39 var that = this;
40 // unbind potential previous callback
36 // unbind potential previous callback
41 this.element.unbind('click');
37 this.element.unbind('click');
42 this.inner.attr('class', options.icon);
38 this.inner.attr('class', options.icon);
@@ -48,50 +44,58 define([
48 this.element.removeClass();
44 this.element.removeClass();
49 this.style();
45 this.style();
50 if (options.class){
46 if (options.class){
51
47 this.element.addClass(options.class);
52 this.element.addClass(options.class)
53 }
48 }
49
50 // clear previous timer
54 if (this.timeout !== null) {
51 if (this.timeout !== null) {
55 clearTimeout(this.timeout);
52 clearTimeout(this.timeout);
56 this.timeout = null;
53 this.timeout = null;
57 }
54 }
58 if (timeout !== undefined && timeout >=0) {
55
56 // set the timer if a timeout is given
57 var that = this;
58 if (timeout !== undefined && timeout >= 0) {
59 this.timeout = setTimeout(function () {
59 this.timeout = setTimeout(function () {
60 that.element.fadeOut(100, function () {that.inner.text('');});
60 that.element.fadeOut(100, function () {that.inner.text('');});
61 that.element.unbind('click');
61 that.timeout = null;
62 that.timeout = null;
62 }, timeout);
63 }, timeout);
63 } else {
64 }
65
66 // bind the click callback if it is given
67 if (click_callback !== undefined) {
64 this.element.click(function() {
68 this.element.click(function() {
65 if( callback() !== false ) {
69 if (click_callback() !== false) {
66 that.element.fadeOut(100, function () {that.inner.text('');});
70 that.element.fadeOut(100, function () {that.inner.text('');});
67 that.element.unbind('click');
71 that.element.unbind('click');
68 }
72 }
69 if (that.timeout !== undefined) {
73 if (that.timeout !== null) {
70 that.timeout = undefined;
71 clearTimeout(that.timeout);
74 clearTimeout(that.timeout);
75 that.timeout = null;
72 }
76 }
73 });
77 });
74 }
78 }
75 };
79 };
76
80
77
78 NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
81 NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
79 var options = options || {};
82 options = options || {};
80 options.class = options.class +' info';
83 options.class = options.class + ' info';
81 var timeout = timeout || 3500;
84 timeout = timeout || 3500;
82 this.set_message(msg, timeout, click_callback, options);
85 this.set_message(msg, timeout, click_callback, options);
83 }
86 };
87
84 NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
88 NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
85 var options = options || {};
89 options = options || {};
86 options.class = options.class +' warning';
90 options.class = options.class + ' warning';
87 this.set_message(msg, timeout, click_callback, options);
91 this.set_message(msg, timeout, click_callback, options);
88 }
92 };
93
89 NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
94 NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
90 var options = options || {};
95 options = options || {};
91 options.class = options.class +' danger';
96 options.class = options.class + ' danger';
92 this.set_message(msg, timeout, click_callback, options);
97 this.set_message(msg, timeout, click_callback, options);
93 }
98 };
94
95
99
96 NotificationWidget.prototype.get_message = function () {
100 NotificationWidget.prototype.get_message = function () {
97 return this.inner.html();
101 return this.inner.html();
General Comments 0
You need to be logged in to leave comments. Login now