Ext.define("Portfolio.designer.ui.save.view.Tree", {
	extend: "Ext.tree.Panel",
	alias: ["widget.designer.ui.save.view.tree"],
	useArrows: true,
	rootVisible: false,
	permissions: null,

	requires: [
		"Ext.ux.navigation.model.RolePermissionTree"
	],

	initComponent: function() {

		var i_self = this;

		var a_columns = [
			{
				xtype: "treecolumn",
				text: ip2.i18n({
					da: "Applikation",
					en: "Application"
				}),
				flex: 1,
				sortable: false,
				dataIndex: "text"
			},
			{
				text: ip2.i18n({
					da: "Url",
					en: "Url"
				}),
				flex: 1,
				sortable: false,
				dataIndex: "url",
				renderer: function(value, i_meta, i_node) {
					return i_node.get("url") || "";
				}
			}
		];

		i_self.columns = a_columns;

		var i_store = Ext.create("Ext.data.TreeStore", {
			model: "Ext.ux.navigation.model.RolePermissionTree",
			autoLoad: true,
			listeners: {
				beforeload: function(i_store, i_operation) {

					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();

					Ext.apply(o_extraParams, {
						action: "ip2NavProxy.listMenuNodes",
						tree: true
					});

					delete o_extraParams.registry;
				},
				load: function(i_store, a_records) {

					Ext.Array.each(a_records, function(i_node) {

						var s_text = ip2.i18n(i_node.get("title"));
						var o_properties = i_node.get("properties") || {};

						if (i_node.get("title")) {
							s_text = ip2.i18n(i_node.get("title"));
						}
						else {
							s_text = ip2.i18n(i_node.get("text"));
						}

						i_node.set("text", s_text);

						if (!i_node.isLeaf() || o_properties.__cid__ === 2) {
							i_node.set("checked", false);
						}

						i_node.commit();
					});
				}
			}
		});

		i_self.store = i_store;

		i_self.callParent();
		i_self.enableBubble("dirtychange");
	}
});
