ShareObjectUI.js 7.19 KB
/**
 * Project   : AMDA-NG
 * Name      : ShareObjectUI.js
 * @class 	 amdaUI.ShareObjectUI
 * @extends  Ext.form.Panel
 * @brief	 Share object UI definition (View)
 * @author 	 Benjamin
 * @version  $Id:  $
 ********************************************************************************
 *    FT Id     :   Date   : Name - Description
 *******************************************************************************
 */ 
Ext.define('amdaUI.ShareObjectUI',{
	extend: 'Ext.form.Panel',
	alias: 'widget.shareobject',
	
	infoTextArea : null,
	folderStore  : null,
	folderCombo  : null,
	nameField    : null,
	descriptionTextArea : null,
	objectType   : 'unknown',
	objectProp   : null,
	onAfterShareObject : null,
	
	constructor: function(config) {
		this.init(config);	    
		this.callParent(arguments);
	},
	
	setProperties: function(objectType, objProp) {
		var typeName = '';
		
		switch (objectType) {
		case 'timetab' :
			typeName = 'TimeTable';
			break;
		case 'catalog' :
			typeName = 'Catalog';
			break;
		default:
			typeName = 'Unknown Type';
		}
		
		this.objectType = objectType;
		this.objectProp = objProp;
		
		var message = 'Do you really want to share "' + objProp.name + '" to the community?';
		message += '\n\nNote: You will not be able to delete or change this object once shared.';
		
		this.infoTextArea.setValue(message);
		this.nameField.setValue(objProp.name);
		this.descriptionTextArea.setValue('');
		
		//Update folders list
		AmdaAction.getSharedObjectFolders({'type' : this.objectType}, function (result, e) {
			var t = e.getTransaction();
            if (e.status) 
            {	
            	if (result && result.success)
                {
            		this.folderCombo.store.loadData(result.folders, false);
                }
                else
                	Ext.Msg.show( {
                		title : 'Share Object', 
                		msg : 'Cannot get folder list ('+result.message+')',
                		modal : true,
                        icon : Ext.Msg.ERROR,
                        buttons : Ext.Msg.OK
                	});
            }
            else
            {
                    // FAILURE
                    Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
            }
		},this);
	},
	
	setDescription : function(description) {
		this.descriptionTextArea.setValue(description);
	},
	
	init : function(config) {
		var me = this;
		
		//Callback called when a new shared object is added
		if (config.onAfterShareObject)
			me.onAfterShareObject = config.onAfterShareObject;
		
		this.infoTextArea = Ext.create('Ext.form.field.TextArea', {
    		xtype     : 'textareafield',
        	hideLabel : true,
        	autoScroll : true,
    	});
    	
		this.folderStore = Ext.create('Ext.data.Store', {
		    fields: ['id', 'name'],
		    data : [],
		    listeners: { 
		    	datachanged : function(store) {
		    		if ((store.getCount() <= 0) || !me.folderCombo)
		    			return;
		    		me.folderCombo.select(store.getAt(0));
	            }
		    }
		});
		
    	this.folderCombo = Ext.create('Ext.form.field.ComboBox', {
    		fieldLabel: 'Choose Folder',
        	store: this.folderStore,
		    queryMode: 'local',
		    displayField: 'name',
		    valueField: 'id',
		    value: 'None',
		    editable: false,
		    forceSelection: true
    	});
		
    	this.nameField = Ext.create('Ext.form.field.Text', {
    		fieldLabel: 'Name',
    	    allowBlank: false  // requires a non-empty value
    	});
    	
    	this.descriptionTextArea = Ext.create('Ext.form.field.TextArea', {
    		xtype     : 'textareafield',
    		fieldLabel: 'Description',
        	autoScroll : true,
        	allowBlank : false // requires a non-empty value
    	});
    	
		var myConf = {
			layout: {
				type: 'vbox',
				align: 'stretch'
			},
			items: [
			        this.infoTextArea,
			        this.folderCombo,
			        this.nameField,
			        this.descriptionTextArea
			],
			fbar: [
			       { 
			    	   text: 'Share', 
			    	   scope: this,
			    	   handler: function() {
			    		   if(!me.isValid())
			    			   return;
			    		   
			    		   var folder = me.folderCombo.getValue();
			    		   var description = me.descriptionTextArea.getValue();
			    		   var name = me.nameField.getValue();
			    		   
			    		   AmdaAction.isSharedObjectNameAlreadyUsed({'type'  : me.objectType, 'name' : name}, function (result, e) {
			    			   var t = e.getTransaction();
			    	            if (e.status) 
			    	            {	
			    	            	if (result && result.success)
			    	                {
			    	            		var request_obj = {
			 			    				   'type'        : me.objectType,
			 			    				   'object'      : me.objectProp,
			 			    				   'folder'      : folder,
			 			    				   'name'        : name,
			 			    				   'description' : description
			 			    		   	};
			    	            		
			    	            		if (result.alreadyUsed)
			    	            		{
			    	            			Ext.Msg.show( {
	 			    	                		title : 'Share Object', 
	 			    	                		msg : 'Name "' + name + '" already used',
	 			    	                		modal : true,
	 			    	                        icon : Ext.Msg.ERROR,
	 			    	                        buttons : Ext.Msg.OK
	 			    	                	});
			    	            			return;
			    	            		}
			 			    		   
			 			    		   AmdaAction.shareObjects(request_obj, function (result, e) {
			 			    				var t = e.getTransaction();
			 			    	            if (e.status) 
			 			    	            {	
			 			    	            	if (result && result.success)
			 			    	                {
			 			    	            		if (me.onAfterShareObject)
			 			    	            			me.onAfterShareObject(result.folder_id, result.object_id);
			 			    	                }
			 			    	                else
			 			    	                	Ext.Msg.show( {
			 			    	                		title : 'Share Object', 
			 			    	                		msg : 'Cannot share objects ('+result.message+')',
			 			    	                		modal : true,
			 			    	                        icon : Ext.Msg.ERROR,
			 			    	                        buttons : Ext.Msg.OK
			 			    	                	});
			 			    	            }
			 			    	            else
			 			    	            {
			 			    	                    // FAILURE
			 			    	                    Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
			 			    	            }
			 			    			},me);
			    	                }
			    	                else
			    	                	Ext.Msg.show( {
			    	                		title : 'Share Object', 
			    	                		msg : 'Cannot share objects ('+result.message+')',
			    	                		modal : true,
			    	                        icon : Ext.Msg.ERROR,
			    	                        buttons : Ext.Msg.OK
			    	                	});
			    	            }
			    	            else
			    	            {
			    	                    // FAILURE
			    	                    Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
			    	            }
			    		   });
			    	   }
			       }
            ]
		};
		
		Ext.apply (this , Ext.apply (arguments, myConf));
	}  
});