/* 作者: tianzh
* 创建时间: 2012/7/19 16:23:54
*
*/
(function ($) {
//全局系统对象
window['lg'] = {};
lg.cookies = (function () {
var fn = function () {
};
fn.prototype.get = function (name) {
var cookievalue = "";
var search = name + "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexof(search);
if (offset != -1) {
offset += search.length;
end = document.cookie.indexof(";", offset);
if (end == -1) end = document.cookie.length;
cookievalue = decodeuricomponent(document.cookie.substring(offset, end))
}
}
return cookievalue;
};
fn.prototype.set = function (cookiename, cookievalue, dayvalue) {
var expire = "";
var day_value = 1;
if (dayvalue != null) {
day_value = dayvalue;
}
expire = new date((new date()).gettime() + day_value * 86400000);
expire = "; expires=" + expire.togmtstring();
document.cookie = cookiename + "=" + encodeuricomponent(cookievalue) + ";path=/" + expire;
}
fn.prototype.remvoe = function (cookiename) {
var expire = "";
expire = new date((new date()).gettime() - 1);
expire = "; expires=" + expire.togmtstring();
document.cookie = cookiename + "=" + escape("") + ";path=/" + expire;
/*path=/*/
};
return new fn();
})();
//右下角的提示框
lg.tip = function (message) {
if (lg.wintip) {
lg.wintip.set('content', message);
lg.wintip.show();
}
else {
lg.wintip = $.ligerdialog.tip({ content: message });
}
settimeout(function () {
lg.wintip.hide()
}, 4000);
};
//预加载图片
lg.prevloadimage = function (rootpath, paths) {
for (var i in paths) {
$('').attr('src', rootpath + paths[i]);
}
};
//显示loading
lg.showloading = function (message) {
message = message || "正在加载中...";
$('body').append("
" + message + "
");
$.ligerui.win.mask();
};
//隐藏loading
lg.hideloading = function (message) {
$('body > div.jloading').remove();
$.ligerui.win.unmask({ id: new date().gettime() });
}
//显示成功提示窗口
lg.showsuccess = function (message, callback) {
if (typeof (message) == "function" || arguments.length == 0) {
callback = message;
message = "操作成功!";
}
$.ligerdialog.success(message, '提示信息', callback);
};
//显示失败提示窗口
lg.showerror = function (message, callback) {
if (typeof (message) == "function" || arguments.length == 0) {
callback = message;
message = "操作失败!";
}
$.ligerdialog.error(message, '提示信息', callback);
};
//预加载dialog的图片
lg.prevdialogimage = function (rootpath) {
rootpath = rootpath || "";
lg.prevloadimage(rootpath + '../content/ligerui/skins/aqua/images/win/', ['dialog-icons.gif']);
lg.prevloadimage(rootpath + '../content/ligerui/skins/gray/images/win/', ['dialogicon.gif']);
};
//提交服务器请求
//返回json格式
//1,提交给类 options.type 方法 options.method 处理
//2,并返回 ajaxresult(这也是一个类)类型的的序列化好的字符串
lg.ajax = function (options) {
var p = options || {};
// var ashxurl = options.ashxurl || "/admin/user/";
// var url = p.url || ashxurl + $.param({ method: p.method });
$.ajax({
cache: false,
async: true,
url: p.url,
data: p.data,
datatype: 'json',
type: 'post',
beforesend: function () {
lg.loading = true;
if (p.beforesend)
p.beforesend();
else
lg.showloading(p.loading);
},
complete: function () {
lg.loading = false;
if (p.complete)
p.complete();
else
lg.hideloading();
},
success: function (result) {
if (!result) return;
if (!result.iserror) {
if (p.success)
p.success(result.data, result.message);
}
else {
if (p.error)
p.error(result.message);
}
},
error: function (result, b) {
lg.tip('发现系统错误
错误码:' + result.status);
}
});
};
//获取当前页面的menuno
//优先级1:如果页面存在menuno的表单元素,那么加载它的值
//优先级2:加载querystring,名字为menuno的值
lg.getpagemenuno = function () {
var menuno = $("#menuno").val();
if (!menuno) {
menuno = getquerystringbyname("menuno");
}
return menuno;
};
//创建按钮
lg.createbutton = function (options) {
var p = $.extend({
appendto: $('body')
}, options || {});
var btn = $('');
if (p.icon) {
btn.removeclass("buttonnoicon");
btn.append(' ');
}
//绿色皮肤
if (p.green) {
btn.removeclass("button2");
}
if (p.width) {
btn.width(p.width);
}
if (p.click) {
btn.click(p.click);
}
if (p.text) {
$("span", btn).html(p.text);
}
if (typeof (p.appendto) == "string") p.appendto = $(p.appendto);
btn.appendto(p.appendto);
};
//创建过滤规则(查询表单)
lg.bulidfiltergroup = function (form) {
if (!form) return null;
var group = { op: "and", rules: [] };
$(":input", form).not(":submit, :reset, :image,:button, [disabled]")
.each(function () {
if (!this.name) return;
if (!$(this).hasclass("field")) return;
if ($(this).val() == null || $(this).val() == "") return;
var ltype = $(this).attr("ltype");
var optionsjson = $(this).attr("ligerui"), options;
if (optionsjson) {
options = json2.parse(optionsjson);
}
var op = $(this).attr("op") || "like";
//get the value type(number or date)
var type = $(this).attr("vt") || "string";
var value = $(this).val();
var name = this.name;
//如果是下拉框,那么读取下拉框关联的隐藏控件的值(id值,常用与外表关联)
if (ltype == "select" && options && options.valuefieldid) {
value = $("#" + options.valuefieldid).val();
name = options.valuefieldid;
}
group.rules.push({
op: op,
field: name,
value: value,
type: type
});
});
return group;
};
//附加表单搜索按钮:搜索、高级搜索
lg.appendsearchbuttons = function (form, grid) {
if (!form) return;
form = $(form);
//搜索按钮 附加到第一个li 高级搜索按钮附加到 第二个li
var container = $('').appendto(form);
lg.addsearchbuttons(form, grid, container.find("li:eq(0)"), container.find("li:eq(1)"));
};
//附加表单搜索按钮:搜索、高级搜索
lg.appendsearchbuttonslqy = function (form, grid) {
if (!form) return;
form = $(form);
//搜索按钮 附加到第一个li
var container = $('').appendto(form);
lg.addsearchbuttons(form, grid, container.find("li:eq(0)"), false);
};
//创建表单搜索按钮:搜索、高级搜索
lg.addsearchbuttons = function (form, grid, btn1container, btn2container) {
if (!form) return;
if (btn1container) {
lg.createbutton({
appendto: btn1container,
text: '搜索',
click: function () {
var rule = lg.bulidfiltergroup(form);
if (rule.rules.length) {
grid.set('parms', { where: json2.stringify(rule) });
} else {
grid.set('parms', {});
}
grid.loaddata();
}
});
}
if (btn2container) {
lg.createbutton({
appendto: btn2container,
width: 80,
text: '高级搜索',
click: function () {
grid.showfilter();
}
});
}
};
//快速设置表单底部默认的按钮:保存、取消
lg.setformdefaultbtn = function (canclecallback, savedcallback) {
//表单底部按钮
var buttons = [];
if (canclecallback) {
buttons.push({ text: '取消', onclick: canclecallback });
}
if (savedcallback) {
buttons.push({ text: '保存', onclick: savedcallback });
}
lg.addformbuttons(buttons);
};
//增加表单底部按钮,比如:保存、取消
lg.addformbuttons = function (buttons) {
if (!buttons) return;
var formbar = $("body > div.form-bar");
if (formbar.length == 0)
formbar = $('').appendto('body');
if (!(buttons instanceof array)) {
buttons = [buttons];
}
$(buttons).each(function (i, o) {
var btn = $(' ');
$("div.l-dialog-btn-inner:first", btn).html(o.text || "button");
if (o.onclick) {
btn.bind('click', function () {
o.onclick(o);
});
}
if (o.width) {
btn.width(o.width);
}
$("> div:first", formbar).append(btn);
});
};
//填充表单数据
lg.loadform = function (mainform, options, callback) {
options = options || {};
if (!mainform)
mainform = $("form:first");
var p = $.extend({
beforesend: function () {
lg.showloading('正在加载表单数据中...');
},
complete: function () {
lg.hideloading();
},
success: function (data) {
var preid = options.preid || "";
//根据返回的属性名,找到相应id的表单元素,并赋值
for (var p in data) {
var ele = $("[name=" + (preid + p) + "]", mainform);
//针对复选框和单选框 处理
if (ele.is(":checkbox,:radio")) {
ele[0].checked = data[p] ? true : false;
}
else {
ele.val(data[p]);
}
}
//下面是更新表单的样式
var managers = $.ligerui.find($.ligerui.controls.input);
for (var i = 0, l = managers.length; i < l; i++) {
//改变了表单的值,需要调用这个方法来更新ligerui样式
var o = managers[i];
o.updatestyle();
if (managers[i] instanceof $.ligerui.controls.textbox)
o.checkvalue();
}
if (callback)
callback(data);
},
error: function (message) {
lg.showerror('数据加载失败!
错误信息:' + message);
}
}, options);
lg.ajax(p);
};
//带验证、带loading的提交
lg.submitform = function (mainform, success, error) {
if (!mainform)
mainform = $("form:first");
if (mainform.valid()) {
mainform.ajaxsubmit({
datatype: 'json',
success: success,
beforesubmit: function (formdata, jqform, options) {
//针对复选框和单选框 处理
$(":checkbox,:radio", jqform).each(function () {
if (!existinformdata(formdata, this.name)) {
formdata.push({ name: this.name, type: this.type, value: this.checked });
}
});
for (var i = 0, l = formdata.length; i < l; i++) {
var o = formdata[i];
if (o.type == "checkbox" || o.type == "radio") {
o.value = $("[name=" + o.name + "]", jqform)[0].checked ? "true" : "false";
}
}
},
beforesend: function (a, b, c) {
lg.showloading('正在保存数据中...');
},
complete: function () {
lg.hideloading();
},
error: function (result) {
lg.tip('发现系统错误
错误码:' + result.status);
}
});
}
else {
lg.showinvalid();
}
function existinformdata(formdata, name) {
for (var i = 0, l = formdata.length; i < l; i++) {
var o = formdata[i];
if (o.name == name) return true;
}
return false;
}
};
//提示 验证错误信息
lg.showinvalid = function (validator) {
validator = validator || lg.validator;
if (!validator) return;
var message = '存在' + validator.errorlist.length + '个字段验证不通过,请检查!
';
//top.lg.tip(message);
$.ligerdialog.error(message);
};
//表单验证
lg.validate = function (form, options) {
if (typeof (form) == "string")
form = $(form);
else if (typeof (form) == "object" && form.nodetype == 1)
form = $(form);
options = $.extend({
errorplacement: function (lable, element) {
if (!element.attr("id"))
element.attr("id", new date().gettime());
if (element.hasclass("l-textarea")) {
element.addclass("l-textarea-invalid");
}
else if (element.hasclass("l-text-field")) {
element.parent().addclass("l-text-invalid");
}
$(element).removeattr("title").ligerhidetip();
$(element).attr("title", lable.html()).ligertip({
distancex: 5,
distancey: -3,
auto: true
});
},
success: function (lable) {
if (!lable.attr("for")) return;
var element = $("#" + lable.attr("for"));
if (element.hasclass("l-textarea")) {
element.removeclass("l-textarea-invalid");
}
else if (element.hasclass("l-text-field")) {
element.parent().removeclass("l-text-invalid");
}
$(element).removeattr("title").ligerhidetip();
}
}, options || {});
lg.validator = form.validate(options);
return lg.validator;
};
//三个参数,第三个为默认的菜单元素(一般为不和服务器发生交互的菜单按钮)
lg.loadtoolbar = function (grid, toolbarbtnitemclick, toolbardefaultoptions) {
var menuno = lg.getpagemenuno();
lg.ajax({
loading: '正在加载工具条中...',
url: rootpath + 'corerights_roles_rights/getmybutton',
data: { menuno: menuno },
success: function (data) {
if (!grid.toolbarmanager) return;
if (!data || !data.length) return;
var items = [];
//添加服务端的按钮
for (var i = 0, l = data.length; i < l; i++) {
var o = data[i];
items[items.length] = {
click: toolbarbtnitemclick,
text: o.btnname,
img: o.btnicon,
id: o.btnno
};
items[items.length] = { line: true };
}
if (toolbardefaultoptions) {
for (var i = 0, l = toolbardefaultoptions.length; i < l; i++) {
items[items.length] = {
click: toolbarbtnitemclick,
text: toolbardefaultoptions[i].text,
img: toolbardefaultoptions[i].img,
id: toolbardefaultoptions[i].id
};
}
}
//如果客户端存在按钮则添加客户端的按钮(一般代表没有和服务器发生交互的事件)
grid.toolbarmanager.set('items', items);
}
});
};
//关闭tab项,如果tabid不指定,那么关闭当前显示的
lg.closecurrenttab = function (tabid) {
if (!tabid) {
tabid = $("#framecenter > .l-tab-content > .l-tab-content-item:visible").attr("tabid");
}
if (tab) {
tab.removetabitem(tabid);
}
};
//关闭tab项并且刷新父窗口
lg.closeandreloadparent = function (tabid, parentmenuno) {
lg.closecurrenttab(tabid);
var menuitem = $("#mainmenu ul.menulist li[menuno=" + parentmenuno + "]");
var parenttabid = menuitem.attr("tabid");
var iframe = window.frames[parenttabid];
if (tab) {
tab.selecttabitem(parenttabid);
}
if (iframe && iframe.f_reload) {
iframe.f_reload();
}
else if (tab) {
tab.reload(parenttabid);
}
};
//覆盖页面grid的loading效果
lg.overridegridloading = function () {
$.extend($.ligerdefaults.grid, {
onloading: function () {
lg.showloading('正在加载表格数据中...');
},
onloaded: function () {
lg.hideloading();
}
});
};
//根据字段权限调整 页面配置
lg.adujestconfig = function (config, forbidfields) {
if (config.form && config.form.fields) {
for (var i = config.form.fields.length - 1; i >= 0; i--) {
var field = config.form.fields[i];
if ($.inarray(field.name, forbidfields) != -1)
config.form.fields.splice(i, 1);
}
}
if (config.grid && config.grid.columns) {
for (var i = config.grid.columns.length - 1; i >= 0; i--) {
var column = config.grid.columns[i];
if ($.inarray(column.name, forbidfields) != -1)
config.grid.columns.splice(i, 1);
}
}
if (config.search && config.search.fields) {
for (var i = config.search.fields.length - 1; i >= 0; i--) {
var field = config.search.fields[i];
if ($.inarray(field.name, forbidfields) != -1)
config.search.fields.splice(i, 1);
}
}
};
//查找是否存在某一个按钮
lg.findtoolbaritem = function (grid, itemid) {
if (!grid.toolbarmanager) return null;
if (!grid.toolbarmanager.options.items) return null;
var items = grid.toolbarmanager.options.items;
for (var i = 0, l = items.length; i < l; i++) {
if (items[i].id == itemid) return items[i];
}
return null;
}
//设置grid的双击事件(带权限控制)
lg.setgriddoubleclick = function (grid, btnid, btnitemclick) {
btnitemclick = btnitemclick || toolbarbtnitemclick;
if (!btnitemclick) return;
grid.bind('dblclickrow', function (rowdata) {
var item = lg.findtoolbaritem(grid, btnid);
if (!item) return;
grid.select(rowdata);
btnitemclick(item);
});
}
})(jquery);