File size: 7,381 Bytes
4eb44b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Pure sentence-assembly logic. Loaded by the page and by tests/test_compose.js (Node).
(function (root, factory) {
  if (typeof module === "object" && module.exports) module.exports = factory();
  else root.Compose = factory();
})(typeof self !== "undefined" ? self : this, function () {
  "use strict";

  // True if any item contains text that would make a comma list ambiguous.
  function needsSemicolons(items) {
    return items.some(function (s) { return /\band\b|\/|,/.test(s); });
  }

  // "a", "a and b", "a, b, and c"; semicolons if items themselves contain "and" or commas.
  function joinList(items) {
    if (items.length === 0) return "";
    if (items.length === 1) return items[0];
    var semi = needsSemicolons(items);
    if (items.length === 2) return items[0] + (semi ? "; and " : " and ") + items[1];
    var sep = semi ? "; " : ", ";
    return items.slice(0, -1).join(sep) + sep + "and " + items[items.length - 1];
  }

  function lowerFirst(s) {
    return s.charAt(0).toLowerCase() + s.slice(1);
  }

  // Resolve selected ids to display strings, in taxonomy order.
  function taskPhrases(data, selectedIds, mode, rollup) {
    var out = [];
    var sel = {};
    selectedIds.forEach(function (id) { sel[id] = true; });
    data.groups.forEach(function (g) {
      var hits = g.terms.filter(function (t) { return sel[t.id]; });
      if (hits.length === 0) return;
      if (rollup) {
        out.push(mode === "short" ? g.short : g.label.toLowerCase());
      } else {
        hits.forEach(function (t) {
          out.push(mode === "short" ? t.short : lowerFirst(t.label));
        });
      }
    });
    return out;
  }

  function fill(tpl, vars) {
    return tpl.replace(/\{(\w+)\}/g, function (_, k) {
      return Object.prototype.hasOwnProperty.call(vars, k) ? vars[k] : "";
    });
  }

  /**
   * state: {
   *   used: boolean, style: "document"|"slide"|"minimal", tasks: [ids],
   *   tools: [strings], party: string (phrase), plural: boolean, work: string,
   *   review: boolean, cite: boolean, rollup: boolean
   * }
   * Returns { text, ok }.
   */
  function compose(state, data, templates) {
    var style = templates.styles[state.style] || templates.styles.document;
    var work = (state.work || "document").trim() || "document";
    var party = (state.party || "the authors").trim() || "the authors";

    if (!state.used) {
      return { ok: true, text: fill(templates.none[state.style] || templates.none.document, { work: work }) };
    }

    var tasks = taskPhrases(data, state.tasks || [], style.labels, !!state.rollup);
    if (tasks.length === 0) return { ok: false, text: templates.empty };

    var tools = (state.tools || []).map(function (s) { return s.trim(); }).filter(Boolean);
    var vars = {
      tools: joinList(tools),
      tasks: joinList(tasks),
      party: party,
      take: state.plural ? "take" : "takes",
      work: work,
      cite: state.cite ? style.cite : "",
      review: ""
    };
    var reviewTpl = isTechnical(data, templates, state.tasks || []) && style.reviewTechnical ? style.reviewTechnical : style.review;
    if (state.review && reviewTpl) vars.review = fill(reviewTpl, vars);

    var text = fill(tools.length ? style.withTools : style.noTools, vars);
    if (state.style === "document" && state.review && reviewTpl) text += fill(reviewTpl, vars);
    return { ok: true, text: text };
  }

  // Map each selected id to its group key.
  function groupsOf(data, selectedIds) {
    var byId = {};
    data.groups.forEach(function (g) { g.terms.forEach(function (t) { byId[t.id] = g.key; }); });
    return selectedIds.map(function (id) { return byId[id]; }).filter(Boolean);
  }

  // True if any selected task belongs to a group that produces code, analyses, or model outputs.
  function isTechnical(data, templates, selectedIds) {
    var tech = templates.technicalGroups || [];
    return groupsOf(data, selectedIds).some(function (k) { return tech.indexOf(k) !== -1; });
  }

  function workInfo(templates, workId) {
    return (templates.works || []).filter(function (w) { return w.id === workId; })[0] || { id: workId, kind: "other" };
  }

  // Guidance notes shown under the statement. Returns an array of strings (possibly empty).
  function notes(state, data, templates) {
    var out = [];
    var w = workInfo(templates, state.workId);
    var ids = state.tasks || [];
    if (!state.used || ids.length === 0) {
      if (w.journal) out.push(templates.journalNote);
      return out;
    }
    if (w.kind === "research" && templates.placement) {
      var pl = templates.placement;
      var byId = {};
      data.groups.forEach(function (g) { g.terms.forEach(function (t) { byId[t.id] = g.key; }); });
      var ack = false, meth = false;
      ids.forEach(function (id) {
        var isAck = pl.ackTerms.indexOf(id) !== -1 || pl.acknowledgementGroups.indexOf(byId[id]) !== -1;
        if (isAck) ack = true; else meth = true;
      });
      if (ack && meth) out.push(pl.both);
      else if (ack) out.push(pl.acknowledgements);
      else if (meth) out.push(pl.methods);
    }
    if (w.kind === "communications" && templates.routineTerms) {
      var allRoutine = ids.every(function (id) { return templates.routineTerms.indexOf(id) !== -1; });
      if (allRoutine) out.push(templates.routineNote);
    }
    if (w.journal) out.push(templates.journalNote);
    return out;
  }

  // URL round-trip. Ids are stored without leading zeros to keep links short.
  function toParams(state) {
    var p = new URLSearchParams();
    if (!state.used) p.set("none", "1");
    if (state.style && state.style !== "document") p.set("style", state.style);
    if (state.tasks && state.tasks.length) p.set("t", state.tasks.map(function (id) { return String(parseInt(id, 10)); }).join(","));
    if (state.tools && state.tools.length) p.set("tools", state.tools.join("|"));
    if (state.partyId && state.partyId !== "authors") p.set("party", state.partyId);
    if (state.partyId === "custom") { p.set("partyText", state.party || ""); p.set("plural", state.plural ? "1" : "0"); }
    if (state.workId && state.workId !== "document") p.set("work", state.workId);
    if (state.workId === "custom") p.set("workText", state.work || "");
    if (state.review === false) p.set("review", "0");
    if (state.cite) p.set("cite", "1");
    if (state.rollup) p.set("rollup", "1");
    return p;
  }

  function fromParams(p) {
    var s = {};
    if (p.has("none")) s.used = false;
    if (p.has("style")) s.style = p.get("style");
    if (p.has("t")) s.tasks = p.get("t").split(",").filter(Boolean).map(function (n) {
      return ("0000000" + parseInt(n, 10)).slice(-7);
    });
    if (p.has("tools")) s.tools = p.get("tools").split("|").filter(Boolean);
    if (p.has("party")) s.partyId = p.get("party");
    if (p.has("partyText")) s.party = p.get("partyText");
    if (p.has("plural")) s.plural = p.get("plural") === "1";
    if (p.has("work")) s.workId = p.get("work");
    if (p.has("workText")) s.work = p.get("workText");
    if (p.has("review")) s.review = p.get("review") !== "0";
    if (p.has("cite")) s.cite = p.get("cite") === "1";
    if (p.has("rollup")) s.rollup = p.get("rollup") === "1";
    return s;
  }

  return { compose: compose, notes: notes, isTechnical: isTechnical, joinList: joinList, taskPhrases: taskPhrases, toParams: toParams, fromParams: fromParams };
});