Skip to content

Commit ee1b46d

Browse files
committed
before, after, prepend, append methods introduced
1 parent 32085b5 commit ee1b46d

File tree

9 files changed

+258
-25
lines changed

9 files changed

+258
-25
lines changed

.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"node": true
99
},
1010
"globals": {
11-
"console": true,
12-
"_": true
11+
"_": true,
12+
"uxr": true
1313
},
1414
"extends": "eslint:recommended",
1515
"rules": {

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ A minimal in mind library for DOM related routines and element selections. UXR w
77

88
UXR has the philosophy of fewer code and low file size. Because of this, widely supported ES6 codes are not transpiled to ES5 versions and not trying to cover all JavaScript methods which are normally written without much effort. UXR provides easy to wrappers for normally complex once.
99

10+
## Browser Support
11+
| Browser | [![chrome][chrome]] | [![firefox][firefox]] | [![opera][opera]] | [![safari][safari]] | [![edge][edge]] |
12+
| ------- | ------------------- | --------------------- | ----------------- | ------------------- | --------------- |
13+
| Version | 49+ | 36+ | 37+ | 10+ | 12+ |
14+
1015
## How To Use
1116
After adding the `dist/uxr.min.js` to your page, you can select an element set from DOM and start to manipulate/modify the selection.
1217

@@ -185,11 +190,40 @@ All of the following strings are valid wrapper definitions
185190
* `<div class="wrapper" />` _tag name with attributes_
186191
* `<div class='wrapper' id="container"></div>`
187192
193+
### Element Insertions
194+
By using `before`, `after`, `prepend` and `append` you can control where to insert newly created elements.
195+
196+
``` js
197+
let el = uxr('.container');
198+
199+
// adds an element before selection
200+
el.before('<p>This will before "el"</p>');
201+
el.before(uxr('#new'));
202+
203+
// adds an element after selection
204+
el.after('<p>This will before "el"</p>');
205+
el.after(uxr('#new'));
206+
207+
// appends an element add the end of selection's content
208+
el.append('<p>This will before "el"</p>');
209+
el.append(uxr('#new'));
210+
211+
// appends an element add the beginning of selection's content
212+
el.prepend('<p>This will before "el"</p>');
213+
el.prepend(uxr('#new'));
214+
```
215+
188216
[npm]: https://img.shields.io/npm/v/uxr.svg
189217
[npm-url]: https://npmjs.com/package/uxr
190218
191219
[tests]: http://img.shields.io/travis/bcinarli/uxr.svg
192220
[tests-url]: https://travis-ci.org/bcinarli/uxr
193221
194222
[cover]: https://codecov.io/gh/bcinarli/uxr/branch/master/graph/badge.svg
195-
[cover-url]: https://codecov.io/gh/bcinarli/uxr
223+
[cover-url]: https://codecov.io/gh/bcinarli/uxr
224+
225+
[chrome]: http://uxrocket.io/browsers/chrome.png
226+
[firefox]: http://uxrocket.io/browsers/firefox.png
227+
[opera]: http://uxrocket.io/browsers/opera.png
228+
[safari]: http://uxrocket.io/browsers/safari.png
229+
[edge]: http://uxrocket.io/browsers/edge.png

dist/uxr.js

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* uxr
44
**/
55

6-
const _ = window.uxr = function (selector, context) {
7-
return new uxr(selector, context);
6+
const _ = window.uxr = function (selector) {
7+
return new uxr(selector);
88
};
99

10-
const uxr = function (selector, context) {
10+
const uxr = function (selector) {
1111
this.selector = selector;
12-
this.context = context;
1312
this.init();
1413
};
1514

@@ -279,6 +278,9 @@ Element.prototype.matches = Element.prototype.matches ? Element.prototype.matche
279278
* manipulation
280279
**/
281280

281+
/* global getInsertableElement */
282+
/* global insertBefore */
283+
282284
_.extend.empty = function () {
283285
this.el.forEach(item => item.innerHTML = '');
284286

@@ -290,6 +292,33 @@ _.extend.remove = function () {
290292

291293
return this;
292294
};
295+
296+
_.extend.append = function (stringOrObject) {
297+
this.el.forEach(item => item.appendChild(getInsertableElement(stringOrObject)));
298+
299+
return this;
300+
};
301+
302+
_.extend.prepend = function (stringOrObject) {
303+
this.el.forEach(
304+
item => insertBefore(stringOrObject, item, 'firstChild'));
305+
306+
return this;
307+
};
308+
309+
_.extend.after = function (stringOrObject) {
310+
this.el.forEach(
311+
item => insertBefore(stringOrObject, item, 'nextSibling', true));
312+
313+
return this;
314+
};
315+
316+
_.extend.before = function (stringOrObject) {
317+
this.el.forEach(
318+
item => insertBefore(stringOrObject, item, 'self', true));
319+
320+
return this;
321+
};
293322
/**
294323
* ready
295324
**/
@@ -349,6 +378,40 @@ const maybeMultiple = s => typeof s === 'string' ? justifyString(s).split(' ') :
349378
// Dom String Format
350379
// eslint-disable-next-line
351380
const toDomString = s => s.substr(0, 1).toLowerCase() + s.split('-').map(chunk => chunk.charAt(0).toUpperCase() + chunk.slice(1)).join('').substring(1);
381+
382+
// Element from string
383+
// eslint-disable-next-line
384+
const elementFromString = s => {
385+
if (typeof s === 'string') {
386+
let template = document.createElement('template');
387+
template.innerHTML = s.trim();
388+
389+
return template.content.firstChild;
390+
}
391+
392+
return s;
393+
};
394+
395+
// Insertable Element
396+
// eslint-disable-next-line
397+
const getInsertableElement = s => {
398+
let insertableElement = elementFromString(s);
399+
400+
if (insertableElement instanceof uxr) {
401+
insertableElement = insertableElement.el[0];
402+
}
403+
404+
return insertableElement;
405+
};
406+
407+
// InserBefore
408+
// eslint-disable-next-line
409+
const insertBefore = (insert, target, ref, parent) => {
410+
let to = parent === true ? target.parentNode : target;
411+
let where = ref === 'self' ? target : target[ref];
412+
413+
to.insertBefore(getInsertableElement(insert), where);
414+
};
352415
/**
353416
* wrap
354417
**/

dist/uxr.min.js

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

src/manipulation.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* manipulation
33
**/
44

5+
/* global getInsertableElement */
6+
/* global insertBefore */
7+
58
_.extend.empty = function () {
69
this.el.forEach(item => item.innerHTML = '');
710

@@ -11,5 +14,32 @@ _.extend.empty = function () {
1114
_.extend.remove = function () {
1215
this.el.forEach(item => item.parentNode.removeChild(item));
1316

17+
return this;
18+
};
19+
20+
_.extend.append = function (stringOrObject) {
21+
this.el.forEach(item => item.appendChild(getInsertableElement(stringOrObject)));
22+
23+
return this;
24+
};
25+
26+
_.extend.prepend = function (stringOrObject) {
27+
this.el.forEach(
28+
item => insertBefore(stringOrObject, item, 'firstChild'));
29+
30+
return this;
31+
};
32+
33+
_.extend.after = function (stringOrObject) {
34+
this.el.forEach(
35+
item => insertBefore(stringOrObject, item, 'nextSibling', true));
36+
37+
return this;
38+
};
39+
40+
_.extend.before = function (stringOrObject) {
41+
this.el.forEach(
42+
item => insertBefore(stringOrObject, item, 'self', true));
43+
1444
return this;
1545
};

src/utils.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,38 @@ const maybeMultiple = s => typeof s === 'string' ? justifyString(s).split(' ') :
4545

4646
// Dom String Format
4747
// eslint-disable-next-line
48-
const toDomString = s => s.substr(0, 1).toLowerCase() + s.split('-').map(chunk => chunk.charAt(0).toUpperCase() + chunk.slice(1)).join('').substring(1);
48+
const toDomString = s => s.substr(0, 1).toLowerCase() + s.split('-').map(chunk => chunk.charAt(0).toUpperCase() + chunk.slice(1)).join('').substring(1);
49+
50+
// Element from string
51+
// eslint-disable-next-line
52+
const elementFromString = s => {
53+
if (typeof s === 'string') {
54+
let template = document.createElement('template');
55+
template.innerHTML = s.trim();
56+
57+
return template.content.firstChild;
58+
}
59+
60+
return s;
61+
};
62+
63+
// Insertable Element
64+
// eslint-disable-next-line
65+
const getInsertableElement = s => {
66+
let insertableElement = elementFromString(s);
67+
68+
if (insertableElement instanceof uxr) {
69+
insertableElement = insertableElement.el[0];
70+
}
71+
72+
return insertableElement;
73+
};
74+
75+
// InserBefore
76+
// eslint-disable-next-line
77+
const insertBefore = (insert, target, ref, parent) => {
78+
let to = parent === true ? target.parentNode : target;
79+
let where = ref === 'self' ? target : target[ref];
80+
81+
to.insertBefore(getInsertableElement(insert), where);
82+
};

0 commit comments

Comments
 (0)