landers.msgbox
Version:
landers.msgbox
183 lines (174 loc) • 6.03 kB
JavaScript
! function(window, $) {
window.Landers = window.Landers || {};
function is_mobile() {
return navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i);
}
var language = $('meta[http-equiv=Content-Language]').attr('content');
var is_zh_cn = !language || language == 'zh_CN';
Landers.messageBox = Landers.messageBox = {
mod_complete: true,
_parse_opts: function(opts) {
opts = opts || {};
var txt1 = is_zh_cn ? '确 定' : 'OK',
txt2 = is_zh_cn ? '关 闭' : 'Close',
me = this;
opts.scroll = opts.scroll || false;
opts.padding = opts.padding || 20;
opts.resize = opts.resize || false;
if (typeof opts.yes === 'function') opts.yes = {
text: txt1,
fn: opts.yes
};
if (typeof opts.yes === 'string') opts.yes = {
text: opts.yes,
fn: true
};
if (typeof opts.yes === 'boolean') opts.yes = {
text: txt1,
fn: true
};
if (typeof opts.no === 'function') opts.no = {
text: txt2,
fn: opts.no
};
if (typeof opts.no === 'string') opts.no = {
text: opts.no,
fn: true
};
if (typeof opts.no === 'boolean') opts.no = {
text: txt2,
fn: true
};
opts.ok = opts.yes ? opts.yes.fn : null;
opts.okValue = opts.yes ? opts.yes.text || txt1 : txt1;
opts.cancel = opts.no ? opts.no.fn : null;
opts.cancelValue = opts.no ? opts.no.text || txt2 : txt2;
opts.onshow = opts.onshow || opts.onload;
return opts;
},
show: function(opts) {
opts = this._parse_opts(opts);
if (is_mobile()) {
opts = $.extend({}, opts, {
// skin : 'for-mob ',
width: 260
});
opts.skin += opts.ok && opts.cancel ? 'two_button' : 'one_button';
};
opts.zIndex = 19820;
var d = dialog(opts);
if (opts.lock) d.showModal();
else d.show();
return d;
}
};
function str_replace(str, str1, str2) {
if (!str) return str;
var reg = new RegExp( str1 , 'g' );
return str.replace( reg , str2 );
}
Landers.msgbox = Landers.msgbox || function() {
var opts;
if (arguments.length == 1) {
args = arguments[0];
if (typeof args == 'string') opts = {
type: 10,
content: args
};
if ($.isArray(args)) opts = {
type: args[0],
content: args[1]
};
if ($.isPlainObject(args)) opts = {
type: args.code || (args.success ? 0 : 10),
content: args.message
};
} else if (arguments.length == 2) {
var rspn = arguments[0];
opts = {
type: rspn.success ? 0 : 10,
content: rspn.message,
callback: arguments[1]
};
} else {
opts = Landers.parseArgs(arguments, {
'number': 'type',
'string': 'content',
'boolean': 'lock',
'json': 'pack',
'function': 'callback'
});
if (opts.data) {
opts.type = opts.data[0];
opts.content = opts.data[1];
}
};
if ('string' == typeof opts.type) {
opts.title = opts.type;
} else {
switch (opts.type) {
case 0:
case true:
case 200:
opts.title = is_zh_cn ? '操作成功' : 'Successful operation';
break;
case 1:
opts.title = is_zh_cn ? '信息提示' : 'Information Tips';
break;
case 10:
case 11:
case false:
opts.title = is_zh_cn ? '温馨提示' : 'Information Tips';
break;
case 20:
opts.title = is_zh_cn ? '出错啦' : 'Error';
break;
case 29:
opts.title = is_zh_cn ? '意外错误' : 'Error';
break;
case 39:
opts.title = is_zh_cn ? '意外错误' : 'Illegal error';
break;
case 401:
opts.title = is_zh_cn ? '未登录错误' : 'Not logged';
break;
case 82:
opts.title = is_zh_cn ? '未登录或登录超时' : 'Not logged or timeout';
break;
case 83:
opts.title = is_zh_cn ? '权限受限制' : 'Permission restricted';
break;
case 99:
opts.title = is_zh_cn ? '严重错误' : 'Serious error';
break;
case 100:
opts.title = '调试输出';
break;
}
}
if ($.isPlainObject(opts.content)) {
opts.content = JSON.stringify(opts.content);
}
Landers.messageBox.show({
id: opts.digid,
title: opts.title,
content: str_replace(opts.content, '\n', '<br/>'),
fixed: true,
lock: opts.lock !== false,
width: 500,
onclose: function() {
if (opts.type == 82 && Landers.login && Landers.login.show) {
Landers.login.show();
}
},
onload: function() {
if (Landers.response) {
Landers.response.hide();
}
},
yes: function() {
if (opts.callback) return opts.callback(this);
}
});
};
}(window, jQuery);