Skip to content

Commit 1d51e7d

Browse files
committed
Methods is a class
1 parent 2b8dda0 commit 1d51e7d

File tree

7 files changed

+66
-53
lines changed

7 files changed

+66
-53
lines changed

Methods.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
# What is Methods?
77
Have you ever get frustated of writing the same code in html to get what you want. Then here you are use this package? to make it easy by defining your method using a **"def"** tag and **"name"** attribute to name your method.
88

9+
# Version info:
10+
- Methods class - Added because the import is not working when ``window.onload`` is used in the user script because Methods.js uses the same event.
11+
- Organised the folder structure
12+
- As Methods class is available user can access in their own script only when using ``window.onload`` event. If the event is not used in user script there is no need to access the class they can just link methods.js and can use import tags. For more info visit [docs](https://the-atelier.ml/Pages/Methods/methods.html)
913
# Usage:
1014
**Using on same file (``<def>``):**
1115
- Methods package is very simple to use.
1216
- All you need to do is to define the method using **"def"** tag and **"name"** attribute.
1317
- And then to call the method you can use the value you provided in name attribute.
1418
- That's it.
19+
1520
**Example**
1621
```html
1722
<test></test>

anotherHtml.html renamed to examples/anotherHtml.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<meta charset="UTF-8">
44
<meta http-equiv="X-UA-Compatible" content="IE=edge">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<script src="Methods.js"></script>
6+
<script src="/source/Methods.js"></script>
77
<title>Document</title>
88
</head>
99
<body>

Resources/ex.html renamed to examples/ex.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="X-UA-Compatible" content="IE=edge">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Document</title>
7-
<script src="../Methods.js"></script>
7+
<script src="/source/Methods.js"></script>
88
</head>
99
<body>
1010
<def name="example">

index.html renamed to examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Document</title>
8-
<script src="Methods.js"></script>
8+
<script src="/source/Methods.js"></script>
99
</head>
1010
<body>
1111
<!--use the method name as the tag name i.e here test and commit are the method names-->
File renamed without changes.

source/Methods.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
window.onload = function () {
2+
methods = new Methods()
3+
methods.run()
4+
}
5+
6+
class Methods {
7+
run() {
8+
let definition = document.getElementsByTagName('def')
9+
let importStatements = document.getElementsByTagName('import')
10+
let methodNames = []
11+
let methodContent = []
12+
let methodDefTags = []
13+
let DOMparser = new DOMParser()
14+
15+
for (let k = 0; k < importStatements.length; k++) {
16+
let importStatement = importStatements[k]
17+
let importFrom = importStatement.getAttribute('from')
18+
let importTag = importStatement.getAttribute('name')
19+
20+
fetch(importFrom)
21+
.then(response => {
22+
response.text().then(data => {
23+
let defTags = (DOMparser.parseFromString(data, 'text/html').body.getElementsByTagName('def'))
24+
for (let d = 0; d < defTags.length; d++) {
25+
const def = defTags[d]
26+
const defAttr = def.getAttribute('name')
27+
28+
if (importTag == defAttr) {
29+
importStatement.innerHTML = def.innerHTML
30+
importStatement.style.width = '100%'
31+
}
32+
}
33+
})
34+
})
35+
}
36+
37+
for (let i = 0; i < definition.length; i++) {
38+
let methodDefs = definition[i];
39+
methodDefs.style.visibility = 'hidden'
40+
methodNames.push(methodDefs.getAttribute('name'))
41+
methodContent.push(methodDefs.innerHTML)
42+
methodDefTags.push(methodDefs)
43+
}
44+
45+
for (let j = 0; j < methodNames.length; j++) {
46+
const methodName = methodNames[j];
47+
let methodCall = document.getElementsByTagName(methodName)
48+
for (let k = 0; k < methodCall.length; k++) {
49+
let tag = methodCall[k];
50+
tag.innerHTML = methodContent[j]
51+
}
52+
}
53+
54+
for (var l = 0; l < methodDefTags.length; l++) {
55+
methodDefTags[l].remove()
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)