提交 9176ee01 编写于 作者: C Catouse

* improve messager UI:

 * support custom actions;
 * support custom CSS class;
上级 9196e8ba
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
* ======================================================================== */ * ======================================================================== */
(function($, window) { (function($, window, undefined) {
'use strict'; 'use strict';
var id = 0; var id = 0;
var template = '<div class="messager messager-{type} {placement}" id="messager{id}" style="display:none"><div class="messager-content"></div><div class="messager-actions"><button type="button" class="close action">&times;</button></div></div>'; var template = '<div class="messager messager-{type} {placement}" id="messager{id}" style="display:none"><div class="messager-actions"></div><div class="messager-content"></div></div>';
var defaultOptions = { var defaultOptions = {
type: 'default', type: 'default',
placement: 'top', placement: 'top',
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
// clear: false, // clear: false,
icon: null, icon: null,
close: true, close: true,
// actions: [{icon, name, action, title}],
// contentClass: null,
// cssClass: null,
// onAction: function,
fade: true, fade: true,
scale: true scale: true
}; };
...@@ -31,16 +35,63 @@ ...@@ -31,16 +35,63 @@
that.message = (options.icon ? '<i class="icon-' + options.icon + ' icon"></i> ' : '') + message; that.message = (options.icon ? '<i class="icon-' + options.icon + ' icon"></i> ' : '') + message;
that.$ = $(template.format(options)).toggleClass('fade', options.fade).toggleClass('scale', options.scale).attr('id', 'messager-' + that.id); that.$ = $(template.format(options)).toggleClass('fade', options.fade).toggleClass('scale', options.scale).attr('id', 'messager-' + that.id);
if(!options.close) {
that.$.find('.close').remove(); if(options.cssClass) that.$.addClass(options.cssClass);
} else {
that.$.on('click', '.close', function() { var hasCloseAction = false;
that.hide(); var $actions = that.$.find('.messager-actions');
var appendAction = function(action) {
var $btn = $('<button type="button" class="action action-' + action.name + '"/>');
if(action.name === 'close') $btn.addClass('close');
if(action.html !== undefined) {
$btn.html(action.html);
}
if(action.icon !== undefined) {
$btn.append('<i class="action-icon icon-' + action.icon + '"/>');
}
if(action.text !== undefined) {
$btn.append('<span class="action-text">' + action.text + '</span>');
}
if(action.tooltip !== undefined) {
$btn.attr('title', action.tooltip).tooltip();
}
$btn.data('action', action);
$actions.append($btn);
};
if(options.actions) {
$.each(options.actions, function(idx, action) {
if(action.name === undefined) action.name = idx;
if(action.name == 'close') hasCloseAction = true;
appendAction(action);
}); });
} }
if(!hasCloseAction && options.close) {
appendAction({name: 'close', html: '&times;'});
}
that.$.find('.messager-content').html(that.message); that.$.on('click', '.action', function(e) {
var action = $(this).data('action'), result;
if(options.onAction) {
result = options.onAction.call(this, action.name, action, that);
if(result === false) return;
}
if($.isFunction(action.action)) {
result = action.action.call(this, that);
if(result === false) return;
}
that.hide();
e.stopPropagation();
});
that.$.on('click', function(e) {
if(options.onAction) {
result = options.onAction.call(this, 'content', null, that);
if(result === true) that.hide();
}
});
var $content = that.$.find('.messager-content').html(that.message);
if(options.contentClass) $content.addClass(options.cssClass);
that.$.data('zui.messager', that); that.$.data('zui.messager', that);
}; };
...@@ -70,11 +121,11 @@ ...@@ -70,11 +121,11 @@
that.$.appendTo(options.parent).show(); that.$.appendTo(options.parent).show();
if(options.placement === 'top' || options.placement === 'bottom' || options.placement === 'center') { if(options.placement === 'top' || options.placement === 'bottom' || options.placement === 'center') {
that.$.css('left', ($(window).width() - that.$.width() - 50) / 2); that.$.css('left', ($(window).width() - that.$.width()) / 2);
} }
if(options.placement === 'left' || options.placement === 'right' || options.placement === 'center') { if(options.placement === 'left' || options.placement === 'right' || options.placement === 'center') {
that.$.css('top', ($(window).height() - that.$.height() - 50) / 2); that.$.css('top', ($(window).height() - that.$.height()) / 2);
} }
that.$.addClass('in'); that.$.addClass('in');
...@@ -172,5 +223,5 @@ ...@@ -172,5 +223,5 @@
} }
} }
}); });
}(jQuery, window)); }(jQuery, window, undefined));
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
background-color: @color-dark; background-color: @color-dark;
background-color: rgba(0, 0, 0, 0.8); background-color: rgba(0, 0, 0, 0.8);
border-radius: @messager-radius; border-radius: @messager-radius;
padding-right: 50px;
max-width: 80%; max-width: 80%;
z-index: 99999; z-index: 99999;
&:extend(.clearfix all);
&.top { &.top {
top: 20px; top: 20px;
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
.messager-content { .messager-content {
padding: 10px 20px; padding: 10px 20px;
float: left;
> [class^='icon-'] { > [class^='icon-'] {
display: inline-block; display: inline-block;
...@@ -57,18 +58,21 @@ ...@@ -57,18 +58,21 @@
} }
.messager-actions { .messager-actions {
position: absolute; float: right;
right: 15px;
top: 8px;
> .action { > .action {
color: #fafafa; color: #fafafa;
color: rgba(255, 255, 255, 0.6); color: rgba(255,255,255,.6);
background: none; background: none;
border: none; border: none;
text-shadow: none; text-shadow: none;
min-width: 40px;
padding: 0 12px;
height: 40px;
text-align: center;
&.close { &.close {
padding-bottom: 5px;
opacity: 1; opacity: 1;
font-size: 20px; font-size: 20px;
font-weight: normal; font-weight: normal;
...@@ -76,8 +80,11 @@ ...@@ -76,8 +80,11 @@
&:hover { &:hover {
color: #fff; color: #fff;
background: rgba(0,0,0,.1);
} }
} }
.action-icon + .action-text {margin-left: 8px;}
} }
.messager-primary { .messager-primary {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册