gistfile1.txt /// //对话框管理模块 全局的存储模块define(function (require) { var dialogInstances = {};//实例存储对象 var dialogtypes = {};//实例生成对象管理函数 var id = 1;//生成出来的dialog实例的id对象
///gistfile2.txt用于创建dialog对象的类型 /// 用于创建dialog时传入的参数 /// 是否进行克隆操作(true表示创建一个新的实例,false表示公用一个实例) /// if (!dialogInstances[dialogtype]) { dialogInstances[dialogtype] = [];//不存在实例存储对象,创建存储对象 if (setting && setting.content) { var content = $(setting.content); var domid = content.attr("id");//模板是否含有dom id if (domid) { content.removeAttr("id"); } dialogInstances[dialogtype].domStruct = content.prop("outerHTML");//获取模板 if (domid) { content.attr("id", domid); } } return this.createDialog(dialogtype, setting,isclonestruct); } else { if (isclonestruct) { return this.createDialog(dialogtype, setting,isclonestruct); } else { return dialogInstances[dialogtype][0]; } } } function getdialogid() { return this._id; } manager.createDialog = function (dialogtype, setting, isclonestruct) { /// /// new 一个modaldialog实例(内部使用方法 不要在外部调用该方法,请使用newdialog) /// /// 实例类型 /// 参数 ///dialog实例 var types = dialogInstances[dialogtype]; var create = dialogtypes[dialogtype]; if (!create || typeof create !== "function") { throw new Error("不存在对应的对话框或注册的对话框生成的方法错误!"); } if (isclonestruct && setting && types.domStruct) { if (setting.content.attr("id") !== "Content") { setting.content = $(types.domStruct); } } var dialog = new create(setting); dialog._id = getdialogId(); types.push(dialog); dialog.GetDialogId = getdialogid; return dialog; } manager.RegistDialogType = function (type, initfunc) { ////// 注册modaldialog类型 /// /// modaldialog的类型 /// 用于创建modaldialog的方法 if (typeof initfunc == "function") { if (dialogtypes[type]) { throw new Error("已经存在" + type + "类型的modal"); } dialogtypes[type] = initfunc; } } manager.GetDialogTypeConstructor = function (type) { return dialogtypes[type]; } manager.GetDialogInstance = function (type, id) { ////// 获取dialog实例没有id时默认返回第一个 有id时返回指定id对应的实例 没有实例时返回 null /// /// 实例类型 /// 实例的唯一id ///var dlg = dialogInstances[type]; //if (id != undefined && typeof id == "number") { // id = parseInt(id); // dlg = dlg[id]; //} id = parseInt(id) || 0; if (dlg) { return dlg[id]; } else { return null; } } manager.GetAllDialogInstance = function (type) { /// /// 获取所有的dialog实例 /// /// dialog的类型 ///所有实例组成的数组或者空 return dialogInstances[type] || null; } return manager; });
define(function () {
/**
* 这个是对所有的弹出方式的模态对话框(新增、修改、查看)的一个抽象
* 主要抽象了一下的几个功能点:
* 1.显示
* 2.隐藏
* 3.销毁
* 4.控件的生成(绑定)
* 5.form控件的生成(semantic中的form控件)
* 6.value的生成
* 7.控件的赋值
*
* 具体的使用方式如下:
* function example(setting){
* }
*
* 依据需要的功能编写其它的功能
*
* example.prototype.extend({},basefuncs,example.prototype)
*
*
* 当example中有同名的Show等方法时,使用example中的方法,
* 当没有时使用base提供的方法,可以使用base提供的extend的接口进行功能的拓展和个性化
*/
var basefuncs = {};
/*
* 模态对话框的显示
* 当含有shwoExtend方法时,调用showExtend方法进行功能拓展或个性化
*/
basefuncs.Show = function (data) {
if (data && typeof data === "object") {
this.InitData(data);
}
if (typeof this.showExtend === "function") {
this.showExtend(data);
}
this.modalDialog.Show();
};
/**
* 隐藏控件
*/
basefuncs.Hide = function () {
if (typeof this.hideExtend === "function") {
this.hideExtend();
}
if (this.isInitDialog) {
this.modalDialog.Hide();
}
};
/**
* 销毁控件
*/
basefuncs.Destroy = function () {
if (this.isInitDialog) {
this.modalDialog.Destroy();
}
if (typeof this.destroyExtend === "function") {
this.destroyExtend();
}
};
/**
* 控件的数据初始化
*/
basefuncs.InitData = function (data) {
if (!this.isInitDialog) {
this.initModalDialog();
}
this.data = data;
if (typeof this.initDataExtend === "function") {
this.initDataExtend(data);
}
};
/**
* 控件的结构的初始化
*/
basefuncs.initStruct = function () {
this.content = $("");
this.content.append($(this.setting.content));
this.control = {};
this.control.form = this.content.find(".ui.form");
this.initForm();
this.initCheckBox();
if (typeof this.initStructExtend === "function") {
this.initStructExtend();
}
return this.content;
};
basefuncs.initCheckBox = function () {
this.control.allcheckbox = this.content.find("input[type='checkbox'],input[type='radio']");
this.control.allcheckbox.each(function () {
var input = $(this);
input.parent().find("label").click(function () {
input.click();
});
})
}
//var a = {
// setting: {},
// async: false,
// promise: {},//请求
// dropdownkey: "",//控件id
// fields: {
// values: "",
// value: "Id",
// name: "Name"
// }
//}
basefuncs.initDropdown = function (parm) {
var _self = this;
var setting = $.extend(true, {}, parm.setting);//生成dropdown的参数
setting.namespace = setting.namespace || "dropdown";
if (parm.async) {
setting.onShow = function () {
if ($(this).data("promise").state() === "pending") {
return false;
}
if (parm.setting && parm.setting.onShow && typeof parm.setting.onShow === "function") {
return parm.setting.onShow.apply(this, arguments);
}
}
}
this.control[parm.dropdownkey].dropdown(setting);
if (parm.promise.state() === "pending") {
this.control[parm.dropdownkey].dropdown("set.loading");//load数据
}
parm.promise.success(function (context, data) {
//数据填充
var resp = {};
resp[parm.fields.values] = data;
_self.control[parm.dropdownkey].dropdown("setup.menu", resp, parm.fields);
}).fail(function (context, err) {
}).always(function () {
_self.control[parm.dropdownkey].dropdown("remove.loading");
});
}
/**
* form控件的生成
*/
basefuncs.initForm = function () {
this.control.form.form(this.initFormParm());
};
/**
* form控件的参数的初始化
*/
basefuncs.initFormParm = function () {
var parm = {};
parm.fields = this.initFormField();
var rules = this.initFormRule();
if (rules) {
parm.rules = rules;
}
if (typeof this.initFormParmExtend === "function") {
this.initFormParmExtend(parm);
}
return parm;
};
/**
* 生成form控件的初始化参数的fileds
* 生成方式通过form节点中的所有的含有name属性的dom节点进行生成
* 用name作为name 用节点的data-rule作为验证规则,用label作为错误信息提示符
*/
basefuncs.initFormField = function () {
//默认的
if (this.fields) {
return this.fields;
}
var fields = this.fields = {};
this.control.form.find("div.field").each(function () {
var field = $(this).find("[name]");
var name = field.prop("name");
if (fields[name])
return;
fields[name] = {
identifier: name,
rules: []
};
var rule = field.data("rule");
if (!rule || /^\s{0,}$/.test(rule)) {
return;
}
rule = rule.split(',');
label = $(this).find("label").html();
for (var i = 0; i < rule.length; i++) {
fields[name].rules.push({
type: rule[i],
prompt: label + "验证失败"
})
}
});
return fields;
};
/**
* 提供的form验证的个性化验证规则
* 默认使用的全是系统的验证规则,当用户自定义了规则时,需要重写该方法
*/
basefuncs.initFormRule = function () {
//example:
//var rules = {};
//rules.valid1 = function (value, ancillary) {
// //当前的this指向field
//};
};
/**
* 获取所有的form的fields的名称
*/
basefuncs.getFieldName = function () {
var fs = this.initFormField();
var names = [];
for (var i in fs) {
names.push(i);
}
return names;
};
/**
* 进行数据的验证,判断是否通过
*/
basefuncs.valid = function () {
return this.control.form.form("is.valid");
};
/**
* 数据验证结果 result表示成功(true)与否(false)
* value为fields中取出来的值,key为field的name
*/
basefuncs.validValue = function () {
var result = {
result: false
};
if (!this.valid())
return result;
result.result = true;
result.value = this.control.form.form("get.values");
if (typeof this.validValueExtend === "function") {
this.validValueExtend(result);
}
return result;
};
/**
* 设置form的值
* parms为string(多个需要赋值时,可以使用“,”进行拼接)或字符数组
* value为进行赋值的数据来源对象
*/
basefuncs.setValue = function (parms, value) {
if (arguments.length === 2) {
if (typeof parms === "string" && !/^\s{0,}$/.test(parms)) {
parms = parms.split(",");
}
if (parms instanceof Array) {
var temp = value;
value = {};
for (var i = 0; i < parms.length; i++) {
value[parms[i]] = temp[parms[i]] || "";
}
}
} else {
value = parms;
}
if (typeof this.setValueExtend === "function") {
this.setValueExtend(value);
}
this.control.form.form("set.values", value);
}
/**
* 单独的设置某一field的值
*/
basefuncs.setItemValue = function (field, value) {
this.control.form.form("set.value", field, value);
}
/**
* 清除数据
*/
basefuncs.clearValue = function () {
this.control.form.form("clear");
}
return basefuncs;
})
