-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.notification.js
99 lines (97 loc) · 3.18 KB
/
jquery.notification.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Created by gitkv on 02.03.16.
*/
'use strict';
;(function($){
$.notification = {
settings:{
time:400,
delay:10000
},
element:'.notification_block',
elementClose:'.notification_block .close_notice',
count: 0,
elementBuffer: [],
/**
* показ сообщения
* @param type
* @param text
* @returns {*|number}
*/
show:function(type, text){
var that = this;
var elementId = this.append(type, text);
var element = this.elementBuffer[elementId];
this.restructuring(elementId);
element.fadeIn(this.settings.time);
setTimeout(function(){
that.hide(elementId);
}, this.settings.delay);
$(document).on("click", this.elementClose, function(){
var noteId = $(this).parents(that.element).data('id');
that.hide(noteId);
return false;
});
return elementId;
},
/**
* скрытие сообщения
* @param elementId
* @returns {boolean}
*/
hide:function(elementId){
var that = this;
var element = this.elementBuffer[elementId];
if(typeof element != 'undefined') {
element.fadeOut(this.settings.time);
setTimeout(function () {
element.remove();
delete that.elementBuffer[elementId];
that.restructuring(elementId);
}, that.settings.time);
return true;
}
return false;
},
/**
* добавление сообщения
* @param type
* @param text
* @returns {number}
*/
append:function(type, text){
var templete =
'<div id="note_'+this.count+'" data-id="'+this.count+'" class="notification_block '+type+'">' +
'<a class="close_notice" href="#"></a>' +
'<div class="content_notice">'+text+'</div>' +
'</div>';
$('body').append(templete);
this.elementBuffer[this.count] = $(document).find('#note_'+this.count);
this.count++;
return this.count-1;
},
/**
* перерасчет позиций блоков
*/
restructuring: function(){
var elements = this.elementBuffer;
var newPosY = 20;
for(var i in elements) {
if (!elements.hasOwnProperty(i)) continue;
elements[i].css({top:newPosY+'px'});
var elementTop = elements[i].css('top');
var elementHeight = elements[i].outerHeight();
newPosY = parseInt(elementTop)+parseInt(elementHeight)+10;
}
},
/**
* инициализация
* @param options
* @returns {$.notification}
*/
init:function(options){
this.settings = $.extend(this.settings, options);
return this;
}
};
})(jQuery);