<commit_before>/** @jsx m */

"use strict";
require("./StudyFinder.scss");

var _ = require("underscore");
var m = require("mithril");
var SearchResults = require("./SearchResults.js");
var ArticleCollection = require("../collections/ArticleCollection.js");
var Spinner = require("./Spinner.js");
var Tooltip = require("./Tooltip.js");
var Popover = require("./Popover.js");
var cx = require("../utils/ClassSet.js");

var StudyFinder = {};

StudyFinder.controller = function(options) {
  this.search = m.prop("");
  this.submitted = m.prop(false);
  this.matchingArticles = new ArticleCollection();
  this.selectedArticle = null;
  this.selectStudy = options.selectStudy;
  this.parentStudy = options.parentStudy;


  var _this = this;
  this.selectArticle = function(article) {
    return function() {
      if (_this.selectedArticle === article) {
        _this.selectedArticle = null;
      } else {
        _this.selectedArticle = article;
        article.get("studies").fetch();
      }
    };
  };

  this.runSearch = function(e) {
    e.preventDefault();
    _this.submitted(true);
    _this.matchingArticles.search({query: _this.search()});
  };

  this.clickStudyButton = function(study) {
    return function() {
      _this.selectStudy(study);
    };
  };
};

StudyFinder.view = function(ctrl) {
  var content;
  if (false && ctrl.selectedArticle) {
    content = StudyFinder.articleView(ctrl, ctrl.selectedArticle);
  } else {
    content = StudyFinder.searchView(ctrl);
  }
  return (
    <div className="StudyFinder">
      {content}
    </div>
  );
};

StudyFinder.articleView = function(ctrl, article) {
  var studies = article.get("studies");
  var content;

  if (studies.loading) {
    var spinner = Spinner.view();
  }

  if (studies.length === 0 && !studies.loading) {
    content = (
      <div> This article has no studies. <a href={article.url()}>Go to article</a> to add a study.</div>
    )
  } else if (studies.length > 0) {
    var parentReplications = false;
    if (ctrl.parentStudy && ctrl.parentStudy()) {
      parentReplications = ctrl.parentStudy().get("replications");
    }
    var list = studies.map(function(study) {
      var classes = cx({
        btn: true,
        btn_subtle: true,
        active: parentReplications && parentReplications.find(function(replication) {
          return replication.get("replicating_study_id") === study.get("id")
        })
      });
  
      var parentStudy = ctrl.parentStudy();
      console.log(study);
      console.log(parentStudy);
      console.log(article);
      return (
        <li>
          <button type="button" className={classes} onclick={ctrl.clickStudyButton(study)} title={"Add " + study.get("article").etAl(3) + " (" + study.get("year") + ") " + study.get("number") + " as a replication of " + parentStudy.etAl(3) + " (" + parentStudy.get("year") +") "+ parentStudy.get("number")}>
              <span>+</span>
	      <span className="icon icon_replication"> {study.get("number")}</span>
	  </button>
	  {/*study.get("independent_variables").join(", ")} vs {study.get("dependent_variables").join(", ")*/}
        </li>
      );
    });
    content = (
      <div>
        <h6>Studies</h6>
        <ul>{list}</ul>
      </div>
    );
  }

  return (
    <div className="articleView">
      {spinner}
      {content}<commit_msg>add tooltip to add replication button in add replication modal<commit_after>/** @jsx m */

"use strict";
require("./StudyFinder.scss");

var _ = require("underscore");
var m = require("mithril");
var SearchResults = require("./SearchResults.js");
var ArticleCollection = require("../collections/ArticleCollection.js");
var Spinner = require("./Spinner.js");
var Tooltip = require("./Tooltip.js");
var Popover = require("./Popover.js");
var cx = require("../utils/ClassSet.js");

var StudyFinder = {};

StudyFinder.controller = function(options) {
  this.search = m.prop("");
  this.submitted = m.prop(false);
  this.matchingArticles = new ArticleCollection();
  this.selectedArticle = null;
  this.selectStudy = options.selectStudy;
  this.parentStudy = options.parentStudy;


  var _this = this;
  this.selectArticle = function(article) {
    return function() {
      if (_this.selectedArticle === article) {
        _this.selectedArticle = null;
      } else {
        _this.selectedArticle = article;
        article.get("studies").fetch();
      }
    };
  };

  this.runSearch = function(e) {
    e.preventDefault();
    _this.submitted(true);
    _this.matchingArticles.search({query: _this.search()});
  };

  this.clickStudyButton = function(study) {
    return function() {
      _this.selectStudy(study);
    };
  };
};

StudyFinder.view = function(ctrl) {
  var content;
  if (false && ctrl.selectedArticle) {
    content = StudyFinder.articleView(ctrl, ctrl.selectedArticle);
  } else {
    content = StudyFinder.searchView(ctrl);
  }
  return (
    <div className="StudyFinder">
      {content}
    </div>
  );
};

StudyFinder.articleView = function(ctrl, article) {
  var studies = article.get("studies");
  var content;

  if (studies.loading) {
    var spinner = Spinner.view();
  }

  if (studies.length === 0 && !studies.loading) {
    content = (
      <div> This article has no studies. <a href={article.url()}>Go to article</a> to add a study.</div>
    )
  } else if (studies.length > 0) {
    var parentReplications = false;
    if (ctrl.parentStudy && ctrl.parentStudy()) {
      parentReplications = ctrl.parentStudy().get("replications");
    }
    var list = studies.map(function(study) {
      var classes = cx({
        btn: true,
        btn_subtle: true,
        active: parentReplications && parentReplications.find(function(replication) {
          return replication.get("replicating_study_id") === study.get("id")
        })
      });
  
      var parentStudy = ctrl.parentStudy();
      console.log(study);
      console.log(parentStudy);
      console.log(article);
      return (
        <li>
          <button type="button" className={classes} onclick={ctrl.clickStudyButton(study)} title={"Add " + study.etAl(3) + " (" + study.get("year") + ") " + study.get("number") + " as a replication of " + parentStudy.etAl(3) + " (" + parentStudy.get("year") +") "+ parentStudy.get("number")}>
              <span>+</span>
	      <span className="icon icon_replication"> {study.get("number")}</span>
	  </button>
	  {/*study.get("independent_variables").join(", ")} vs {study.get("dependent_variables").join(", ")*/}
        </li>
      );
    });
    content = (
      <div>
        <h6>Studies</h6>
        <ul>{list}</ul>
      </div>
    );
  }

  return (
    <div className="articleView">
      {spinner}
      {content}
{'fraction_non_alphanumeric': 0.1687125748502994, 'fraction_numerical': 0.0017964071856287425, 'mean_word_length': 3.0663420572124163, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '341492', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2081, 'n_words': 396}