Skip to content

Commit

Permalink
add toggleButton fixed #281
Browse files Browse the repository at this point in the history
  • Loading branch information
shiren authored and seonim-ryu committed Jan 6, 2020
1 parent 7dd634e commit 33d7611
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/editor/demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

$('#editSection').neonEditor({
previewStyle: 'vertical',
height: 300,
height: 800,
delay: 300,
//initialValue: 'a\nb\nc\nd\ne\nf\ng\n\na\nb\nc\nd\ne\nf\ng\n\na\nb\nc\nd\ne\nf\ng\n\n',
//initialValue: 'test1\ntest2\ntest3\ntest4',
Expand Down
22 changes: 14 additions & 8 deletions apps/editor/src/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ function Button(options) {
className: options.className
});

this.command = options.command;
this.event = options.event;
this.text = options.text;
this.style = options.style;
this._setOptions(options);

this.render();

this.attachEvents({
'click': '_onClick'
});
}

Button.prototype = util.extend(
{},
UIController.prototype
);

Button.prototype._setOptions = function(options) {
this.command = options.command;
this.event = options.event;
this.text = options.text;
this.style = options.style;
};

/**
* Button의 모습을 그린다
*/
Expand All @@ -50,10 +58,6 @@ Button.prototype.render = function() {
if (this.style) {
this.$el.attr('style', this.style);
}

this.attachEvents({
'click': '_onClick'
});
};

/**
Expand All @@ -66,6 +70,8 @@ Button.prototype._onClick = function() {
} else {
this.trigger('event', this.event);
}

this.trigger('clicked');
};

module.exports = Button;
20 changes: 10 additions & 10 deletions apps/editor/src/js/extensions/scrollFollow.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ extManager.defineExtension('scrollFollow', function(editor) {
});

//UI
editor.layout.toolbar.addButton({
className: 'scrollFollowEnable',
command: 'scrollFollow.enable',
text: 'SF_ON'
});

editor.layout.toolbar.addButton({
className: 'scrollFollowDisable',
editor.layout.toolbar.addButton([{
classname: 'scrollfollowEnable',
command: 'scrollFollow.disable',
text: 'SF_OFF'
});
text: 'SF',
style: 'background-color: #fff'
}, {
className: 'scrollFollowDisable',
command: 'scrollFollow.enable',
text: 'SF',
style: 'background-color: #ddd'
}]);
});
57 changes: 57 additions & 0 deletions apps/editor/src/js/toggleButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @fileoverview
* @author Sungho Kim([email protected]) FE Development Team/NHN Ent.
*/
'use strict';

var Button = require('./button');

var util = ne.util;

/**
* ToggleButton
* initialize toggle button
* @exports ToggleButton
* @augments Button
* @constructor
* @class
* @param {object[]} options 옵션
* @param {string} options.className 만들어진 RootElement에 추가할 클래스
* @param {string} options.command 클릭되면 실행될 커맨드명
* @param {string} options.text 버튼안에 들어갈 텍스트
* @param {string} options.style 추가적으로 적용될 CSS스타일
*/
function ToggleButton(options) {
this.options = options;
this.current = this.options[0];

Button.call(this, this.current);

this._initEvent();
}

ToggleButton.prototype = util.extend(
{},
Button.prototype
);

ToggleButton.prototype._initEvent = function() {
var self = this;

this.on('clicked', function() {
self._toggle();
});
};

ToggleButton.prototype._toggle = function() {
if (this.current === this.options[0]) {
this.current = this.options[1];
} else {
this.current = this.options[0];
}

this._setOptions(this.current);
this.render();
};

module.exports = ToggleButton;
9 changes: 7 additions & 2 deletions apps/editor/src/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
'use strict';

var UIController = require('./uicontroller'),
Button = require('./button');
Button = require('./button'),
ToggleButton = require('./toggleButton');

var util = ne.util;

Expand Down Expand Up @@ -53,7 +54,11 @@ Toolbar.prototype.addButton = function(button) {
var ev = this.eventManager;

if (!button.render) {
button = new Button(button);
if (!util.isArray(button)) {
button = new Button(button);
} else {
button = new ToggleButton(button);
}
}

button.on('command', function emitCommandEvent($, commandName) {
Expand Down
4 changes: 2 additions & 2 deletions apps/editor/test/button.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var Button = require('../src/js/button');

describe('Button', function() {
'use strict';

var button;

beforeEach(function() {
Expand Down
57 changes: 57 additions & 0 deletions apps/editor/test/toggleButton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

var ToggleButton = require('../src/js/toggleButton');

describe('ToggleButton', function() {
var button;

beforeEach(function() {
$('body').empty();
});

describe('버튼 생성', function() {
it('버튼 태그를 만든다.', function() {
button = new ToggleButton([{}]);
expect(button.$el.prop('tagName')).toEqual('BUTTON');
});

it('옵션으로 전달된 className이 버튼태그에 적용된다.', function() {
button = new ToggleButton([{
className: 'myclass'
}]);
expect(button.$el.hasClass('myclass')).toBe(true);
});

it('옵션으로 전달된 text가 버튼태그에 적용된다.', function() {
button = new ToggleButton([{
text: 'buttonText'
}]);

expect(button.$el.text()).toEqual('buttonText');
});

it('옵션으로 전달된 style 버튼태그에 적용된다.', function() {
button = new ToggleButton([{
style: 'display:none'
}]);

expect(button.$el.css('display')).toEqual('none');
});
});

describe('toggle button', function() {
it('toggle button with second options', function() {
button = new ToggleButton([{
text: 'first'
}, {
text: 'second'
}]);

expect(button.text).toEqual('first');

button._onClick();

expect(button.text).toEqual('second');
});
});
});

0 comments on commit 33d7611

Please sign in to comment.