Skip to content

Commit

Permalink
button option added
Browse files Browse the repository at this point in the history
  • Loading branch information
msawired committed May 11, 2022
1 parent 26e9405 commit 2b91bcb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ OPC.text(variableName, defaultValue, [placeholder]);
OPC.text('my_text', '', 'Enter Title');
```

### Button

Displays a button. You should use buttonPressed and/or buttonReleased events (see below) to detect user interaction. For simple interactions, using buttonReleased function should suffice.
**buttonText** parameter is used in the button text, and it is also set as the default value for the button variable.

```javascript
OPC.button(variableName, buttonText);
//example: OPC.button('myButton', 'Click Me!');
```

**Default values**
defaultValue: #333333

### Color

Displays a single color selector. Uses the native browser color picker, so the interface may vary. Hex values are recommended. Alpha values are not supported.
Expand Down Expand Up @@ -125,6 +138,35 @@ function parameterChanged(variableName, value) {
}
```

### buttonPressed([variableName], [value])

To get an alert everytime user presses a button. If you are using multiple buttons, you can differentiate by looking up the variableName.

**Example**

```javascript
function buttonPressed(variableName, value) {
if (variableName === 'myButton') {
print('Button is pressed');
}
}
```

### buttonReleased([variableName], [value])

To get an alert everytime user releases a pressed a button. If you are using multiple buttons, you can differentiate by looking up the variableName.

**Example**

```javascript
function buttonPressed(variableName, value) {
if (variableName === 'myButton') {
print('Button is released');
}
}
```


## Utilities

### collapse()
Expand Down
32 changes: 32 additions & 0 deletions opc.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ class OPC {
}
return this.initVariable(this.options[variableName]);
}
static button(variableName, buttonText) {
//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);
}

this.options[variableName] = {
name: variableName,
type: 'button',
value: buttonText
}
return this.initVariable(this.options[variableName]);
}

static initVariable = function(option){
Object.defineProperty(window, option.name, {
Expand All @@ -112,6 +127,23 @@ class OPC {
}


static set = function (variableName, value){
window[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){
OPC.options[variableName].value = value;
if (typeof window.buttonReleased == 'function') {
window.buttonReleased(variableName, value);
}

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

0 comments on commit 2b91bcb

Please sign in to comment.