forked from marcosesperon/Messi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessi.js
278 lines (194 loc) · 9.02 KB
/
messi.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
/*
* jQuery Messi Plugin 1.3
* https://github.com/marcosesperon/jquery-messi
*
* Copyright 2012, Marcos Esperón
* http://marcosesperon.es
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
// Clase principal
function Messi(data, options) {
var _this = this;
_this.options = jQuery.extend({}, Messi.prototype.options, options || {});
// preparamos el elemento
_this.messi = jQuery(_this.template);
_this.setContent(data);
// ajustamos el título
if(_this.options.title == null) {
jQuery('.messi-titlebox', _this.messi).remove();
} else {
jQuery('.messi-title', _this.messi).append(_this.options.title);
if(_this.options.buttons.length === 0 && !_this.options.autoclose) {
if(_this.options.closeButton) {
var close = jQuery('<span class="messi-closebtn"></span>');
close.bind('click', function() {
_this.hide();
});
jQuery('.messi-titlebox', this.messi).prepend(close);
};
};
if(_this.options.titleClass != null) jQuery('.messi-titlebox', this.messi).addClass(_this.options.titleClass);
};
// ajustamos el ancho
if(_this.options.width != null) jQuery('.messi-box', _this.messi).css('width', _this.options.width);
// preparamos los botones
if(_this.options.buttons.length > 0) {
for (var i = 0; i < _this.options.buttons.length; i++) {
var cls = (_this.options.buttons[i]["class"]) ? _this.options.buttons[i]["class"] : '';
var btn = jQuery('<div class="btnbox"><button class="btn ' + cls + '" href="#">' + _this.options.buttons[i].label + '</button></div>').data('value', _this.options.buttons[i].val);
btn.on('click', 'button', function() {
var value = jQuery.data(this, 'value');
var after = (_this.options.callback != null) ? function() { _this.options.callback(value); } : null;
_this.hide(after);
});
jQuery('.messi-actions', this.messi).append(btn);
};
} else {
jQuery('.messi-footbox', this.messi).remove();
};
// preparamos el botón de cerrar automáticamente
if(_this.options.buttons.length === 0 && _this.options.title == null && !_this.options.autoclose) {
if(_this.options.closeButton) {
var close = jQuery('<span class="messi-closebtn"></span>');
close.bind('click', function() {
_this.hide();
});
jQuery('.messi-content', this.messi).prepend(close);
};
};
// activamos la pantalla modal
_this.modal = (_this.options.modal) ? jQuery('<div class="messi-modal"></div>').css({opacity: _this.options.modalOpacity, width: jQuery(document).width(), height: jQuery(document).height(), 'z-index': _this.options.zIndex + jQuery('.messi').length}).appendTo(document.body) : null;
// mostramos el mensaje
if(_this.options.show) _this.show();
// controlamos el redimensionamiento de la pantalla
jQuery(window).bind('resize', function(){ _this.resize(); });
// configuramos el cierre automático
if(_this.options.autoclose != null) {
setTimeout(function(_this) {
_this.hide();
}, _this.options.autoclose, this);
};
return _this;
};
Messi.prototype = {
options: {
autoclose: null, // autoclose message after 'x' miliseconds, i.e: 5000
buttons: [], // array of buttons, i.e: [{id: 'ok', label: 'OK', val: 'OK'}]
callback: null, // callback function after close message
center: true, // center message on screen
closeButton: true, // show close button in header title (or content if buttons array is empty).
height: 'auto', // content height
title: null, // message title
titleClass: null, // title style: info, warning, success, error
modal: false, // shows message in modal (loads background)
modalOpacity: .2, // modal background opacity
padding: '10px', // content padding
show: true, // show message after load
unload: true, // unload message after hide
viewport: {top: '0px', left: '0px'}, // if not center message, sets X and Y position
width: '500px', // message width
zIndex: 99999 // message z-index
},
template: '<div class="messi"><div class="messi-box"><div class="messi-wrapper"><div class="messi-titlebox"><span class="messi-title"></span></div><div class="messi-content"></div><div class="messi-footbox"><div class="messi-actions"></div></div></div></div></div>',
content: '<div></div>',
visible: false,
setContent: function(data) {
jQuery('.messi-content', this.messi).css({padding: this.options.padding, height: this.options.height}).empty().append(data);
},
viewport: function() {
return {
top: ((jQuery(window).height() - this.messi.height()) / 2) + jQuery(window).scrollTop() + "px",
left: ((jQuery(window).width() - this.messi.width()) / 2) + jQuery(window).scrollLeft() + "px"
};
},
show: function() {
if(this.visible) return;
if(this.options.modal && this.modal != null) this.modal.show();
this.messi.appendTo(document.body);
// obtenemos el centro de la pantalla si la opción de centrar está activada
if(this.options.center) this.options.viewport = this.viewport(jQuery('.messi-box', this.messi));
this.messi.css({top: this.options.viewport.top, left: this.options.viewport.left, 'z-index': this.options.zIndex + jQuery('.messi').length}).show().animate({opacity: 1}, 300);
// cancelamos el scroll
//document.documentElement.style.overflow = "hidden";
this.visible = true;
},
hide: function(after) {
if (!this.visible) return;
var _this = this;
this.messi.animate({opacity: 0}, 300, function() {
if(_this.options.modal && _this.modal != null) _this.modal.remove();
_this.messi.css({display: 'none'}).remove();
// reactivamos el scroll
//document.documentElement.style.overflow = "visible";
_this.visible = false;
if (after) after.call();
if(_this.options.unload) _this.unload();
});
return this;
},
resize: function() {
if(this.options.modal) {
jQuery('.messi-modal').css({width: jQuery(document).width(), height: jQuery(document).height()});
};
if(this.options.center) {
this.options.viewport = this.viewport(jQuery('.messi-box', this.messi));
this.messi.css({top: this.options.viewport.top, left: this.options.viewport.left});
};
},
toggle: function() {
this[this.visible ? 'hide' : 'show']();
return this;
},
unload: function() {
if (this.visible) this.hide();
jQuery(window).unbind('resize', function () { this.resize(); });
this.messi.remove();
},
};
// llamadas especiales
jQuery.extend(Messi, {
alert: function(data, callback, options) {
var buttons = [{id: 'ok', label: 'OK', val: 'OK'}];
options = jQuery.extend({closeButton: false, buttons: buttons, callback:function() {}}, options || {}, {show: true, unload: true, callback: callback});
return new Messi(data, options);
},
ask: function(data, callback, options) {
var buttons = [
{id: 'yes', label: 'Yes', val: 'Y', "class": 'btn-success'},
{id: 'no', label: 'No', val: 'N', "class": 'btn-danger'},
];
options = jQuery.extend({closeButton: false, modal: true, buttons: buttons, callback:function() {}}, options || {}, {show: true, unload: true, callback: callback});
return new Messi(data, options);
},
img: function(src, options) {
var img = new Image();
jQuery(img).load(function() {
var vp = {width: jQuery(window).width() - 50, height: jQuery(window).height() - 50};
var ratio = (this.width > vp.width || this.height > vp.height) ? Math.min(vp.width / this.width, vp.height / this.height) : 1;
jQuery(img).css({width: this.width * ratio, height: this.height * ratio});
options = jQuery.extend(options || {}, {show: true, unload: true, closeButton: true, width: this.width * ratio, height: this.height * ratio, padding: 0});
new Messi(img, options);
}).error(function() {
console.log('Error loading ' + src);
}).attr('src', src);
},
load: function(url, options) {
options = jQuery.extend(options || {}, {show: true, unload: true, params: {}});
var request = {
url: url,
data: options.params,
dataType: 'html',
cache: false,
error: function (request, status, error) {
console.log(request.responseText);
},
success: function(html) {
//html = jQuery(html);
new Messi(html, options);
}
};
jQuery.ajax(request);
}
});