Commit 3c07cfd5 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Implementa selección con settings, corrige Atlas

El módulo de selección ahora controla también el nuevo formato a través
de settings de usuario, y sigue siendo compatible con el antiguo. Adapta
mayormente su capa de persistencia, el resto permanece casi igual.

Atlas incorpora el nuevo target de selección, y vuelve a ser capaz de
mostrar en el mapa las capas seleccionadas.
parent 78168109
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -279,6 +279,7 @@ define([], function() {

		'atlasLayer': baseUri + 'atlas/view/layer',
		'atlasLayerEdition': baseUri + 'atlas/commands/layer',
		'atlasLayerSelection': baseUri + 'atlas/{endpoint}/layer/settings',
		'atlasLayerRefresh': baseUri + 'atlas/commands/layer/refresh/{id}',
		'atlasLayerDiscovery': baseUri + 'atlas/commands/discover-layers',
		'atlasCategory': baseUri + 'atlas/view/category',
+4 −3
Original line number Diff line number Diff line
@@ -81,7 +81,8 @@ define([
				_itemsSelected: {},
				localTarget: "local",
				// TODO este target no hace falta, pero si uno de selección común a categorias y capas
				//target: [redmicConfig.services.atlasLayer, redmicConfig.services.atlasCategory],
				target: [redmicConfig.services.atlasLayer, redmicConfig.services.atlasCategory],
				selectionTarget: redmicConfig.services.atlasLayerSelection,
				pathSeparator: ".",
				pathProperty: "path",
				layerIdSeparator: "_",
@@ -161,7 +162,7 @@ define([
			this.catalogConfig = this._merge([{
				parentChannel: this.getChannel(),
				browserExts: [_HierarchicalSelect],
				//target: this.target,
				selectionTarget: this.selectionTarget,
				perms: this.perms,
				buttonsInTopZone: true,
				buttons: {
@@ -360,7 +361,7 @@ define([
			this._itemsSelected[id] = path;

			this._emitEvt('GET', {
				target: this.target,
				target: this.target[0],
				requesterId: this.getOwnChannel(),
				id: id
			});
+22 −37
Original line number Diff line number Diff line
@@ -24,20 +24,12 @@ define([
		//	summary:
		//		Módulo de selección global y persistente.
		//	description:
		//		Proporciona métodos manejar seleccionar / deseleccionar los items de la base de datos
		//		Proporciona métodos para seleccionar/deseleccionar los items de cada servicio.

		constructor: function(args) {

			this.config = {
				// own events
				events: {},
				// own actions
				actions: {},

				// mediator params
				ownChannel: "selection",

				// Items selected structure
				selections: {}
			};

@@ -90,10 +82,10 @@ define([
			});
		},

		_subSelect: function(request) {
		_subSelect: function(req) {

			var items = request.items,
				target = request.target;
			var items = req.items,
				target = req.target;

			this._emitSelectionTargetLoading(target);
			this._initializeSelection(target);
@@ -103,10 +95,8 @@ define([
			}

			if (target.indexOf('{apiUrl}') !== -1) {
				// Seleccionamos a la vuelta del server
				this._emitSave(this._getDataToSave(this.actions.SELECT, items, target));
				this._emitSave(this._getDataToSave('SELECT', req));
			} else {
				//Selección local
				this._select(items, target);
			}
		},
@@ -128,7 +118,6 @@ define([

		_selectItem: function(id, target, selectionDfd) {

			// Si seleccionamos un elemento ya englobado en la selección
			if (this._isSelected(id, target)) {
				selectionDfd.resolve();
				return;
@@ -162,9 +151,10 @@ define([
				return o;
			}, {});

			this._initializeSelection(target);
			this.selections[target].items = obj;
			this.selections[target].total = ids.length;
			this.selections[target] = {
				items: obj,
				total: ids.length
			};

			this._emitEvt('SELECT', {
				ids: ids,
@@ -173,10 +163,10 @@ define([
			});
		},

		_subDeselect: function(request) {
		_subDeselect: function(req) {

			var items = request.items,
				target = request.target;
			var items = req.items,
				target = req.target;

			this._emitSelectionTargetLoading(target);
			this._initializeSelection(target);
@@ -186,10 +176,8 @@ define([
			}

			if (target.indexOf('{apiUrl}') !== -1) {
				// Deseleccionamos a la vuelta del server
				this._emitSave(this._getDataToSave(this.actions.DESELECT, items, target));
				this._emitSave(this._getDataToSave('DESELECT', req));
			} else {
				//Deselección local
				this._deselect(items, target);
			}
		},
@@ -211,7 +199,6 @@ define([

		_deselectItem: function(id, target, deselectionDfd) {

			// Si deseleccionamos un elemento no seleccionado
			if (!this._isSelected(id, target)) {
				deselectionDfd.resolve();
				return;
@@ -251,21 +238,21 @@ define([
			return this.selections && this.selections[target] ? this.selections[target] : {};
		},

		_subGroupSelected: function(request) {
		_subGroupSelected: function(req) {

			this._emitSelectionTargetLoading(request.target);
			this._groupSelected(request);
			this._emitSelectionTargetLoading(req.target);
			this._groupSelected(req);
		},

		_subClearSelection: function(request) {
		_subClearSelection: function(req) {

			this._clearSelectionSave(request);
			this._clearSelectionSave(req);
		},

		_clearSelectionSave: function(request) {
		_clearSelectionSave: function(req) {

			this._emitSelectionTargetLoading(request.target);
			this._emitSave(this._getDataToSave(this.actions.CLEAR_SELECTION, null, request.target));
			this._emitSelectionTargetLoading(req.target);
			this._emitSave(this._getDataToSave('CLEAR_SELECTION', req));
		},

		_clearSelection: function(target) {
@@ -309,8 +296,6 @@ define([

		_selectById: function(itemPath, target) {

			this._initializeSelection(target);

			if (!this._getItems(target)[itemPath]) {
				this.selections[target].items[itemPath] = true;
				this.selections[target].total++;
+194 −41
Original line number Diff line number Diff line
@@ -16,23 +16,74 @@ define([
		//	summary:
		//		Lógica necesaria para hacer persistente la selección de elementos (usando un servicio remoto).

		target: [],
		selectionTargetSuffix: '/_selection',

		selectionTargetSuffixesByAction: {
			select: 'select',
			deselect: 'deselect',
			clearSelection: 'clearselection'
		},

		selectionEndpointsByAction: {
			select: 'commands',
			deselect: 'commands',
			clearSelection: 'commands',
			groupSelected: 'view'
		},

		endpointVariableName: '{endpoint}',

		omitRefreshAfterSuccess: true,

		target: [],


		_getSelectionTarget: function(action, target) {

			var selectionEndpoint = this.selectionEndpointsByAction[action];

			if (!selectionEndpoint) {
				console.error('Selection endpoint not found for action "%s"', action);
				return target;
			}

			var targetWithEndpointReplaced = target.replace(this.endpointVariableName, selectionEndpoint),
				selectionTarget;

			if (this._isSettingsSelectionFormat(target)) {
				selectionTarget = targetWithEndpointReplaced;
			} else {
				selectionTarget = targetWithEndpointReplaced + this.selectionTargetSuffix;
			}

			this._addSelectionTarget(selectionTarget);
			return selectionTarget;
		},

		_recoverSelectionTarget: function(target) {

			var replacement = '/' + this.endpointVariableName + '/';

			return target.replace('/commands/', replacement).replace('/view/', replacement);
		},

		_isSettingsSelectionFormat: function(target) {

			return target.indexOf(this.endpointVariableName) !== -1;
		},

		_groupSelected: function(req) {

			var target = req.target,
				selectIds = this._getSelectionIds(),
				selectionId = target && selectIds && selectIds[target];
				selectionIds = this._getSelectionIds(),
				selectionId = selectionIds[target];

			this._initializeSelection(target);

			if (selectionId) {
				this._emitEvt('GET', {
					target: this._getTarget(target),
					target: this._getSelectionTarget(this.actions.GROUP_SELECTED, target),
					id: selectionId,
					options: {},
					requesterId: this.getOwnChannel()
				});
			} else {
@@ -48,10 +99,28 @@ define([

		_itemAvailable: function(res, resWrapper) {

			var targetBase = this._getTargetBase(resWrapper.target);
			var receivedTarget = resWrapper.target,
				data = res.data,
				selectionTarget = this._recoverSelectionTarget(receivedTarget),
				selectedIds;

			this._removeSelectionTarget(receivedTarget);

			if (!data) {
				return;
			}

			if (this._isSettingsSelectionFormat(selectionTarget)) {
				selectedIds = data.selection;
			} else {
				selectionTarget = this._getTargetWithoutSelectionSuffix(selectionTarget);
				selectedIds = data.ids;
			}

			var selection = this.selections[selectionTarget];

			if (this.selections[targetBase] && res.data && res.data.ids) {
				this._selectedAll(res.data.ids, targetBase);
			if (selection && selectedIds) {
				this._selectedAll(selectedIds, selectionTarget);
			}
		},

@@ -60,40 +129,124 @@ define([
			this._emitEvt('SAVE', obj);
		},

		_getDataToSave: function(action, ids, target) {
		_getDataToSave: function(actionName, req) {

			var action = this.actions[actionName],
				selectionIds = this._getSelectionIds(),
				selectionId = selectionIds[req.target],
				dataToSave = this._getDataToSaveInRightFormat(action, req);

			dataToSave.omitSuccessNotification = true;
			if (selectionId) {
				dataToSave.data.id = selectionId;
			}

			return dataToSave;
		},

		_getDataToSaveInRightFormat: function(action, req) {

			if (this._isSettingsSelectionFormat(req.target)) {
				return this._createDataToSaveInSettingsFormat(action, req);
			}

			return this._createDataToSaveInOldFormat(action, req);
		},

		_createDataToSaveInSettingsFormat: function(action, req) {

			var targetWithSuffix = req.target + '/' + this._getTargetSuffix(action),
				selectionTarget = this._getSelectionTarget(action, targetWithSuffix);

			return {
				data: {
					selection: req.items,
					userId: this.userSelectionId
				},
				target: selectionTarget
			};
		},

		_getTargetSuffix: function(action) {

			var suffix = this.selectionTargetSuffixesByAction[action];

			if (!suffix) {
				console.error('Selection target suffix not found for action "%s"', action);
			}

			return suffix;
		},

		_createDataToSaveInOldFormat: function(action, req) {

			var selectionTarget = req.target + this.selectionTargetSuffix;

			var obj = {
			return {
				data: {
					ids: ids,
					ids: req.items,
					action: action,
					idUser: this.userSelectionId
				},
				target: target + this.selectionTargetSuffix,
				omitSuccessNotification: true
				target: selectionTarget
			};
		},

		_afterSaved: function(res, resWrapper) {

			var receivedTarget = resWrapper.target,
				selectionTarget = this._recoverSelectionTarget(receivedTarget);

			this._removeSelectionTarget(receivedTarget);
			resWrapper.target = selectionTarget;

			if (this._isSettingsSelectionFormat(receivedTarget)) {
				this._afterSavedInSettingsFormat(res, resWrapper);
			} else {
				this._afterSavedInOldFormat(res, resWrapper);
			}
		},

		_afterSavedInSettingsFormat: function(res, resWrapper) {

			var selectIds = this._getSelectionIds();
			var data = res.data,
				selectedIds = data.selection,
				settingsId = data.id,
				resTarget = resWrapper.target,
				resTargetSplitted = resTarget.split('/'),
				suffix = resTargetSplitted.pop(),
				target = resTargetSplitted.join('/'),
				selectionIds = this._getSelectionIds();

			selectionIds[target] = settingsId;
			this._setSelectionIds(selectionIds);

			if (selectIds && selectIds[target]) {
				obj.data.id = selectIds[target];
			for (var action in this.selectionTargetSuffixesByAction) {
				var suffixByAction = this.selectionTargetSuffixesByAction[action];
				if (suffixByAction !== suffix) {
					continue;
				}

			return obj;
				if (action === this.actions.SELECT) {
					this._select(selectedIds, target);
				} else if (action === this.actions.DESELECT) {
					var deselectedIds = resWrapper.req.data.selection;
					this._deselect(deselectedIds, target);
				} else if (action === this.actions.CLEAR_SELECTION) {
					this._clearSelection(target);
				}
			}
		},

		_afterSaved: function(response, resWrapper) {
		_afterSavedInOldFormat: function(res, resWrapper) {

			var data = response.data,
			var data = res.data,
				action = data.action,
				target = this._getTargetBase(resWrapper.target),
				target = this._getTargetWithoutSelectionSuffix(resWrapper.target),
				selectionId = data.id,
				selectedIds = data.ids,
				selectionIds = this._getSelectionIds();

			if (!selectionIds) {
				selectionIds = {};
			}

			selectionIds[target] = selectionId;
			this._setSelectionIds(selectionIds);

@@ -111,40 +264,40 @@ define([
			console.error('Selection persistence error:', error);
		},

		_getTarget: function(target) {
		_addSelectionTarget: function(target) {

			var currentTarget = target + this.selectionTargetSuffix;
			if (this.target.indexOf(currentTarget) === -1) {
				this.target.push(currentTarget);
			if (this.target.indexOf(target) === -1) {
				this.target.push(target);
			}

			return currentTarget;
		},

		_getTargetBase: function(target) {
		_removeSelectionTarget: function(target) {

			return target.replace(this.selectionTargetSuffix, "");
			var index = this.target.indexOf(target);
			if (index !== -1) {
				this.target.splice(index, 1);
			}
		},

		_errorAvailable: function(error, status, resWrapper) {
		_getTargetWithoutSelectionSuffix: function(target) {

			var selectIds = this._getSelectionIds(),
				target = this._getTargetBase(this._cleanTrailingSlash(resWrapper.target));
			return this._cleanTrailingSlash(target).replace(this.selectionTargetSuffix, "");
		},

			if (!selectIds) {
				selectIds = {};
			}
		_errorAvailable: function(error, status, resWrapper) {

			delete selectIds[target];
			var selectionIds = this._getSelectionIds(),
				target = this._getTargetWithoutSelectionSuffix(resWrapper.target);

			this._setSelectionIds(selectIds);
			delete selectionIds[target];
			this._setSelectionIds(selectionIds);

			this._emitSelectionTargetLoaded(target);
		},

		_getSelectionIds: function() {

			return Credentials.get("selectIds");
			return Credentials.get("selectIds") || {};
		},

		_setSelectionIds: function(selectionIds) {