"use strict"; import classNames from "classnames"; import SearchCorpusBox from "./searchcorpusbox.jsx"; import PropTypes from "prop-types"; import createReactClass from "create-react-class"; var PT = PropTypes; var CorpusView = createReactClass({ //fixme! - class CorpusView extends React.Component { propTypes: { corpora: PT.object.isRequired, languageMap: PT.object.isRequired, }, getInitialState() { return { viewSelected: false, // only show the selected collections //showDisabled: false, // dont hide items with {visible = false} // implemented, but out commented feature... } }, toggleSelection: function (corpus, e) { var s = !corpus.selected; this.props.corpora.recurseCorpus(corpus, function(c) { c.selected = s; }); this.props.corpora.update(); this.stop(e); }, toggleViewSelected(evt) { this.setState( (st)=> ({viewSelected:!st.viewSelected}) ); }, toggleShowDisabled(evt) { this.setState( (st)=> ({showDisabled:!st.showDisabled}) ); }, toggleDescExpansion: function (corpus) { corpus.descExpanded = !corpus.descExpanded; this.props.corpora.update(); }, toggleExpansion: function (corpus) { corpus.expanded = !corpus.expanded; this.props.corpora.update(); }, selectAll: function(value) { // select all _visible_ this.props.corpora.recurse(function(c) { c.visible ? c.selected = value : false }); this.props.corpora.update(); }, searchCorpus: function(query) { // sort fn: descending priority, stable sort var sortFn = function(a, b){ if (b.priority === a.priority) { return b.index - a.index; // stable sort } return b.priority - a.priority; }; query = query.toLowerCase(); if (!query) { this.props.corpora.recurse(function(corpus) {corpus.priority = 1; }); this.props.corpora.update(); return; } // clean up all priorities this.props.corpora.recurse(function(corpus) { corpus.priority = 0; }); // find priority for each corpus var querytokens = query.split(" ").filter(function(x){ return x.length > 0; }); this.props.corpora.recurse(function(corpus){ var title = corpus.title; querytokens.forEach(function(qtoken){ if (title && title.toLowerCase().indexOf(qtoken) >= 0) { corpus.priority ++; } if (corpus.description && corpus.description.toLowerCase().indexOf(qtoken) >= 0) { corpus.priority ++; } if (corpus.institution && corpus.institution.name && corpus.institution.name.toLowerCase().indexOf(qtoken) >= 0) { corpus.priority ++; } if (corpus.languages){ corpus.languages.forEach(function(lang){ if (lang.toLowerCase().indexOf(qtoken) >= 0){ corpus.priority ++; } }); corpus.languages.forEach(function(lang){ if (this.props.languageMap[lang].toLowerCase().indexOf(qtoken) >= 0){ corpus.priority ++; } }.bind(this)); } }.bind(this)); }.bind(this)); // ensure parents of visible corpora are also visible; maximum depth = 3 var isVisibleFn = function(corpus){ return corpus.priority > 0; }; var parentBooster = function(corpus){ if (corpus.priority <= 0 && corpus.subCorpora) { if (corpus.subCorpora.some(isVisibleFn)) { corpus.priority = 0.5; } } }; for (var i = 3; i > 0; i --) { this.props.corpora.recurse(parentBooster); } this.props.corpora.recurse(function(corpus) { corpus.subCorpora.sort(sortFn); }); this.props.corpora.corpora.sort(sortFn); // display this.props.corpora.update(); }, stop: function(e) { e.stopPropagation(); }, getMinMaxPriority: function() { var min = 1, max = 0; this.props.corpora.recurse(function(c) { if (c.priority < min) min = c.priority; if (max < c.priority) max = c.priority; }); return [min, max]; }, renderCheckbox: function(corpus) { return ; }, renderExpansion: function(corpus) { if (!corpus.subCorpora || corpus.subCorpora.length === 0) { return false; } return
{corpus.expanded ?
; }, renderLanguages: function(languages) { return languages .map(function(l) { return this.props.languageMap[l]; }.bind(this)) .sort() .join(", "); }, shouldShowItem(level, corpus) { if (this.state.viewSelected && !corpus.selected) { return false; } if (!this.state.showDisabled && !corpus.visible) { return false; } // normal search filter. if (level === 0 && corpus.priority <= 0) { return false; } return true; }, renderFilteredMessage() { var total = 0; var visible = 0; this.props.corpora.recurse((corpus) => { if (corpus.visible || this.state.showDisabled) { total ++; if (this.shouldShowItem(0, corpus)) { visible++; } } }); if (visible === total) { return false; } if (visible === 0) { return false; // we do have an "empty" message anyway } return
Showing {visible} out of {total} (sub)collections.
; }, renderCorpus: function(level, minmaxp, corpus) { if (!this.shouldShowItem(level, corpus)) { return; } var indent = {marginLeft:level*50}; var corpusContainerClass = "corpus-container "+(corpus.priority>0?"":"dimmed"); var hue = 120 * corpus.priority / minmaxp[1]; var color = minmaxp[0] === minmaxp[1] ? 'transparent' : 'hsl('+hue+', 50%, 50%)'; var priorityStyle = {paddingBottom: 4, paddingLeft: 2, borderBottom: '3px solid '+color }; var expansive = corpus.descExpanded ? {overflow:'hidden'} : {whiteSpace:'nowrap', overflow:'hidden', textOverflow: 'ellipsis'}; return
{this.renderCheckbox(corpus)}

{corpus.title} { corpus.landingPage ? – Homepage : false}

{corpus.description}

{this.renderExpansion(corpus)}

{corpus.institution.name}

{this.renderLanguages(corpus.languages)}

{corpus.expanded ? corpus.subCorpora.map(this.renderCorpus.bind(this, level+1, minmaxp)) : false}
; }, renderCorpList() { var minmaxp = this.getMinMaxPriority(); const corpListRender = []; // this is so we get a non-undefined items .length in corpListRender. this.props.corpora.corpora.forEach( c => { var rend = this.renderCorpus(0, minmaxp, c); if (rend) corpListRender.push(rend); }); return
{ corpListRender.length > 0 ? corpListRender :

{ this.state.viewSelected ? "No collections selected yet!" : "No collections found." }

}
}, render() { var selectedCount = 0; //var disabledCount = 0; this.props.corpora.recurse( c => { if (c.selected && c.visible) selectedCount++; //if (c.selected) selectedCount++; //if (!c.visible) disabledCount++; }); return
{/*

{this.props.corpora.getSelectedMessage()}

*/}
{/* */}
{this.renderFilteredMessage()}
{this.renderCorpList()}
; } }); module.exports = CorpusView;