@cocreate/calculate 1.14.1
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/.github/FUNDING.yml +3 -0
- package/.github/workflows/automated.yml +44 -0
- package/.github/workflows/manual.yml +44 -0
- package/CHANGELOG.md +1518 -0
- package/CONTRIBUTING.md +96 -0
- package/CoCreate.config.js +23 -0
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/demo/index.html +30 -0
- package/docs/index.html +378 -0
- package/package.json +66 -0
- package/release.config.js +22 -0
- package/src/index.js +169 -0
- package/webpack.config.js +90 -0
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cocreate/calculate",
|
|
3
|
+
"version": "1.14.1",
|
|
4
|
+
"description": "A handy vanilla JavaScript calculator, concatenate multiple elements containing integers & execute calculates. Can be used for creating invoices,making payments & any kind of complex calculate. Easily configured using HTML5 attributes and/or JavaScript API.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"calculate",
|
|
7
|
+
"calculate-values",
|
|
8
|
+
"calculate-total",
|
|
9
|
+
"cocreate",
|
|
10
|
+
"no-code-framework",
|
|
11
|
+
"cocreatejs",
|
|
12
|
+
"cocreatejs-component",
|
|
13
|
+
"cocreate-framework",
|
|
14
|
+
"no-code",
|
|
15
|
+
"low-code",
|
|
16
|
+
"collaborative-framework",
|
|
17
|
+
"realtime",
|
|
18
|
+
"realtime-framework",
|
|
19
|
+
"collaboration",
|
|
20
|
+
"shared-editing",
|
|
21
|
+
"html5-framework",
|
|
22
|
+
"javascript-framework"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"start": "npx webpack --config webpack.config.js",
|
|
29
|
+
"build": "npx webpack --mode=production --config webpack.config.js",
|
|
30
|
+
"dev": "npx webpack --config webpack.config.js --watch",
|
|
31
|
+
"postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/CoCreate-app/CoCreate-calculate.git"
|
|
36
|
+
},
|
|
37
|
+
"author": "CoCreate LLC",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/CoCreate-app/CoCreate-calculate/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://cocreate.app/docs/calculate",
|
|
43
|
+
"funding": {
|
|
44
|
+
"type": "GitHub Sponsors ❤",
|
|
45
|
+
"url": "https://github.com/sponsors/CoCreate-app"
|
|
46
|
+
},
|
|
47
|
+
"main": "./src/index.js",
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@babel/core": "^7.9.6",
|
|
50
|
+
"@babel/preset-env": "^7.9.6",
|
|
51
|
+
"babel-loader": "^8.1.0",
|
|
52
|
+
"clean-webpack-plugin": "^3.0.0",
|
|
53
|
+
"file-loader": "^6.2.0",
|
|
54
|
+
"mini-css-extract-plugin": "^1.5.0",
|
|
55
|
+
"style-loader": "^3.3.1",
|
|
56
|
+
"terser-webpack-plugin": "^5.1.1",
|
|
57
|
+
"webpack": "^5.24.4",
|
|
58
|
+
"webpack-cli": "^4.5.0",
|
|
59
|
+
"webpack-log": "^3.0.1"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@cocreate/element-prototype": "^1.15.0",
|
|
63
|
+
"@cocreate/observer": "^1.14.0",
|
|
64
|
+
"@cocreate/utils": "^1.29.0"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
dryRun: false,
|
|
3
|
+
branches: ["master"],
|
|
4
|
+
plugins: [
|
|
5
|
+
"@semantic-release/commit-analyzer",
|
|
6
|
+
"@semantic-release/release-notes-generator",
|
|
7
|
+
[
|
|
8
|
+
"@semantic-release/changelog",
|
|
9
|
+
{
|
|
10
|
+
changelogFile: "CHANGELOG.md",
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
"@semantic-release/npm",
|
|
14
|
+
"@semantic-release/github",
|
|
15
|
+
[
|
|
16
|
+
"@semantic-release/git",
|
|
17
|
+
{
|
|
18
|
+
assets: ["CHANGELOG.md", "package.json"],
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
],
|
|
22
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import observer from '@cocreate/observer';
|
|
2
|
+
import { getAttributes } from '@cocreate/utils';
|
|
3
|
+
// import { renderValue } from '@cocreate/render';
|
|
4
|
+
import '@cocreate/element-prototype';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function init() {
|
|
8
|
+
let calculateElements = document.querySelectorAll('[calculate]');
|
|
9
|
+
initElements(calculateElements);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function initElements(elements) {
|
|
13
|
+
for (let el of elements)
|
|
14
|
+
initElement(el);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function initElement(element) {
|
|
18
|
+
let calculate = element.getAttribute('calculate');
|
|
19
|
+
if (calculate.includes('{{') || calculate.includes('{['))
|
|
20
|
+
return;
|
|
21
|
+
|
|
22
|
+
let selectors = getSelectors(calculate);
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < selectors.length; i++) {
|
|
25
|
+
// if (selectors[i].includes('{{')) return;
|
|
26
|
+
|
|
27
|
+
// initEvents(element, selectors[i]);
|
|
28
|
+
let inputs = document.querySelectorAll(selectors[i]);
|
|
29
|
+
for (let input of inputs) {
|
|
30
|
+
initEvent(element, input);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
observer.init({
|
|
34
|
+
name: 'calculateSelectorInit',
|
|
35
|
+
observe: ['addedNodes'],
|
|
36
|
+
target: selectors[i],
|
|
37
|
+
callback(mutation) {
|
|
38
|
+
initEvent(element, mutation.target);
|
|
39
|
+
setCalcationResult(element);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
setCalcationResult(element);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getSelectors(string) {
|
|
47
|
+
let regex = /\{\((?:(?!\{\().)*?\)\}/;
|
|
48
|
+
let selectors = [];
|
|
49
|
+
|
|
50
|
+
let match;
|
|
51
|
+
while ((match = regex.exec(string)) !== null) {
|
|
52
|
+
// Extract the content inside the braces (excluding the leading '{(' and trailing ')}')
|
|
53
|
+
let selector = match[0].slice(2, -2);
|
|
54
|
+
|
|
55
|
+
if (selectors.indexOf(selector) === -1) {
|
|
56
|
+
selectors.push(selector);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Replace the found match with an empty string to avoid reprocessing in the next iteration
|
|
60
|
+
string = string.replace(match[0], '');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return selectors;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function getValues(calculate) {
|
|
67
|
+
let selectors = getSelectors(calculate);
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < selectors.length; i++) {
|
|
70
|
+
let selector = selectors[i];
|
|
71
|
+
|
|
72
|
+
let value = null;
|
|
73
|
+
let inputs = document.querySelectorAll(selector);
|
|
74
|
+
|
|
75
|
+
for (let input of inputs) {
|
|
76
|
+
let val = null;
|
|
77
|
+
if (input.getValue)
|
|
78
|
+
val = Number(await input.getValue());
|
|
79
|
+
|
|
80
|
+
if (!Number.isNaN(value)) {
|
|
81
|
+
value += val;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (value != null && !Number.isNaN(value)) {
|
|
86
|
+
calculate = calculate.replaceAll('{' + selector + '}', value);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return calculate;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function initEvent(element, input) {
|
|
94
|
+
input.addEventListener('input', function () {
|
|
95
|
+
setCalcationResult(element);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// if (input.hasAttribute('calculate')) {
|
|
99
|
+
// input.addEventListener('changedCalcValue', function(e) {
|
|
100
|
+
// setCalcationResult(element);
|
|
101
|
+
// });
|
|
102
|
+
// }
|
|
103
|
+
// setCalcationResult(element);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function setCalcationResult(element) {
|
|
107
|
+
const { object, isRealtime } = getAttributes(element);
|
|
108
|
+
let calculate = element.getAttribute('calculate');
|
|
109
|
+
|
|
110
|
+
let calString = await getValues(calculate);
|
|
111
|
+
|
|
112
|
+
if (calString) {
|
|
113
|
+
let result = calculate(calString);
|
|
114
|
+
|
|
115
|
+
// TODO: input event below triggers save for all input elements but will not save for regular elements
|
|
116
|
+
if (element.setValue) {
|
|
117
|
+
element.setValue(result)
|
|
118
|
+
if (object && isRealtime != "false") {
|
|
119
|
+
element.save(element);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// if (element.value){
|
|
124
|
+
|
|
125
|
+
// }
|
|
126
|
+
// else {
|
|
127
|
+
element.innerHTML = result;
|
|
128
|
+
// }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let inputEvent = new CustomEvent('input', { bubbles: true });
|
|
132
|
+
Object.defineProperty(inputEvent, 'target', { writable: false, value: element });
|
|
133
|
+
element.dispatchEvent(inputEvent);
|
|
134
|
+
|
|
135
|
+
//. set custom event
|
|
136
|
+
// var event = new CustomEvent('changedCalcValue', {
|
|
137
|
+
// bubbles: true,
|
|
138
|
+
// });
|
|
139
|
+
// element.dispatchEvent(event);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function calculate(string) {
|
|
145
|
+
if (/^[0-9+\-*/()%||?\s:=.]*$/.test(string))
|
|
146
|
+
return eval(string);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
observer.init({
|
|
150
|
+
name: 'CoCreateCalculateChangeValue',
|
|
151
|
+
observe: ['attributes'],
|
|
152
|
+
attributeName: ['calculate'],
|
|
153
|
+
callback(mutation) {
|
|
154
|
+
setCalcationResult(mutation.target);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
observer.init({
|
|
159
|
+
name: 'CoCreateCalculateInit',
|
|
160
|
+
observe: ['addedNodes'],
|
|
161
|
+
target: '[calculate]',
|
|
162
|
+
callback(mutation) {
|
|
163
|
+
initElement(mutation.target);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
init();
|
|
168
|
+
|
|
169
|
+
export default { initElements, initElement, calculate };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const path = require("path")
|
|
2
|
+
const TerserPlugin = require("terser-webpack-plugin")
|
|
3
|
+
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
|
|
4
|
+
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
|
|
5
|
+
|
|
6
|
+
module.exports = (env, argv) => {
|
|
7
|
+
let isProduction = false
|
|
8
|
+
if (argv.mode === 'production')
|
|
9
|
+
isProduction = true
|
|
10
|
+
|
|
11
|
+
const config = {
|
|
12
|
+
entry: {
|
|
13
|
+
"CoCreate-calculate": "./src/index.js",
|
|
14
|
+
},
|
|
15
|
+
output: {
|
|
16
|
+
path: path.resolve(__dirname, "dist"),
|
|
17
|
+
filename: isProduction ? "[name].min.js" : "[name].js",
|
|
18
|
+
libraryTarget: "umd",
|
|
19
|
+
libraryExport: "default",
|
|
20
|
+
library: ["CoCreate", "calculate"],
|
|
21
|
+
globalObject: "this",
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
plugins: [
|
|
25
|
+
new CleanWebpackPlugin(),
|
|
26
|
+
new MiniCssExtractPlugin({
|
|
27
|
+
filename: "[name].css",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
// Default mode for Webpack is production.
|
|
31
|
+
// Depending on mode Webpack will apply different things
|
|
32
|
+
// on final bundle. For now we don't need production's JavaScript
|
|
33
|
+
// minifying and other thing so let's set mode to development
|
|
34
|
+
mode: isProduction ? "production" : "development",
|
|
35
|
+
module: {
|
|
36
|
+
rules: [
|
|
37
|
+
{
|
|
38
|
+
test: /.js$/,
|
|
39
|
+
exclude: /(node_modules)/,
|
|
40
|
+
use: {
|
|
41
|
+
loader: "babel-loader",
|
|
42
|
+
options: {
|
|
43
|
+
plugins: ["@babel/plugin-transform-modules-commonjs"],
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
test: /.css$/i,
|
|
49
|
+
use: [
|
|
50
|
+
{ loader: "style-loader", options: { injectType: "linkTag" } },
|
|
51
|
+
"file-loader",
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
// add source map
|
|
58
|
+
...(isProduction ? {} : { devtool: "eval-source-map" }),
|
|
59
|
+
|
|
60
|
+
optimization: {
|
|
61
|
+
minimize: true,
|
|
62
|
+
minimizer: [
|
|
63
|
+
new TerserPlugin({
|
|
64
|
+
extractComments: true,
|
|
65
|
+
// cache: true,
|
|
66
|
+
parallel: true,
|
|
67
|
+
// sourceMap: true, // Must be set to true if using source-maps in production
|
|
68
|
+
terserOptions: {
|
|
69
|
+
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
|
|
70
|
+
// extractComments: 'all',
|
|
71
|
+
compress: {
|
|
72
|
+
drop_console: true,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
splitChunks: {
|
|
78
|
+
chunks: "all",
|
|
79
|
+
minSize: 200,
|
|
80
|
+
// maxSize: 99999,
|
|
81
|
+
//minChunks: 1,
|
|
82
|
+
|
|
83
|
+
cacheGroups: {
|
|
84
|
+
defaultVendors: false,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
return config
|
|
90
|
+
}
|