Commit 9edb07fd authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Nace SelectionManager a partir de SelectionBox

Toda la lógica de gestión de la selección se abstrae de SelectionBox,
que no es más que un indicador con botones asociados (y así debería
serlo).

Fix #50.
parent 21bb6a3f
Loading
Loading
Loading
Loading
+92 −273
Original line number Diff line number Diff line
define([
	'alertify/alertify.min'
	, "app/designs/textSearchList/main/Selection"
	, "dojo/_base/declare"
	, "dojo/_base/lang"
	, 'dojo/Deferred'
	, "put-selector/put"
	, "redmic/modules/base/_Module"
	, "redmic/modules/base/_Selection"
	, "redmic/modules/base/_Show"
	, "redmic/modules/base/_ShowInPopup"
	, "redmic/modules/base/_ShowInTooltip"
	, "redmic/modules/base/_ShowOnEvt"
	, "redmic/modules/layout/listMenu/ListMenu"
	, "redmic/base/Credentials"
	'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'put-selector/put'
	, 'redmic/modules/base/_Module'
	, 'redmic/modules/base/_Show'
	, 'redmic/modules/base/_Selection'
	, 'redmic/modules/base/_ShowInTooltip'
	, 'redmic/modules/base/_ShowOnEvt'
	, 'redmic/modules/layout/listMenu/ListMenu'
	, 'redmic/modules/selection/SelectionManager'
	, 'redmic/base/Credentials'
], function(
	alertify
	, Selection
	, declare
	declare
	, lang
	, Deferred
	, put
	, _Module
	, _Selection
	, _Show
	, _ShowInPopup
	, _Selection
	, _ShowInTooltip
	, _ShowOnEvt
	, ListMenu
	, SelectionManager
	, Credentials
) {

@@ -34,34 +28,22 @@ define([
		//	summary:
		//		Indicador del número de seleccionados con botones asociados.
		//	description:
		//		Informa del número de elementos seleccionados para un determinado 'target'. Es decir, se encarga
		//		de mostrar los elementos seleccionados de un tipo de dato concreto.

		//	config: Object
		//		Opciones por defecto.
		//		Informa del número de elementos seleccionados para un determinado 'target' (un tipo de datos concreto).

		constructor: function(args) {

			this.config = {
				ownChannel: "selectionBox",
				ownChannel: 'selectionBox',
				events: {
					STORE_SELECTION: 'storeSelection',
					RETRIEVE_SELECTIONS_TARGET: 'retrieveSelectionsTarget',
					CLONE_SELECTION: 'cloneSelection'
					SET_SELECTION_TARGET: 'setSelectionTarget',
					SAVE_SELECTION: 'saveSelection',
					RESTORE_SELECTION: 'restoreSelection'
				},
				actions: {
					REFRESH: "refresh",
					STORE_SELECTION: 'storeSelection',
					SELECTION_STORED: 'selectionStored',
					RETRIEVE_SELECTIONS_TARGET: 'retrieveSelectionsTarget',
					SELECTIONS_TARGET_RETRIEVED: 'selectionsTargetRetrieved',
					CLONE_SELECTION: 'cloneSelection'
					REFRESH: 'refresh'
				},

				idProperty: "id",

				menuInTooltip: true,
				omitLoading: true
				idProperty: 'id'
			};

			lang.mixin(this, this.config, args);
@@ -69,7 +51,6 @@ define([

		_setConfigurations: function() {

			if (this.menuInTooltip) {
			this.listMenuSelectionConfig = this._merge([{
				parentChannel: this.getChannel(),
				items: [{
@@ -92,92 +73,78 @@ define([
				}],
				indicatorLeft: true,
				notIndicator: true,
					classTooltip: "tooltipButtonMenu tooltipButtonChart"
				classTooltip: 'tooltipButtonMenu tooltipButtonChart'
			}, this.listMenuSelectionConfig || {}]);
			}
		},

			this.loadSelectionConfig = this._merge([{
		_initialize: function() {

			var ListMenuDefinition = declare([ListMenu, _ShowOnEvt]).extend(_ShowInTooltip);
			this.loadSelectionListMenu = new ListMenuDefinition(this.listMenuSelectionConfig);

			this._selectionManager = new SelectionManager({
				parentChannel: this.getChannel(),
				title: this.i18n.restoreSelection
			}, this.loadSelectionConfig || {}]);
				idProperty: this.idProperty
			});
		},

		_defineSubscriptions: function () {

			this.subscriptionsConfig.push({
				channel: this.getChannel("REFRESH"),
				callback: "_subRefresh"
			},{
				channel: this._buildChannel(this.selectorChannel, this.actions.SELECTION_STORED),
				callback: '_subSelectionStored'
				channel: this.getChannel('REFRESH'),
				callback: '_subRefresh'
			},{
				channel: this._buildChannel(this.selectorChannel, this.actions.SELECTIONS_TARGET_RETRIEVED),
				callback: '_subSelectionsTargetRetrieved'
			});

			if (this.menuInTooltip) {
				this.subscriptionsConfig.push({
					channel: this.loadSelectionListMenu.getChannel("EVENT_ITEM"),
					callback: "_subLoadSelectionListEventItem"
				channel: this.loadSelectionListMenu.getChannel('EVENT_ITEM'),
				callback: '_subLoadSelectionListEventItem'
			});
			}
		},

		_definePublications: function() {

			this.publicationsConfig.push({
				event: 'STORE_SELECTION',
				channel: this._buildChannel(this.selectorChannel, this.actions.STORE_SELECTION)
				event: 'SET_SELECTION_TARGET',
				channel: this._selectionManager.getChannel('SET_PROPS')
			},{
				event: 'RETRIEVE_SELECTIONS_TARGET',
				channel: this._buildChannel(this.selectorChannel, this.actions.RETRIEVE_SELECTIONS_TARGET)
				event: 'SAVE_SELECTION',
				channel: this._selectionManager.getChannel('SAVE_SELECTION')
			},{
				event: 'CLONE_SELECTION',
				channel: this._buildChannel(this.selectorChannel, this.actions.CLONE_SELECTION)
				event: 'RESTORE_SELECTION',
				channel: this._selectionManager.getChannel('RESTORE_SELECTION')
			});
		},

		_initialize: function() {

			if (this.menuInTooltip) {
				this.loadSelectionListMenu = new declare([ListMenu, _ShowOnEvt])
					.extend(_ShowInTooltip)(this.listMenuSelectionConfig);
			}
		},

		postCreate: function() {

			put(this.domNode, ".selectionBox.form-control");
			put(this.domNode, "span", "Sel:");
			this.selectionCount = put(this.domNode, "span", "0");
			this.buttonsContainer = put(this.domNode, "span.fa.fa-caret-down");
			put(this.domNode, '.selectionBox.form-control');
			put(this.domNode, 'span', 'Sel:');
			this.selectionCount = put(this.domNode, 'span', '0');
			this.buttonsContainer = put(this.domNode, 'span.fa.fa-caret-down');

			if (this.menuInTooltip) {
				this._publish(this.loadSelectionListMenu.getChannel("ADD_EVT"), {
			this._publish(this.loadSelectionListMenu.getChannel('ADD_EVT'), {
				sourceNode: this.domNode
			});
			}

			this.inherited(arguments);
		},

		_subLoadSelectionListEventItem: function(response) {
		_subLoadSelectionListEventItem: function(res) {

			var cbk = response.callback;
			var cbk = res.callback;

			if (cbk && this[cbk]) {
				this[cbk](response);
				this[cbk](res);
			}
		},

		_subRefresh: function(request) {
			//	summary:
			//		Se ejecuta cada vez que se cambia la vista principal para refescar
			//		el contador de selección y el target del servicio.
			//	tags:
			//		private
		_subRefresh: function(req) {

			var selectionTarget = req.selectionTarget;

			this.selectionTarget = request.selectionTarget;
			this.selectionTarget = selectionTarget;

			this._emitEvt('SET_SELECTION_TARGET', {
				selectionTarget: selectionTarget
			});

			this._clearSelection();
			this._emitEvt('GROUP_SELECTED');
@@ -191,10 +158,6 @@ define([
		_select: function(item, total) {

			this._updateSelectionBox(total);

			if (this._loadSelectionDfd) {
				this._loadSelectionDfd.resolve();
			}
		},

		_deselect: function(item, total) {
@@ -205,10 +168,6 @@ define([
		_clearSelection: function() {

			this._updateSelectionBox(0);

			if (this._loadSelectionDfd) {
				this._loadSelectionDfd.resolve();
			}
		},

		_updateSelectionBox: function(total) {
@@ -227,20 +186,19 @@ define([
				info: {
					category: TRACK.category.button,
					action: TRACK.action.click,
					label: "saveSelection"
					label: 'saveSelection'
				}
			});
		},

		_totalAvailable: function(response) {
		_totalAvailable: function(res) {

			var selectionId = Credentials.get("selectIds")[this.selectionTarget],
				obj = {
					selectionId: selectionId
				};
			var selectionId = Credentials.get('selectIds')[this.selectionTarget];

			if (response.total > 0 && selectionId) {
				this._requestNameAndSave(obj);
			if (res.total && selectionId) {
				this._emitEvt('SAVE_SELECTION', {
					selectionId: selectionId
				});
			} else {
				this._emitEvt('COMMUNICATION', {
					description: this.i18n.noItem
@@ -248,72 +206,6 @@ define([
			}
		},

		_requestNameAndSave: function(obj) {

			if (this.idSelectionLoaded) {
				alertify.confirm(this.i18n.saveSelection,
					this.i18n.saveSelectionConfirmationMessage,
					lang.hitch(this, this._updateSelection, obj),
					lang.hitch(this, this._saveSelection, obj))
				.set("labels", {
					ok: this.i18n.overwrite,
					cancel: this.i18n.save
				});
			} else {
				this._saveSelection(obj);
			}
		},

		_updateSelection: function(obj) {

			obj[this.idProperty] = this.idSelectionLoaded[this.idProperty];
			obj.name = this.idSelectionLoaded.name;

			this._storeSelection(obj);
		},

		_saveSelection: function(obj) {

			var prompt = alertify.prompt(this.i18n.newNameMessage, "", lang.hitch(this, function(obj, evt, value) {

				obj.name = value;
				obj.shared = this._sharedCheckbox.checked;

				delete this.idSelectionLoaded;
				this._storeSelection(obj);
			}, obj));

			prompt.setHeader(this.i18n.saveSelection);

			if (!this._sharedCheckbox) {
				var promptContent = prompt.elements.content,
					sharedCheckboxId = this.getOwnChannel() + '-sharedCheckbox';

				this._sharedCheckbox = put(promptContent, 'input[type=checkbox]#' + sharedCheckboxId);
				put(promptContent, 'label[for=' + sharedCheckboxId + ']', this.i18n.shareSelection);
			}
		},

		_storeSelection: function(data) {

			this._emitEvt('STORE_SELECTION', this._getDataToStore(data));
		},

		_getDataToStore: function(data) {

			return {
				target: this.selectionTarget,
				data: data
			};
		},

		_subSelectionStored: function(res) {

			if (res.data) {
				this.idSelectionLoaded = res.data;
			}
		},

		_loadSavedSelectionsButtonCallback: function() {

			this._emitEvt('TRACK', {
@@ -321,34 +213,11 @@ define([
				info: {
					category: TRACK.category.button,
					action: TRACK.action.click,
					label: "loadSelection"
				}
			});

			if (this.idSelectionLoaded) {
				alertify.confirm(this.i18n.saveSelection,
					this.i18n.loseSelectionConfirmationMessage,
					lang.hitch(this, function() {

						delete this.idSelectionLoaded;
						this._showSelectionList();
					}),function(){})
				.set("labels", {
					ok: this.i18n.ok,
					cancel: this.i18n.cancel
				});
			} else {
				this._showSelectionList();
					label: 'loadSelection'
				}
		},

		_showSelectionList: function() {

			this._emitEvt('RETRIEVE_SELECTIONS_TARGET', {
				target: this.selectionTarget
			});

			this._publish(this.loadSelection.getChannel("SHOW"));
			this._emitEvt('RESTORE_SELECTION');
		},

		_clearSelectionButtonCallback: function() {
@@ -360,59 +229,9 @@ define([
				info: {
					category: TRACK.category.button,
					action: TRACK.action.click,
					label: "clearSelection"
					label: 'clearSelection'
				}
			});
		},

		_subSelectionsTargetRetrieved: function(res) {

			var selectionTarget = res.target;

			if (!this.loadSelection) {
				this.loadSelectionConfig.target = selectionTarget;
				this.loadSelection = new declare(Selection).extend(_ShowInPopup)(this.loadSelectionConfig);

				this._subscribe(this.loadSelection.getChannel("UPDATE_DATA"), lang.hitch(this, this._subSelectionLoad));
			} else {
				this._publish(this.loadSelection.getChannel("UPDATE_TARGET"), {
					target: selectionTarget
				});
			}
		},

		_subSelectionLoad: function(res) {

			this._publish(this.loadSelection.getChannel("HIDE"));

			if (this._loadSelectionDfd && !this._loadSelectionDfd.isFulfilled()) {
				this._loadSelectionDfd.cancel();
			}
			this._loadSelectionDfd = new Deferred();

			this._loadSelectionDfd.then(lang.hitch(this, this._continueSelectionLoadAfterClear, res));

			var isOldFormat = res.data && res.data.ids;
			this._emitEvt('CLEAR_SELECTION', {
				omitPersistence: !isOldFormat
			});
		},

		_continueSelectionLoadAfterClear: function(res) {

			var data = res.data,
				settingsId = data.id,
				selection = data && data.ids;

			if (selection) {
				this._emitEvt('SELECT', selection);
				this.idSelectionLoaded = data;
			} else {
				this._emitEvt('CLONE_SELECTION', {
					target: this.selectionTarget,
					id: settingsId
				});
			}
		}
	});
});
+271 −0
Original line number Diff line number Diff line
define([
	'alertify/alertify.min'
	, 'app/designs/textSearchList/main/Selection'
	, 'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'dojo/Deferred'
	, 'put-selector/put'
	, 'redmic/modules/base/_Module'
	, 'redmic/modules/base/_Selection'
	, 'redmic/modules/base/_ShowInPopup'
], function(
	alertify
	, Selection
	, declare
	, lang
	, Deferred
	, put
	, _Module
	, _Selection
	, _ShowInPopup
) {

	return declare([_Module, _Selection], {
		//	summary:
		//		Gestor de selecciones persistentes.
		//	description:
		//		Permite al usuario almacenar su selección en privado, compartirla y también restaurarla posteriormente.

		constructor: function(args) {

			this.config = {
				ownChannel: 'selectionManager',
				events: {
					STORE_SELECTION: 'storeSelection',
					RETRIEVE_SELECTIONS_TARGET: 'retrieveSelectionsTarget',
					CLONE_SELECTION: 'cloneSelection'
				},
				actions: {
					SAVE_SELECTION: 'saveSelection',
					RESTORE_SELECTION: 'restoreSelection',
					STORE_SELECTION: 'storeSelection',
					SELECTION_STORED: 'selectionStored',
					RETRIEVE_SELECTIONS_TARGET: 'retrieveSelectionsTarget',
					SELECTIONS_TARGET_RETRIEVED: 'selectionsTargetRetrieved',
					CLONE_SELECTION: 'cloneSelection'
				},

				idProperty: 'id'
			};

			lang.mixin(this, this.config, args);
		},

		_setConfigurations: function() {

			this.loadSelectionConfig = this._merge([{
				parentChannel: this.getChannel(),
				title: this.i18n.restoreSelection
			}, this.loadSelectionConfig || {}]);
		},

		_defineSubscriptions: function () {

			this.subscriptionsConfig.push({
				channel: this.getChannel('SAVE_SELECTION'),
				callback: '_subSaveSelection'
			},{
				channel: this.getChannel('RESTORE_SELECTION'),
				callback: '_subRestoreSelection'
			},{
				channel: this._buildChannel(this.selectorChannel, this.actions.SELECTION_STORED),
				callback: '_subSelectionStored'
			},{
				channel: this._buildChannel(this.selectorChannel, this.actions.SELECTIONS_TARGET_RETRIEVED),
				callback: '_subSelectionsTargetRetrieved'
			});
		},

		_definePublications: function() {

			this.publicationsConfig.push({
				event: 'STORE_SELECTION',
				channel: this._buildChannel(this.selectorChannel, this.actions.STORE_SELECTION)
			},{
				event: 'RETRIEVE_SELECTIONS_TARGET',
				channel: this._buildChannel(this.selectorChannel, this.actions.RETRIEVE_SELECTIONS_TARGET)
			},{
				event: 'CLONE_SELECTION',
				channel: this._buildChannel(this.selectorChannel, this.actions.CLONE_SELECTION)
			});
		},

		_subSaveSelection: function(req) {

			var selectionId = req.selectionId,
				saveCbk = lang.hitch(this, this._saveSelection, selectionId),
				updateCbk = lang.hitch(this, this._updateSelection, selectionId);

			if (!this.idSelectionLoaded) {
				saveCbk();
				return;
			}

			alertify.confirm(
				this.i18n.saveSelection,
				this.i18n.saveSelectionConfirmationMessage,
				updateCbk,
				saveCbk
			).set('labels', {
				ok: this.i18n.overwrite,
				cancel: this.i18n.save
			});
		},

		_updateSelection: function(selectionId) {

			var obj = {
				selectionId: selectionId,
				name: this.idSelectionLoaded.name
			};

			obj[this.idProperty] = this.idSelectionLoaded[this.idProperty];

			this._storeSelection(obj);
		},

		_saveSelection: function(selectionId) {

			var promptCbk = lang.hitch(this, this._getSaveParametersAndStore, selectionId),
				prompt = alertify.prompt(this.i18n.newNameMessage, '', promptCbk);

			prompt.setHeader(this.i18n.saveSelection);
			this._addSharedCheckbox(prompt);
		},

		_getSaveParametersAndStore: function(selectionId, evt, value) {

			var obj = {
				selectionId: selectionId,
				name: value,
				shared: this._sharedCheckbox.checked
			};

			delete this.idSelectionLoaded;
			this._storeSelection(obj);
		},

		_addSharedCheckbox: function(prompt) {

			if (this._sharedCheckbox) {
				return;
			}

			var promptContent = prompt.elements.content,
				sharedCheckboxId = this.getOwnChannel() + '-sharedCheckbox';

			this._sharedCheckbox = put(promptContent, 'input[type=checkbox]#' + sharedCheckboxId);
			put(promptContent, 'label[for=' + sharedCheckboxId + ']', this.i18n.shareSelection);
		},

		_storeSelection: function(data) {

			this._emitEvt('STORE_SELECTION', this._getDataToStore(data));
		},

		_getDataToStore: function(data) {

			return {
				target: this.selectionTarget,
				data: data
			};
		},

		_subSelectionStored: function(res) {

			if (res.data) {
				this.idSelectionLoaded = res.data;
			}
		},

		_subRestoreSelection: function() {

			if (!this.idSelectionLoaded) {
				this._showSelectionList();
				return;
			}

			var overwriteCbk = lang.hitch(this, function() {

				delete this.idSelectionLoaded;
				this._showSelectionList();
			});

			alertify.confirm(
				this.i18n.saveSelection,
				this.i18n.loseSelectionConfirmationMessage,
				overwriteCbk,
				function() {}
			).set('labels', {
				ok: this.i18n.ok,
				cancel: this.i18n.cancel
			});
		},

		_showSelectionList: function() {

			this._emitEvt('RETRIEVE_SELECTIONS_TARGET', {
				target: this.selectionTarget
			});

			this._publish(this.loadSelection.getChannel("SHOW"));
		},

		_subSelectionsTargetRetrieved: function(res) {

			var selectionTarget = res.target;

			if (!this.loadSelection) {
				this.loadSelectionConfig.target = selectionTarget;
				this.loadSelection = new declare(Selection).extend(_ShowInPopup)(this.loadSelectionConfig);

				this._subscribe(this.loadSelection.getChannel("UPDATE_DATA"), lang.hitch(this, this._subSelectionLoad));
			} else {
				this._publish(this.loadSelection.getChannel("UPDATE_TARGET"), {
					target: selectionTarget
				});
			}
		},

		_subSelectionLoad: function(res) {

			this._publish(this.loadSelection.getChannel("HIDE"));

			if (this._loadSelectionDfd && !this._loadSelectionDfd.isFulfilled()) {
				this._loadSelectionDfd.cancel();
			}
			this._loadSelectionDfd = new Deferred();

			this._loadSelectionDfd.then(lang.hitch(this, this._continueSelectionLoadAfterClear, res));

			var isOldFormat = res.data && res.data.ids;
			this._emitEvt('CLEAR_SELECTION', {
				omitPersistence: !isOldFormat
			});
		},

		_clearSelection: function() {

			if (this._loadSelectionDfd) {
				this._loadSelectionDfd.resolve();
			}
		},

		_continueSelectionLoadAfterClear: function(res) {

			var data = res.data,
				settingsId = data.id,
				selection = data && data.ids;

			if (selection) {
				this._emitEvt('SELECT', selection);
				this.idSelectionLoaded = data;
			} else {
				this._emitEvt('CLONE_SELECTION', {
					target: this.selectionTarget,
					id: settingsId
				});
			}
		}
	});
});