forked from framework7io/framework7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagebar.js
More file actions
140 lines (122 loc) · 4.48 KB
/
messagebar.js
File metadata and controls
140 lines (122 loc) · 4.48 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
/*======================================================
************ Messagebar ************
======================================================*/
var Messagebar = function (container, params) {
var defaults = {
textarea: null,
maxHeight: null,
};
params = params || {};
for (var def in defaults) {
if (typeof params[def] === 'undefined' || params[def] === null) {
params[def] = defaults[def];
}
}
// Instance
var m = this;
// Params
m.params = params;
// Container
m.container = $(container);
if (m.container.length === 0) return;
// Textarea
m.textarea = m.params.textarea ? $(m.params.textarea) : m.container.find('textarea');
// Is In Page
m.pageContainer = m.container.parents('.page').eq(0);
m.pageContent = m.pageContainer.find('.page-content');
// Initial Sizes
m.pageContentPadding = parseInt(m.pageContent.css('padding-bottom'));
m.initialBarHeight = m.container[0].offsetHeight;
m.initialAreaHeight = m.textarea[0].offsetHeight;
// Resize textarea
m.sizeTextarea = function () {
// Reset
m.textarea.css({'height': ''});
var height = m.textarea[0].offsetHeight;
var diff = height - m.textarea[0].clientHeight;
var scrollHeight = m.textarea[0].scrollHeight;
// Update
if (scrollHeight + diff > height) {
var newAreaHeight = scrollHeight + diff;
var newBarHeight = m.initialBarHeight + (newAreaHeight - m.initialAreaHeight);
var maxBarHeight = m.params.maxHeight || m.container.parents('.view')[0].offsetHeight - 88;
if (newBarHeight > maxBarHeight) {
newBarHeight = parseInt(maxBarHeight, 10);
newAreaHeight = newBarHeight - m.initialBarHeight + m.initialAreaHeight;
}
m.textarea.css('height', newAreaHeight + 'px');
m.container.css('height', newBarHeight + 'px');
var onBottom = (m.pageContent[0].scrollTop === m.pageContent[0].scrollHeight - m.pageContent[0].offsetHeight);
if (m.pageContent.length > 0) {
m.pageContent.css('padding-bottom', newBarHeight + 'px');
if (m.pageContent.find('.messages-new-first').length === 0 && onBottom) {
m.pageContent.scrollTop(m.pageContent[0].scrollHeight - m.pageContent[0].offsetHeight);
}
}
}
else {
if (m.pageContent.length > 0) {
m.container.css({'height': '', 'bottom': ''});
m.pageContent.css({'padding-bottom': ''});
}
}
};
// Clear
m.clear = function () {
m.textarea.val('').trigger('change');
};
m.value = function (value) {
if (typeof value === 'undefined') return m.textarea.val();
else m.textarea.val(value).trigger('change');
};
// Handle textarea
m.textareaTimeout = undefined;
m.handleTextarea = function (e) {
clearTimeout(m.textareaTimeout);
m.textareaTimeout = setTimeout(function () {
m.sizeTextarea();
}, 0);
};
//Events
function preventSubmit(e) {
e.preventDefault();
}
m.attachEvents = function (destroy) {
var method = destroy ? 'off' : 'on';
m.container[method]('submit', preventSubmit);
m.textarea[method]('change keydown keypress keyup paste cut', m.handleTextarea);
};
m.detachEvents = function () {
m.attachEvents(true);
};
// Init Destroy
m.init = function () {
m.attachEvents();
};
m.destroy = function () {
m.detachEvents();
m = null;
};
// Init
m.init();
m.container[0].f7Messagebar = m;
return m;
};
app.messagebar = function (container, params) {
return new Messagebar(container, params);
};
app.initPageMessagebar = function (pageContainer) {
pageContainer = $(pageContainer);
var messagebar = pageContainer.hasClass('messagebar') ? pageContainer : pageContainer.find('.messagebar');
if (messagebar.length === 0) return;
if (!messagebar.hasClass('messagebar-init')) return;
var mb = app.messagebar(messagebar, messagebar.dataset());
// Destroy on page remove
function pageBeforeRemove() {
mb.destroy();
pageContainer.off('pageBeforeRemove', pageBeforeRemove);
}
if (pageContainer.hasClass('page')) {
pageContainer.on('pageBeforeRemove', pageBeforeRemove);
}
};