<script type="text/javascript">
<!--
var userGroup;
var CanAddDeleteComponent;
var setting = {
// async: {
// enable: true,
// url: '<%= Url.Action("GetTree") %>',
// autoParam: ["id", "pId", "name=n", "level=lv"],
// otherParam: { "productId": '1' }
//// otherParam: { "otherParam": "zTreeAsyncTest" },
//// dataFilter: filter
// },
view: {
addHoverDom: addHoverDom,
removeHoverDom: removeHoverDom,
selectedMulti: false
},
// check: {
// enable: true
// },
edit: {
enable: true,
editNameSelectAll: true,
//showRenameBtn: setRenameBtn,
showRenameBtn: false,
showRemoveBtn: setRemoveBtn
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeDrag: beforeDrag,
beforeEditName: beforeEditName,
beforeRemove: beforeRemove,
beforeRename: beforeRename,
onRemove: onRemove,
onRename: onRename
}
};
// var zNodes = [
// { id: 1, pId: 0, name: "Parent Node 1", open: true },
// { id: 11, pId: 1, name: "2nd Level 1-1", open: true },
// { id: 111, pId: 11, name: "3nd Level 1-1-1" },
// { id: 112, pId: 11, name: "3nd Level 1-1-2" },
// { id: 12, pId: 1, name: "2nd Level 1-2" },
// { id: 121, pId: 12, name: "3nd Level 1-1-1" },
// { id: 122, pId: 12, name: "3nd Level 1-1-2" },
// { id: 13, pId: 1, name: "2nd Level 1-3" }
// ];
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
var log, className = "dark";
function beforeDrag(treeId, treeNodes) {
return false;
}
function beforeEditName(treeId, treeNode) {
className = (className === "dark" ? "":"dark");
showLog("[ "+getTime()+" beforeEditName ] " + treeNode.name);
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.selectNode(treeNode);
return confirm("Edit Node -- " + treeNode.name + "?");
}
function beforeRemove(treeId, treeNode) {
className = (className === "dark" ? "" : "dark");
showLog("[ " + getTime() + " beforeRemove ] " + treeNode.name);
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.selectNode(treeNode);
//confirm("Are you sure to DELETE node -- " + treeNode.name + " ?")
var canDelete;
$.ajax({
url: '<%=Url.Action("CanDeleteTreeNode")%>',
type: 'POST',
async: false,
data: { productId: $('#ProductsDropDownList').val(), treenodeId: treeNode.id },
success: function (data) {
if (data == "0") {
alert("Node is NOT allowed to delete since there is product definition for the node.");
canDelete=false;
} else {
canDelete = true;
}
}
});
return canDelete;
}
function onRemove(e, treeId, treeNode) {
showLog("[ " + getTime() + " onRemove ] " + treeNode.name);
}
function beforeRename(treeId, treeNode, newName) {
className = (className === "dark" ? "" : "dark");
showLog("[ " + getTime() + " beforeRename ] " + treeNode.name);
if (newName.length == 0) {
alert("Name cannot be blank.");
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
setTimeout(function () { zTree.editName(treeNode) }, 10);
return false;
}
return true;
}
function onRename(e, treeId, treeNode) {
showLog("[ " + getTime() + " onRename ] " + treeNode.name);
}
function showLog(str) {
if (!log) log = $("#log");
log.append("<li class='" + className + "'>" + str + "</li>");
if (log.children("li").length > 8) {
log.get(0).removeChild(log.children("li")[0]);
}
}
function getTime() {
var now = new Date(),
h = now.getHours(),
m = now.getMinutes(),
s = now.getSeconds(),
ms = now.getMilliseconds();
return (h + ":" + m + ":" + s + " " + ms);
}
var newCount = 1;
function addHoverDom(treeId, treeNode) {
if (treeNode.level == 1) {
if (CanAddDeleteComponent == "True") {
var sObj = $("#" + treeNode.tId + "_span");
if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
var addStr = "<span class='button add' id='addBtn_" + treeNode.id
+ "' title='add node' onfocus='this.blur();'></span>";
sObj.after(addStr);
var btn = $("#addBtn_" + treeNode.id);
if (btn) btn.bind("click", function () {
//$("#lbl_ParentName").innerText = treeNode.name;
$("#MarketsDropDownListNew").attr("value", $("#MarketsDropDownList").val());
$("#NewDialog").dialog({
resizable: false,
width: 500,
modal: true,
buttons: {
"Confirm": function () {
if ($("#txt_NewNodeName").val() == "") {
alert("Please input the New Node Name.");
} else {
var adddedname = $("#txt_NewNodeName").val();
var newTree = $.fn.zTree.getZTreeObj("treeDemo");
var Nodes = newTree.getNodes();
var act = newTree.transformToArray(Nodes);
for (var i = 0; i < act.length; i++) {
if (act[i].name == adddedname)
{ alert('duplicate node name!'); return false; }
}
if ($("#MarketsDropDownListNew").val() != $("#MarketsDropDownList").val() && $("#MarketsDropDownListNew").val() != "General") {
$.ajax({
url: '<%=Url.Action("AddNewComponent")%>',
type: 'POST',
async: false,
data: 'productId=' + $('#ProductsDropDownList').val() + '&marketName=' + $("#MarketsDropDownListNew").val() + '&id=' + RndNum(8) + '&pId=' + treeNode.id + '&name=' + $("#txt_NewNodeName").val() + '&brandname=' + $("#txt_brand").val() + '&productname=' + $("#HiddenTXTProduct").val(),
success: function (data) {
if (data == "fail") {
alert("Adding component failed!");
}
else if (data == "isnew") {
alert("Please save first for this is a new brand or product!");
}
else if (data == "exist") {
alert("The component has already exsited!");
}
}
});
} else {
newCount++;
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
if ($("#chk_OwnProduct").is(':checked')) {
//zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, name: $("#txt_NewNodeName").val(), marketName: $("#MarketsDropDownListNew").val() });
zTree.addNodes(treeNode, { id: RndNum(8), pId: treeNode.id, name: GetNodeName($("#txt_NewNodeName").val(), $("#MarketsDropDownListNew").val()), marketName: $("#MarketsDropDownListNew").val() });
// zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, name: "new node" + (newCount++), marketName: $("#MarketsDropDownListNew").val() });
} else {
zTree.addNodes(treeNode, { id: RndNum(8), pId: treeNode.id, name: $("#txt_NewNodeName").val(), marketName: "General" });
}
$("#txt_NewNodeName").attr("value", '');
// $(this).dialog("close");
// return false;
}
$(this).dialog("close");
return false;
}
},
Cancel: function () {
$(this).dialog("close");
}
}
});
});
}
}
};
function removeHoverDom(treeId, treeNode) {
$("#addBtn_" + treeNode.id).unbind().remove();
};
function selectAll() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked");
}
$(document).ready(function () {
userGroup = $('#HIddenUsergroup').val();
//$("#CanAddDeleteComponent").attr("value", '<%=ViewBag.AddDeleteLocalComponent %>');
CanAddDeleteComponent = '<%=ViewBag.AddDeleteLocalComponent %>';
// $.fn.zTree.init($("#treeDemo"), setting, zNodes);
// $.fn.zTree.init($("#treeDemo"), setting);
// $("#selectAll").bind("click", selectAll);
});
function setRemoveBtn(treeId, treeNode) {
// return !treeNode.isParent;
var showButton = false;
if (treeNode.level == 2) {
showButton = true;
}else{
showButton = false;
}
//show button for admin
if (treeNode.level == 2) {
if (userGroup != "SuperUser" && treeNode.marketName == "General") {
showButton = false;
}
if (CanAddDeleteComponent != "True") {
showButton = false;
}
}
return showButton;
}
function setRenameBtn(treeId, treeNode) {
//return !treeNode.isParent;
var showButton = false;
if (treeNode.level == 2) {
showButton = true;
}else{
showButton = false;
}
//show button for admin
if (treeNode.level == 2) {
if (userGroup != "SuperUser" && treeNode.marketName == "General") {
return false;
}
}
return showButton;
}
function GetNodeRandomId() {
var date = new Date();
var times1970 = date.getTime();
var NodeRandomId = parseInt(date.getDate() + date.getHours() + date.getMinutes() + date.getSeconds());
return RndNum(4);
}
function RndNum(n) {
var rnd = "";
for (var i = 0; i < n; i++)
rnd += Math.floor(Math.random() * 10);
return rnd;
}
function GetNodeName(name, marketname) {
if (marketname == "General") {
return name
}
return name + '(' + marketname + ')'
}
//-->
</script>
<script type="text/javascript">
$("#SaveTree").click(function () {
//var IsNewProduct = false;
if ($('#txt_brand').val() == "") {
alert("Please enter brand for the product.");
return false;
}
if ($("#txt_product").val() == "") {
alert("Please enter product for the brand.");
return false;
}
var newTree = $.fn.zTree.getZTreeObj("treeDemo");
if (newTree==null) {
alert("Please click New first.");
return false;
}
var Nodes = newTree.getNodes();
var act = newTree.transformToArray(Nodes);
var postdata = new Array();
for (var i = 0; i < act.length; i++) {
if (!act[i].pId && typeof (act[i].pId) != "undefined") {
act[i].pId = 0;
}
postdata[i] = { id: act[i].id, pId: act[i].pId, name: act[i].name, level: act[i].level, marketName: act[i].marketName };
}
var postData = $.toJSON(postdata);
$("#HiddenTreeInfo_Json").attr("value", postData);
// $.ajax({
// url: '<%= Url.Action("SaveTree") %>',
// type: 'POST',
// // dataType: 'json',
// data: 'TreeNodes=' + postData,
// success: function (data) {
// alert(data.Msg);
// }
// });
$(document.forms[0]).ajaxSubmit({
url: '<%= Url.Action("SaveTree")%>',
success: function (data) {
alert(data.Msg);
if (data.Result == "1") {
$(document.forms[0]).submit();
}
}
});
});
</script>
[HttpPost]
public JsonResult SaveTree(ComponentIndexModel model, string TreeNodes)
{
try
{
string brandname = model.TXTbrand;
int productId;
ComponentManageProxy component = new ComponentManageProxy();
IBrandRepository<BrandRow> _BrandRepository = new BrandRepository<BrandRow>();
BrandRow row = _BrandRepository.GetBrandByName(brandname);
if (row == null)
{
row = new BrandRow();
row.BrandName = brandname;
row.UniqueID = Guid.NewGuid();
row.BrandType = model.ProductType ? 1 : 0;
_BrandRepository.AddBrand(row);
}
ProductRepository<ProductRow> _ProductRepository = new ProductRepository<ProductRow>();
if (model.hidden_product != null)
{
ProductRow _row = _ProductRepository.GetProductByName(model.hidden_product);
if (_row == null)
{
_row = new ProductRow();
_row.UniqueID = Guid.NewGuid();
_row.ProductName = model.hidden_product;
_row.ComponentTreeID = Guid.NewGuid();
_row.BrandName = brandname;
_ProductRepository.AddProduct(_row);
_row = _ProductRepository.GetProductByUniqueID(_row.UniqueID);
productId = _row.ID;
}
else
{
//productId = int.Parse(model.Product);
productId = _row.ID;
if (productId.ToString() != model.Product)
{
return Json(new
{
Msg = "The product is already exsit.",
Result = "2",
});
}
}
}
else
{
productId = int.Parse(model.Product);
}
//component.DeleteTree(productId);
ProductManage Producttarget = new ProductManage(_ProductRepository);
ProductRow _Product = _ProductRepository.GetProductByID(productId);
//component.GetAllComponentNodes(_Product.ComponentTreeID);
IComponentRepository<ComponentRowwithMultipleLCA> _ComponentBackupRepo = new ComponentRepository<ComponentRowwithMultipleLCA>();
IComponentTreeNodeRepository<ComponentTree> _NodeBackupRepo = new ComponentTreeNodeRepository<ComponentTree>();
IComponentTreeNodeCompoundRepository<ComponentTreeCompound> _TreeBackupRepo = new ComponentTreeCompoundRepository<ComponentTreeCompound>();
IList<ComponentTreeCompound> _TreeBackupList = _TreeBackupRepo.GetAllComponentNodes(_Product.ComponentTreeID);
JavaScriptSerializer jss = new JavaScriptSerializer();
List<array> nodelist = new List<array>();
IList<ComponentTreeCompound> _MarketSpecificNodeList;
if (model.Market == "General") _MarketSpecificNodeList = _TreeBackupList.Where(x => (x.Level == 3 && x.MarketName != "General" && (x.Name != null && x.Name != String.Empty))).ToList();
else _MarketSpecificNodeList = _TreeBackupList.Where(x => (x.Level == 3 && (x.MarketName != "General" && x.MarketName != model.Market && (x.Name != null && x.Name != String.Empty)))).ToList();
//nodelist = jss.Deserialize(TreeNodes, typeof(List<array>)) as List<array>;
nodelist = jss.Deserialize(model.TreeInfo_Json, typeof(List<array>)) as List<array>;
foreach (var node in nodelist)
{
if (_TreeBackupList.Count == 0)
{
//if (node.level == 2)
//{
//inserttodb()
ComponentRowwithMultipleLCA componentRow = new ComponentRowwithMultipleLCA();
componentRow.UniqueID = Guid.NewGuid();
componentRow.AssignedID = node.id;
componentRow.ComponentName = GetNameWithoutMarket(node.name);
componentRow.MarketName = (node.marketName == null || node.marketName == "") ? "General" : node.marketName;
if (node.level == 0) { componentRow.Description = "root"; }
//component.AddComponentTreeNode(componentRow, _ParentUniqueID, _RootID, level);
component.AddCompoment(componentRow, productId, node.pId, node.level + 1);
}
else
{
if (node.level != 2)
{
if (node.level == 1)
{
IList<ComponentTreeCompound> _nodeEnum = _MarketSpecificNodeList.Where(i => (i.PId == node.id && i.RootID == _Product.ComponentTreeID)).ToList();
foreach (ComponentTreeCompound o in _nodeEnum)
{
ComponentTreeCompound obj1 = o;
//UpdateModel;
ComponentRowwithMultipleLCA __row = _ComponentBackupRepo.FindComponentByUniqueID(obj1.UniqueID);
__row.ComponentName = node.name;
//_ComponentBackupRepo.UpdateComponent(__row);
_MarketSpecificNodeList.Remove(obj1);
ComponentTreeCompound _MarketSpecificnode = _TreeBackupList.First(i => (i.Sid == obj1.Sid && i.RootID == _Product.ComponentTreeID));
_TreeBackupList.Remove(_MarketSpecificnode);
}
}
ComponentTreeCompound _node = _TreeBackupList.First(i => (i.Sid == node.id && i.RootID == _Product.ComponentTreeID));
_TreeBackupList.Remove(_node);
}
if (node.level == 2)
{
IList<ComponentTreeCompound> _nodeEnum = _TreeBackupList.Where(i => (i.Sid == node.id && i.RootID == _Product.ComponentTreeID)).ToList();
if (_nodeEnum.Count >= 1)
{
ComponentTreeCompound obj1 = _nodeEnum.First();
//UpdateModel;
ComponentRowwithMultipleLCA __row = _ComponentBackupRepo.FindComponentByUniqueID(obj1.UniqueID);
__row.ComponentName = node.name;
//_ComponentBackupRepo.UpdateComponent(__row);
_TreeBackupList.Remove(obj1);
}
else
{
ComponentRowwithMultipleLCA componentRow = new ComponentRowwithMultipleLCA();
componentRow.UniqueID = Guid.NewGuid();
componentRow.AssignedID = node.id;
componentRow.ComponentName = GetNameWithoutMarket(node.name);
componentRow.MarketName = (node.marketName == null || node.marketName == "") ? "General" : node.marketName;
if (node.level == 0) { componentRow.Description = "root"; }
component.AddCompoment(componentRow, productId, node.pId, node.level + 1);
}
}
}
}
foreach (ComponentTreeCompound ob in _TreeBackupList)
{
//delete
_NodeBackupRepo.DeleteComponentTreeNodeByComponentUniqueID(ob.TreeNodeUniqueID);
_ComponentBackupRepo.DeleteComponent(ob.UniqueID);
}
return Json(new
{
Msg = "The hierachy definition was saved.",
Result = "1",
});
}
catch (Exception e)
{
return Json(new
{
Msg = "The hierachy definition was saved unsucessful. " + e.Message,
Result = "0",
});
}
}