Skip to content

Commit

Permalink
added OPC.delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
msawired committed Dec 15, 2022
1 parent 2b91bcb commit 09a6d9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,12 @@ Expands the OPC configurator panel.
```javascript
OPC.expand();
```

### delete(variableName)

Deletes a variable and removes its UI component from the interface.
**Example**

```javascript
OPC.delete('myVariable');
```
52 changes: 30 additions & 22 deletions opc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ class OPC {
this.collapsed = false;
}

static slider(variableName, value, min = 0, max = null, step = null){
static slider(variableName, value, min = 0, max = null, step = null) {
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)){
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = +url.searchParams.get(variableName);
}


max = max == null? value*2: max;
step = step == null? value/10: step;
max = max == null ? value * 2 : max;
step = step == null ? value / 10 : step;

this.options[variableName] = {
name: variableName,
Expand All @@ -26,14 +26,14 @@ class OPC {
step: step
}
return this.initVariable(this.options[variableName]);
}
}

static toggle(variableName, value = true) {
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = +url.searchParams.get(variableName) == 1 ? true: false;
value = +url.searchParams.get(variableName) == 1 ? true : false;
}

this.options[variableName] = {
Expand All @@ -42,7 +42,7 @@ class OPC {
value: value
}
return this.initVariable(this.options[variableName]);
}
}

static palette(variableName, value, options) {
//check existing params
Expand All @@ -59,7 +59,7 @@ class OPC {
options: options
}
return this.initVariable(this.options[variableName]);
}
}
static color(variableName, value = '#333333') {
//check existing params
let url = new URL(document.location.href);
Expand All @@ -74,7 +74,7 @@ class OPC {
value: value
}
return this.initVariable(this.options[variableName]);
}
}

static text(variableName, value, placeholder = null, maxChars = 1000) {
//check existing params
Expand All @@ -92,7 +92,7 @@ class OPC {
max: maxChars
}
return this.initVariable(this.options[variableName]);
}
}
static button(variableName, buttonText) {
//check existing params
let url = new URL(document.location.href);
Expand All @@ -107,10 +107,11 @@ class OPC {
value: buttonText
}
return this.initVariable(this.options[variableName]);
}
}

static initVariable = function(option){
static initVariable = function (option) {
Object.defineProperty(window, option.name, {
configurable: true,
get: function () {
return OPC.options[option.name].value;
},
Expand All @@ -124,38 +125,45 @@ class OPC {
});
this.callParentFunction('OPC', option);
return true;
}
}


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

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

}
static set = function (variableName, value){
static set = function (variableName, value) {
window[variableName] = value;
}

static collapse = function (){
static collapse = function () {
OPC.collapsed = true;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}
static expand = function (){
static expand = function () {
OPC.collapsed = false;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}
static delete = function (variableName) {
if (OPC.options[variableName]){
delete OPC.options[variableName];
delete window.variableName;
}
OPC.callParentFunction('OPC_delete', variableName);
}

static callParentFunction = function (functionName, arg = {}) {
// console.log(arg);
Expand Down

0 comments on commit 09a6d9f

Please sign in to comment.