forked from X2Engine/X2CRM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX2Flashes.js
311 lines (267 loc) · 10.9 KB
/
X2Flashes.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/***********************************************************************************
* X2Engine Open Source Edition is a customer relationship management program developed by
* X2 Engine, Inc. Copyright (C) 2011-2017 X2 Engine Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact X2Engine, Inc. P.O. Box 610121, Redwood City,
* California 94061, USA. or at email address [email protected].
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* X2 Engine" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by X2 Engine".
**********************************************************************************/
x2.Flashes = (function () {
/**
* Manages x2 gridview mass action actions and ui element behavior
*/
function X2Flashes (argsDict) {
argsDict = typeof argsDict === 'undefined' ? {} : argsDict;
var defaultArgs = {
DEBUG: x2.DEBUG && false,
containerId: null, // used to create container to hold flashes
translations: [],
expandWidgetSrc: '', // image src
collapseWidgetSrc: '', // image src
closeWidgetSrc: '', // image src
successFadeTimeout: 3000, // time before success flashes begin to fade out
};
auxlib.applyArgs (this, defaultArgs, argsDict);
this.container$ = null;
this._successFlashFadeTimeout = null;
this._init ();
}
/*
Public static methods
*/
/*
Private static methods
*/
/*
Public instance methods
*/
/*
Private instance methods
*/
/***********************************************************************
* Flashes setup functions
***********************************************************************/
/**
* Display flashes of a given type
* @param string key the type of flash ('notice' | 'error' | 'success')
* @param array of strings flashes flash messages which will be displayed
*/
X2Flashes.prototype._displayKeyFlashes = function (key, flashes, rawHtml) {
var that = this;
rawHtml = typeof rawHtml === 'undefined' ? false : rawHtml;
that.DEBUG && console.log ('x2.massActions._displayKeyFlashes');
that.DEBUG && console.log ('flashes = ');
that.DEBUG && console.log (flashes);
var flashNum = flashes.length;
var hideList = false;
var $flashContainer = this[key + 'container'];
$flashContainer.show ();
that.DEBUG && console.log ('$flashContainer = ');
that.DEBUG && console.log ($flashContainer);
if (flashNum > 3 || flashes['header']) { // show header and make flash list expandable
// add list header
$flashContainer.append (
$('<p>', {
'class': 'flash-list-header left',
text: flashes['header'] ? flashes['header'] :
that.translations[key + 'FlashList'] + ' ' + flashNum + ' ' +
that.translations[key + 'ItemName']
}),
$('<img>', {
'class': 'flash-list-left-arrow',
'src': that.expandWidgetSrc,
'alt': '<'
}),
$('<img>', {
'class': 'flash-list-down-arrow',
'style': 'display: none;',
'src': that.collapseWidgetSrc,
'alt': 'v'
})
);
// set up flashes list expand and collapse behavior
$flashContainer.find ('.flash-list-left-arrow').
click (function () {
$(this).hide ();
$(this).next ().show ();
$flashContainer.find ('.x2-flashes-list').show ();
});
$flashContainer.find ('.flash-list-down-arrow').
click (function () {
$(this).hide ();
$(this).prev ().show ();
$flashContainer.find ('.x2-flashes-list').hide ();
});
hideList = true;
}
// build flashes list
var $flashList = $('<ul>', {
'class': 'x2-flashes-list' + (hideList ? '' : ' x2-flashes-list-no-style'),
style: (hideList ? 'display: none;' : '')
});
$flashContainer.append ($flashList);
for (var i in flashes) {
if (!i.match (/^\d+$/)) continue;
that.DEBUG && console.log ('x2.massActions._displayKeyFlashes: i = ' + i);
var attrs = {};
if (rawHtml) attrs.html = flashes[i];
else attrs.text = flashes[i];
$flashContainer.find ('.x2-flashes-list').append ($('<li>', attrs));
}
if (key === 'success' && flashes['fade'] !== '0') {
// other types of flash containers have close buttons
//if (that._successFlashFadeTimeout) window.clearTimeout (that._successFlashFadeTimeout);
/*that._successFlashFadeTimeout = */
setTimeout (
function () {
$flashContainer.fadeOut (3000, function () {
$flashContainer.remove ();
});
}, that.successFadeTimeout);
}
}
/**
* Append flash section container div to parent element
* @param string key the type of flash
* @param object parent the jQuery object for the flashes container associated with key
*/
X2Flashes.prototype._appendFlashSectionContainer = function (key, parent, flashes) {
var that = this;
var $flashContainer =
$('<div>', {
'class': 'flash-' + key,
style: 'display: none;'
})
$(parent).append ($flashContainer);
// add close button, not needed for success flash container since it fades out
if (key === 'notice' || key === 'error' || flashes['fade'] === '0') {
$flashContainer.append (
$('<img>', {
//id: key + '-container-close-button',
'class': 'right',
title: that.translations['close'],
'src': that.closeWidgetSrc,
alt: '[x]'
})
);
// set up close button behavior
$flashContainer.find ('img').click (function () {
$flashContainer.fadeOut (function () {
$flashContainer.remove ();
});
});
}
this[key + 'container'] = $flashContainer;
};
/**
* Build the flash container, fill it with given flashes
* @param dictionary flashes keys are the type of flash ('success', 'notice', 'error'), values
* are arrays of messages
*/
X2Flashes.prototype.displayFlashes = function (flashes, rawHtml) {
var that = this;
that.DEBUG && console.log ('x2.massActions._displayFlashes: flashes = ');
that.DEBUG && console.log (flashes);
if (!flashes['success'] && !flashes['notice'] && !flashes['error']) return;
rawHtml = typeof rawHtml === 'undefined' ? false : rawHtml;
this.container$.show ();
// remove previous flashes container
/*if ($('#x2-gridview-flashes-container').length) {
$('#x2-gridview-flashes-container').remove ();
}*/
// fill container with flashes
var types = ['success', 'notice', 'error'];
for (var i in types) {
var type = types[i];
var flashesOfType = flashes[type];
if (flashes[type] && (flashesOfType.length > 0 || auxlib.keys (flashesOfType).length > 0)) {
that._appendFlashSectionContainer (type, this.container$, flashesOfType);
that._displayKeyFlashes (type, flashesOfType, rawHtml);
}
}
$('#content-container').css ('margin-bottom', this.container$.height ());
};
X2Flashes.prototype.clearFlashes = function () {
this.container$.children ().remove ();
};
/**
* Checks if flashes container should be made sticky and if so, makes it sticky
*/
X2Flashes.prototype._checkFlashesSticky = function () {
var that = this;
if (this.container$.position ().top + $(window).scrollTop () >
$('#content-container').position ().top + $('#content-container').height ()) {
this.container$.removeClass ('fixed-flashes-container');
$('#content-container').css ('margin-bottom', '');
$(window).unbind ('scroll._checkFlashesSticky').
bind ('scroll._checkFlashesSticky', function () {
return that._checkFlashesUnsticky (); });
}
};
/**
* Checks if flashes container should be made unsticky and if so, unsticks it
*/
X2Flashes.prototype._checkFlashesUnsticky = function () {
var that = this;
if (this.container$.offset ().top - $(window).scrollTop () >
($(window).height () - 5) - this.container$.height ()) {
this.container$.addClass ('fixed-flashes-container');
$('#content-container').css ('margin-bottom', this.container$.height ());
$(window).unbind ('scroll._checkFlashesUnsticky').
bind ('scroll._checkFlashesUnsticky', function () { that._checkFlashesSticky (); });
} else {
return false;
}
};
/**
* set up mass action ui behavior, this gets run on every grid update
*/
X2Flashes.prototype._init = function () {
var that = this;
// build new flashes container
this.container$ = $('<div>', {
id: this.containerId,
'class': 'flashes-container'
});
$('#content-container').append (this.container$);
$('#content-container').attr (
'style', 'padding-bottom: ' + this.container$.height () + 'px;');
this.container$.width ($('#content-container').width () - 10);
$(window).unbind ('resize.contentContainer').bind ('resize.contentContainer', function () {
that.container$.width ($('#content-container').width () - 10);
});
that.DEBUG && console.log ('this.container$.positoin ().top = ');
that.DEBUG && console.log (this.container$.position ().top);
if (!that._checkFlashesUnsticky ()) {
$(window).unbind ('scroll._X2Flashes', that._checkFlashesUnsticky).
bind ('scroll._X2Flashes', function () { that._checkFlashesUnsticky (); });
}
};
return X2Flashes;
}) ();