2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-26 17:25:03 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2015-05-21 17:59:38 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-18 10:17:49 +01:00
|
|
|
onUpdateEditionList({ pieceId, editionListOfPiece, page, pageSize, orderBy, orderAsc, count, filterBy, maxEdition }) {
|
2015-12-08 13:41:15 +01:00
|
|
|
// if editionList for a specific piece does not exist yet,
|
|
|
|
// just initialize a new array
|
|
|
|
const pieceEditionList = this.editionList[pieceId] || [];
|
|
|
|
|
2015-07-06 17:07:41 +02:00
|
|
|
/*
|
|
|
|
Basically there are two modes an edition list can be updated.
|
2015-06-12 15:00:26 +02:00
|
|
|
|
2015-07-06 17:07:41 +02:00
|
|
|
1. The elements that have been requested from the server are not yet defined in the store => just assign them
|
|
|
|
2. The elements are already defined => merge current objects with the new ones from the server
|
|
|
|
|
|
|
|
*/
|
2015-12-08 13:41:15 +01:00
|
|
|
editionListOfPiece.forEach((updatedEdition, index) => {
|
|
|
|
// this is the index formula for accessing an edition starting from a specific page
|
|
|
|
const storeEditionIndex = (page - 1) * pageSize + index;
|
2016-01-13 17:14:09 +01:00
|
|
|
|
2015-07-06 15:42:04 +02:00
|
|
|
// if edition already exists, just merge
|
2016-01-18 10:17:49 +01:00
|
|
|
if (pieceEditionList[storeEditionIndex]) {
|
|
|
|
pieceEditionList[storeEditionIndex] = React.addons.update(pieceEditionList[storeEditionIndex], { $merge: updatedEdition });
|
2015-07-06 15:42:04 +02:00
|
|
|
} else {
|
|
|
|
// if does not exist, assign
|
2015-12-08 13:41:15 +01:00
|
|
|
pieceEditionList[storeEditionIndex] = updatedEdition;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove editions after specified max by finding the index of the first
|
|
|
|
// edition larger than the max edition and using that to cut off the rest of the list
|
|
|
|
if (typeof maxEdition === 'number') {
|
|
|
|
const largerThanMaxIndex = pieceEditionList.findIndex(edition => edition.edition_number > maxEdition);
|
|
|
|
|
|
|
|
if (largerThanMaxIndex !== -1) {
|
|
|
|
// The API defines inflexible page buckets based on the page number
|
|
|
|
// and page size, so we cannot just arbitrarily cut off the end of
|
|
|
|
// a page and expect get the rest of it on the next pagination request.
|
|
|
|
// Hence, we use the max edition index as a guide for which page to
|
|
|
|
// cut off to so as to always provide complete pages.
|
|
|
|
page = Math.ceil(largerThanMaxIndex / pageSize);
|
|
|
|
|
|
|
|
// We only want to cut off the list if there are more editions than
|
|
|
|
// there should be (ie. we're not already at the end of the editions)
|
|
|
|
const totalPageSize = page * pageSize;
|
|
|
|
if (pieceEditionList.length > totalPageSize) {
|
|
|
|
pieceEditionList.length = totalPageSize;
|
|
|
|
}
|
2015-07-03 16:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 16:41:14 +02:00
|
|
|
|
2015-12-08 13:41:15 +01:00
|
|
|
const lastEdition = pieceEditionList[pieceEditionList.length - 1];
|
|
|
|
|
2015-06-04 16:41:14 +02:00
|
|
|
/**
|
2015-07-06 17:07:41 +02:00
|
|
|
* page, pageSize, orderBy, orderAsc and count 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.
|
|
|
|
*
|
2015-07-06 17:07:41 +02:00
|
|
|
* Default values for both are set in the editon_list_actions.
|
2015-06-04 16:41:14 +02:00
|
|
|
*/
|
2015-12-08 13:41:15 +01:00
|
|
|
pieceEditionList.page = page;
|
|
|
|
pieceEditionList.pageSize = pageSize;
|
|
|
|
pieceEditionList.orderBy = orderBy;
|
|
|
|
pieceEditionList.orderAsc = orderAsc;
|
|
|
|
pieceEditionList.count = count;
|
|
|
|
pieceEditionList.filterBy = filterBy;
|
|
|
|
|
|
|
|
if (pieceEditionList.maxSeen == null || lastEdition.edition_number > pieceEditionList.maxSeen) {
|
|
|
|
pieceEditionList.maxSeen = lastEdition.edition_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.editionList[pieceId] = pieceEditionList;
|
2015-05-26 16:48:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 14:45:33 +02:00
|
|
|
/**
|
|
|
|
* We often just have to refresh the edition list for a certain pieceId,
|
|
|
|
* this method provides exactly that functionality without any side effects
|
|
|
|
*/
|
2016-01-18 10:17:49 +01:00
|
|
|
onRefreshEditionList({ pieceId, filterBy }) {
|
2015-11-30 11:32:48 +01:00
|
|
|
const pieceEditionList = this.editionList[pieceId];
|
|
|
|
|
2015-07-16 11:50:52 +02:00
|
|
|
// It may happen that the user enters the site logged in already
|
|
|
|
// through /editions
|
|
|
|
// If he then tries to delete a piece/edition and this method is called,
|
|
|
|
// we'll not be able to refresh his edition list since its not yet there.
|
|
|
|
// Therefore we can just return, since there is no data to be refreshed
|
2016-01-18 10:17:49 +01:00
|
|
|
if (!this.editionList[pieceId]) {
|
2015-07-16 11:50:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-08 13:44:10 +01:00
|
|
|
if (typeof filterBy !== 'object') {
|
|
|
|
filterBy = pieceEditionList.filterBy;
|
2015-12-03 17:43:15 +01:00
|
|
|
}
|
|
|
|
|
2015-12-08 13:44:10 +01:00
|
|
|
const { maxSeen, orderAsc, orderBy, pageSize } = pieceEditionList;
|
|
|
|
|
2015-07-14 14:45:33 +02:00
|
|
|
// to clear an array, david walsh recommends to just set it's length to zero
|
|
|
|
// http://davidwalsh.name/empty-array
|
2015-11-30 11:32:48 +01:00
|
|
|
pieceEditionList.length = 0;
|
2015-12-03 17:43:15 +01:00
|
|
|
|
2015-12-08 14:55:04 +01:00
|
|
|
EditionsListActions
|
|
|
|
.fetchEditionList({
|
|
|
|
pieceId,
|
2016-01-18 10:40:13 +01:00
|
|
|
pageSize,
|
2016-01-18 10:53:01 +01:00
|
|
|
orderBy,
|
|
|
|
orderAsc,
|
|
|
|
filterBy,
|
|
|
|
maxSeen,
|
2016-01-18 10:40:13 +01:00
|
|
|
page: 1
|
2015-12-03 17:43:15 +01:00
|
|
|
})
|
2015-11-30 11:32:48 +01:00
|
|
|
.catch(console.logGlobal);
|
2015-07-14 14:45:33 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 17:14:09 +01: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
|
2016-01-18 10:17:49 +01:00
|
|
|
if (typeof toValue !== 'undefined' && edition.id === editionId) {
|
2015-06-08 14:13:35 +02:00
|
|
|
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
|
|
|
|
};
|
2015-08-07 13:18:33 +02:00
|
|
|
|
2015-08-07 13:20:31 +02:00
|
|
|
// When loading all editions of a piece, closing the table and then applying the filter
|
|
|
|
// the merge fails, as the edition list is not refreshed when closed.
|
|
|
|
// Therefore in the case of a filter application when closed, we need to reload the
|
|
|
|
// edition list
|
2016-01-18 10:17:49 +01:00
|
|
|
if (!this.isEditionListOpenForPieceId[pieceId].show) {
|
2015-08-07 13:18:33 +02:00
|
|
|
// to clear an array, david walsh recommends to just set it's length to zero
|
|
|
|
// http://davidwalsh.name/empty-array
|
2016-01-13 17:14:09 +01:00
|
|
|
|
2015-08-07 13:18:33 +02:00
|
|
|
this.editionList[pieceId].length = 0;
|
|
|
|
}
|
2015-06-15 09:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|