Commit 265fd2fc authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Permite conectar a un Filter externo

Tanto inicialmente como a posteriori, se permite el cambio de canal
hacia la instancia de Filter, para poder comunicar con un Filter creado
desde otro módulo, por ejemplo.
parent b6f9d851
Loading
Loading
Loading
Loading
+75 −24
Original line number Diff line number Diff line
define([
	"dojo/_base/declare"
	, "dojo/_base/lang"
	, "dojo/aspect"
	, "redmic/modules/filter/Filter"
	, "./_FilterItfc"
	'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'dojo/aspect'
	, 'redmic/modules/filter/Filter'
	, './_FilterItfc'
], function(
	declare
	, lang
@@ -20,20 +20,28 @@ define([

			this.config = {
				filterEvents: {
					ADD_TO_QUERY: "addToQuery",
					REFRESH: "refresh",
					UPDATE_TARGET: "updateTarget"
					ADD_TO_QUERY: 'addToQuery',
					REFRESH: 'refresh',
					UPDATE_TARGET: 'updateTarget',
					QUERY_CHANNEL_SET: 'queryChannelSet'
				},
				filterActions: {}
				filterActions: {
					SERIALIZED: 'serialized',
					REQUEST_FILTER: 'requestFilter',
					ADD_TO_QUERY: 'addToQuery',
					REFRESH: 'refresh',
					UPDATE_TARGET: 'updateTarget'
				}
			};

			lang.mixin(this, this.config);

			aspect.before(this, "_mixEventsAndActions", lang.hitch(this, this._mixFilterEventsAndActions));
			aspect.before(this, "_afterSetConfigurations", lang.hitch(this, this._setFilterConfigurations));
			aspect.before(this, "_beforeInitialize", lang.hitch(this, this._initializeFilter));
			aspect.after(this, "_defineSubscriptions", lang.hitch(this, this._defineFilterSubscriptions));
			aspect.after(this, "_definePublications", lang.hitch(this, this._defineFilterPublications));
			aspect.before(this, '_mixEventsAndActions', lang.hitch(this, this._mixFilterEventsAndActions));
			aspect.before(this, '_afterSetConfigurations', lang.hitch(this, this._setFilterConfigurations));
			aspect.before(this, '_beforeInitialize', lang.hitch(this, this._initializeFilter));
			aspect.after(this, '_defineSubscriptions', lang.hitch(this, this._defineFilterSubscriptions));
			aspect.after(this, '_definePublications', lang.hitch(this, this._defineFilterPublications));
			aspect.after(this, '_setOwnCallbacksForEvents', lang.hitch(this, this._setFilterOwnCallbacksForEvents));
		},

		_mixFilterEventsAndActions: function() {
@@ -53,37 +61,54 @@ define([
			}, this.filterConfig || {}]);
		},

		_initializeFilter: function() {

			if (!this.queryChannel) {
				this.filter = new Filter(this.filterConfig);
				this.queryChannel = this.filter.getChannel();
			}

			this._setQueryChannelInModules(this.queryChannel);
		},

		_defineFilterSubscriptions: function() {

			this._subscribeToFilter(this.queryChannel);
		},

		_subscribeToFilter: function(queryChannel) {

			this.subscriptionsConfig.push({
				channel: this.filter.getChannel('SERIALIZED'),
				channel: this._buildChannel(queryChannel, this.actions.SERIALIZED),
				callback: '_subFilterSerialized'
			},{ // TODO: REQUEST_FILTER parece lo mismo que SERIALIZE, hacer que todo vaya por SERIALIZE
				channel: this.filter.getChannel('REQUEST_FILTER'),
				channel: this._buildChannel(queryChannel, this.actions.REQUEST_FILTER),
				callback: '_subFilterSerialized'
			});
		},

		_defineFilterPublications: function() {

			this._setPublicationsToFilter(this.queryChannel);
		},

		_setPublicationsToFilter: function(queryChannel) {

			this.publicationsConfig.push({
				event: 'ADD_TO_QUERY',
				channel: this.filter.getChannel("ADD_TO_QUERY")
				channel: this._buildChannel(queryChannel, this.actions.ADD_TO_QUERY)
			},{
				event: 'REFRESH',
				channel: this.filter.getChannel("REFRESH")
				channel: this._buildChannel(queryChannel, this.actions.REFRESH)
			},{
				event: 'UPDATE_TARGET',
				channel: this.filter.getChannel("UPDATE_TARGET")
				channel: this._buildChannel(queryChannel, this.actions.UPDATE_TARGET)
			});
		},

		_initializeFilter: function() {
		_setFilterOwnCallbacksForEvents: function() {

			this.filter = new Filter(this.filterConfig);
			this.queryChannel = this.filter.getChannel();

			this._setQueryChannelInModules();
			this._onEvt('QUERY_CHANNEL_SET', lang.hitch(this, this._onQueryChannelUpdated));
		},

		_subFilterSerialized: function(res) {
@@ -105,6 +130,32 @@ define([
			this.inherited(arguments);

			this._emitEvt('UPDATE_TARGET', obj);
		},

		_onQueryChannelUpdated: function(obj) {

			console.log('entra', obj)
			this._disconnectFromFilter(obj.oldValue);
			this._connectToFilter(obj.value);
		},

		_disconnectFromFilter: function(oldQueryChannel) {

			this._removeSubscriptions([
				this._buildChannel(oldQueryChannel, this.actions.SERIALIZED),
				this._buildChannel(oldQueryChannel, this.actions.REQUEST_FILTER)
			]);
			this._removePublications([
				this._buildChannel(oldQueryChannel, this.actions.ADD_TO_QUERY),
				this._buildChannel(oldQueryChannel, this.actions.REFRESH),
				this._buildChannel(oldQueryChannel, this.actions.UPDATE_TARGET)
			]);
		},

		_connectToFilter: function(newQueryChannel) {

			this._subscribeToFilter(newQueryChannel);
			this._setPublicationsToFilter(newQueryChannel);
		}
	});
});