Skip to content

Commit

Permalink
Class i-bem on elems with js (close #119)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishanga committed Mar 13, 2015
1 parent c1bd846 commit b1c4fad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function BH() {
this._options = {};
this._optJsAttrName = 'onclick';
this._optJsAttrIsJs = true;
this._optJsCls = 'i-bem';
this._optEscapeContent = false;
this.utils = {
_expandoId: new Date().getTime(),
Expand Down Expand Up @@ -559,6 +560,9 @@ BH.prototype = {
if (options.jsAttrScheme) {
this._optJsAttrIsJs = options.jsAttrScheme === 'js';
}
if (typeof options.jsCls === 'string') {
this._optJsCls = options.jsCls;
}
if (options.escapeContent) {
this._optEscapeContent = options.escapeContent;
}
Expand Down Expand Up @@ -939,8 +943,6 @@ BH.prototype = {
}
}

var addJSInitClass = jsParams && !json.elem;

var mixes = json.mix;
if (mixes && mixes.length) {
for (i = 0, l = mixes.length; i < l; i++) {
Expand All @@ -955,15 +957,14 @@ BH.prototype = {
if (mix.js) {
(jsParams = jsParams || {})[mixBase] = mix.js === true ? {} : mix.js;
hasMixJsParams = true;
if (!addJSInitClass) addJSInitClass = mixBlock && !mixElem;
}
}
}
}
}

if (jsParams) {
if (addJSInitClass) cls += ' i-bem';
if (this._optJsCls) cls += ' ' + this._optJsCls;
var jsData = (!hasMixJsParams && json.js === true ?
'{"' + base + '":{}}' :
jsAttrEscape(JSON.stringify(jsParams)));
Expand Down
19 changes: 19 additions & 0 deletions test/test.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ describe('options', function() {
beforeEach(function() {
bh = new BH();
});

it('should use onclick and js format as default', function() {
bh.apply({ block: 'button', js: true }).should.equal(
'<div class="button i-bem" onclick=\'return {"button":{}}\'></div>'
);
});

it('should return current options', function() {
bh.setOptions({ foo: 'bar' });
bh.getOptions().foo.should.equal('bar');
});

it('should use js format as default and use jsAttrName option', function() {
bh.setOptions({
jsAttrName: 'ondblclick'
Expand All @@ -24,6 +27,7 @@ describe('options', function() {
'<div class="button i-bem" ondblclick=\'return {"button":{}}\'></div>'
);
});

it('should use onclick as default and use jsAttrScheme option', function() {
bh.setOptions({
jsAttrScheme: 'json'
Expand All @@ -32,6 +36,7 @@ describe('options', function() {
'<div class="button i-bem" onclick=\'{"button":{}}\'></div>'
);
});

it('should use jsAttrName and jsAttrScheme options', function() {
bh.setOptions({
jsAttrName: 'data-bem',
Expand All @@ -41,5 +46,19 @@ describe('options', function() {
'<div class="button i-bem" data-bem=\'{"button":{}}\'></div>'
);
});

it('should use jsCls option', function() {
bh.setOptions({ jsCls: 'js' });
bh.apply({ block: 'button', js: true }).should.equal(
'<div class="button js" onclick=\'return {"button":{}}\'></div>'
);
});

it('should use empty jsCls option', function() {
bh.setOptions({ jsCls: '' });
bh.apply({ block: 'button', js: true }).should.equal(
'<div class="button" onclick=\'return {"button":{}}\'></div>'
);
});
});
});
8 changes: 4 additions & 4 deletions test/test.toHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ describe('bh.toHtml()', function() {
beforeEach(function() {
bh = new BH();
});
it('should not set `i-bem` class on element', function() {
it('should set `i-bem` class on element', function() {
bh.apply({ block: 'button', elem: 'control', js: true, content: 'submit' }).should.equal(
'<div class="button__control" onclick=\'return {"button__control":{}}\'>submit</div>');
'<div class="button__control i-bem" onclick=\'return {"button__control":{}}\'>submit</div>');
});
it('should not set `i-bem` class on mixed element', function() {
it('should set `i-bem` class on mixed element', function() {
bh.apply({ block: 'icon', content: 'submit', mix: { block: 'button', elem: 'control', js: true } }).should.equal(
'<div class="icon button__control" onclick=\'return {"button__control":{}}\'>submit</div>');
'<div class="icon button__control i-bem" onclick=\'return {"button__control":{}}\'>submit</div>');
});
});

Expand Down

0 comments on commit b1c4fad

Please sign in to comment.