{* Smarty *}

{literal}
<script type="text/javascript">
$(function() {
		$("#add_default").button({
      icons: {
        primary: "ui-icon-document"
      }
    });
    $("#rename").button({
      icons: {
        primary: "ui-icon-pencil"
      }
    });
    $("#remove").button({
      icons: {
        primary: "ui-icon-trash"
      }
    });
    $("#cut").button({
      icons: {
        primary: "ui-icon-scissors"
      }
    });
    $("#copy").button({
      icons: {
        primary: "ui-icon-copy"
      }
    });
    $("#paste").button({
      icons: {
        primary: "ui-icon-clipboard"
      }
    });
	});
  </script>
{/literal}

<div class="ui-widget-content ui-corner-all">
  <h3 class="ui-widget-header ui-corner-all">Структура предприятия</h3>
  <div id="mmenu" style="padding-bottom: 7px;">
    <button id="add_default" title="добавить">добавить</button>
    <button id="rename" title="переименовать">переименовать</button>
    <button id="remove" title="удалить">удалить</button>
    <button id="cut" title="вырезать">вырезать</button>
    <button id="copy" title="копировать">копировать</button>
    <button id="paste" title="вставить">вставить</button>
  </div>
  <div id="demo" class="demo"></div>
</div>

{literal}
<script type="text/javascript">

$(function () {
  //alert(e);
	$("#demo")
		.jstree({
			"plugins" : [ "themes", "json_data", "ui", "crrm", "cookies", "dnd", "types", "hotkeys"],
      "themes" : {
        "theme" : "classic",
        "icons" : false
      },
			"json_data" : {
				"ajax" : {
          "url" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganization.ajax.php{literal}",
					"data" : function (n) {
						return {
							"operation" : "get_children",
							"id" : n.attr ? n.attr("id").replace("node_","") : 0
						};
					}
				}
			},
			"types" : {
				"max_depth" : -2,
				"max_children" : -2,
				"valid_children" : [ "default" ],
				"types" : {
					"default" : {
						"valid_children" : "default"
					},
					"folder" : {
						"valid_children" : [ "default"],
            "start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false
					}
				}
			},
			"core" : {
				"initially_open" : [ "node_1" ],
        "strings" : {
          "loading" : "Загрузка ...",
          "new_node" : "Новый элемент"
        }
			}
		})
		.bind("create.jstree", function (e, data) {
        $.post(
          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
          {
            "operation" : "create_node",
            "id" : data.rslt.parent.attr("id").replace("node_",""),
            "position" : data.rslt.position,
            "title" : data.rslt.name,
            "type" : data.rslt.obj.attr("rel")
          },
          function (r) {
            if(r.status) {
              $(data.rslt.obj).attr("id", "node_" + r.id);
            }
            else {
              $.jstree.rollback(data.rlbk);
            }
          }
        );
      //}
		})
		.bind("remove.jstree", function (e, data) {
      if (data.rslt.obj !== undefined && data.rslt.obj.attr('id') !== undefined) {
        data.rslt.obj.each(function () {
          $.ajax({
            async : false,
            type: "POST",
            url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
            data : {
              "operation" : "remove_node",
              "id" : this.id.replace("node_","")
            },
            success : function (r) {
              if(!r.status) {
                data.inst.refresh();
              }
            }
          });
        });
      }
      else{
        DialogStandartMessageOpen('error', 'Вы не можете удалить данную запись!');
      }
		})
		.bind("rename.jstree", function (e, data) {
      //alert(data.rslt.obj.attr("rel"));
      //if('folder' == data.rslt.obj.attr("rel")){
        $.post(
          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
          {
            "operation" : "rename_node",
            "id" : data.rslt.obj.attr("id").replace("node_",""),
            "title" : data.rslt.new_name
          },
          function (r) {
            if(!r.status) {
              $.jstree.rollback(data.rlbk);
            }
          }
        );
      //}
		})
		.bind("move_node.jstree", function (e, data) {
			data.rslt.o.each(function (i) {
				$.ajax({
					async : false,
					type: "POST",
					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
					data : {
						"operation" : "move_node",
						"id" : $(this).attr("id").replace("node_",""),
						"ref" : data.rslt.np.attr("id").replace("node_",""),
						"position" : data.rslt.cp + i,
						"title" : data.rslt.name,
						"copy" : data.rslt.cy ? 1 : 0
					},
					success : function (r) {
						if(!r.status) {
							$.jstree.rollback(data.rlbk);
						}
						else {
							$(data.rslt.oc).attr("id", "node_" + r.id);
							if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
								data.inst.refresh(data.inst._get_parent(data.rslt.oc));
							}
						}
					}
				});
			});
		})
});

$(function () {
	$("#mmenu button").click(function () {
    switch(this.id) {
			case "add_default":
        $("#demo").jstree("create", null, "last", { "attr" : { "rel" : this.id.toString().replace("add_", "") } });
        break;
			case "add_folder":
				$("#demo").jstree("create", null, "last", { "attr" : { "rel" : this.id.toString().replace("add_", "") } });
				break;
			//case "search":
				//$("#demo").jstree("search", document.getElementById("text").value);
				//break;
//        case "rename":
//          //$("#demo").jstree(this.id);
//				break;
			//case "text": break;
			default:
				$("#demo").jstree(this.id);
				break;
		}
	});
});
</script>
{/literal}