Skip to content

Commit 3937d51

Browse files
authored
Merge pull request #14 from victorvoid/dev
remove loader container support
2 parents b091277 + 8a76846 commit 3937d51

File tree

8 files changed

+5272
-52
lines changed

8 files changed

+5272
-52
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Placeload
5656

5757
import Placeload from 'placeload.js'
5858

59-
const userContainer = Placeload
59+
const userLoader = Placeload
6060
.$('.user-placeload')
6161
.config({speed: '2s'})
6262
.line((element) => element.width(300).height(200))
@@ -68,10 +68,16 @@ const userContainer = Placeload
6868
.line((element) => element.width(250).height(20))
6969

7070

71-
userContainer.fold(
71+
userLoader.fold(
7272
(err) => console.log('error: ', err),
7373
(allElements) => console.log('allElements: ', allElements)
7474
)
75+
76+
API.getUsers()
77+
.then(users => {
78+
userLoader.remove()
79+
}
80+
)
7581
```
7682

7783
![](https://github.com/victorvoid/placeload.js/blob/master/docs/imgs/placeload_example.gif)

dist/placeload.js

Lines changed: 36 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/placeload.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/placeload.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h2 class="user__name"><a href="#">Sr. João</a></h2>
6262

6363
<script src="../dist/placeload.js"></script>
6464
<script>
65-
Placeload
65+
const user = Placeload
6666
.$('.user-placeload')
6767
.config({speed: '2s'})
6868
.line((element) => element.width(300).height(200))
@@ -74,6 +74,10 @@ <h2 class="user__name"><a href="#">Sr. João</a></h2>
7474
(err) => console.log('error: ', err),
7575
(allElements) => console.log('allElements: ', allElements)
7676
)
77+
// simulation API
78+
setTimeout(() => {
79+
user.remove()
80+
}, 2000)
7781
</script>
7882
</body>
7983
</html>

lib/placeload.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,48 @@ const Right = Either.Right
66
const Left = Either.Left
77

88
const Placeload = {
9-
$: (x) => place(IO(() => {
10-
const container = document.querySelector(x)
11-
if (container) {
9+
$: (x) => utils(getHoldersElements(x), selector(x))
10+
}
11+
12+
// selector :: String -> Either
13+
const selector = (x) => {
14+
const element = document.querySelector(x)
15+
if(element)
16+
return Right(element)
17+
return Left(`Don\' found ${x} element`)
18+
}
19+
20+
// getHoldersElements :: String -> Either
21+
const getHoldersElements = (x) => {
22+
return IO(() => {
23+
return selector(x).chain((container) => {
1224
const elementPlaceload = styl(document.createElement('div')).addClass('placeload-background').toDOM()
1325
container.appendChild(elementPlaceload)
1426
return Right({
1527
container: container,
1628
placeload: elementPlaceload,
1729
elems: []
1830
})
19-
}
20-
return Left(`Don\' found ${x} element`)
21-
}))
31+
})
32+
})
2233
}
2334

2435
// place :: IO -> Object
25-
const place = (_IO) => {
36+
const utils = (_IO, container) => {
2637
return ({
27-
config: (configs) => place(configIO(_IO, configs)),
28-
line: f => place(drawIO(f, _IO)),
29-
spaceBetween: size => place(drawIO({spaceBetween: size}, _IO)),
30-
fold: (err, succ) => _IO.runIO().either(err, succ),
38+
config: (configs) => utils(configIO(_IO, configs), container),
39+
line: f => utils(drawIO(f, _IO), container),
40+
spaceBetween: size => utils(drawIO({spaceBetween: size}, _IO), container),
41+
fold: (err, succ) => {
42+
_IO.runIO().either(err, succ)
43+
return {
44+
remove: () => {
45+
container.map(element => {
46+
element.parentNode.removeChild(element);
47+
})
48+
}
49+
}
50+
},
3151
inspect: () => console.log('IO: ', _IO.toString())
3252
})
3353
}
@@ -58,10 +78,10 @@ const configIO = (_IO, config) => {
5878
const placeloadHeight = elem.placeload.style.height || 0
5979
const newHeightToPlaceload = parseInt(placeloadHeight) + parseInt(stylWithDefault.spaceBetween)
6080
const newElement = styl(document.createElement('div'))
61-
.width('100%')
62-
.height(stylWithDefault.spaceBetween)
63-
.marginTop(placeloadHeight)
64-
.addClass('placeload-masker').toDOM()
81+
.width('100%')
82+
.height(stylWithDefault.spaceBetween)
83+
.marginTop(placeloadHeight)
84+
.addClass('placeload-masker').toDOM()
6585

6686
if (stylWithDefault.right) {
6787
runDrawRightLine(elem, stylWithDefault).runIO()
@@ -80,11 +100,11 @@ const runDrawRightLine = (elem, stylWithDefault) => {
80100
const lastChild = elem.elems[elem.elems.length - 2]
81101
const itemToRight = elem.elems[elem.elems.length - 1]
82102
const elementRow = styl(document.createElement('div'))
83-
.addClass('placeload-masker')
84-
.width('100%')
85-
.height('10px')
86-
.marginTop('20px')
87-
.marginLeft(lastChild.style.marginLeft).toDOM()
103+
.addClass('placeload-masker')
104+
.width('100%')
105+
.height('10px')
106+
.marginTop('20px')
107+
.marginLeft(lastChild.style.marginLeft).toDOM()
88108

89109
styl(lastChild).hide()
90110
styl(itemToRight)
@@ -103,13 +123,13 @@ const createElemFolk = (elements, newElement, prop, value) => {
103123
return elements.map(elem => {
104124
const height = elem.placeload.style.height || 0
105125
switch (prop) {
106-
case 'width':
107-
styl(newElement).width(`100%`).marginLeft(`${value}%`).marginTop(height)
108-
break
109-
case 'height':
110-
styl(newElement).height(`${value}px`)
111-
styl(elem.placeload).height(`${parseInt(height) + value}px`)
112-
break
126+
case 'width':
127+
styl(newElement).width(`100%`).marginLeft(`${value}%`).marginTop(height)
128+
break
129+
case 'height':
130+
styl(newElement).height(`${value}px`)
131+
styl(elem.placeload).height(`${parseInt(height) + value}px`)
132+
break
113133
}
114134
elem.placeload.appendChild(newElement)
115135
return elem

0 commit comments

Comments
 (0)