@oat-sa/tao-core-ui 1.67.0 → 1.69.0

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.
Files changed (54) hide show
  1. package/dist/ckeditor/ckConfigurator.js +10 -1
  2. package/dist/maths/calculator/basicCalculator.js +4 -4
  3. package/dist/maths/calculator/calculatorComponent.js +22 -25
  4. package/dist/maths/calculator/core/board.js +12329 -720
  5. package/dist/maths/calculator/core/labels.js +7924 -138
  6. package/dist/maths/calculator/core/plugin.js +4 -5
  7. package/dist/maths/calculator/css/calculator.css +18 -5
  8. package/dist/maths/calculator/css/calculator.css.map +1 -1
  9. package/dist/maths/calculator/defaultCalculator.js +10 -6
  10. package/dist/maths/calculator/plugins/keyboard/templateKeyboard/templateKeyboard.js +23 -25
  11. package/dist/maths/calculator/plugins/screen/simpleScreen/simpleScreen.js +7979 -194
  12. package/dist/maths/calculator/scientificCalculator.js +7 -12
  13. package/package.json +5 -7
  14. package/src/ckeditor/ckConfigurator.js +11 -4
  15. package/src/maths/calculator/basicCalculator.js +1 -4
  16. package/src/maths/calculator/calculatorComponent.js +49 -60
  17. package/src/maths/calculator/core/board.js +372 -493
  18. package/src/maths/calculator/core/labels.js +46 -48
  19. package/src/maths/calculator/core/plugin.js +3 -5
  20. package/src/maths/calculator/core/tpl/terms.tpl +7 -1
  21. package/src/maths/calculator/css/calculator.css +18 -5
  22. package/src/maths/calculator/css/calculator.css.map +1 -1
  23. package/src/maths/calculator/defaultCalculator.js +7 -9
  24. package/src/maths/calculator/plugins/keyboard/templateKeyboard/defaultTemplate.tpl +3 -3
  25. package/src/maths/calculator/plugins/keyboard/templateKeyboard/templateKeyboard.js +17 -20
  26. package/src/maths/calculator/plugins/screen/simpleScreen/simpleScreen.js +102 -108
  27. package/src/maths/calculator/scientificCalculator.js +2 -10
  28. package/src/maths/calculator/scss/calculator.scss +14 -1
  29. package/src/maths/calculator/tpl/basicKeyboard.tpl +3 -3
  30. package/src/maths/calculator/tpl/scientificKeyboard.tpl +4 -4
  31. package/dist/maths/calculator/core/areaBroker.js +0 -43
  32. package/dist/maths/calculator/core/expression.js +0 -463
  33. package/dist/maths/calculator/core/terms.js +0 -456
  34. package/dist/maths/calculator/core/tokenizer.js +0 -229
  35. package/dist/maths/calculator/core/tokens.js +0 -167
  36. package/dist/maths/calculator/plugins/core/degrad.js +0 -71
  37. package/dist/maths/calculator/plugins/core/history.js +0 -149
  38. package/dist/maths/calculator/plugins/core/remind.js +0 -76
  39. package/dist/maths/calculator/plugins/core/stepNavigation.js +0 -148
  40. package/dist/maths/calculator/plugins/modifiers/pow10.js +0 -136
  41. package/dist/maths/calculator/plugins/modifiers/sign.js +0 -314
  42. package/dist/maths/calculator/pluginsLoader.js +0 -47
  43. package/src/maths/calculator/core/areaBroker.js +0 -38
  44. package/src/maths/calculator/core/expression.js +0 -430
  45. package/src/maths/calculator/core/terms.js +0 -459
  46. package/src/maths/calculator/core/tokenizer.js +0 -245
  47. package/src/maths/calculator/core/tokens.js +0 -178
  48. package/src/maths/calculator/plugins/core/degrad.js +0 -90
  49. package/src/maths/calculator/plugins/core/history.js +0 -166
  50. package/src/maths/calculator/plugins/core/remind.js +0 -96
  51. package/src/maths/calculator/plugins/core/stepNavigation.js +0 -175
  52. package/src/maths/calculator/plugins/modifiers/pow10.js +0 -143
  53. package/src/maths/calculator/plugins/modifiers/sign.js +0 -339
  54. package/src/maths/calculator/pluginsLoader.js +0 -46
@@ -1,175 +0,0 @@
1
- /**
2
- * This program is free software; you can redistribute it and/or
3
- * modify it under the terms of the GNU General Public License
4
- * as published by the Free Software Foundation; under version 2
5
- * of the License (non-upgradable).
6
- *
7
- * This program is distributed in the hope that it will be useful,
8
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- * GNU General Public License for more details.
11
- *
12
- * You should have received a copy of the GNU General Public License
13
- * along with this program; if not, write to the Free Software
14
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
- *
16
- * Copyright (c) 2018 Open Assessment Technologies SA ;
17
- */
18
- /**
19
- * Plugin that manages a navigation by tokens within the current expression
20
- * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com>
21
- */
22
-
23
- import __ from 'i18n';
24
- import nsHelper from 'util/namespace';
25
- import pluginFactory from 'ui/maths/calculator/core/plugin';
26
-
27
- var pluginName = 'stepNavigation';
28
-
29
- export default pluginFactory({
30
- name: pluginName,
31
-
32
- /**
33
- * Called when the plugin is installing in its host.
34
- */
35
- install: function install() {
36
- var calculator = this.getCalculator();
37
-
38
- calculator
39
- .setCommand('stepMoveLeft', __('Move Left'), __('Move the cursor one step on the left'))
40
- .setCommand('stepMoveRight', __('Move Right'), __('Move the cursor one step on the right'))
41
- .setCommand('stepDeleteLeft', __('Delete Left'), __('Delete the term on the left side of the cursor'))
42
- .setCommand('stepDeleteRight', __('Delete Right'), __('Delete the term on the right side of the cursor'));
43
- },
44
-
45
- /**
46
- * Called when the plugin should be initialized.
47
- */
48
- init: function init() {
49
- var calculator = this.getCalculator();
50
-
51
- /**
52
- * Remove a sub-expresion from the expression
53
- * @param expression
54
- * @param position
55
- * @param length
56
- */
57
- function remove(expression, position, length) {
58
- if (length) {
59
- expression = calculator.getExpression();
60
- calculator
61
- .setExpression(expression.substr(0, position) + expression.substr(position + length))
62
- .setPosition(position);
63
- }
64
- }
65
-
66
- /**
67
- * Remove a token from the expression
68
- * @param expression
69
- * @param token
70
- */
71
- function removeToken(expression, token) {
72
- var from, length;
73
- if (token) {
74
- expression = calculator.getExpression();
75
- from = token.offset;
76
- length = token.value.length;
77
- while (from + length < expression.length && expression.charAt(from + length) === ' ') {
78
- length++;
79
- }
80
-
81
- remove(expression, from, length);
82
- }
83
- }
84
-
85
- calculator
86
- .on(nsHelper.namespaceAll('command-stepMoveLeft', pluginName), function() {
87
- var position = calculator.getPosition();
88
- var tokens = calculator.getTokens();
89
- var index = calculator.getTokenIndex();
90
- var token = calculator.getToken();
91
- var offset = position;
92
-
93
- if (token && position > 0) {
94
- if (position === token.offset) {
95
- if (index > 0) {
96
- token = tokens[index - 1];
97
- } else {
98
- token = null;
99
- }
100
- }
101
- } else {
102
- token = null;
103
- }
104
- if (token) {
105
- offset = token.offset;
106
- } else {
107
- offset = 0;
108
- }
109
-
110
- if (offset !== position) {
111
- calculator.setPosition(offset);
112
- }
113
- })
114
- .on(nsHelper.namespaceAll('command-stepMoveRight', pluginName), function() {
115
- var expression = calculator.getExpression();
116
- var position = calculator.getPosition();
117
- var tokens = calculator.getTokens();
118
- var index = calculator.getTokenIndex();
119
- var token = calculator.getToken();
120
- var offset = expression.length;
121
-
122
- if (token && index < tokens.length - 1) {
123
- token = tokens[index + 1];
124
- if (token) {
125
- offset = token.offset;
126
- }
127
- }
128
-
129
- if (offset !== position) {
130
- calculator.setPosition(offset);
131
- }
132
- })
133
- .on(nsHelper.namespaceAll('command-stepDeleteLeft', pluginName), function() {
134
- var expression = calculator.getExpression();
135
- var position = calculator.getPosition();
136
- var tokens = calculator.getTokens();
137
- var index = calculator.getTokenIndex();
138
- var token = calculator.getToken();
139
-
140
- if (token) {
141
- if (position > token.offset) {
142
- removeToken(expression, token);
143
- } else {
144
- if (index > 0) {
145
- removeToken(expression, tokens[index - 1]);
146
- } else if (position > 0) {
147
- remove(expression, 0, token.offset);
148
- }
149
- }
150
- }
151
- })
152
- .on(nsHelper.namespaceAll('command-stepDeleteRight', pluginName), function() {
153
- var expression = calculator.getExpression();
154
- var position = calculator.getPosition();
155
- var token = calculator.getToken();
156
-
157
- if (token && position >= token.offset && position < token.offset + token.value.length) {
158
- removeToken(expression, token);
159
- }
160
- });
161
- },
162
-
163
- /**
164
- * Called when the plugin is destroyed. Mostly when the host is destroyed itself.
165
- */
166
- destroy: function destroy() {
167
- var calculator = this.getCalculator();
168
- calculator
169
- .deleteCommand('stepMoveLeft')
170
- .deleteCommand('stepMoveRight')
171
- .deleteCommand('stepDeleteLeft')
172
- .deleteCommand('stepDeleteRight')
173
- .off('.' + pluginName);
174
- }
175
- });
@@ -1,143 +0,0 @@
1
- /**
2
- * This program is free software; you can redistribute it and/or
3
- * modify it under the terms of the GNU General Public License
4
- * as published by the Free Software Foundation; under version 2
5
- * of the License (non-upgradable).
6
- *
7
- * This program is distributed in the hope that it will be useful,
8
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- * GNU General Public License for more details.
11
- *
12
- * You should have received a copy of the GNU General Public License
13
- * along with this program; if not, write to the Free Software
14
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
- *
16
- * Copyright (c) 2019 Open Assessment Technologies SA ;
17
- */
18
- /**
19
- * Plugin that multiplies the current operand by 10^x.
20
- * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com>
21
- */
22
-
23
- import __ from 'i18n';
24
- import nsHelper from 'util/namespace';
25
- import pluginFactory from 'ui/maths/calculator/core/plugin';
26
- import tokensHelper from 'ui/maths/calculator/core/tokens';
27
-
28
- var pluginName = 'pow10';
29
-
30
- /**
31
- * The basic `10^x` expression.
32
- * @type {String}
33
- */
34
- var termsPow10 = '10^';
35
-
36
- /**
37
- * The multiply by `10^x` expression.
38
- * @type {String}
39
- */
40
- var termsMulByPow10 = '*' + termsPow10;
41
-
42
- export default pluginFactory({
43
- name: pluginName,
44
-
45
- /**
46
- * Called when the plugin is installing in its host.
47
- */
48
- install: function install() {
49
- this.getCalculator().setCommand('pow10', __('Power of 10'), __('Multiply the value by 10^x'));
50
- },
51
-
52
- /**
53
- * Called when the plugin should be initialized.
54
- */
55
- init: function init() {
56
- var calculator = this.getCalculator();
57
-
58
- /**
59
- * Checks if the expression contains a space before the current position
60
- * @returns {Boolean}
61
- */
62
- function spaceBefore() {
63
- return calculator.getExpression().charAt(calculator.getPosition() - 1) === ' ';
64
- }
65
-
66
- /**
67
- * Checks if the token is considered as a separator
68
- * @param {token} token
69
- * @returns {Boolean}
70
- */
71
- function isSeparator(token) {
72
- return tokensHelper.isFunction(token) || (tokensHelper.isSeparator(token) && token.type !== 'RPAR');
73
- }
74
-
75
- // insert the sub-expression based on strategies
76
- calculator.on(nsHelper.namespaceAll('command-pow10', pluginName), function() {
77
- var position = calculator.getPosition();
78
- var tokens = calculator.getTokens();
79
- var index = calculator.getTokenIndex();
80
- var token = tokens[index] || null;
81
- var prevToken = tokens[index - 1] || null;
82
- var nextToken = tokens[index + 1] || null;
83
- var aligned = false;
84
- var expr = termsMulByPow10;
85
-
86
- if (token) {
87
- // refine the current position, as it should be either on the start or the end of the token:
88
- // "aligned" means the position is properly aligned with the token, said at the start of the value,
89
- // while not "aligned" means any other position inside the token
90
- aligned = token.offset === position;
91
- if (token.type === 'MUL') {
92
- if (aligned) {
93
- // the "multiply" operator is a special case, as it is part of the complete expression,
94
- // so if it is already there, we consider to use it, and move the position just after
95
- calculator.setPosition(position + token.value.length);
96
- aligned = false;
97
- }
98
- } else if (!aligned) {
99
- // the strategy is to consider the position is in front of the token,
100
- // the allowed exception is when the position is the last one
101
- if (nextToken || position < token.offset + token.value.length) {
102
- calculator.setPosition(token.offset);
103
- aligned = true;
104
- }
105
- }
106
- }
107
-
108
- // empty expression or 0, should be replaced by the shorter version
109
- if ((tokens.length <= 1 && !token) || token.type === 'NUM0') {
110
- calculator.replace(termsPow10);
111
- }
112
- // will insert expression at position:
113
- // - use the shorter version if there is no token before, or the previous one is either an operator
114
- // or a function
115
- // - user the longer version in any other cases
116
- else {
117
- if (aligned) {
118
- if (index === 0 || isSeparator(prevToken)) {
119
- expr = termsPow10;
120
- }
121
- } else if (isSeparator(token)) {
122
- expr = termsPow10;
123
-
124
- // the expression may need a space before if the previous token is a function
125
- if (tokensHelper.isFunction(token) && !spaceBefore()) {
126
- expr = ' ' + expr;
127
- }
128
- }
129
-
130
- calculator.insert(expr);
131
- }
132
- });
133
- },
134
-
135
- /**
136
- * Called when the plugin is destroyed. Mostly when the host is destroyed itself.
137
- */
138
- destroy: function destroy() {
139
- this.getCalculator()
140
- .deleteCommand('pow10')
141
- .off('.' + pluginName);
142
- }
143
- });
@@ -1,339 +0,0 @@
1
- /**
2
- * This program is free software; you can redistribute it and/or
3
- * modify it under the terms of the GNU General Public License
4
- * as published by the Free Software Foundation; under version 2
5
- * of the License (non-upgradable).
6
- *
7
- * This program is distributed in the hope that it will be useful,
8
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- * GNU General Public License for more details.
11
- *
12
- * You should have received a copy of the GNU General Public License
13
- * along with this program; if not, write to the Free Software
14
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
- *
16
- * Copyright (c) 2019 Open Assessment Technologies SA ;
17
- */
18
- /**
19
- * Plugin that allows to change the sign of the current operand
20
- * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com>
21
- */
22
- import _ from 'lodash';
23
- import __ from 'i18n';
24
- import nsHelper from 'util/namespace';
25
- import pluginFactory from 'ui/maths/calculator/core/plugin';
26
- import tokensHelper from 'ui/maths/calculator/core/tokens';
27
- import registeredTerms from 'ui/maths/calculator/core/terms';
28
-
29
- var pluginName = 'sign';
30
-
31
- /**
32
- * List of tokens that refuse explicit positive sign
33
- * @type {String[]}
34
- */
35
- var refuseExplicitPositive = ['LPAR', 'SUB', 'ADD', 'MUL', 'DIV', 'MOD', 'POW', 'ASSIGN'];
36
-
37
- /**
38
- * @typedef {Object} signChange
39
- * @property {String} sign - The sign to insert
40
- * @property {Number} offset - The offset where insert the sign
41
- * @property {Number} length - The length of text to replace
42
- * @property {Number} move - The move to apply on the current position
43
- */
44
-
45
- /**
46
- * List of known strategies to apply on an expression in order to process sign change.
47
- * Each strategy will return either `null` if cannot apply, or the descriptor of the change to apply.
48
- * @type {Function[]}
49
- */
50
- var strategies = [
51
- /**
52
- * Strategy that applies on numeric operands only
53
- * @param {Number} index - The index of the current token
54
- * @param {token[]} tokens - The list of tokens that represent the expression
55
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
56
- */
57
- function strategyNumeric(index, tokens) {
58
- var token = tokens[index] || null;
59
- var type = tokensHelper.getType(token);
60
- var result = null;
61
-
62
- if (tokensHelper.isDigit(type) && index >= 0) {
63
- // find the first token on the left of the operand
64
- while (index && tokensHelper.isDigit(type)) {
65
- index--;
66
- token = tokens[index] || null;
67
- type = tokensHelper.getType(token);
68
- }
69
-
70
- if (tokensHelper.isDigit(type) && index === 0) {
71
- // the operand is the first of the expression, so the sign is implicit +, simply negate the value
72
- result = insertNegativeSign(token);
73
- } else {
74
- // the operand is preceded by something else
75
- result = applySignChange(index, tokens);
76
- }
77
- }
78
-
79
- return result;
80
- },
81
-
82
- /**
83
- * Strategy that applies on operators only
84
- * @param {Number} index - The index of the current token
85
- * @param {token[]} tokens - The list of tokens that represent the expression
86
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
87
- */
88
- function strategyOperator(index, tokens) {
89
- var token = tokens[index] || null;
90
- var type = tokensHelper.getType(token);
91
- var result = null;
92
-
93
- if (tokensHelper.isOperator(type) && index >= 0) {
94
- if (token.type === 'SUB') {
95
- // the operator is -, simply replace it by +
96
- result = replaceByPositiveSign(token, index, tokens);
97
- } else if (token.type === 'ADD') {
98
- // the operator is +, simply replace it by -
99
- result = replaceByNegativeSign(token);
100
- } else if (token.type === 'FAC' && index > 0) {
101
- // the operator is !, need to identify the operand
102
- result = applyStrategies(index - 1, tokens);
103
- }
104
- }
105
-
106
- return result;
107
- },
108
-
109
- /**
110
- * Strategy that applies on identifiers only (constants, variables, functions)
111
- * @param {Number} index - The index of the current token
112
- * @param {token[]} tokens - The list of tokens that represent the expression
113
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
114
- */
115
- function strategyIdentifier(index, tokens) {
116
- var token = tokens[index] || null;
117
- var type = tokensHelper.getType(token);
118
- var result = null;
119
-
120
- if (tokensHelper.isIdentifier(type) && index >= 0) {
121
- if (index === 0) {
122
- // the token is the first of the expression, so the sign is implicit +, simply negate the value
123
- result = insertNegativeSign(token);
124
- } else {
125
- // the token is preceded by something else
126
- result = applySignChange(index - 1, tokens);
127
- }
128
- }
129
-
130
- return result;
131
- },
132
-
133
- /**
134
- * Strategy that applies on sub-expression only
135
- * @param {Number} index - The index of the current token
136
- * @param {token[]} tokens - The list of tokens that represent the expression
137
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
138
- */
139
- function strategyExpression(index, tokens) {
140
- var token = tokens[index] || null;
141
- var type = tokensHelper.getType(token);
142
- var result = null;
143
- var count = 0;
144
-
145
- if (tokensHelper.isAggregator(type) && index >= 0) {
146
- if (token.type === 'RPAR') {
147
- count++;
148
- }
149
-
150
- // find the opening parenthesis
151
- while (index && (token.type !== 'LPAR' || count)) {
152
- index--;
153
- token = tokens[index] || null;
154
-
155
- if (token.type === 'RPAR') {
156
- count++;
157
- }
158
- if (token.type === 'LPAR') {
159
- count--;
160
- }
161
- }
162
-
163
- if (!count && token.type === 'LPAR') {
164
- if (index === 0) {
165
- // the token is the first of the expression, so the sign is implicit +, simply negate the value
166
- result = insertNegativeSign(token);
167
- } else {
168
- // the token is preceded by something else
169
- result = applySignChange(index - 1, tokens);
170
- }
171
- }
172
- }
173
-
174
- return result;
175
- }
176
- ];
177
-
178
- /**
179
- * Apply strategies to produce a sign change descriptor
180
- * @param {Number} index - The index of the current token
181
- * @param {token[]} tokens - The list of tokens that represent the expression
182
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
183
- */
184
- function applyStrategies(index, tokens) {
185
- var result = null;
186
-
187
- _.forEach(strategies, function(strategy) {
188
- result = strategy(index, tokens);
189
- return !result;
190
- });
191
-
192
- return result;
193
- }
194
-
195
- /**
196
- * Apply a sign change at the current index
197
- * @param {Number} index - The index of the current token
198
- * @param {token[]} tokens - The list of tokens that represent the expression
199
- * @returns {signChange|null} - The result of the strategy: `null` if cannot apply, or the descriptor of the change
200
- */
201
- function applySignChange(index, tokens) {
202
- var token = tokens[index] || null;
203
- var nextToken = tokens[index + 1] || null;
204
- var type = tokensHelper.getType(token);
205
- var result = null;
206
-
207
- if (token) {
208
- if (tokensHelper.isOperator(type)) {
209
- // an operator precedes the operand
210
- if (token.type === 'SUB') {
211
- // the operator is -, simply replace it by +
212
- result = replaceByPositiveSign(token, index, tokens);
213
- } else if (token.type === 'ADD') {
214
- // the operator is +, simply replace it by -
215
- result = replaceByNegativeSign(token);
216
- } else if (nextToken) {
217
- // the operator is not + or -, simply negate the value
218
- result = insertNegativeSign(nextToken);
219
- }
220
- } else if (nextToken && (tokensHelper.isFunction(type) || token.type === 'LPAR')) {
221
- // a function or a left parenthesis precedes the operand, simply negate the operand
222
- result = insertNegativeSign(nextToken);
223
- }
224
- }
225
-
226
- return result;
227
- }
228
-
229
- /**
230
- * Checks if a token accept an explicit positive sign on the right
231
- * @param {Object} token
232
- * @returns {Boolean}
233
- */
234
- function acceptExplicitPositive(token) {
235
- return (
236
- !token ||
237
- (refuseExplicitPositive.indexOf(token.type) === -1 && !tokensHelper.isFunction(tokensHelper.getType(token)))
238
- );
239
- }
240
-
241
- /**
242
- * Produces a descriptor to insert a negative sign
243
- * @param {token} token
244
- * @returns {signChange}
245
- */
246
- function insertNegativeSign(token) {
247
- return {
248
- offset: token.offset,
249
- length: 0,
250
- sign: registeredTerms.SUB.value,
251
- move: registeredTerms.SUB.value.length
252
- };
253
- }
254
-
255
- /**
256
- * Produces a descriptor to replace a sign by a negative sign
257
- * @param {token} token
258
- * @returns {signChange}
259
- */
260
- function replaceByNegativeSign(token) {
261
- return {
262
- offset: token.offset,
263
- length: token.value.length,
264
- sign: registeredTerms.SUB.value,
265
- move: registeredTerms.SUB.value.length - token.value.length
266
- };
267
- }
268
-
269
- /**
270
- * Produces a descriptor to replace a sign by a positive sign
271
- * @param {token} token
272
- * @param {Number} index
273
- * @param {token[]} tokens
274
- * @returns {signChange}
275
- */
276
- function replaceByPositiveSign(token, index, tokens) {
277
- var allowExplicit = index && acceptExplicitPositive(tokens[index - 1]);
278
- return {
279
- offset: token.offset,
280
- length: token.value.length,
281
- sign: allowExplicit ? registeredTerms.ADD.value : '',
282
- move: (allowExplicit ? registeredTerms.ADD.value.length : 0) - token.value.length
283
- };
284
- }
285
-
286
- /**
287
- * Replaces text at position
288
- * @param {String} str - The string in which apply the replacement
289
- * @param {Number} index - The index from which applying the replacement
290
- * @param {Number} length - The length of the string to replace
291
- * @param {String} sub - The replacement string
292
- * @returns {String}
293
- */
294
- function splice(str, index, length, sub) {
295
- return str.substring(0, index) + sub + str.substring(index + length);
296
- }
297
-
298
- export default pluginFactory({
299
- name: pluginName,
300
-
301
- /**
302
- * Called when the plugin is installing in its host.
303
- */
304
- install: function install() {
305
- var calculator = this.getCalculator();
306
- calculator.setCommand('sign', __('Sign change'), __('Change the sign of the current operand'));
307
- },
308
-
309
- /**
310
- * Called when the plugin should be initialized.
311
- */
312
- init: function init() {
313
- var calculator = this.getCalculator();
314
-
315
- // applies a sign change based on strategies
316
- calculator.on(nsHelper.namespaceAll('command-sign', pluginName), function() {
317
- var tokens = calculator.getTokens();
318
- var index = calculator.getTokenIndex();
319
- var expression = calculator.getExpression();
320
- var result;
321
-
322
- if (expression !== '0') {
323
- result = applyStrategies(index, tokens);
324
- if (result) {
325
- expression = splice(expression, result.offset, result.length, result.sign);
326
- calculator.replace(expression, calculator.getPosition() + result.move);
327
- }
328
- }
329
- });
330
- },
331
-
332
- /**
333
- * Called when the plugin is destroyed. Mostly when the host is destroyed itself.
334
- */
335
- destroy: function destroy() {
336
- var calculator = this.getCalculator();
337
- calculator.deleteCommand('sign').off('.' + pluginName);
338
- }
339
- });