From e074f8caafe4a9d4d67e5eeaa6f26c9d0abe0eff Mon Sep 17 00:00:00 2001 From: Alexey Belousov Date: Tue, 22 Apr 2014 19:51:27 +0600 Subject: [PATCH] isp: @zhigalov @frgt --- tasks/isp/refactoring/button.js | 66 +++++++++++++++++---------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/tasks/isp/refactoring/button.js b/tasks/isp/refactoring/button.js index 0f840ed..9cd0110 100644 --- a/tasks/isp/refactoring/button.js +++ b/tasks/isp/refactoring/button.js @@ -2,8 +2,8 @@ function notImplemented() { throw new Error('Method is not implemented'); } -function AbstractButton(label) { - this.label = label; +function AbstractButton(options) { + this.label = options.label; this.$el = this.render(); this.bind(); } @@ -11,33 +11,15 @@ AbstractButton.prototype = { getLabel: function() { return this.label; }, - getIcon: function () { - notImplemented(); - }, - getUrl: function () { - notImplemented(); - }, - onClick: function (e) { - notImplemented(); - }, + onClick: function (e) {}, render: function () { - var icon = this.getIcon(); - var url = this.getUrl(); var label = this.getLabel(); var $el = $(''); - if (icon) { - $el.append($('').attr('src', icon)); - } - if (label) { $el.append(label); } - if (url) { - $el.wrap(''); - } - return $el; }, bind: function () { @@ -45,26 +27,48 @@ AbstractButton.prototype = { } }; -function IconButton(icon) { - AbstractButton.call(this, void 0); - this.icon = icon; +function IconButton(options) { + AbstractButton.call(this, options); + this.icon = options.icon; } IconButton.prototype = Object.extend(Object.create(AbstractButton.prototype), { getIcon: function () { return this.icon; }, - getUrl: function () {}, - onClick: function (e) {} + onClick: function (e) { + // some logic + }, + render: function () { + var icon = this.getIcon(), + $el = IconButton.prototype.render.call(this); + + if (icon) { + $el.append($('').attr('src', icon)); + } + + return $el; + } }); -function AnchorButton(label, url) { - AbstractButton.call(this, label); - this.url = url; +function AnchorButton(options) { + AbstractButton.call(this, options); + this.url = options.url || '#'; } AnchorButton.prototype = Object.extend(Object.create(AbstractButton.prototype), { - getIcon: function () {}, getUrl: function () { return this.url; }, - onClick: function (e) {} + onClick: function (e) { + // some logic + }, + render: function () { + var label = this.getLabel(); + var $el = $(''); + + if (label) { + $el.append(label); + } + + return $el; + } });