Skip to content

Commit 19737f5

Browse files
authoredJul 12, 2023
Merge pull request #43 from MarceloZapatta/feat/allow-element-constructor
feat: accept HTMLElement or string on the constructor
2 parents 4ef5f51 + 8e4442f commit 19737f5

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed
 

‎build/scratchcard.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎build/scratchcard.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<script type="text/javascript">
1919
window.addEventListener('load', function () {
2020
var scContainer = document.getElementById('js--sc--container');
21-
var sc = new ScratchCard('#js--sc--container', {
21+
var sc = new ScratchCard(scContainer, {
2222
enabledPercentUpdate: true,
2323
scratchType: SCRATCH_TYPE.LINE,
2424
// brushSrc: './images/brush.png',

‎package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ and pick in the folder **./build** the file **scratchard.min.js**
2323
import {ScratchCard, SCRATCH_TYPE} from 'scratchcard-js'
2424

2525
const scContainer = document.getElementById('js--sc--container')
26+
27+
// You also can pass HTML Element or a string for querySelector
28+
// new ScratchCard(htmlElement...
29+
2630
const sc = new ScratchCard('#js--sc--container', {
2731
scratchType: SCRATCH_TYPE.SPRAY,
2832
containerWidth: scContainer.offsetWidth,

‎src/ScratchCard.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ScratchCard {
1818
public zone: {top: number, left: number};
1919
public percent: number;
2020

21-
constructor (selector: string, config: SC_CONFIG) {
21+
constructor (selector: string|HTMLElement, config: SC_CONFIG) {
2222
const self = this;
2323
const defaults = {
2424
scratchType: SCRATCH_TYPE.LINE,
@@ -40,7 +40,10 @@ class ScratchCard {
4040

4141
this.config = {...defaults, ...config};
4242
this.scratchType = this.config.scratchType;
43-
this.container = <HTMLElement> document.querySelector(selector);
43+
this.container = <HTMLElement> (
44+
this.isString(selector) ?
45+
document.querySelector(String(selector)) : selector
46+
);
4447
this.position = [0, 0]; // init position
4548
this.readyToClear = false;
4649
this.percent = 0;
@@ -125,6 +128,16 @@ class ScratchCard {
125128
}, 16));
126129
}
127130

131+
132+
/**
133+
* Check if selector is a string
134+
* @param selector
135+
* @returns {boolean}
136+
*/
137+
isString(selector: string|HTMLElement): boolean {
138+
return (typeof selector === 'string' || selector instanceof String)
139+
}
140+
128141
/**
129142
* Get percent of scratchCard
130143
* @returns {number}

0 commit comments

Comments
 (0)
Failed to load comments.