1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-16 01:55:07 +01:00
onion/js/stores/edition_list_store.js

125 lines
4.6 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);
}
2015-07-06 15:56:14 +02:00
onUpdateEditionList({pieceId, editionListOfPiece, page, pageSize, orderBy, orderAsc, count}) {
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;
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
*/
onRefreshEditionList(pieceId) {
const prevEditionListLength = this.editionList[pieceId].length;
const prevEditionListPage = this.editionList[pieceId].page;
const prevEditionListPageSize = this.editionList[pieceId].pageSize;
// 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, this.editionList[pieceId].orderBy, this.editionList[pieceId].orderAsc)
.then(() => {
// reset back to the normal pageSize and page
this.editionList[pieceId].page = prevEditionListPage;
this.editionList[pieceId].pageSize = prevEditionListPageSize;
})
.catch((err) => {
console.error(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) {
this.isEditionListOpenForPieceId[pieceId] = {
show: this.isEditionListOpenForPieceId[pieceId] ? !this.isEditionListOpenForPieceId[pieceId].show : true
};
}
onCloseAllEditionLists() {
this.isEditionListOpenForPieceId = {};
}
}
export default alt.createStore(EditionListStore, 'EditionListStore');