AmdaNode.js 4.79 KB
/**
 * Project  : AMDA-NG4
 * Name     : AmdaNode.js
 * @class   amdaModel.AmdaNode
 * @extends Ext.data.Model
 * @brief   Generic Model of Node for AMDA project
 * @author  CDA
 */
Ext.define('amdaModel.AmdaNode', {
	extend: 'Ext.data.TreeModel',

	requires: ['amdaReader.ExplorerReader'],

	fields: [
		{name: 'leaf', type: 'boolean'},
		{name: 'nodeType',type:'string'},
		{name: 'id', type: 'string'},
		{name:'text',type: 'string'},
		{name:'info',type: 'string'},
		{name: 'help', type: 'string', defaultValue: ''},
		{name:'rootNode',  persist: false},
		{name: 'ownerTreeId', persist: false},
		{name: 'ownerTree',  persist: false},
		{name: 'checked', defaultValue: null, persist: false}
	],

	statics : {
		NEW_DIR_NAME : 'new Folder',
		MULTI_PREFIX : 'mult'
	},

	/**
	* @cfg proxy used to load nodes
	*/
	proxy: {
		type: 'direct',
		api :
		{
			read: AmdaAction.getTree,
			update: AmdaAction.saveTree,
			create: AmdaAction.saveTree,
			destroy: AmdaAction.doNothing
		},
		paramsAsHash : true,
		reader: {
			type: 'explorer'
		}
	},

	/**
	* Abstract getter of complete array of item context menu
	* @return {Ext.Array} the complete array of item context menu
	*/
	getAllContextMenuItems: Ext.emptyFn,

	/**
	* Abstract method call on click on item of context menu
	*/
	onMenuItemClick: Ext.emptyFn,

	/**
	* Abstract Update Data method
	*/
	update : Ext.emptyFn,

	/**
	* Abstract Rename Data method
	*/
	rename : Ext.emptyFn,

	/**
	* Abstract Rename  D&D Data method
	*/
	renameDD : Ext.emptyFn,

	/**
	* Abstract Create Data method
	*/
	create : Ext.emptyFn,

	/**
	* filtering method of context menu
	* @param {String} itemKind the kind of right clicked node
	* @return {Ext.Array} the Array of filtered item menu
	*/
	getFilteredContextMenu : function(itemKind) {
		var menuitems = this.getAllContextMenuItems();
		
		var realmenuitems = 0;
		if (menuitems && menuitems.length){
			for ( var i = 0; i < menuitems.length; i++) {
					var currentItem = menuitems[i];
					var myItemKind = Ext.util.Format.substr(currentItem.fnId, 0, 4);
					if (myItemKind === itemKind) {
						realmenuitems ++;
						currentItem.hidden = false;
					}
					else {
						currentItem.hidden = true;
					}
			}
			if ( realmenuitems == 0 )
				return null;

			return menuitems;
		} else {
			return null;
		}
	},

	getMultiContextMenu : function(){
		var menuitems = this.getMultiContextMenuItems();
		if (menuitems && menuitems.length){
			return menuitems;
		} else {
			return null;
		}
	},

	/**
	* get the kind of this node
	* @returns itemKind string
	*/
	getNodeKind : function() {
		var itemKind;
	// if id of this node have root subcategory suffix
		if (this.id && Ext.util.Format.substr(this.id, -(amdaUI.ExplorerUI.ROOT_SUFFIX.length), this.id.length) === amdaUI.ExplorerUI.ROOT_SUFFIX ) {
			itemKind = amdaUI.ExplorerUI.ITEM_KIND_ROOT;
		}
		// if this node is a leaf and have no child
		else if (this.isLeaf()) {
			itemKind = amdaUI.ExplorerUI.ITEM_KIND_LEAF;
		}
		else if (this.get('isParameter')) {
					itemKind = amdaUI.ExplorerUI.ITEM_KIND_PARA;
			}
		else if (this.get('rank')) {
					itemKind = amdaUI.ExplorerUI.ITEM_KIND_MISS;
			}
		// base
		else if (this.id && Ext.util.Format.substr(this.id, -(amdaUI.ExplorerUI.CAT_SUFFIX.length), this.id.length) === amdaUI.ExplorerUI.CAT_SUFFIX ) {
			itemKind = amdaUI.ExplorerUI.ITEM_KIND_BASE;
		}
		// other case
		else {
			itemKind = amdaUI.ExplorerUI.ITEM_KIND_DIRE;
		}
		
		return itemKind;
	},

	/**
	* Getter on context menu of this node
	* @returns {Ext.menu.Menu} the contextMenu of this node
	*/
	getContextMenuItems : function() {
		// set the node kind
		var itemKind = this.getNodeKind();
		// filter the global context menu by node kind
		var menuItems = this.getFilteredContextMenu(itemKind);
		// return the context menu for this node
		return menuItems;
	},

	getContextMenuMultiItems : function() {
		return this.getMultiContextMenu();
	},

	/**
	* Method to retrieve the root node corresponding to the nodeType of this node
	* @return {amdaModel.AmdaNode} the root node of category of this node
	*/
	getRootNode : function() {
		if (!this.get('rootNode')) {
			// if this node is the root of its category
			if(this.getNodeKind() == amdaUI.ExplorerUI.ITEM_KIND_ROOT) {
					this.set('rootNode',this);
			} else {
					// get resources tree panel
					var tree = this.myGetOwnerTree();
					var treeRoot = tree.getRootNode();
					var rootNodeId = this.get('nodeType')+amdaUI.ExplorerUI.ROOT_SUFFIX;

					this.set('rootNode',treeRoot.findChild( 'id', rootNodeId, true));
			}
		}
		return this.get('rootNode');
	},

	/**
	* getter on ownerTree (use ownerTreeId property)
	* @return {Ext.tree.Panel} the tree which contains this node
	*/
	myGetOwnerTree : function() {
		if (!this.get('ownerTree')) {
			this.set('ownerTree',Ext.getCmp(this.get('ownerTreeId')));
		}
		return this.get('ownerTree');
	}

});