forked from framework7io/framework7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.js
More file actions
156 lines (137 loc) · 6.1 KB
/
notifications.js
File metadata and controls
156 lines (137 loc) · 6.1 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*======================================================
************ Notifications ************
======================================================*/
var _tempNotificationElement;
app.addNotification = function (params) {
if (!params) return;
if (typeof params.media === 'undefined') params.media = app.params.notificationMedia;
if (typeof params.title === 'undefined') params.title = app.params.notificationTitle;
if (typeof params.subtitle === 'undefined') params.subtitle = app.params.notificationSubtitle;
if (typeof params.closeIcon === 'undefined') params.closeIcon = app.params.notificationCloseIcon;
if (typeof params.hold === 'undefined') params.hold = app.params.notificationHold;
if (typeof params.closeOnClick === 'undefined') params.closeOnClick = app.params.notificationCloseOnClick;
if (typeof params.button === 'undefined') params.button = app.params.notificationCloseButtonText && {
text: app.params.notificationCloseButtonText,
close: true
};
if (!_tempNotificationElement) _tempNotificationElement = document.createElement('div');
params.material = app.params.material;
var container = $('.notifications');
if (container.length === 0) {
$('body').append('<div class="notifications list-block' + (params.material ? '' : ' media-list') + '"><ul></ul></div>');
container = $('.notifications');
}
var list = container.children('ul');
var notificationTemplate = app.params.notificationTemplate ||
'{{#if custom}}' +
'<li>{{custom}}</li>' +
'{{else}}' +
'<li class="notification-item notification-hidden">' +
'<div class="item-content">' +
'{{#if material}}' +
'<div class="item-inner">' +
'<div class="item-title">{{js "this.message || this.title || this.subtitle"}}</div>' +
'{{#if ../button}}{{#button}}' +
'<div class="item-after">' +
'<a href="#" class="button {{#if color}}color-{{color}}{{/if}} {{#js_compare "this.close !== false"}}close-notification{{/js_compare}}">{{text}}</a>' +
'</div>' +
'{{/button}}{{/if}}' +
'</div>' +
'{{else}}' +
'{{#if media}}' +
'<div class="item-media">{{media}}</div>' +
'{{/if}}' +
'<div class="item-inner">' +
'<div class="item-title-row">' +
'{{#if title}}' +
'<div class="item-title">{{title}}</div>' +
'{{/if}}' +
'{{#if closeIcon}}' +
'<div class="item-after"><a href="#" class="close-notification"><span></span></a></div>' +
'{{/if}}' +
'</div>' +
'{{#if subtitle}}' +
'<div class="item-subtitle">{{subtitle}}</div>' +
'{{/if}}' +
'{{#if message}}' +
'<div class="item-text">{{message}}</div>' +
'</div>' +
'{{/if}}' +
'{{/if}}' +
'</div>' +
'</li>' +
'{{/if}}';
if (!app._compiledTemplates.notification) {
app._compiledTemplates.notification = t7.compile(notificationTemplate);
}
_tempNotificationElement.innerHTML = app._compiledTemplates.notification(params);
var item = $(_tempNotificationElement).children();
item.on('click', function (e) {
var close = false;
var target = $(e.target);
if (params.material && target.hasClass('button')) {
if (params.button && params.button.onClick) params.button.onClick.call(target[0], e, item[0]);
}
if (target.is('.close-notification') || $(e.target).parents('.close-notification').length > 0) {
close = true;
}
else {
if (params.onClick) params.onClick(e, item[0]);
if (params.closeOnClick) close = true;
}
if (close) app.closeNotification(item[0]);
});
if (params.onClose) {
item.data('f7NotificationOnClose', function () {
params.onClose(item[0]);
});
}
if (params.additionalClass) {
item.addClass(params.additionalClass);
}
if (params.hold) {
setTimeout(function () {
if (item.length > 0) app.closeNotification(item[0]);
}, params.hold);
}
list[params.material ? 'append' : 'prepend'](item[0]);
container.show();
var itemHeight = item.outerHeight(), clientLeft;
if (params.material) {
container.transform('translate3d(0, '+itemHeight+'px, 0)');
container.transition(0);
clientLeft = item[0].clientLeft;
container.transform('translate3d(0, 0, 0)');
container.transition('');
}
else {
item.css('marginTop', -itemHeight + 'px');
item.transition(0);
clientLeft = item[0].clientLeft;
item.transition('');
item.css('marginTop', '0px');
}
container.transform('translate3d(0, 0,0)');
item.removeClass('notification-hidden');
return item[0];
};
app.closeNotification = function (item) {
item = $(item);
if (item.length === 0) return;
if (item.hasClass('notification-item-removing')) return;
var container = $('.notifications');
var itemHeight = item.outerHeight();
item.css('height', itemHeight + 'px').transition(0).addClass('notification-item-removing');
var clientLeft = item[0].clientLeft;
item.css('height', '0px').transition('');
if (item.data('f7NotificationOnClose')) item.data('f7NotificationOnClose')();
if (container.find('.notification-item:not(.notification-item-removing)').length === 0) {
container.transform('');
}
item.addClass('notification-hidden').transitionEnd(function () {
item.remove();
if (container.find('.notification-item').length === 0) {
container.hide();
}
});
};