"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
; }, 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{corpus.description}
{this.renderExpansion(corpus)}{corpus.institution.name}
{this.renderLanguages(corpus.languages)}