1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 18:13:49 +01:00
onion/js/stores/edition_list_store.js

159 lines
6.2 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-26 17:25:03 +02:00
import React from 'react';
import alt from '../alt';
import EditionsListActions from '../actions/edition_list_actions';
class EditionListStore {
constructor() {
2015-05-26 13:14:35 +02:00
this.editionList = {};
this.isEditionListOpenForPieceId = {};
this.bindActions(EditionsListActions);
}
onUpdateEditionList({pieceId, editionListOfPiece, page, pageSize, orderBy, orderAsc, count, filterBy}) {
2015-07-06 15:42:04 +02:00
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-07-06 15:42:04 +02:00
for(let i = 0; i < editionListOfPiece.length; i++) {
2015-07-03 16:31:09 +02:00
2015-07-06 15:42:04 +02:00
// if editionList for a specific piece does not exist yet,
// just initialize a new array
if(!this.editionList[pieceId]) {
this.editionList[pieceId] = [];
}
2015-07-03 16:31:09 +02:00
2015-07-06 15:42:04 +02:00
// this is the index formula for accessing an edition of a specific
// page
let storeEditionIndex = (page - 1) * pageSize + i;
let editionsForPieces = this.editionList[pieceId];
// if edition already exists, just merge
if(editionsForPieces[storeEditionIndex]) {
editionsForPieces[storeEditionIndex] = React.addons.update(editionsForPieces[storeEditionIndex], {$merge: editionListOfPiece[i]});
} else {
// if does not exist, assign
editionsForPieces[storeEditionIndex] = editionListOfPiece[i];
2015-07-03 16:31:09 +02:00
}
}
/**
2015-07-06 17:07:41 +02:00
* page, pageSize, orderBy, orderAsc and count are specific to a single list of editions
* 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-07-03 16:31:09 +02:00
this.editionList[pieceId].page = page;
this.editionList[pieceId].pageSize = pageSize;
this.editionList[pieceId].orderBy = orderBy;
this.editionList[pieceId].orderAsc = orderAsc;
2015-07-06 15:56:14 +02:00
this.editionList[pieceId].count = count;
this.editionList[pieceId].filterBy = filterBy;
2015-05-26 16:48:48 +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
*/
2015-08-07 13:05:50 +02:00
onRefreshEditionList({pieceId, filterBy}) {
// 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
if(!this.editionList[pieceId]) {
return;
}
2015-08-07 13:05:50 +02:00
let prevEditionListLength = this.editionList[pieceId].length;
let prevEditionListPage = this.editionList[pieceId].page;
let prevEditionListPageSize = this.editionList[pieceId].pageSize;
// we can also refresh the edition list using filterBy,
// if we decide not to do that then the old filter will just be applied.
if(filterBy && Object.keys(filterBy).length <= 0) {
filterBy = this.editionList[pieceId].filterBy;
prevEditionListLength = 10;
prevEditionListPage = 1;
prevEditionListPageSize = 10;
}
// to clear an array, david walsh recommends to just set it's length to zero
// http://davidwalsh.name/empty-array
this.editionList[pieceId].length = 0;
// refetch editions with adjusted page size
EditionsListActions.fetchEditionList(pieceId, 1, prevEditionListLength,
2015-08-04 16:18:14 +02:00
this.editionList[pieceId].orderBy,
this.editionList[pieceId].orderAsc,
2015-08-07 13:05:50 +02:00
filterBy)
.then(() => {
// reset back to the normal pageSize and page
this.editionList[pieceId].page = prevEditionListPage;
this.editionList[pieceId].pageSize = prevEditionListPageSize;
})
.catch((err) => {
console.logGlobal(err);
});
}
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-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
});
});
}
onToggleEditionList(pieceId) {
2015-08-07 13:18:33 +02:00
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
2015-08-07 13:18:33 +02:00
if(!this.isEditionListOpenForPieceId[pieceId].show) {
// to clear an array, david walsh recommends to just set it's length to zero
// http://davidwalsh.name/empty-array
this.editionList[pieceId].length = 0;
}
}
onCloseAllEditionLists() {
this.isEditionListOpenForPieceId = {};
}
}
export default alt.createStore(EditionListStore, 'EditionListStore');