Skip to content

Commit

Permalink
all OPC variables are made reactive through "set"
Browse files Browse the repository at this point in the history
- Also, added the documentation for "expand" and "collapse functions
  • Loading branch information
msawired committed Apr 8, 2022
1 parent 53a2d87 commit 9b768aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 56 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ OPC.palette('palette',
## Events

### parameterChanged(variableName, value)

To get an alert everytime a variable changes, you can create "parameterChanged" function in your sketch. For example, if your sketch requires resizing when user changes a variable, you can use this function to get the alert and make necessary changes.

**Example**
Expand All @@ -123,3 +124,23 @@ function parameterChanged(variableName, value) {
}
}
```

## Utilities

### collapse()

Collapses the OPC configurator panel.
**Example**

```javascript
OPC.collapse();
```

### expand()

Expands the OPC configurator panel.
**Example**

```javascript
OPC.expand();
```
82 changes: 26 additions & 56 deletions opc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
class OPC {
static options = {};
static variables = [];
constructor(container = null) {
this.container = container;
constructor() {
this.options = {};
this.existingOptions = {};
this.variables = [];
this.minimized = false;
this.collapsed = false;
}

static slider(variableName, value, min = 0, max = null, step = null){
Expand All @@ -29,15 +25,7 @@ class OPC {
max: max,
step: step
}
this.callParentFunction('OPC', this.options[variableName] );
//delete existing
Object.defineProperty(window, variableName, {
get: function () {
return OPC.options[variableName].value;
}
});
this.callParentFunction('OPC', this.options[variableName]);
return true;
return this.initVariable(this.options[variableName]);
}

static toggle(variableName, value = true) {
Expand All @@ -53,15 +41,7 @@ class OPC {
type: 'toggle',
value: value
}
this.callParentFunction('OPC', this.options[variableName]);
//delete existing
Object.defineProperty(window, variableName, {
get: function () {
return OPC.options[variableName].value;
}
});
this.callParentFunction('OPC', this.options[variableName]);
return true;
return this.initVariable(this.options[variableName]);
}

static palette(variableName, value, options) {
Expand All @@ -78,15 +58,7 @@ class OPC {
value: value,
options: options
}
this.callParentFunction('OPC', this.options[variableName]);
//delete existing
Object.defineProperty(window, variableName, {
get: function () {
return OPC.options[variableName].value;
}
});
this.callParentFunction('OPC', this.options[variableName]);
return true;
return this.initVariable(this.options[variableName]);
}
static color(variableName, value = '#333333') {
//check existing params
Expand All @@ -101,15 +73,7 @@ class OPC {
type: 'color',
value: value
}
this.callParentFunction('OPC', this.options[variableName]);
//delete existing
Object.defineProperty(window, variableName, {
get: function () {
return OPC.options[variableName].value;
}
});
this.callParentFunction('OPC', this.options[variableName]);
return true;
return this.initVariable(this.options[variableName]);
}

static text(variableName, value, placeholder = null, maxChars = 1000) {
Expand All @@ -127,32 +91,38 @@ class OPC {
placeholder: placeholder,
max: maxChars
}
this.callParentFunction('OPC', this.options[variableName]);
//delete existing
Object.defineProperty(window, variableName, {
return this.initVariable(this.options[variableName]);
}

static initVariable = function(option){
Object.defineProperty(window, option.name, {
get: function () {
return OPC.options[variableName].value;
return OPC.options[option.name].value;
},
set: function (value) {
OPC.options[option.name].value = value;
OPC.callParentFunction('OPC', OPC.options[option.name]);
if (typeof window.parameterChanged == 'function') {
window.parameterChanged(option.name, value);
}
}
});
this.callParentFunction('OPC', this.options[variableName]);
this.callParentFunction('OPC', option);
return true;
}


static set = function (variableName, value){
OPC.options[variableName].value = value;
if (typeof window.parameterChanged == 'function') {
window.parameterChanged(variableName, value);
}
window[variableName] = value;
}

static minimize = function (){
OPC.minimized = true;
this.callParentFunction('OPC_minimized', OPC.minimized);
static collapse = function (){
OPC.collapsed = true;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}
static expand = function (){
OPC.minimized = false;
this.callParentFunction('OPC_minimized', OPC.minimized);
OPC.collapsed = false;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}

static callParentFunction = function (functionName, arg = {}) {
Expand Down

0 comments on commit 9b768aa

Please sign in to comment.