Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added jsElem option #148

Merged
merged 1 commit into from
May 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function BH() {
this._optJsAttrName = 'onclick';
this._optJsAttrIsJs = true;
this._optJsCls = 'i-bem';
this._optJsElem = true;
this._optEscapeContent = false;
this._optNobaseMods = false;
this.utils = {
Expand Down Expand Up @@ -564,6 +565,9 @@ BH.prototype = {
if (options.jsCls !== undefined) {
this._optJsCls = options.jsCls;
}
if (options.hasOwnProperty('jsElem')) {
this._optJsElem = options.jsElem;
}
if (options.clsNobaseMods) {
this._optNobaseMods = true;
}
Expand Down Expand Up @@ -1009,6 +1013,8 @@ BH.prototype = {
}
}

var addJSInitClass = this._optJsCls && (this._optJsElem || !json.elem);

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

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

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

it('should use jsElem option for mixed element', function() {
bh.setOptions({ jsElem: false });
bh.apply({ block: 'button', elem: 'box', mix: { block: 'icon', elem: 'wrap', js: true } }).should.equal(
'<div class="button__box icon__wrap" onclick=\'return {"icon__wrap":{}}\'></div>');
});

it('should use clsNobaseMods options', function() {
bh.setOptions({ clsNobaseMods: true });
bh.apply({
Expand Down
4 changes: 4 additions & 0 deletions test/test.toHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ describe('bh.toHtml()', function() {
bh.apply({ block: 'icon', content: 'submit', mix: { block: 'button', elem: 'control', js: true } }).should.equal(
'<div class="icon button__control i-bem" onclick=\'return {"button__control":{}}\'>submit</div>');
});
it('should set `i-bem` class on mixed block', function() {
bh.apply({ block: 'button', elem: 'box', content: 'submit', mix: { block: 'icon', js: true } }).should.equal(
'<div class="button__box icon i-bem" onclick=\'return {"icon":{}}\'>submit</div>');
});
});

describe('cls', function() {
Expand Down