{* Smarty *}


<div class="ui-widget-content ui-corner-all">
  <h3 class="ui-widget-header ui-corner-all">Структура организации</h3>
  <div id="mmenu" style="padding-bottom: 7px;">
  <ul id="icons" class="ui-widget ui-helper-clearfix">
    <!--li class="ui-state-default ui-corner-all" id="add_folder" title="создать" style="display:block; float:left;"><span class="ui-icon ui-icon-folder-collapsed"></span>создать&nbsp;</li-->
    <li class="ui-state-default ui-corner-all" id="add_default" title="добавить" style="display:block; float:left;"><span class="ui-icon ui-icon-document"></span>добавить&nbsp;</li>
    <li class="ui-state-default ui-corner-all" id="rename" title="переименовать" style="display:block; float:left;"><span class="ui-icon ui-icon-pencil"></span>переименовать&nbsp;</li>
    <li class="ui-state-default ui-corner-all" id="remove" title="удалить" style="display:block; float:left;"><span class="ui-icon ui-icon-trash"></span>удалить&nbsp;</li>
    <li class="ui-state-default ui-corner-all" id="cut" title="вырезать" style="display:block; float:left;"><span class="ui-icon ui-icon-scissors"></span>вырезать&nbsp;</li>
    <li class="ui-state-default ui-corner-all" id="copy" title="копировать" style="display:block; float:left;"><span class="ui-icon ui-icon-copy"></span>копировать&nbsp;</li>
    <li class="ui-state-default ui-corner-all" id="paste" title="вставить" style="display:block; float:left;"><span class="ui-icon ui-icon-clipboard"></span>вставить&nbsp;</li>
    <!--li class="ui-state-default ui-corner-all" id="detail" title="подробно" style="display:block; float:left;"><span class="ui-icon ui-icon-clipboard"></span>подробно&nbsp;</li-->
  </ul>
  </div>
  <div id="demo" class="demo"></div>
</div>

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

$(function () {
  //alert(e);
	$("#demo")
		.jstree({
			"plugins" : [ "themes", "json_data", "ui", "crrm", "cookies", "dnd", "search", "types", "hotkeys", "contextmenu"],
      "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",
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/file.png{literal}"
						}
					},
					"folder" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/folder.png{literal}"
						}
					},
					"drive" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/root.png{literal}"
						},
						"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) {
			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();
						}
					}
				});
			});
		})
		.bind("rename.jstree", function (e, data) {
      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 #icons li").click(function () {
    switch(this.id) {
			case "add_default":
        //SearchView();
        $("#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 "detail":
//				//$("#demo").jstree("create", null, "last", { "attr" : { "rel" : this.id.toString().replace("add_", "") } });
//        //alert($(".jstree-clicked").parent().attr("id"));
//
//				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;
		}
	});
});
{/literal}

</script>
