@pie-element/ebsr 7.0.13 → 7.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@pie-element/ebsr",
3
- "version": "7.0.13",
3
+ "version": "7.0.17",
4
4
  "description": "",
5
5
  "repository": "pie-framework/pie-elements",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "dependencies": {
10
- "@pie-element/multiple-choice": "^6.1.11",
10
+ "@pie-element/multiple-choice": "^6.2.3",
11
11
  "@pie-framework/pie-player-events": "^0.1.0",
12
12
  "classnames": "^2.2.5",
13
13
  "debug": "^4.1.1",
@@ -15,10 +15,14 @@
15
15
  },
16
16
  "author": "pie framework developers",
17
17
  "license": "ISC",
18
- "gitHead": "eff88d43498ff36da95f6dff4b6c0fa4b22fa44a",
18
+ "gitHead": "711fb90f7f30b2b7438fd252d104b2c9c25accba",
19
19
  "scripts": {
20
20
  "postpublish": "../../scripts/postpublish"
21
21
  },
22
22
  "main": "lib/index.js",
23
- "module": "src/index.js"
23
+ "module": "src/index.js",
24
+ "exports": {
25
+ ".": "./src/index.js",
26
+ "./print": "./src/print.js"
27
+ }
24
28
  }
package/src/print.js ADDED
@@ -0,0 +1,183 @@
1
+ import React from 'react';
2
+ import cloneDeep from 'lodash/cloneDeep';
3
+ import MultipleChoice from '@pie-element/multiple-choice';
4
+ import debug from 'debug';
5
+ import get from 'lodash/get';
6
+
7
+ import {SessionChangedEvent} from '@pie-framework/pie-player-events';
8
+ const MC_TAG_NAME = 'ebsr-multiple-choice';
9
+ const SESSION_CHANGED = SessionChangedEvent.TYPE;
10
+
11
+ const log = debug('pie-element:ebsr:print');
12
+
13
+ /**
14
+ * Live in same package as main element - so we can access some of the shared comps!
15
+ *
16
+ * - update pslb to build print if src/print.js is there
17
+ * - update demo el
18
+ * - get configure/controller building
19
+ */
20
+
21
+ const preparePrintModel = (model, opts) => {
22
+ const instr = opts.role === 'instructor';
23
+
24
+ model.prompt = model.promptEnabled !== false ? model.prompt : undefined;
25
+ model.teacherInstructions = instr && model.teacherInstructionsEnabled !== false ? model.teacherInstructions : undefined;
26
+ model.showTeacherInstructions = instr;
27
+ model.alwaysShowCorrect = instr;
28
+ model.mode = instr ? 'evaluate' : model.mode;
29
+
30
+ model.disabled = true;
31
+ model.animationsDisabled = true;
32
+ model.lockChoiceOrder = true;
33
+ model.choicesLayout = model.choicesLayout || 'vertical';
34
+
35
+ const choices = cloneDeep(model.choices);
36
+
37
+ model.choices = choices.map((c) => {
38
+ c.rationale = instr && model.rationaleEnabled !== false ? c.rationale : undefined;
39
+ c.hideTick = instr;
40
+ c.feedback = undefined;
41
+ return c;
42
+ });
43
+
44
+ model.keyMode = model.choicePrefix || 'letters';
45
+
46
+ return model;
47
+ };
48
+
49
+ class EbsrMC extends MultipleChoice {}
50
+
51
+ const defineMultipleChoice = () => {
52
+ if (!customElements.get(MC_TAG_NAME)) {
53
+ customElements.define(MC_TAG_NAME, EbsrMC);
54
+ }
55
+ };
56
+
57
+ defineMultipleChoice();
58
+
59
+ const isNonEmptyArray = (a) => Array.isArray(a) && a.length > 0;
60
+
61
+ export const isSessionComplete = (session) => {
62
+ const a = get(session, 'value.partA.value');
63
+ const b = get(session, 'value.partB.value');
64
+
65
+ return isNonEmptyArray(a) && isNonEmptyArray(b);
66
+ };
67
+
68
+ export default class Ebsr extends HTMLElement {
69
+ constructor() {
70
+ super();
71
+ this._model = {};
72
+ this._session = {};
73
+ this._options = null;
74
+ }
75
+
76
+ onSessionUpdated = (e) => {
77
+ if (e.target === this) {
78
+ return;
79
+ }
80
+
81
+ e.preventDefault();
82
+ e.stopImmediatePropagation();
83
+
84
+ const id = e.target.getAttribute('id');
85
+
86
+ if (id) {
87
+ const key = `part${id.toUpperCase()}`;
88
+
89
+ if (e.update) {
90
+ this._model[key] = e.update;
91
+ }
92
+ //TODO: accessing a private property here. The session event should contain the update in future to prevent this.
93
+ this.dispatchSessionChanged(e.srcElement._session, key);
94
+ }
95
+ };
96
+
97
+ set model(m) {
98
+ this._model = m;
99
+
100
+ customElements.whenDefined(MC_TAG_NAME).then(() => {
101
+ this.setPartModel(this.partA, 'partA');
102
+ this.setPartModel(this.partB, 'partB');
103
+ });
104
+ }
105
+
106
+ set session(s) {
107
+ this._session = s;
108
+
109
+ customElements.whenDefined(MC_TAG_NAME).then(() => {
110
+ this.setPartSession(this.partA, 'partA');
111
+ this.setPartSession(this.partB, 'partB');
112
+ });
113
+ }
114
+
115
+ setPartModel(part, key) {
116
+ if (this._model && this._model[key]) {
117
+ const { mode } = this._model;
118
+
119
+ part.model = {
120
+ ...preparePrintModel(this._model[key], this._options),
121
+ mode,
122
+ keyMode: this._model[key].choicePrefix,
123
+ };
124
+
125
+ if (!part._session) {
126
+ // for print, "set session" is not called,
127
+ // but ebsr needs sessions in order to render the elements,
128
+ // so we set it here it was not set already
129
+ part.session = {};
130
+ }
131
+ }
132
+ }
133
+
134
+ set options(o) {
135
+ this._options = o;
136
+ }
137
+
138
+ setPartSession(part, key) {
139
+ if (this._session && this._model) {
140
+ const { value } = this._session;
141
+ part.session = value && value[key] ? value[key] : { id: key };
142
+ }
143
+ }
144
+
145
+ dispatchSessionChanged(partSession, key) {
146
+ this._session.value = {
147
+ ...this._session.value,
148
+ [key]: partSession,
149
+ };
150
+
151
+ log('[onSessionChanged] session: ', this._session);
152
+ const complete = isSessionComplete(this._session);
153
+ this.dispatchEvent(
154
+ new SessionChangedEvent(this.tagName.toLowerCase(), complete)
155
+ );
156
+ }
157
+
158
+ get partA() {
159
+ return this.querySelector(`${MC_TAG_NAME}#a`);
160
+ }
161
+
162
+ get partB() {
163
+ return this.querySelector(`${MC_TAG_NAME}#b`);
164
+ }
165
+
166
+ connectedCallback() {
167
+ this._render();
168
+ this.addEventListener(SESSION_CHANGED, this.onSessionUpdated);
169
+ }
170
+
171
+ disconnectedCallback() {
172
+ this.removeEventListener(SESSION_CHANGED, this.onSessionUpdated);
173
+ }
174
+
175
+ _render() {
176
+ this.innerHTML = `
177
+ <div>
178
+ <${MC_TAG_NAME} id="a"></${MC_TAG_NAME}>
179
+ <${MC_TAG_NAME} id="b"></${MC_TAG_NAME}>
180
+ </div>
181
+ `;
182
+ }
183
+ }