").appendTo(tr);
rowInfo.txtWidth = $("").appendTo(td);
rowInfo.txtWidth.val(c.Width || c.width);
if (c.alwaysshow)
rowInfo.txtWidth.prop("disabled", true);
if (c.name !== "Selected")
bindDrag(tr);
}
}
var draggingrow = null;
function bindDrag(tr) {//拖动调整顺序
tr.attr('draggable', true);
tr.bind('dragstart', function (e) {
draggingrow = $(this);
});
tr.find('input').attr('draggable', true).bind('dragstart', function (e) {
e.originalEvent.preventDefault()
});
tr.bind('dragend', function (e) {
draggingrow = null;
});
tr.bind('dragover', function (e) {
e.originalEvent.preventDefault()
});
tr.bind('drop', function (e) {
var t = $(this);
if (t.is(draggingrow))
return;
var t = $(this);
var after = e.originalEvent.clientY > t.offset().top + t.height() / 2;
if (after) {
t.after(draggingrow);
}
else {
t.before(draggingrow);
}
});
}
function onOKClick() {
var colinfos = [];
var trs = tbdata.find("tr");
for (var i = 0; i < trs.length; i++) {
var tr = $(trs[i]);
var rowInfo = tr.data("rowInfo");
var col = rowInfo.ColumnData;
if (rowInfo) {
var sort = 0;
if (col.sortDesc != undefined)
sort = col.sortDesc ? 2 : 1;
var colinfo = {
'Visible': rowInfo.chkSelected.prop("checked"),
'ColumnKey': col.name,
'Caption': $.trim(rowInfo.txtCaption.val()),
'Width': $.trim(rowInfo.txtWidth.val()),
'Sort': sort
}
if (isNaN(colinfo.Width) || eval(colinfo.Width) <= 0)
colinfo.Width = col.width;
colinfos.push(colinfo);
}
}
if (colinfos.length == 0) {
showAlert(GetTextByKey("P_GRID_PLEASESELECTATLEASTONECOLUMN", "Please select at least one column."), GetTextByKey("P_GRID_XXXXXX", 'Layout'));
return;
}
this.onOK && this.onOK(colinfos);
}
})();
var $layoutsavedialog = function (grid, layoutid, type) {
this.title = GetTextByKey("P_GRID_XXXXXX", 'Save Layout');
this.type = type;
this.dialogId = 'dialog_savelayout';
$("#" + this.dialogId).remove();
this.grid = grid;
this.layoutid = layoutid;
this.dialog = null;
this.onOK = null;
this.onDialogClosed = null;
};
(function () {
var __proto = $layoutsavedialog.prototype;
var txtLayoutName;
var chkMyDefault;
var chkPublic;
var chkCompanyDefault;
var _this = null;
__proto.showSaveLayoutDialog = function (topRatio, force) {
_this = this;
var dialog;
if (force) {
$('#' + this.dialogId).remove();
dialog = createSaveLayoutDialog.call(this);
} else {
dialog = $('#' + this.dialogId);
if (!dialog.length) {
dialog = createSaveLayoutDialog.call(this);
}
}
this.dialog = dialog;
dialog.attr('init', '1').showDialog().css({
'top': (document.documentElement.clientHeight - $('#dialog_machines').height()) / (topRatio || 3),
'left': (document.documentElement.clientWidth - $('#dialog_machines').width()) / 2
});
getAllGridLayoutsForSave(this.layoutid);
};
function createSaveLayoutDialog() {
var dialog = $('
').attr('id', this.dialogId);
var title = $('').appendTo(dialog);
title.append($('').text(this.title));
title.append('');
var layoutNameDiv = $('').appendTo(dialog);
layoutNameDiv.append($('').text(GetTextByKey("P_XXXXXX", "Layout Name:")));
txtLayoutName = $("").appendTo(layoutNameDiv);
if (currentLayout && this.type == 1) {//type == 1 在列表上直接保存
if (!currentLayout.IsPublic || IsAdmin)
txtLayoutName.val(currentLayout.LayoutName || "");
}
var layoutAttrDiv = $('').appendTo(dialog);
chkMyDefault = $("").appendTo(layoutAttrDiv);
$("").text(GetTextByKey("P_XXXXXX", "Save as my Default")).appendTo(layoutAttrDiv);
if (IsAdmin) {
layoutAttrDiv = $('').appendTo(dialog);
chkPublic = $("").appendTo(layoutAttrDiv);
$("").text(GetTextByKey("P_XXXXXX", "Make Public")).appendTo(layoutAttrDiv);
layoutAttrDiv = $('').appendTo(dialog);
chkCompanyDefault = $("").appendTo(layoutAttrDiv);
$("").text(GetTextByKey("P_XXXXXX", "Save as Company Default")).appendTo(layoutAttrDiv);
chkMyDefault.on('click', function (e) {
if (chkMyDefault.prop("checked")) {
chkPublic.prop("checked", false);
chkCompanyDefault.prop("checked", false);
}
});
chkPublic.on('click', function (e) {
if (chkPublic.prop("checked"))
chkMyDefault.prop("checked", false);
else
chkCompanyDefault.prop("checked", false);
});
chkCompanyDefault.on('click', function (e) {
if (chkCompanyDefault.prop("checked")) {
chkMyDefault.prop("checked", false);
chkPublic.prop("checked", true);
}
});
if (currentLayout && this.type == 1) {//type == 1 在列表上直接保存
if (!currentLayout.IsPublic)
chkMyDefault.prop('checked', currentLayout.IsDefault);
if (IsAdmin) {
chkPublic.prop('checked', currentLayout.IsPublic);
if (currentLayout.IsPublic)
chkCompanyDefault.prop('checked', currentLayout.IsDefault);
}
}
}
else {
dialog.css('height', 140);
}
var dialogFunction = $('').appendTo(dialog);
$('').appendTo(dialogFunction);
$('').on('click', onSaveLayoutClick.bind(this)).appendTo(dialogFunction);
$('').appendTo(dialogFunction);
$('').appendTo(dialog);
$(document.body).append(dialog);
dialog.dialog(this.onDialogClosed);
return dialog;
}
function onSaveLayoutClick() {
var layoutinfo = {
LayoutName: txtLayoutName.val().trim(),
IsPublic: chkPublic == null ? false : chkPublic.prop("checked"),
};
if (!layoutinfo.LayoutName || layoutinfo.LayoutName.trim() == '') {
showAlert(GetTextByKey("P_GRID_XXXXXX", "Layout name is required."), GetTextByKey("P_GRID_XXXXXX", 'Layout'));
return;
}
if (layoutinfo.IsPublic)
layoutinfo.IsDefault = chkCompanyDefault == null ? false : chkCompanyDefault.prop("checked");
else
layoutinfo.IsDefault = chkMyDefault.prop("checked");
var overwrite = false;//private and name not changed, overwrite without prompt
if (currentLayout && !currentLayout.IsPublic && layoutinfo.IsPublic && currentLayout.LayoutName == layoutinfo.LayoutName)
overwrite = true;
this.onOK && this.onOK(layoutinfo, overwrite);
}
function getAllGridLayoutsForSave(layoutid) {
_network.commonpagequery(15, layoutid, function (data) {//getGridLayouts
if (typeof data !== 'string') {
loadLayoutTip(data);
}
});
}
function loadLayoutTip(data) {
$("#div_layout_tooltip").remove();
if (data.length == 0) return;
var text = "";
var priLayouts = [];
var pubLayouts = [];
for (var i = 0; i < data.length; i++) {
var item = data[i];
if (!item) continue;
var ls = item.IsPublic ? pubLayouts : priLayouts;
if (item.IsDefault)
ls.splice(0, 0, item);
else
ls.push(item);
}
if (priLayouts.length > 0) {
text += GetTextByKey("P_XXXXXX", "My Saved Layouts");
for (var i = 0; i < priLayouts.length; i++) {
var item = priLayouts[i];
text += " " + item.LayoutName + (item.IsDefault ? " *" : "");
}
}
if (IsAdmin) {
if (pubLayouts.length > 0) {
if (text != "")
text += " ";
text += GetTextByKey("P_XXXXXX", "Company Layouts");
for (var i = 0; i < pubLayouts.length; i++) {
var item = pubLayouts[i];
text += " " + item.LayoutName + (item.IsDefault ? " *" : "");
}
}
}
var div = $("").html(text);
var left = _this.dialog.offset().left + _this.dialog.width() + 2;
var top = _this.dialog.offset().top;
div.css({ 'display': '', 'left': left, 'top': top, 'max-height': 320, 'max-width': 240, 'overflow': 'hidden' });
$(window.document.body).append(div);
}
})();
var $managelayoutdialog = function (layoutid) {
this.title = GetTextByKey("P_GRID_XXXXXX", 'Manage Layout');
this.dialogId = 'dialog_managelayout';
$("#" + this.dialogId).remove();
this.layoutid = layoutid;
this.dialog = null;
this.onDialogClosed = null;
};
(function () {
var __proto = $managelayoutdialog.prototype;
var myLayoutsDiv = null;
var companyLayoutsDiv = null;
var _this = null;
__proto.showManageLayoutDialog = function (topRatio, force) {
var dialog;
_this = this;
if (force) {
$('#' + this.dialogId).remove();
dialog = createManageLayoutDialog.call(this);
} else {
dialog = $('#' + this.dialogId);
if (!dialog.length) {
dialog = createManageLayoutDialog.call(this);
}
}
this.dialog = dialog;
dialog.attr('init', '1').showDialog().css({
'top': (document.documentElement.clientHeight - $('#dialog_machines').height()) / (topRatio || 3),
'left': (document.documentElement.clientWidth - $('#dialog_machines').width()) / 2
});
getAllGridLayoutsForManage(this.layoutid);
};
function createManageLayoutDialog() {
var dialog = $('
').attr('id', this.dialogId);
var title = $('').appendTo(dialog);
title.append($('').text(this.title));
title.append('');
var content = $('
').appendTo(dialog);
var labelDiv = $('').appendTo(content);
labelDiv.append($('').text(GetTextByKey("P_XXXXXX", "My Saved Layouts")));
myLayoutsDiv = $('').appendTo(content);
if (IsAdmin) {
labelDiv = $('').appendTo(content);
labelDiv.append($('').text(GetTextByKey("P_XXXXXX", "Company Layouts")));
companyLayoutsDiv = $('').appendTo(content);
}
var dialogFunction = $('').appendTo(dialog);
$('').appendTo(dialogFunction);
$('').appendTo(dialogFunction);
$('').appendTo(dialog);
$(document.body).append(dialog);
dialog.dialog(this.onDialogClosed);
return dialog;
}
function getAllGridLayoutsForManage(layoutid) {
_network.commonpagequery(15, layoutid, function (data) {//getGridLayouts
if (typeof data !== 'string') {
var priLayouts = [];
var pubLayouts = [];
for (var i = 0; i < data.length; i++) {
var item = data[i];
if (!item) continue;
var ls = item.IsPublic ? pubLayouts : priLayouts;
if (item.IsDefault)
ls.splice(0, 0, item);
else
ls.push(item);
}
if (priLayouts.length > 0) {
for (var i = 0; i < priLayouts.length; i++) {
var item = priLayouts[i];
var div = $('');
div.append($('').text(item.LayoutName + (item.IsDefault ? " *" : "")));
var icon = $('').appendTo(div);
icon.on('click', item, function (e) {
deleteLayout(e.data.ID, $(e.target).parent());
});
myLayoutsDiv.append(div);
}
}
if (IsAdmin) {
if (pubLayouts.length > 0) {
for (var i = 0; i < pubLayouts.length; i++) {
var item = pubLayouts[i];
var div = $('');
div.append($('').text(item.LayoutName + (item.IsDefault ? " *" : "")));
var icon = $('').appendTo(div);
icon.on('click', item, function (e) {
deleteLayout(e.data.ID, $(e.target).parent());
});
companyLayoutsDiv.append(div);
}
}
}
}
});
}
function deleteLayout(id, div) {
var msg = GetTextByKey("P_GRID_XXXXXX", 'Are you sure you want to delete this layout?');
var title = GetTextByKey("P_GRID_XXXXXX", 'Layout');
showLayoutConfirm(_this.dialog, msg, title, function (e) {
div.remove();
_network.commonpagequery(9, id, function (data) { }, function (e) { });
}, function (e) {
}, function (e) {
});
}
})();
/***************************Public Method********************************************/
function CreateLayoutMenus(sender, layoutid, grid) {
hidePanels();//hide other menu
$("#layout_panel_holder").remove();// remove layout_panel_holder
var panel_holder = $('').appendTo(document.body);
var funmenu = $('');
panel_holder.append(funmenu);
var ulmenus = $('
');
funmenu.append(ulmenus);
var li = $('').text(GetTextByKey("P_GRID_XXXXXX", 'Update Layout'));
ulmenus.append(li);
li.on('click', function () {
ShowSelectLayoutColumnsDialog(layoutid, grid);
});
li = $('').text(GetTextByKey("P_GRID_XXXXXX", 'Save Layout'));
ulmenus.append(li);
li.on('click', function () {
ShowSaveLayoutDialog(null, null, layoutid, grid, 1);
});
li = $('').text(GetTextByKey("P_GRID_XXXXXX", 'Manage Layout'));
ulmenus.append(li);
li.on('click', function () {
ShowManageLayoutDialog(layoutid);
});
var left = $(sender).offset().left;
var top = $(sender).offset().top + $(sender).height();
var minwidth = 150;
if (minwidth < $(sender).width())
minwidth = $(sender).width()
panel_holder.css({
'opacity': 0,
'left': left,
'right': 'auto',
'top': top + 4,
'min-width': minwidth,
}).show().animate({ 'opacity': 1 }, 100);
getAllGridLayouts(layoutid, grid, ulmenus);
}
function getAllGridLayouts(layoutid, grid, ulmenus) {
_network.commonpagequery(15, layoutid, function (data) {//getGridLayouts
if (typeof data !== 'string') {
var priLayouts = [];
var pubLayouts = [];
for (var i = 0; i < data.length; i++) {
var item = data[i];
if (!item) continue;
var ls = item.IsPublic ? pubLayouts : priLayouts;
if (item.IsDefault)
ls.splice(0, 0, item);
else
ls.push(item);
}
if (priLayouts.length > 0) {
var li = $('');
ulmenus.append(li);
for (var i = 0; i < priLayouts.length; i++) {
var item = priLayouts[i];
var li = $('');
if (item.LayoutName.length > 35) {
li.text(item.LayoutName.substring(0, 35) + '...' + (item.IsDefault ? " *" : ""));
li.attr("title", item.LayoutName);
}
else
li.text(item.LayoutName + (item.IsDefault ? " *" : ""));
li.on('click', item, function (e) {
currentLayout = e.data;
showGridLayout(grid, JSON.parse(e.data.Layout), OnRefresh);
});
ulmenus.append(li);
}
}
if (pubLayouts.length > 0) {
var li = $('');
ulmenus.append(li);
for (var i = 0; i < pubLayouts.length; i++) {
var item = pubLayouts[i];
var li = $('');
if (item.LayoutName.length > 35) {
li.text(item.LayoutName.substring(0, 35) + '...' + (item.IsDefault ? " *" : ""));
li.attr("title", item.LayoutName);
}
else
li.text(item.LayoutName + (item.IsDefault ? " *" : ""));
li.on('click', item, function (e) {
currentLayout = e.data;
showGridLayout(grid, JSON.parse(e.data.Layout), OnRefresh);
});
ulmenus.append(li);
}
}
}
});
}
function ShowSelectLayoutColumnsDialog(layoutid, grid) {
showmaskbg(true);
var dialogColumns = new $layoutcolumnselector(grid);
dialogColumns.onDialogClosed = function () {
showmaskbg(false);
};
dialogColumns.onOK = function (colinfos) {
dialogColumns.dialog.find('.maskbg').css('display', '');
ShowSaveLayoutDialog(dialogColumns, colinfos, layoutid, grid, 0, function () {
currentLayout = currentLayout || {};
currentLayout.Layout = JSON.stringify(colinfos);
showGridLayout(grid, colinfos, OnRefresh);
dialogColumns && dialogColumns.dialog.hideDialog();
showmaskbg(false);
});
};
dialogColumns.showLayoutSelector(3, true);
}
function ShowSaveLayoutDialog(sender, colinfos, layoutid, grid, type, next) {
showmaskbg(true);
var dialogSaveLayout = new $layoutsavedialog(grid, layoutid, type);
dialogSaveLayout.onDialogClosed = function () {
if (sender)
sender.dialog.find('.maskbg').css('display', 'none');
else
showmaskbg(false);
$("#div_layout_tooltip").remove();
};
dialogSaveLayout.onOK = function (layoutinfo, overwrite) {
saveGridLayout(dialogSaveLayout.dialog, layoutid, grid, layoutinfo, colinfos, next, overwrite);
};
dialogSaveLayout.showSaveLayoutDialog(3, true);
}
function ShowManageLayoutDialog(layoutid) {
showmaskbg(true);
var dialogManageLayout = new $managelayoutdialog(layoutid);
dialogManageLayout.onDialogClosed = function () {
showmaskbg(false);
};
dialogManageLayout.showManageLayoutDialog(3, true);
}
var currentLayout = null;
function getGridLayout(layoutid, grid, next) {
_network.commonpagequery(7, layoutid, function (data) {//GetGridLayout
currentLayout = data;
showGridLayout(grid, data == null ? null : JSON.parse(data.Layout), next);
});
}
function showGridLayout(grid, columninfos, next) {
var allColumns = $.extend(true, [], grid.allcolumns);
for (var i = 0; i < allColumns.length; i++) {
allColumns[i].OriginalCaption = allColumns[i].caption;
if (allColumns[i].alwaysshow && columninfos && columninfos.length > 0) {//new alwaysshow column
var exists = false;
for (var j = 0; j < columninfos.length; j++) {
if (allColumns[i].name == columninfos[j].ColumnKey) {
exists = true;
break;
}
}
if (!exists)
columninfos.push({ ColumnKey: allColumns[i].name, Caption: allColumns[i].caption, Width: allColumns[i].width });
}
}
grid.sortIndex = -1;
var tmpColumns = allColumns;
if (columninfos && columninfos.length > 0) {
tmpColumns = [];
for (var i = 0; i < columninfos.length; i++) {
var tmpcol = readColumnWidthSort(allColumns, columninfos[i]);
if (tmpcol) {
if (tmpcol.Sort != undefined && tmpcol.Sort != 0) {
if (grid.$refs && grid.$refs.grid) {//datagrid:vue
grid.$refs.grid.sortKey = tmpcol.name;
grid.$refs.grid.sortDirection = tmpcol.Sort;
}
else {
grid.sortIndex = i;
grid.sortDirection = tmpcol.Sort;
}
}
if (tmpcol.name == "Selected")
tmpColumns.splice(0, 0, tmpcol);
else
tmpColumns.push(tmpcol);
}
}
}
//grid.setColumns(tmpColumns);
grid.columns = tmpColumns;
if (grid.sortIndex >= grid.columns.length)
grid.sortIndex = -1;
if (typeof grid.init == "function")
grid.init();
if (next) {
next(tmpColumns);
}
}
function readColumnWidthSort(columns, colinfo) {
for (var i = 0; i < columns.length; i++) {
if (columns[i].name == colinfo.ColumnKey) {
if (colinfo.Caption && colinfo.Caption.trim() != "") {
columns[i].caption = colinfo.Caption;
columns[i].UserCaption = colinfo.Caption;
}
columns[i].width = parseInt(colinfo.Width);
columns[i].Sort = colinfo.Sort;
if (columns[i].visible !== false || !columns[i].alwaysshow)
columns[i].visible = colinfo.Visible == undefined ? true : colinfo.Visible;
return columns[i];
}
}
return undefined;
}
function saveGridLayout(dialog, layoutid, grid, layoutinfo, colinfos, next, overwrite) {
if (!colinfos) {
colinfos = getLayoutOnGrid(grid);
}
layoutinfo.LayoutId = layoutid;
layoutinfo.Layout = JSON.stringify(colinfos)
_network.commonpagequery(8, htmlencode(JSON.stringify([JSON.stringify(layoutinfo), overwrite ? 1 : 0])), function (e) {
if (e == -1) {
showAlert(GetTextByKey("P_GRID_XXXXXX", "Layout {0} is in use by the Organization and cannot be used. Please select another name.").replace('{0}', layoutinfo.LayoutName)
, GetTextByKey("P_GRID_XXXXXX", 'Layout'));
}
else if (e == -2) {
showLayoutConfirm(dialog, GetTextByKey('P_GRID_XXXXXX', 'Layout {0} already exists, Do you want to overwrite it?').replace('{0}', layoutinfo.LayoutName)
, GetTextByKey('P_GRID_XXXXXX', 'Layout'), function () {
saveGridLayout(dialog, layoutid, grid, layoutinfo, colinfos, next, true);
});
}
else {//Successfully
dialog && dialog.hideDialog();
$("#div_layout_tooltip").remove();
if (next) {
next();
return;
}
showmaskbg(false);
}
});
}
function getLayoutOnGrid(grid) {
colinfos = [];
for (var i = 0; i < grid.columns.length; i++) {
var sort = 0;
var sortKey = "";
var sortDirection = 0;
if (grid.$refs && grid.$refs.grid) { //datagrid:vue
sortKey = grid.$refs.grid.sortKey;
sortDirection = grid.$refs.grid.sortDirection;
}
else {
sortKey = grid.sortKey;
sortDirection = grid.sortDirection;
}
if (sortKey && (sortKey == grid.columns[i].name || sortKey == grid.columns[i].key))
sort = sortDirection;
if (isNaN(sort))
sort = 0;
var columnwidth = {
'Visible': grid.columns[i].visible !== false,
'ColumnKey': grid.columns[i].name,
'Caption': grid.columns[i].UserCaption,
'Width': grid.columns[i].width,
'Sort': sort
}
colinfos.push(columnwidth);
}
return colinfos;
}
function showLayoutConfirm(dialog, msg, title, fok, fcancel) {
dialog.find('.maskbg').css('display', '');
_dialog.showConfirm(msg, title, function (e) {
dialog.find('.maskbg').css('display', 'none');
if (typeof fok === 'function') {
fok(e);
}
}, function () {
dialog.find('.maskbg').css('display', 'none');
});
}