Skip to content

Commit 3896de2

Browse files
committed
Release v0.5.0
1 parent 3296f35 commit 3896de2

File tree

4 files changed

+278
-26
lines changed

4 files changed

+278
-26
lines changed

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,110 @@ el.first();
251251

252252
// get the last element in uxr object - selection
253253
el.last();
254+
255+
// get the immediate parent
256+
el.parent();
257+
258+
// get the immediate parent if matched to selector
259+
el.parent(selector);
260+
261+
// get the all children element
262+
el.children();
263+
264+
// get the all matched children
265+
el.children(selector);
266+
267+
// get the all siblings
268+
el.siblings();
269+
270+
// get the all matched siblings
271+
el.siblings(selector);
254272
```
255273
274+
### CSS
275+
`css` method helps to set or get style attributes of the elements.
276+
277+
```js
278+
let el = uxr(selector);
279+
280+
// get a style property
281+
el.css('display'); // returns the display property value
282+
283+
// get a list of style properties
284+
// returns an object with listed values.
285+
// note that, you can ask for properties both kebap-case and camelCase
286+
el.css(['display', 'margin', 'padding-top', 'borderLeft']);
287+
// returns {display: value, margin: value, paddingTop: value, borderLeft: value}
288+
289+
// sets or updates a single property
290+
el.css('padding', '10px');
291+
el.css('background-color', '#ccc');
292+
el.css('backgroundSize', '100% auto');
293+
294+
// sets or updates a list of properties
295+
// note that, you can send a object contains property:value pairs
296+
el.css({width: '100px', height: '50px', 'margin-bottom': '5px'});
297+
```
298+
299+
### Dimensions
300+
Dimension related methods returns or sets content width or height according to dimension method. Except setting `width` and `height` methods, all other usages break the chaining.
301+
302+
```js
303+
let el = uxr(selector);
304+
305+
// returns the first elements content width
306+
// note that: this return only the content width, no-border, no-padding, no-margin
307+
el.width();
308+
el.contentWidth(); // alias method
309+
310+
// sets the width of elements in the uxr object.
311+
// similar method to el.css('width', value);
312+
el.width('100px');
313+
el.contentWidth('100%');
314+
315+
// returns the clientWidth of the first element
316+
// note that: this is only differs from width method with addition of padding
317+
el.innerWidth();
318+
el.clientWidth(); // alias method
319+
320+
321+
// returns the offsetWidth of the first element
322+
// note that: this calculates width with border, padding and content-width altogether
323+
el.outerWidth();
324+
el.offsetWidth(); // alias method
325+
326+
// returns the offsetWidth of the first element including margins
327+
// note that: this calculates width with margin, border, padding and content-width altogether
328+
el.outerWidth(true);
329+
el.offsetWidth(true); // alias method
330+
331+
// returns the first elements content height
332+
// note that: this return only the content height, no-border, no-padding, no-margin
333+
el.height();
334+
el.contentHeight(); // alias method
335+
336+
// sets the height of elements in the uxr object.
337+
// similar method to el.css('height', value);
338+
el.height('100px');
339+
el.contentHeight('100%');
340+
341+
// returns the clientHeight of the first element
342+
// note that: this is only differs from width method with addition of padding
343+
el.innerHeight();
344+
el.clientHeight(); // alias method
345+
346+
347+
// returns the offsetHeight of the first element
348+
// note that: this calculates height with border, padding and content-height altogether
349+
el.outerHeight();
350+
el.offsetHeight(); // alias method
351+
352+
// returns the offsetHeight of the first element including margins
353+
// note that: this calculates height with margin, border, padding and content-height altogether
354+
el.outerHeight(true);
355+
el.offsetHeight(true); // alias method
356+
```
357+
256358
[npm]: https://img.shields.io/npm/v/uxr.svg
257359
[npm-url]: https://npmjs.com/package/uxr
258360

dist/uxr.js

Lines changed: 156 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,48 @@ _.extend.toggleClass = function (className) {
109109
return this;
110110
};
111111

112+
/**
113+
* css
114+
**/
115+
116+
/* global toDomString */
117+
/* global isObject */
118+
119+
_.extend.css = function (prop, value) {
120+
let properties = [];
121+
let values = value ? [value] : [];
122+
let list = {};
123+
124+
if (typeof prop === 'string') {
125+
properties = [toDomString(prop)];
126+
}
127+
128+
else if (isObject(prop)) {
129+
Object.keys(prop).forEach(p => {
130+
properties.push(toDomString(p));
131+
values.push(prop[p]);
132+
});
133+
}
134+
135+
else if (Array.isArray(prop)) {
136+
properties = prop.map(p => toDomString(p));
137+
}
138+
139+
if (values.length > 0) {
140+
return this.el.map(item => properties.forEach((p, i) => item.style[p] = values[i]));
141+
}
142+
143+
if (properties.length > 1) {
144+
properties.forEach(prop => {
145+
list[prop] = this.el[0].style[prop];
146+
});
147+
}
148+
else {
149+
list = this.el[0].style[properties[0]];
150+
}
151+
152+
return list;
153+
};
112154
/**
113155
* data
114156
**/
@@ -119,19 +161,85 @@ _.extend.data = function (name, value) {
119161
let item = this.el[0];
120162
let domName = toDomString(name);
121163

122-
if (typeof item.dataset !== 'undefined') {
123-
if (typeof value !== 'undefined') {
124-
item.dataset[domName] = value;
164+
if (typeof value !== 'undefined') {
165+
item.dataset[domName] = value;
166+
return this;
167+
}
168+
169+
else {
170+
return item.dataset[domName];
171+
}
172+
};
173+
/**
174+
* dimensions
175+
**/
176+
177+
/* global removeUnit */
178+
179+
_.extend.contentWidth = _.extend.width = function (newWidth) {
180+
if (this.length > 0) {
181+
if (newWidth) {
182+
this.el.forEach(item => item.style.width = newWidth);
183+
return this;
125184
}
126185

127186
else {
128-
return item.dataset[domName];
187+
return this.el[0].clientWidth - removeUnit(this.el[0].style.paddingLeft) - removeUnit(this.el[0].style.paddingRight);
129188
}
130189
}
131190

132-
else {
133-
return item.getAttribute('data-' + name);
191+
return false;
192+
};
193+
194+
_.extend.clientWidth = _.extend.innerWidth = function () {
195+
return this.length > 0 ? this.el[0].clientWidth : false;
196+
};
197+
198+
_.extend.offsetWidth = _.extend.outerWidth = function (includeMargins = false) {
199+
if (this.length > 0) {
200+
let outerWidth = this.el[0].offsetWidth;
201+
202+
if (includeMargins) {
203+
outerWidth += removeUnit(this.el[0].style.marginLeft) + removeUnit(this.el[0].style.marginRight);
204+
}
205+
206+
return outerWidth;
134207
}
208+
209+
return false;
210+
};
211+
212+
_.extend.contentHeight = _.extend.height = function (newHeight) {
213+
if (this.length > 0) {
214+
if (newHeight) {
215+
this.el.forEach(item => item.style.height = newHeight);
216+
return this;
217+
}
218+
219+
else {
220+
return this.el[0].clientHeight - removeUnit(this.el[0].style.paddingTop) - removeUnit(this.el[0].style.paddingBottom);
221+
}
222+
}
223+
224+
return false;
225+
};
226+
227+
_.extend.clientHeight = _.extend.innerHeight = function () {
228+
return this.length > 0 ? this.el[0].clientHeight : false;
229+
};
230+
231+
_.extend.offsetHeight = _.extend.outerHeight = function (includeMargins = false) {
232+
if (this.length > 0) {
233+
let outerHeight = this.el[0].offsetHeight;
234+
235+
if (includeMargins) {
236+
outerHeight += removeUnit(this.el[0].style.marginTop) + removeUnit(this.el[0].style.marginBottom);
237+
}
238+
239+
return outerHeight;
240+
}
241+
242+
return false;
135243
};
136244
/**
137245
* end
@@ -289,7 +397,7 @@ _.extend.append = function (stringOrObject) {
289397

290398
_.extend.prepend = function (stringOrObject) {
291399
return this.el.forEach(
292-
item => insertBefore(stringOrObject, item, 'firstChild'));
400+
item => insertBefore(stringOrObject, item, 'firstChild', false));
293401
};
294402

295403
_.extend.after = function (stringOrObject) {
@@ -342,12 +450,42 @@ _.extend.closest = function (selector) {
342450
return mutated(this, []);
343451
};
344452

453+
_.extend.parent = function (selector) {
454+
return mutated(
455+
this,
456+
this.el.map(item => item.parentNode)
457+
.filter(item => selector ? item.matches(selector) : item));
458+
};
459+
460+
_.extend.children = function (selector) {
461+
return mutated(
462+
this,
463+
this.el.map(item => Array.from(item.children))
464+
.reduce((acc, cur) => acc.concat(cur), [])
465+
.filter(item => selector ? item.matches(selector) : item));
466+
};
467+
468+
_.extend.siblings = function (selector) {
469+
return mutated(
470+
this,
471+
this.el.map(item =>
472+
Array.from(item.parentNode.children)
473+
.filter(child => !child.isEqualNode(item)))
474+
.reduce((acc, cur) => acc.concat(cur), [])
475+
.filter(item => selector ? item.matches(selector) : item));
476+
};
477+
345478
_.extend.next = function (selector) {
346-
return mutated(this, this.el.map(item => item.nextElementSibling).filter(item => selector ? item.matches(selector) : item));
479+
return mutated(
480+
this,
481+
this.el.map(item => item.nextElementSibling)
482+
.filter(item => selector ? item.matches(selector) : item));
347483
};
348484

349485
_.extend.prev = function (selector) {
350-
return mutated(this, this.el.map(item => item.previousElementSibling).filter(item => selector ? item.matches(selector) : item));
486+
return mutated(this,
487+
this.el.map(item => item.previousElementSibling)
488+
.filter(item => selector ? item.matches(selector) : item));
351489
};
352490

353491
_.extend.first = function () {
@@ -430,6 +568,14 @@ const mutated = (orgObj, newSet) => {
430568

431569
return obj;
432570
};
571+
572+
// Is Object => {key: value}
573+
// eslint-disable-next-line
574+
const isObject = objLike => ({}.toString.call(objLike) === '[object Object]');
575+
576+
// Remove Unit
577+
// eslint-disable-next-line
578+
const removeUnit = number => parseInt(number, 10);
433579
/**
434580
* wrap
435581
**/
@@ -507,5 +653,5 @@ _.extend.unwrap = function (selector) {
507653

508654
return this;
509655
};
510-
_.uxr = { version: '0.4.2' };
656+
_.uxr = { version: '0.5.0' };
511657
})();

0 commit comments

Comments
 (0)