2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-26 17:25:03 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-05-21 17:59:38 +02:00
|
|
|
import alt from '../alt';
|
|
|
|
import EditionsListActions from '../actions/edition_list_actions';
|
|
|
|
|
|
|
|
class EditionListStore {
|
|
|
|
constructor() {
|
2015-05-26 13:14:35 +02:00
|
|
|
this.editionList = {};
|
2015-06-15 09:29:34 +02:00
|
|
|
this.isEditionListOpenForPieceId = {};
|
2015-05-21 17:59:38 +02:00
|
|
|
this.bindActions(EditionsListActions);
|
|
|
|
}
|
|
|
|
|
2015-06-04 09:16:30 +02:00
|
|
|
onUpdateEditionList({pieceId, editionListOfPiece, orderBy, orderAsc}) {
|
2015-05-26 17:25:03 +02:00
|
|
|
if(this.editionList[pieceId]) {
|
|
|
|
this.editionList[pieceId].forEach((edition, i) => {
|
|
|
|
// This uses the index of the new editionList for determining the edition.
|
|
|
|
// If the list of editions can be sorted in the future, this needs to be changed!
|
2015-06-12 15:00:26 +02:00
|
|
|
if (editionListOfPiece[i]) {
|
2015-06-11 15:03:55 +02:00
|
|
|
editionListOfPiece[i] = React.addons.update(edition, {$merge: editionListOfPiece[i]});
|
|
|
|
}
|
2015-06-05 11:06:36 +02:00
|
|
|
});
|
2015-05-26 17:25:03 +02:00
|
|
|
}
|
2015-06-12 15:00:26 +02:00
|
|
|
|
2015-05-26 16:48:48 +02:00
|
|
|
this.editionList[pieceId] = editionListOfPiece;
|
2015-06-04 16:41:14 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-15 09:29:34 +02:00
|
|
|
* orderBy and orderAsc are specific to a single list of editions
|
2015-06-04 16:41:14 +02:00
|
|
|
* therefore they need to be saved in relation to their parent-piece.
|
|
|
|
*
|
|
|
|
* Default values for both are set in the editon_list-actions.
|
|
|
|
*/
|
|
|
|
this.editionList[pieceId].orderBy = orderBy;
|
2015-06-05 11:06:36 +02:00
|
|
|
this.editionList[pieceId].orderAsc = orderAsc;
|
2015-05-26 16:48:48 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 14:13:35 +02:00
|
|
|
onSelectEdition({pieceId, editionId, toValue}) {
|
2015-05-26 16:48:48 +02:00
|
|
|
this.editionList[pieceId].forEach((edition) => {
|
2015-06-08 14:13:35 +02:00
|
|
|
|
2015-06-12 15:00:26 +02:00
|
|
|
// Taken from: http://stackoverflow.com/a/519157/1263876
|
2015-06-08 14:13:35 +02:00
|
|
|
if(typeof toValue !== 'undefined' && edition.id === editionId) {
|
|
|
|
edition.selected = toValue;
|
|
|
|
} else if(edition.id === editionId) {
|
2015-05-26 16:48:48 +02:00
|
|
|
if(edition.selected) {
|
|
|
|
edition.selected = false;
|
|
|
|
} else {
|
|
|
|
edition.selected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-05-21 17:59:38 +02:00
|
|
|
}
|
2015-06-01 15:12:31 +02:00
|
|
|
|
|
|
|
onClearAllEditionSelections() {
|
|
|
|
Object
|
|
|
|
.keys(this.editionList)
|
|
|
|
.forEach((pieceId) => {
|
|
|
|
this.editionList[pieceId]
|
|
|
|
.forEach((edition) => {
|
|
|
|
try {
|
|
|
|
delete edition.selected;
|
2015-06-12 15:00:26 +02:00
|
|
|
} catch(err) {/* ignore and keep going */}
|
2015-06-01 15:12:31 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-06-15 09:29:34 +02:00
|
|
|
|
|
|
|
onToggleEditionList(pieceId) {
|
|
|
|
this.isEditionListOpenForPieceId[pieceId] = {
|
|
|
|
show: this.isEditionListOpenForPieceId[pieceId] ? !this.isEditionListOpenForPieceId[pieceId].show : true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onCloseAllEditionLists() {
|
|
|
|
this.isEditionListOpenForPieceId = {};
|
|
|
|
}
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-21 17:59:38 +02:00
|
|
|
|
2015-06-15 09:59:53 +02:00
|
|
|
export default alt.createStore(EditionListStore, 'EditionListStore');
|