Skip to content

Commit

Permalink
improvements and more simply logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blackout314 committed May 2, 2016
1 parent b181896 commit cc73905
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pii('.class')

### Class manipulation
```javascript
pi.classAdd('#try', 'newClass');
pi('#try').addClass('newClass'); // delClass hasClass toggleClass
pii.classAdd('.class', 'newClass');
```

Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(config) {
frameworks: ['qunit','sinon'],
reporters: ['mocha', 'coverage'],
preprocessors: {
'src/*.js': ['coverage']
'src/*.js': ['coverage']
},
coverageReporter: {
type : 'lcov',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pi",
"version": "0.3.4",
"version": "0.4.0",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jasmine": "^0.9.1",
Expand Down
65 changes: 19 additions & 46 deletions src/pi.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,79 +167,52 @@ pii.forEach = function (elms, operation) {
if (typeof (elms) !== 'object' || elms.length <= 0 || typeof (operation) !== 'string') {
return false;
}
args = [ elms[0] ].concat(args);
while ((a = elms[i++]) !== undefined) {
args[0] = a;
pi[operation].apply(pi, args);
a[operation](args);
}
};

if (pi.C.feature.classList) {
/**
* @name pi.classAdd
* @example pi.classAdd( pi('#id'), 'class' )
*/
pi.classAdd = function (elm, c) {
"use strict";
if (typeof (elm) === 'string') {
elm = pi(elm);
}
elm.classList.add(c);
};
/**
* @name pi.classDel
* @example pi.classDel( pi('#id'), 'class' )
*/
pi.classDel = function (elm, c) {
"use strict";
if (typeof (elm) === 'string') {
elm = pi(elm);
}
elm.classList.remove(c);
};
/**
* @name pi.classHas
* @example pi.classHas( pi('#id'), 'class' )
*/
pi.classHas = function (elm, c) {
Element.prototype.addClass = function (c) {
"use strict";
this.classList.add(c);
};

Element.prototype.delClass = function (c) {
"use strict";
this.classList.remove(c);
};

Element.prototype.hasClass = function (c) {
"use strict";
if (typeof (elm) === 'string') {
elm = pi(elm);
}
return elm.classList.contains(c);
return this.classList.contains(c);
};
/**
* @name pi.classToggle
* @example pi.classToggle( pi('#id'), 'class' )
*/
pi.classToggle = function (elm, c) {

Element.prototype.toggleClass = function (c) {
"use strict";
if (typeof (elm) === 'string') {
elm = pi(elm);
}
elm.classList.toggle(c);
this.classList.toggle(c);
};

/**
* @name pii.classAdd
*/
pii.classAdd = function (elms, c) {
"use strict";
pii.forEach(elms, 'classAdd', c);
pii.forEach(elms, 'addClass', c);
};
/**
* @name pii.classDel
*/
pii.classDel = function (elms, c) {
"use strict";
pii.forEach(elms, 'classDel', c);
pii.forEach(elms, 'delClass', c);
};
/**
* @name pii.classToggle
*/
pii.classToggle = function (elms, c) {
"use strict";
pii.forEach(elms, 'classToggle', c);
pii.forEach(elms, 'toggleClass', c);
};
}

Expand Down
32 changes: 12 additions & 20 deletions tests/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,22 @@ module("tests", {
}
});

test('dom test', function(){
ok( typeof(pi.classAdd) === 'function' );
});

test('dom classAdd classDel classHas', function(){
var test = pi('#test');
pi.classAdd(test,'prova');
ok( pi.classHas(test,'prova') === true );

pi.classAdd('#test','prova2');
ok( pi.classHas('#test','prova2') === true );
test.addClass('prova');
ok( test.hasClass('prova') === true );

pi.classDel(test,'prova');
pi.classDel('#test','prova2');
ok( pi.classHas(test,'prova') === false );
ok( pi.classHas('#test','prova2') === false );
test.delClass('prova');
ok( test.hasClass('prova') === false );
});

test('dom classToggle', function(){
var test = pi('#test');
pi.classToggle(test,'prova');
ok( pi.classHas(test,'prova') === true );
test.toggleClass('prova');
ok( test.hasClass('prova') === true );

pi.classToggle('#test','prova');
ok( pi.classHas('#test','prova') === false );
test.toggleClass('prova');
ok( test.hasClass('prova') === false );
});

test('dom H remove', function(){
Expand All @@ -40,14 +31,15 @@ test('dom H remove', function(){
});

test('dom classAdd classDel classToggle forEach', function(){
var d1 = pi('#d1');
pii.classAdd('.o', 'prova');
ok( pi.classHas('#d1','prova') === true );
ok( d1.hasClass('prova') === true );

pii.classDel('.o', 'prova');
ok( pi.classHas('#d1','prova') === false );
ok( d1.hasClass('prova') === false );

pii.classToggle('.o', 'prova');
ok( pi.classHas('#d1','prova') === true );
ok( d1.hasClass('prova') === true );
});

test('dom H append', function(){
Expand Down

0 comments on commit cc73905

Please sign in to comment.